Skip to content

Commit

Permalink
Switch random_bool to random_selection in hierarchy remove_atoms() to…
Browse files Browse the repository at this point in the history
… have consistent number of atoms removed every time. Fix tests.
  • Loading branch information
olegsobolev committed Oct 14, 2024
1 parent bc6a38f commit 4dacb50
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
3 changes: 2 additions & 1 deletion iotbx/pdb/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2056,7 +2056,8 @@ def rename_chain_id(self, old_id, new_id):

def remove_atoms(self, fraction):
assert fraction>0 and fraction<1.
sel_keep = flex.random_bool(self.atoms_size(), 1-fraction)
n_atoms_to_keep = int(self.atoms_size() * (1-fraction))
sel_keep = flex.random_selection(self.atoms_size(), n_atoms_to_keep)
return self.select(sel_keep)

def set_atomic_charge(self, iselection, charge):
Expand Down
8 changes: 2 additions & 6 deletions iotbx/pdb/tst_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6996,8 +6996,6 @@ def exercise_set_atomic_charge():
assert (ph.atoms()[0].charge == '1-')

def exercise_remove_atoms():
random.seed(1)
flex.set_random_seed(1)
pdb_str = """
ATOM 1 N AMET B 37 7.525 5.296 6.399 1.00 10.00 N
ATOM 2 CA AMET B 37 6.533 6.338 6.634 1.00 10.00 C
Expand Down Expand Up @@ -7052,11 +7050,9 @@ def exercise_remove_atoms():
"""
pi = pdb.input(source_info=None, lines=pdb_str)
ph_in = pi.construct_hierarchy()
s1 = ph_in.atoms_size()
assert ph_in.atoms_size() == 48
ph = ph_in.remove_atoms(fraction=0.1)
s2 = ph.atoms_size()
f = s2*100./s1
assert f>90 and f<100
assert ph.atoms_size() == 43

def exercise_set_atomic_charge():
pdb_str = """
Expand Down
21 changes: 6 additions & 15 deletions mmtbx/regression/tst_pdbtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,6 @@ def exercise_neutralize_scatterers():
assert line1 == line2

def exercise_remove_atoms():
import random
random.seed(1)
flex.set_random_seed(1)
pdb_str = """
ATOM 1 N AMET B 37 7.525 5.296 6.399 1.00 10.00 N
ATOM 2 CA AMET B 37 6.533 6.338 6.634 1.00 10.00 C
Expand Down Expand Up @@ -773,24 +770,18 @@ def exercise_remove_atoms():
TER
END
"""
pi = iotbx.pdb.input(source_info=None, lines=pdb_str)
ph_in = pi.construct_hierarchy()
s1 = ph_in.atoms_size()
pi.write_pdb_file(file_name="exercise_remove_atoms.pdb")
# Initial size - 48 atoms
with open("exercise_remove_atoms.pdb", 'w') as f:
f.write(pdb_str)
cmd = " ".join([
"phenix.pdbtools",
"exercise_remove_atoms.pdb",
"remove_fraction=0.1"])
print(cmd)
run_command(command=cmd, verbose=False)
pi = iotbx.pdb.input(file_name="exercise_remove_atoms_modified.pdb")
ph_in = pi.construct_hierarchy()
s2 = ph_in.atoms_size()
f = s2*100./s1
#
# UNSTABLE 3x
#
assert f>77 and f<100, f # was getting 79.16, 70.8333 on anaconda t96,
inp = iotbx.pdb.input(file_name="exercise_remove_atoms_modified.pdb")
# removed 5 atoms (~10%).
assert inp.atoms().size() == 43, inp.atoms().size()

def exercise_change_of_basis():
with open("tmp_pdbtools_cb_op.pdb", "w") as f:
Expand Down

0 comments on commit 4dacb50

Please sign in to comment.