def run(args): if len(args) == 0: show_usage() return inp_fn = args[0] import time t0 = time.time() pdb_input = iotbx.pdb.input(file_name=inp_fn, source_info=None, raise_sorry_if_format_error=True) t0 = time.time() model = mmtbx.model.manager(model_input=pdb_input) pdb_h = model.get_hierarchy() info = pdb_h.flip_symmetric_amino_acids() print(info) model.set_sites_cart_from_hierarchy() out_fn_prefix = inp_fn if inp_fn.endswith(".pdb") or inp_fn.endswith(".cif"): out_fn_prefix = inp_fn[:-4] out_fn = out_fn_prefix + "_iupac.pdb" if model.input_model_format_cif(): out_fn = out_fn_prefix + "_iupac.cif" txt = model.model_as_mmcif() else: out_fn = out_fn_prefix + "_iupac.cif" txt = model.model_as_pdb() with open(out_fn, 'w') as f: f.write(txt)
def exercise_set_sites_cart_no_ncs(): inp = iotbx.pdb.input(lines=pdb_str_5, source_info=None) model = mmtbx.model.manager(model_input=inp, expand_with_mtrix=False) model.search_for_ncs() nrgl = model.get_ncs_groups() nrgl._show() print('n model atoms:', model.get_number_of_atoms()) print('n master atoms:', model.get_master_selection().count(True)) print('n master atoms:', model.get_master_selection().iselection().size()) print('n master atoms:', model.get_master_hierarchy().atoms_size()) assert model.get_master_selection().count(True) ==\ model.get_master_selection().iselection().size() ==\ model.get_master_hierarchy().atoms_size() h = model.get_hierarchy() # Warning: here here mh is not deep-copy, therefore when we change atom coords # they are changing in model.get_hierarchy() as well mh = model.get_master_hierarchy() new_sites_cart = flex.vec3_double([(1.0, 1.0, 1.0)] * 42) mh.atoms().set_xyz(new_sites_cart) model.set_sites_cart_from_hierarchy(multiply_ncs=True) h = model.get_hierarchy() new_xyz = h.atoms().extract_xyz() # print('h sites:', list(new_xyz)) # checking if setting went as supposed: for j in range(42): assert approx_equal(new_xyz[j], (1.0, 1.0, 1.0), eps=1e-4)
def exercise_set_sites_cart_ncs(): """ No extra atoms """ inp = iotbx.pdb.input(lines=pdb_str_5, source_info=None) model = mmtbx.model.manager(model_input=inp) model.search_for_ncs() nrgl = model.get_ncs_groups() nrgl._show() print('n model atoms:', model.get_number_of_atoms()) print('n master atoms:', model.get_master_selection().count(True)) print('n master atoms:', model.get_master_selection().iselection().size()) print('n master atoms:', model.get_master_hierarchy().atoms_size()) assert model.get_master_selection().count(True) ==\ model.get_master_selection().iselection().size() ==\ model.get_master_hierarchy().atoms_size() h = model.get_hierarchy() # print('h sites:', list(h.atoms().extract_xyz())) # Warning: here here mh is not deep-copy, therefore when we change atom coords # they are changing in model.get_hierarchy() as well mh = model.get_master_hierarchy() new_sites_cart = flex.vec3_double([(1.0, 1.0, 1.0)] * 42) mh.atoms().set_xyz(new_sites_cart) model.set_sites_cart_from_hierarchy(multiply_ncs=True) h = model.get_hierarchy() new_xyz = h.atoms().extract_xyz() # print('h sites:', list(new_xyz)) # checking if setting went as supposed: assert approx_equal(new_xyz[0], (1.0, 1.0, 1.0), eps=1e-4) assert approx_equal( new_xyz[42], (-0.6420293330506949, 1.2600792663765976, 7.999997229451341), eps=1e-4) assert approx_equal( new_xyz[84], (-1.396802536808536, -0.22123285934616377, 1.000000038694047), eps=1e-4) for i in range(3): for j in range(42): assert approx_equal(new_xyz[42 * i + j], new_xyz[42 * i], eps=1e-4)
def exercise(tolerance=0.01): for use_neutron_distances in [True, False]: mon_lib_srv = monomer_library.server.server() ener_lib = monomer_library.server.ener_lib() inp = iotbx.pdb.input(lines=pdb_str.splitlines(), source_info=None) params = iotbx.phil.parse(input_string=grand_master_phil_str, process_includes=True).extract() params.pdb_interpretation.use_neutron_distances = use_neutron_distances model = mmtbx.model.manager(model_input=inp) model.process(pdb_interpretation_params=params, make_restraints=True) ph = model.get_hierarchy() ph.write_pdb_file(file_name="input.pdb") m = geometry_minimization.run2( restraints_manager=model.get_restraints_manager(), pdb_hierarchy=ph, correct_special_position_tolerance=1.0, bond=True, nonbonded=True, angle=True, dihedral=True, chirality=True, planarity=True) model.set_sites_cart_from_hierarchy() suffix = "X" if (use_neutron_distances): suffix = "N" ph.write_pdb_file(file_name="output_%s.pdb" % suffix) # check X-H distances: x-ray cntr = 0 awl = iotbx.pdb.input(file_name="output_X.pdb").atoms_with_labels() for a1 in awl: n1 = a1.name.strip() for a2 in awl: n2 = a2.name.strip() if ([n1, n2] == ["CD1", "HD1"] or [n1, n2] == ["CD2", "HD2"] or [n1, n2] == ["CE1", "HE1"] or [n1, n2] == ["CE2", "HE2"]): assert approx_equal(dist(a1.xyz, a2.xyz), 0.93, tolerance) cntr += 1 if (n1 == "OH" and n2 in ["HH", "DH"]): assert approx_equal(dist(a1.xyz, a2.xyz), 0.84, tolerance) cntr += 1 if ([n1, n2] == ["CB", "HB2"] or [n1, n2] == ["CB", "HB3"] or [n1, n2] == ["CA", "HA"]): assert approx_equal(dist(a1.xyz, a2.xyz), 0.97, tolerance) cntr += 1 if (n1 == "O" and n2 in ["D1", "D2"] and a1.resname == "HOH"): assert approx_equal(dist(a1.xyz, a2.xyz), 0.85, tolerance) cntr += 1 assert cntr == 11, cntr # check X-H distances: neutron cntr = 0 awl = iotbx.pdb.input(file_name="output_N.pdb").atoms_with_labels() for a1 in awl: n1 = a1.name.strip() for a2 in awl: n2 = a2.name.strip() if ([n1, n2] == ["CD1", "HD1"] or [n1, n2] == ["CD2", "HD2"] or [n1, n2] == ["CE1", "HE1"] or [n1, n2] == ["CE2", "HE2"]): assert approx_equal(dist(a1.xyz, a2.xyz), 1.08, tolerance), """ dist(a1.xyz, a2.xyz) : %f tolerance : %f """ % ( dist(a1.xyz, a2.xyz), tolerance, ) cntr += 1 if (n1 == "OH" and n2 in ["HH", "DH"]): assert approx_equal(dist(a1.xyz, a2.xyz), 0.98, tolerance) cntr += 1 if ([n1, n2] == ["CB", "HB2"] or [n1, n2] == ["CB", "HB3"] or [n1, n2] == ["CA", "HA"]): assert approx_equal(dist(a1.xyz, a2.xyz), 1.09, tolerance) cntr += 1 if (n1 == "O" and n2 in ["D1", "D2"] and a1.resname == "HOH"): assert approx_equal(dist(a1.xyz, a2.xyz), 0.98, tolerance) cntr += 1 assert cntr == 11, cntr
def __init__(self, model, target_map, refine_ncs_operators=False, number_of_cycles=1, cycles_to_converge=2, log=None): # completely new way of doing this. using RSR macro-cycle # for test compatibility: print("Minimizing using reference map...", file=log) if model.ncs_constraints_present(): print(" Minimizing... (NCS)", file=log) else: print(" Minimizing...", file=log) from phenix.refinement.macro_cycle_real_space import run as rsr_mc_run import scitbx.math from phenix.command_line.real_space_refine import extract_rigid_body_selections from phenix.command_line.real_space_refine import master_params as rsr_master_params from mmtbx.refinement.real_space.utils import target_map as rsr_target_map import mmtbx.idealized_aa_residues.rotamer_manager sin_cos_table = scitbx.math.sin_cos_table(n=10000) params = rsr_master_params().extract() params.pdb_interpretation = model._pdb_interpretation_params.pdb_interpretation params.refinement.run = "minimization_global+local_grid_search" params.refine_ncs_operators = False params.output.write_all_states = True rotamer_manager = mmtbx.idealized_aa_residues.rotamer_manager.load( rotamers="favored") rigid_body_selections = extract_rigid_body_selections( params=params, ncs_groups=model.get_ncs_groups(), pdb_hierarchy=model.get_hierarchy()) rsr_tm = rsr_target_map(map_data=target_map, xray_structure=model.get_xray_structure(), d_min=3.8, atom_radius=params.refinement.atom_radius) res = rsr_mc_run(params=params, model=model, target_map=rsr_tm, log=log, ncs_groups=model.get_ncs_groups(), rotamer_manager=rotamer_manager, sin_cos_table=sin_cos_table, rigid_body_selections=rigid_body_selections) model.set_sites_cart_from_hierarchy(res.model.get_hierarchy()) res.structure_monitor.states_collector.write( file_name="rsr_all_states.pdb") return # end =================================================== from mmtbx.refinement.geometry_minimization import add_rotamer_restraints from mmtbx.refinement.minimization_monitor import minimization_monitor self.model = model self.log = log print("Minimizing using reference map...", file=self.log) self.log.flush() # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py: # minimize_wrapper_for_ramachandran self.model.get_restraints_manager().geometry.pair_proxies( sites_cart=self.model.get_sites_cart()) ncs_restraints_group_list = self.model.get_ncs_groups() if ncs_restraints_group_list is None: ncs_restraints_group_list = [] ncs_groups = None if len(ncs_restraints_group_list) > 0: ncs_groups = ncs_restraints_group_list min_monitor = minimization_monitor( number_of_cycles=number_of_cycles, max_number_of_cycles=20, cycles_to_converge=cycles_to_converge, mode="no_outliers") selection_real_space = None import mmtbx.refinement.real_space.weight self.w = 1 print("number_of_cycles", number_of_cycles, file=log) print("Stats before minimization:", file=log) ms = self.model.geometry_statistics() ms.show(log=log) while min_monitor.need_more_cycles(): print("Cycle number", min_monitor.get_current_cycle_n(), file=self.log) self.model.get_restraints_manager().geometry.\ update_ramachandran_restraints_phi_psi_targets( hierarchy=self.model.get_hierarchy()) print(" Updating rotamer restraints...", file=self.log) add_rotamer_restraints( pdb_hierarchy=self.model.get_hierarchy(), restraints_manager=self.model.get_restraints_manager(), selection=None, sigma=5, mode="fix_outliers", accept_allowed=False, mon_lib_srv=self.model.get_mon_lib_srv(), rotamer_manager=self.model.get_rotamer_manager()) self.model.set_sites_cart_from_hierarchy() if min_monitor.need_weight_optimization(): # if self.w is None: print(" Determining weight...", file=self.log) self.log.flush() self.weight = mmtbx.refinement.real_space.weight.run( map_data=target_map, xray_structure=self.model.get_xray_structure(), pdb_hierarchy=self.model.get_hierarchy(), geometry_restraints_manager=self.model. get_restraints_manager(), rms_bonds_limit=0.015, rms_angles_limit=1.0, ncs_groups=ncs_groups) # division is to put more weight onto restraints. Checked. Works. self.w = self.weight.weight / 3.0 # self.w = self.weight.weight/15.0 # self.w = 0 # self.w = self.weight.weight for s in self.weight.msg_strings: print(s, file=self.log) if ncs_restraints_group_list is None or len( ncs_restraints_group_list) == 0: #No NCS print(" Minimizing...", file=self.log) print(" with weight %f" % self.w, file=self.log) self.log.flush() refine_object = simple( target_map=target_map, selection=None, max_iterations=150, geometry_restraints_manager=self.model. get_restraints_manager().geometry, selection_real_space=selection_real_space, states_accumulator=None, ncs_groups=ncs_groups) refine_object.refine( weight=self.w, xray_structure=self.model.get_xray_structure()) self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds( ) print("RMSDS:", self.rmsd_bonds_final, self.rmsd_angles_final, file=log) # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers()) self.model.set_sites_cart(refine_object.sites_cart(), update_grm=True) # print >> log, "sizes", self.xrs.scatterers() else: # Yes NCS # copy-paste from macro_cycle_real_space.py # !!! Don't rearrange NCS groups here because master was just fixed! # import mmtbx.ncs.ncs_utils as nu # nu.get_list_of_best_ncs_copy_map_correlation( # ncs_groups = ncs_restraints_group_list, # xray_structure = self.model.get_xray_structure(), # map_data = target_map, # d_min = 3) print(" Minimizing... (NCS)", file=self.log) tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\ target_function_and_grads_real_space( map_data = target_map, xray_structure = self.model.get_xray_structure(), ncs_restraints_group_list = ncs_restraints_group_list, refine_selection = None, real_space_gradients_delta = 1, restraints_manager = self.model.get_restraints_manager(), data_weight = self.w, refine_sites = True) minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs( target_and_grads_object=tfg_obj, xray_structure=self.model.get_xray_structure(), ncs_restraints_group_list=ncs_restraints_group_list, refine_selection=None, finite_grad_differences_test=False, max_iterations=100, refine_sites=True) self.model.set_sites_cart(tfg_obj.xray_structure.sites_cart()) ncs_restraints_group_list.recalculate_ncs_transforms( self.model.get_sites_cart()) ms = self.model.geometry_statistics() min_monitor.save_cycle_results(geometry=ms) ms.show(log=log)
def exercise_set_sites_cart_ncs_with_extra_atoms(): inp = iotbx.pdb.input(lines=test_pdb_6, source_info=None) model = mmtbx.model.manager(model_input=inp) model.search_for_ncs() nrgl = model.get_ncs_groups() nrgl._show(brief=False) print('n model atoms:', model.get_number_of_atoms()) print('n master atoms:', model.get_master_selection().count(True)) print('n master atoms:', model.get_master_selection().iselection().size()) print('n master isel:', list(model.get_master_selection().iselection())) print('n master atoms:', model.get_master_hierarchy().atoms_size()) print('n master hierarchy:\n', model.get_master_hierarchy().as_pdb_string()) # Note that "HETATM 32 C2 NDG H" and "HETATM 35 C2 NDG L" # don't belong to any master/copy assert not show_diff( model.get_master_hierarchy().as_pdb_string(), """\ ATOM 1 N ASP H 5 91.286 -31.834 73.572 1.00 77.83 N ATOM 2 CA ASP H 5 90.511 -32.072 72.317 1.00 78.04 C ATOM 3 C ASP H 5 90.136 -30.762 71.617 1.00 77.70 C ATOM 4 O ASP H 5 89.553 -29.857 72.225 1.00 77.56 O ATOM 5 N THR H 6 91.286 -31.834 73.572 1.00 77.83 N ATOM 6 CA THR H 6 90.511 -32.072 72.317 1.00 78.04 C TER ATOM 7 N GLY I 501 91.286 -31.834 73.572 1.00 77.83 N ATOM 8 CA GLY I 501 90.511 -32.072 72.317 1.00 78.04 C ATOM 9 C GLY I 501 90.136 -30.762 71.617 1.00 77.70 C ATOM 10 O GLY I 501 89.553 -29.857 72.225 1.00 77.56 O TER HETATM 31 C1 NDG H 640 91.286 -31.834 73.572 1.00 77.83 C HETATM 32 C2 NDG H 640 91.286 -31.834 73.572 1.00 77.83 C HETATM 35 C2 NDG L 646 61.028 -14.273 81.262 1.00 69.80 C """) mh = model.get_master_hierarchy() # Note that atoms outside NCS are getting 2.0 as xyz new_sites_cart = flex.vec3_double( [(1.0, 1.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0, 1.0)] + [(3.0, 3.0, 3.0)] * 4 + [ (1.0, 1.0, 1.0), # <--- Note this atom belongs the first NCS group (2.0, 2.0, 2.0), (2.0, 2.0, 2.0) ]) mh.atoms().set_xyz(new_sites_cart) # print('='*80) # print (mh.as_pdb_string()) model.set_sites_cart_from_hierarchy(multiply_ncs=True) h = model.get_hierarchy() new_xyz = h.atoms().extract_xyz() # print(model.model_as_pdb()) # print (list(new_xyz)) assert approx_equal(new_xyz[31], (2.0, 2.0, 2.0), eps=1e-4) assert approx_equal(new_xyz[34], (2.0, 2.0, 2.0), eps=1e-4) assert approx_equal(new_xyz[0], (1.0, 1.0, 1.0), eps=1e-4) assert approx_equal(new_xyz[5], (1.0, 1.0, 1.0), eps=1e-4) assert approx_equal(new_xyz[6], (3.0, 3.0, 3.0), eps=1e-4) assert approx_equal(new_xyz[9], (3.0, 3.0, 3.0), eps=1e-4) assert nrgl.check_for_max_rmsd(sites_cart=new_xyz, chain_max_rmsd=0.0) for i in [[0, 1, 2, 3, 4, 5, 30], [10, 11, 12, 13, 14, 15, 32], [20, 21, 22, 23, 24, 25, 33]]: for j in i: assert approx_equal(new_xyz[j], new_xyz[i[0]], eps=1e-4) for i in [[6, 7, 8, 9], [16, 17, 18, 19], [26, 27, 28, 29]]: for j in i: assert approx_equal(new_xyz[j], new_xyz[i[0]], eps=1e-4)