def exercise_3(): #test torsion restraints for use_reference in ['True', 'False', 'top_out', 'None']: pdb_inp = iotbx.pdb.input( lines=flex.std_string(pdb_str_2.splitlines()), source_info=None) model = manager( model_input=pdb_inp, log=null_out()) grm = model.get_restraints_manager().geometry xrs2 = model.get_xray_structure() awl2 = model.get_hierarchy().atoms_with_labels() pdb2 = model.get_hierarchy() pdb_inp3 = iotbx.pdb.input(source_info=None, lines=pdb_str_3) xrs3 = pdb_inp3.xray_structure_simple() ph3 = pdb_inp3.construct_hierarchy() ph3.atoms().reset_i_seq() awl3 = ph3.atoms_with_labels() sites_cart_reference = flex.vec3_double() selection = flex.size_t() min_selection = flex.size_t() reference_names = ["N", "CA", "CB", "CG", "CD", "NE", "CZ", "NH1", "NH2"] minimize_names = ["CG", "CD", "NE", "CZ", "NH1", "NH2"] for a2,a3 in zip(tuple(awl2), tuple(awl3)): assert a2.resname == a3.resname assert a2.name == a3.name assert a2.i_seq == a3.i_seq if(a2.resname == "ARG" and a2.name.strip() in reference_names): selection.append(a2.i_seq) sites_cart_reference.append(a3.xyz) if a2.name.strip() in minimize_names: min_selection.append(a2.i_seq) assert selection.size() == len(reference_names) selection_bool = flex.bool(xrs2.scatterers().size(), min_selection) if(use_reference == 'True'): grm.add_chi_torsion_restraints_in_place( pdb_hierarchy = pdb2, sites_cart = sites_cart_reference, selection = selection, sigma = 2.5) elif(use_reference == 'top_out'): grm.add_chi_torsion_restraints_in_place( pdb_hierarchy = pdb2, sites_cart = sites_cart_reference, selection = selection, sigma = 2.5, limit = 180.0, top_out_potential=True) elif(use_reference == 'None'): grm.add_chi_torsion_restraints_in_place( pdb_hierarchy = pdb2, sites_cart = sites_cart_reference, selection = selection, sigma = 2.5) grm.remove_chi_torsion_restraints_in_place( selection = selection) d1 = flex.mean(flex.sqrt((xrs2.sites_cart().select(min_selection) - xrs3.sites_cart().select(min_selection)).dot())) print("distance start (use_reference: %s): %6.4f"%(str(use_reference), d1)) assert d1>4.0 assert approx_equal( flex.max(flex.sqrt((xrs2.sites_cart().select(~selection_bool) - xrs3.sites_cart().select(~selection_bool)).dot())), 0) from cctbx import geometry_restraints import mmtbx.refinement.geometry_minimization import scitbx.lbfgs grf = geometry_restraints.flags.flags(default=True) grf.nonbonded = False sites_cart = xrs2.sites_cart() minimized = mmtbx.refinement.geometry_minimization.lbfgs( sites_cart = sites_cart, correct_special_position_tolerance=1.0, geometry_restraints_manager = grm, sites_cart_selection = flex.bool(sites_cart.size(), min_selection), geometry_restraints_flags = grf, lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations=5000)) xrs2.set_sites_cart(sites_cart = sites_cart) d2 = flex.mean(flex.sqrt((xrs2.sites_cart().select(min_selection) - xrs3.sites_cart().select(min_selection)).dot())) print("distance final (use_reference: %s): %6.4f"%(str(use_reference), d2)) if (use_reference in ['True', 'top_out']): assert d2<0.3, "%s, %f" % (use_reference, d2) else: assert d2>4.0, d2 assert approx_equal( flex.max(flex.sqrt((xrs2.sites_cart().select(~selection_bool) - xrs3.sites_cart().select(~selection_bool)).dot())), 0) #test torsion manipulation grm.remove_chi_torsion_restraints_in_place() grm.remove_chi_torsion_restraints_in_place() sites_cart_reference = [] selections_reference = [] for model in pdb2.models(): for chain in model.chains(): for residue in chain.residues(): sites_cart_reference.append(residue.atoms().extract_xyz()) selections_reference.append(residue.atoms().extract_i_seq()) #one residue at a time (effectively chi angles only) for sites_cart, selection in zip(sites_cart_reference, selections_reference): grm.add_chi_torsion_restraints_in_place( pdb_hierarchy = pdb2, sites_cart = sites_cart, selection = selection) assert grm.get_n_chi_torsion_proixes() == 6 grm.remove_chi_torsion_restraints_in_place() #all sites at once, chi angles only sites_cart = xrs2.sites_cart() grm.add_chi_torsion_restraints_in_place( pdb_hierarchy = pdb2, sites_cart = sites_cart, selection = None, chi_angles_only = True) assert grm.get_n_chi_torsion_proixes() == 6 #all sites at once, all torsions grm.add_chi_torsion_restraints_in_place( pdb_hierarchy = pdb2, sites_cart = sites_cart, selection = None, chi_angles_only = False) # grm.get_chi_torsion_proxies().show_sorted( # by_value='residual', # sites_cart=sites_cart, # site_labels=[atom.id_str() for atom in pdb2.atoms()]) assert grm.get_n_chi_torsion_proixes() == 12, grm.get_n_chi_torsion_proixes()
def exercise_2(): for use_reference in [True, False, None]: pdb_inp = iotbx.pdb.input( lines=flex.std_string(pdb_str_2.splitlines()), source_info=None) model = manager( model_input=pdb_inp, log=null_out()) grm = model.get_restraints_manager().geometry xrs2 = model.get_xray_structure() awl2 = model.get_hierarchy().atoms_with_labels() pdb_inp3 = iotbx.pdb.input(source_info=None, lines=pdb_str_3) xrs3 = pdb_inp3.xray_structure_simple() ph3 = pdb_inp3.construct_hierarchy() ph3.atoms().reset_i_seq() awl3 = ph3.atoms_with_labels() sites_cart_reference = flex.vec3_double() selection = flex.size_t() reference_names = ["CG", "CD", "NE", "CZ", "NH1", "NH2"] for a2,a3 in zip(tuple(awl2), tuple(awl3)): assert a2.resname == a3.resname assert a2.name == a3.name assert a2.i_seq == a3.i_seq if(a2.resname == "ARG" and a2.name.strip() in reference_names): selection.append(a2.i_seq) sites_cart_reference.append(a3.xyz) assert selection.size() == len(reference_names) selection_bool = flex.bool(xrs2.scatterers().size(), selection) if(use_reference): grm.adopt_reference_coordinate_restraints_in_place( reference.add_coordinate_restraints( sites_cart = sites_cart_reference, selection = selection, sigma = 0.01)) elif(use_reference is None): grm.adopt_reference_coordinate_restraints_in_place( reference.add_coordinate_restraints( sites_cart = sites_cart_reference, selection = selection, sigma = 0.01)) grm.remove_reference_coordinate_restraints_in_place( selection = selection) d1 = flex.mean(flex.sqrt((xrs2.sites_cart().select(selection) - xrs3.sites_cart().select(selection)).dot())) print("distance start (use_reference: %s): %6.4f"%(str(use_reference), d1)) assert d1>4.0 assert approx_equal( flex.max(flex.sqrt((xrs2.sites_cart().select(~selection_bool) - xrs3.sites_cart().select(~selection_bool)).dot())), 0) from cctbx import geometry_restraints import mmtbx.refinement.geometry_minimization import scitbx.lbfgs grf = geometry_restraints.flags.flags(default=True) sites_cart = xrs2.sites_cart() minimized = mmtbx.refinement.geometry_minimization.lbfgs( sites_cart = sites_cart, correct_special_position_tolerance=1.0, geometry_restraints_manager = grm, sites_cart_selection = flex.bool(sites_cart.size(), selection), geometry_restraints_flags = grf, lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations=5000)) xrs2.set_sites_cart(sites_cart = sites_cart) d2 = flex.mean(flex.sqrt((xrs2.sites_cart().select(selection) - xrs3.sites_cart().select(selection)).dot())) print("distance final (use_reference: %s): %6.4f"%(str(use_reference), d2)) if(use_reference): assert d2<0.005, "failed: %f<0.05" % d2 else: assert d2>4.0, d2 assert approx_equal( flex.max(flex.sqrt((xrs2.sites_cart().select(~selection_bool) - xrs3.sites_cart().select(~selection_bool)).dot())), 0)