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_convert_to_isotropic(): pdb_str = """ ATOM 104 N SER A 8 14.526 19.060 18.223 1.00 5.10 N ANISOU 104 N SER A 8 500 632 808 -107 58 104 N ATOM 105 CA SER A 8 14.099 17.792 18.758 1.00 5.95 C """ cs = crystal.symmetry((5.01, 5.01, 5.47, 90, 90, 120), "P 62 2 2") pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) model = mmtbx.model.manager(model_input=pdb_inp, crystal_symmetry=cs) # m = model.deep_copy() m.convert_to_isotropic() assert approx_equal(m.get_hierarchy().atoms().extract_b(), [5.105875, 5.95]) assert approx_equal(m.get_hierarchy().atoms().extract_uij(), [(-1.0, -1.0, -1.0, -1.0, -1.0, -1.0), (-1.0, -1.0, -1.0, -1.0, -1.0, -1.0)]) # m = model.deep_copy() m.convert_to_isotropic(selection=flex.bool([False, True])) assert approx_equal(m.get_hierarchy().atoms().extract_b(), model.get_hierarchy().atoms().extract_b()) assert approx_equal(m.get_hierarchy().atoms().extract_uij(), model.get_hierarchy().atoms().extract_uij()) # m1 = model.deep_copy() m2 = model.deep_copy() m1.convert_to_isotropic(selection=flex.bool([True, False])) m2.convert_to_isotropic() assert approx_equal(m1.get_hierarchy().atoms().extract_b(), m2.get_hierarchy().atoms().extract_b()) assert approx_equal(m1.get_hierarchy().atoms().extract_uij(), m2.get_hierarchy().atoms().extract_uij())
def main(filename=None): if filename is None: filename = 'ala_term.pdb' with open(filename, 'w') as f: f.write(pdb_str) pdb_inp = pdb.input(filename) hierarchy = pdb_inp.construct_hierarchy() params = mmtbx.model.manager.get_default_pdb_interpretation_params() params.pdb_interpretation.use_neutron_distances = True params.pdb_interpretation.restraints_library.cdl = False print(len(hierarchy.atoms())) asc = hierarchy.atom_selection_cache() sel = asc.selection("element H or element D") print('sel len', len(sel)) print(len(hierarchy.atoms())) hierarchy.show() model = mmtbx.model.manager( model_input=None, pdb_hierarchy=hierarchy, crystal_symmetry=pdb_inp.crystal_symmetry(), log=null_out(), ) print('m1', len(model.get_hierarchy().atoms())) for atom in model.get_hierarchy().atoms(): print(atom.format_atom_record()) model.process(make_restraints=True, grm_normalization=True, pdb_interpretation_params=params) print('m2', len(model.get_hierarchy().atoms())) for atom in model.get_hierarchy().atoms(): print(atom.format_atom_record()) model.idealize_h_riding() model.set_occupancies(0., selection=sel)
def get_inputs(args, log, master_params, need_map=True, need_model_hierarchy=True, need_crystal_symmetry=True): """ Eventually, this will be centralized. """ inputs = mmtbx.utils.process_command_line_args(args=args, master_params=master_params) # Model pdb_file_name, pdb_hierarchy = None, None if (need_model_hierarchy): file_names = inputs.pdb_file_names if (len(file_names) != 1): raise Sorry("One model (PDB or mmCIF) required.") pdb_file_name = file_names[0] pdb_inp = iotbx.pdb.input(file_name=pdb_file_name) pdb_hierarchy = pdb_inp.construct_hierarchy() # Map map_inp = None if (need_map): if (inputs.ccp4_map is None): raise Sorry("Map file has to given.") map_inp = inputs.ccp4_map broadcast(m="Input map:", log=log) map_inp.show_summary(prefix=" ") # Crystal symmetry crystal_symmetry = None if (need_crystal_symmetry): crystal_symmetry = inputs.crystal_symmetry if (crystal_symmetry is None): raise Sorry("No box (unit cell) info found.") # model = None if (pdb_inp is not None): model = mmtbx.model.manager(model_input=pdb_inp) broadcast(m="Input PDB:", log=log) print >> log, pdb_file_name # ideally this should not be available here model.get_hierarchy().show(level_id="chain") crystal_symmetry = cl_mtriage.check_and_set_crystal_symmetry( models=[model], map_inps=[map_inp]) # Crystal symmetry broadcast(m="Box (unit cell) info:", log=log) inputs.crystal_symmetry.show_summary(f=log) base = map_and_model.input(map_data=map_inp.map_data(), model=model, crystal_symmetry=crystal_symmetry, box=True) params = inputs.params.extract() return group_args(params=params, pdb_file_name=pdb_file_name, map_data=base.map_data(), model=base.model(), crystal_symmetry=base.crystal_symmetry())
def tst_1(prefix="gm_ncs_constr_tst1"): log = sys.stdout pdb_in = iotbx.pdb.input(source_info=None, lines=pdb_string1.split('\n')) # print dir(pdb_in) pdb_int_params = mmtbx.model.manager.get_default_pdb_interpretation_params( ) pdb_int_params.pdb_interpretation.ncs_search.enabled = True model = mmtbx.model.manager(model_input=pdb_in, pdb_interpretation_params=pdb_int_params, build_grm=True) ncs_obj = iotbx.ncs.input(hierarchy=model.get_hierarchy()) original_ncs_transform = ncs_obj.ncs_transform ncs_restraints_group_list = ncs_obj.get_ncs_restraints_group_list() ncs_obj.show(format='phil') grm = model.get_restraints_manager() tmp_xrs = model.get_xray_structure().deep_copy_scatterers() refine_selection = flex.size_t(xrange(model.get_number_of_atoms())) # print "refining sites" cycle = 0 tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\ target_function_and_grads_geometry_minimization( xray_structure=tmp_xrs, ncs_restraints_group_list=ncs_restraints_group_list, refine_selection=refine_selection, restraints_manager=grm.geometry, refine_sites=True, refine_transformations=False, ) minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs( target_and_grads_object=tfg_obj, xray_structure=tmp_xrs, ncs_restraints_group_list=ncs_restraints_group_list, refine_selection=refine_selection, finite_grad_differences_test=False, max_iterations=100, refine_sites=True, refine_transformations=False) refined_pdb_h = model.get_hierarchy().deep_copy() refined_pdb_h.adopt_xray_structure(tmp_xrs) refined_pdb_h.write_pdb_file("refined_%d.pdb" % cycle) new_ncs_obj = iotbx.ncs.input(hierarchy=refined_pdb_h) new_ncs_transform = new_ncs_obj.ncs_transform spec = new_ncs_obj.get_ncs_info_as_spec() for k, v in original_ncs_transform.iteritems(): assert approx_equal(v.r.elems, new_ncs_transform[k].r.elems) assert approx_equal(v.t, new_ncs_transform[k].t) overall_rmsd_after = spec.overall_rmsd() assert overall_rmsd_after < 1e-6
def run(prefix): """ Exercise combined energy and gradients from cluster qm. """ for restraints in ["cctbx", "qm"]: if 0: print("Using restraints:", restraints) result = [] for clustering in [True, False]: if 0: print(" clustering", clustering, "-" * 30) model = get_model() if (restraints == "qm"): fq = from_qm(pdb_hierarchy=model.get_hierarchy(), qm_engine_name="mopac", method="PM3", crystal_symmetry=model.crystal_symmetry(), clustering=clustering) else: fq = from_cctbx( restraints_manager=model.get_restraints_manager()) if (clustering): fm = fragments( working_folder=os.path.split("./ase/tmp_ase.pdb")[0] + "/", clustering_method=betweenness_centrality_clustering, maxnum_residues_in_cluster=8, charge_embedding=False, two_buffers=False, fast_interaction=True, pdb_hierarchy=model.get_hierarchy().deep_copy( ), # deep copy just in case qm_engine_name="mopac", crystal_symmetry=model.crystal_symmetry()) fc = from_cluster(restraints_manager=fq, fragment_manager=fm, parallel_params=get_master_phil().extract()) else: fc = fq energy, gradients = fc.target_and_gradients( sites_cart=model.get_sites_cart()) if (restraints == "qm"): energy = energy * (kcal / mol) * (kcal / mol) / Hartree gradients = gradients * (kcal / mol) * (kcal / mol) * (Bohr / Hartree) gradients = gradients.as_double() result.append(gradients.deep_copy()) # diff = flex.abs(result[0] - result[1]) max_diff = flex.max(diff)
def exercise(pdb_str): pdb_inp = iotbx.pdb.input(lines=pdb_str.split("\n"), source_info=None) model = mmtbx.model.manager(model_input=pdb_inp, log=null_out()) model.process(make_restraints=True) pdb_hierarchy = model.get_hierarchy() sites_cart = model.get_sites_cart() atoms = pdb_hierarchy.atoms() model.setup_riding_h_manager() riding_h_manager = model.get_riding_h_manager() h_parameterization = riding_h_manager.h_parameterization diagnostics = riding_h_manager.diagnostics(sites_cart=sites_cart, threshold=0.05) diagnostics = riding_h_manager.diagnostics(sites_cart=sites_cart, threshold=0.05) h_distances = diagnostics.h_distances number_h = model.get_hd_selection().count(True) number_h_para = len(h_parameterization) - h_parameterization.count(None) # There are 90 H atoms in pdb_string, check if all of them are recognized assert (number_h_para == number_h), 'Not all H atoms are parameterized' for ih in h_distances: labels = atoms[ih].fetch_labels() assert (h_distances[ih] < 0.01), \ 'distance too large: %s atom: %s (%s) residue: %s ' \ % (h_parameterization[ih].htype, atoms[ih].name, ih, labels.resseq.strip())
def exercise(pdb_str, use_ideal_bonds_angles): # -------------------------------------------------------------- # code to switch off CDL # -------------------------------------------------------------- #params_line = grand_master_phil_str #params = iotbx.phil.parse( # input_string=params_line, process_includes=True).extract() #params.pdb_interpretation.restraints_library.cdl=False # --------------------------------------------------------------- pdb_inp = iotbx.pdb.input(lines=pdb_str.split("\n"), source_info=None) model = mmtbx.model.manager(model_input=pdb_inp, log=null_out(), build_grm=True) pdb_hierarchy = model.get_hierarchy() geometry_restraints = model.get_restraints_manager().geometry xray_structure = model.get_xray_structure() sites_cart = model.get_sites_cart() grf = cctbx.geometry_restraints.flags.flags(default=True) minimized = mmtbx.refinement.geometry_minimization.lbfgs( sites_cart=sites_cart, correct_special_position_tolerance=1.0, geometry_restraints_manager=geometry_restraints, geometry_restraints_flags=grf, lbfgs_termination_params=scitbx.lbfgs.termination_parameters( max_iterations=500)) xray_structure.set_sites_cart(sites_cart) pdb_hierarchy.adopt_xray_structure(xray_structure) atoms = pdb_hierarchy.atoms() sites_cart = xray_structure.sites_cart() riding_h_manager = riding.manager( pdb_hierarchy=pdb_hierarchy, geometry_restraints=geometry_restraints, use_ideal_bonds_angles=use_ideal_bonds_angles) h_parameterization = riding_h_manager.h_parameterization diagnostics = riding_h_manager.diagnostics(sites_cart=sites_cart, threshold=0.05) h_distances = diagnostics.h_distances number_h = model.get_hd_selection().count(True) number_h_para = len(h_parameterization) - h_parameterization.count(None) assert (number_h_para == number_h), 'Not all H atoms are parameterized' for ih in h_distances: labels = atoms[ih].fetch_labels() if use_ideal_bonds_angles: assert (h_distances[ih] < 0.03), \ 'distance too large: %s atom: %s (%s) residue: %s ' \ % (h_parameterization[ih].htype, atoms[ih].name, ih, labels.resseq.strip()) else: assert (h_distances[ih] < 1e-7), \ 'distance too large: %s atom: %s (%s) residue: %s distance %s' \ % (h_parameterization[ih].htype, atoms[ih].name, ih, labels.resseq.strip(), h_distances[ih])
def exercise_1(): pdb_inp = iotbx.pdb.input(lines=flex.std_string(pdb_str_1.splitlines()), source_info=None) model = mmtbx.model.manager(model_input=pdb_inp) model.process(make_restraints=True) grm = model.get_restraints_manager().geometry pdb_hierarchy = model.get_hierarchy() sites_cart = model.get_sites_cart() # c-beta restraints are added by default!!! assert len(grm.get_c_beta_torsion_proxies()) == 4 #test global selection and removing c-beta restraints tst_boolsel = pdb_hierarchy.atom_selection_cache().selection("resname TYR") tst_iselection = tst_boolsel.iselection() #test global selection grm2 = grm.select(iselection=tst_iselection) assert len(grm2.get_c_beta_torsion_proxies()) == 2 grm2 = grm.select(selection=tst_boolsel) assert len(grm2.get_c_beta_torsion_proxies()) == 2 #remove a selection grm.remove_c_beta_torsion_restraints_in_place(selection=tst_iselection) assert len(grm.get_c_beta_torsion_proxies()) == 2 #add a selection grm.remove_c_beta_torsion_restraints_in_place() assert len(grm.get_c_beta_torsion_proxies()) == 0 c_beta_torsion_proxies = c_beta.get_c_beta_torsion_proxies( pdb_hierarchy, selection=tst_iselection, sigma=2.5) assert len(c_beta_torsion_proxies) == 2
def get_pdb_inputs(pdb_str): pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) model = mmtbx.model.manager(model_input = pdb_inp, log = null_out(), build_grm=True) return group_args( ph = model.get_hierarchy(), grm = model.get_restraints_manager(), xrs = model.get_xray_structure())
def remove_clashing(model, dist_min): h = model.get_hierarchy() # Remove overlapping water-water wsel = h.atom_selection_cache().selection(string="water") wh = h.select(wsel) sel = [] for i, ai in enumerate(wh.atoms()): for j, aj in enumerate(wh.atoms()): if (j >= i or ai.i_seq == aj.i_seq): continue d = ai.distance(aj) if (d < dist_min): sel_ = [ai.i_seq, aj.i_seq] sel_.sort() sel.append(sel_) sel_clash = flex.size_t([s[0] for s in sel]) # Remove overlapping water-nonwater nonwa = h.select(~wsel).atoms() sel = flex.size_t() for aw in wh.atoms(): for ap in nonwa: d = aw.distance(ap) if (d < dist_min): sel.append(aw.i_seq) break sel_clash.extend(sel) # return model.select(~flex.bool(h.atoms_size(), sel_clash))
def run(args): if (len(args) != 1): raise RuntimeError("Please specify one pdb file name.") model_filename = args[0] pdb_inp = iotbx.pdb.input(file_name=model_filename) model = mmtbx.model.manager(model_input=pdb_inp) model.composition().show(log=sys.stdout) pdb_hierarchy = model.get_hierarchy() print('\nLoop over hierarchy:') for model in pdb_hierarchy.models(): for chain in model.chains(): print('Chain: ', chain.id) for rg in chain.residue_groups(): print(' Resnumber: ', rg.resid()) for ag in rg.atom_groups(): if (ag.resname in aa_resnames): print(' Resname: %s, Altloc: %s' % (ag.resname, ag.altloc)) for atom in ag.atoms(): if (atom.name not in ala_atom_names): print(' %s' % atom.name) ag.remove_atom(atom=atom) #print(help(pdb_hierarchy.write_pdb_file)) pdb_hierarchy.write_pdb_file(file_name='polyala.pdb')
def __init__( self, model, rs_values=[2.0, 3.8, 5.2, 5.5, 6.2, 7.0, 8.6, 10.0], # probe distances (A) for radial radial_eta=4, cutoff=8.1, # radial cutoff distance ts_values=[0.392699, 1.178097, 1.963495, 2.748894], # probe angles (rad) for angular angular_rs_values=[3.8, 5.2, 5.5, 6.2], # probe distances (A) for angular angular_eta=4, angular_zeta=8, # ??? ): self.hierarchy = model.get_hierarchy() self.geometry_restraints_manager = model.get_restraints_manager( ).geometry self.rs_values = rs_values self.radial_eta = radial_eta # NOT USED!!! # self.Rj = Rj self.cutoff = cutoff self.ts_values = ts_values self.angular_rs_values = angular_rs_values self.angular_eta = angular_eta self.angular_zeta = angular_zeta self.EAEVs = format_class(length_of_radial=len(self.rs_values)) self.MAEVs = format_class(length_of_radial=len(self.rs_values)) self.BAEVs = format_class(length_of_radial=len(self.rs_values)) self.center_atom = None self.chain_hierarchy = None self.generate_AEV()
def exclude_h_on_SS(model): rm = model.get_restraints_manager() bond_proxies_simple, asu = rm.geometry.get_all_bond_proxies( sites_cart=model.get_sites_cart()) els = model.get_hierarchy().atoms().extract_element() ss_i_seqs = [] all_proxies = [p for p in bond_proxies_simple] for proxy in asu: all_proxies.append(proxy) for proxy in all_proxies: if (isinstance(proxy, ext.bond_simple_proxy)): i, j = proxy.i_seqs elif (isinstance(proxy, ext.bond_asu_proxy)): i, j = proxy.i_seq, proxy.j_seq else: assert 0 # never goes here if ([els[i], els[j] ].count("S") == 2): # XXX may be coordinated if metal edits used ss_i_seqs.extend([i, j]) sel_remove = flex.size_t() for proxy in all_proxies: if (isinstance(proxy, ext.bond_simple_proxy)): i, j = proxy.i_seqs elif (isinstance(proxy, ext.bond_asu_proxy)): i, j = proxy.i_seq, proxy.j_seq else: assert 0 # never goes here if (els[i] in ["H", "D"] and j in ss_i_seqs): sel_remove.append(i) if (els[j] in ["H", "D"] and i in ss_i_seqs): sel_remove.append(j) return model.select(~flex.bool(model.size(), sel_remove))
def exercise3(pdb_str, type_list_known): pdb_inp = iotbx.pdb.input(lines=pdb_str.split("\n"), source_info=None) params = mmtbx.model.manager.get_default_pdb_interpretation_scope( ).extract() params.pdb_interpretation.allow_polymer_cross_special_position = True model = mmtbx.model.manager(model_input=pdb_inp, log=null_out()) model.process(pdb_interpretation_params=params, make_restraints=True) pdb_hierarchy = model.get_hierarchy() sites_cart = model.get_sites_cart() atoms = pdb_hierarchy.atoms() model.setup_riding_h_manager() riding_h_manager = model.get_riding_h_manager() h_para = riding_h_manager.h_parameterization diagnostics = riding_h_manager.diagnostics(sites_cart=sites_cart, threshold=0.05) h_distances = diagnostics.h_distances type_list = diagnostics.type_list number_h = model.get_hd_selection().count(True) number_h_para = len(h_para) - h_para.count(None) assert (number_h_para == number_h - 2), 'Not all H atoms are parameterized' for ih in h_distances: labels = atoms[ih].fetch_labels() assert (h_distances[ih] < 0.2), \ 'distance too large: %s atom: %s (%s) residue: %s ' \ % (h_para[ih].htype, atoms[ih].name, ih, labels.resseq.strip()) for type1, type2 in zip(type_list, type_list_known): assert (type1 == type2)
def get_pdb_inputs(pdb_str): pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str.split('\n')) model = mmtbx.model.manager(model_input=pdb_inp) model.process(make_restraints=True) return group_args(ph=model.get_hierarchy(), grm=model.get_restraints_manager(), xrs=model.get_xray_structure())
def exercise(pdb_str): params = mmtbx.model.manager.get_default_pdb_interpretation_params() params.pdb_interpretation.use_neutron_distances = True pdb_inp = iotbx.pdb.input(lines=pdb_str.split("\n"), source_info=None) model = mmtbx.model.manager(model_input=pdb_inp, log=null_out()) model.process(pdb_interpretation_params=params, make_restraints=True) pdb_hierarchy = model.get_hierarchy() sites_cart = model.get_sites_cart() atoms = pdb_hierarchy.atoms() model.setup_riding_h_manager() riding_h_manager = model.get_riding_h_manager() h_parameterization = riding_h_manager.h_parameterization diagnostics = riding_h_manager.diagnostics(sites_cart=sites_cart, threshold=0.05) h_distances = diagnostics.h_distances number_h = model.get_hd_selection().count(True) number_h_para = len(h_parameterization) - h_parameterization.count(None) if (pdb_str != pdb_str_02): assert (number_h_para == number_h), 'Not all H atoms are parameterized' # For each H atom, check if distance between computed H and that in input model is # not too large for ih in h_distances: labels = atoms[ih].fetch_labels() assert (h_distances[ih] < 0.1), \ 'distance too large: %s atom: %s (%s) residue: %s ' \ % (h_parameterization[ih].htype, atoms[ih].name, ih, labels.resseq.strip())
def run(args, params=None, out=sys.stdout, log=sys.stderr): if (((len(args) == 0) and (params is None)) or ((len(args) > 0) and ((args[0] == "-h") or (args[0] == "--help")))): show_usage() return if (params is None): pcl = iotbx.phil.process_command_line_with_files( args=args, master_phil_string=master_phil_str, pdb_file_def="file_name") work_params = pcl.work.extract() # or use parameters defined by GUI else: work_params = params pdb_inp = iotbx.pdb.input(file_name=work_params.file_name) pdb_int_params = mmtbx.model.manager.get_default_pdb_interpretation_params( ) pdb_int_params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None model = mmtbx.model.manager(model_input=pdb_inp, pdb_interpretation_params=pdb_int_params, build_grm=True) fixed_pdb_h = model.get_hierarchy().deep_copy() fixed_pdb_h.reset_atom_i_seqs() fixed_pdb_h = fix_rotamer_outliers( model=model, radius=work_params.radius, ) res_pdb_str = model.model_as_pdb() with open("%s.pdb" % work_params.output_prefix, "w") as f: f.write(res_pdb_str)
def run(maxnum_residues_in_cluster): result = [] for clustering in [True, False]: if 0: print(" clustering", clustering, "-"*30) model = get_model() fq = from_cctbx(restraints_manager = model.get_restraints_manager()) if(clustering): fm = fragments( working_folder = os.path.split("./ase/tmp_ase.pdb")[0]+ "/", clustering_method = betweenness_centrality_clustering, maxnum_residues_in_cluster = maxnum_residues_in_cluster, altloc_method = "subtract", charge_embedding = False, two_buffers = False, clustering = clustering, pdb_hierarchy = model.get_hierarchy().deep_copy(), qm_engine_name = "mopac", fast_interaction = True, crystal_symmetry = model.crystal_symmetry()) else: fc = fq fc = from_cluster( restraints_manager = fq, fragment_manager = fm, parallel_params = get_master_phil().extract()) energy, gradients = fc.target_and_gradients(sites_cart=model.get_sites_cart()) gradients = gradients.as_double() result.append(gradients.deep_copy()) diff = flex.abs(result[0] - result[1]) max_diff = flex.max(diff) #print " max(diff_grad):", max_diff assert max_diff < 1.e-9
def run(pdb_file_name): pdb_inp = iotbx.pdb.input(file_name=pdb_file_name) params = mmtbx.model.manager.get_default_pdb_interpretation_params() model = mmtbx.model.manager( model_input = pdb_inp, pdb_interpretation_params = params, build_grm = True, stop_for_unknowns = False, log = null_out()) pdb_hierarchy = model.get_hierarchy() sites_cart_dc = model.get_sites_cart().deep_copy() angle = 0. # points_i_seqs = [483] # axis = [479,482] points_i_seqs = [559] axis = [556,557] while angle <= 360: atoms_xyz_tmp = flex.vec3_double(len(points_i_seqs)) atom_xyz_new = rotate_point_around_axis( axis_point_1 = sites_cart_dc[axis[0]], axis_point_2 = sites_cart_dc[axis[1]], point = sites_cart_dc[points_i_seqs[0]], angle = angle, deg = True) print (atom_xyz_new) atoms_xyz_tmp[0] = atom_xyz_new sites_cart_dc[points_i_seqs] = atoms_xyz_tmp[0] model.set_sites_cart(sites_cart = sites_cart_dc) with open(str(angle)+".pdb", "w") as of: print (model.model_as_pdb(),file=of) angle += 30.
def exercise1(pdb_str, cif_str): model = prepare_inputs(pdb_str, cif_str) riding_h_manager = model.get_riding_h_manager() atoms = model.get_hierarchy().atoms() h_para = riding_h_manager.h_parameterization diagnostics = riding_h_manager.diagnostics( sites_cart=model.get_sites_cart(), threshold=0.05) h_distances = diagnostics.h_distances type_list = diagnostics.type_list # number of H atoms number_h = model.get_hd_selection().count(True) number_h_para = len(h_para) - h_para.count(None) assert (number_h_para == number_h), 'Not all H atoms are parameterized' for ih in h_distances: # One atom is expected to be moved if (ih == 16): continue labels = atoms[ih].fetch_labels() assert (h_distances[ih] < 0.1), \ 'distance too large: %s atom: %s (%s) residue: %s ' \ % (h_para[ih].htype, atoms[ih].name, ih, labels.resseq.strip()) for type1, type2 in zip(type_list, type_list_known1): assert (type1 == type2)
def shift_and_box_model(model=None, box_cushion=5, shift_model=True, crystal_symmetry=None): ''' Shift a model near the origin and box around it Use crystal_symmetry if supplied ''' from mmtbx.model import manager as model_manager from scitbx.matrix import col from cctbx import crystal ph = model.get_hierarchy() sites_cart = ph.atoms().extract_xyz() if shift_model: sites_cart = sites_cart - col(sites_cart.min()) + col( (box_cushion, box_cushion, box_cushion)) box_end = col(sites_cart.max()) + col( (box_cushion, box_cushion, box_cushion)) if not crystal_symmetry: a, b, c = box_end crystal_symmetry = crystal.symmetry((a, b, c, 90, 90, 90), 1) ph.atoms().set_xyz(sites_cart) return model_manager(ph.as_pdb_input(), crystal_symmetry=crystal_symmetry, log=null_out())
def exercise3(pdb_str, type_list_known): pdb_inp = iotbx.pdb.input(lines=pdb_str.split("\n"), source_info=None) model = mmtbx.model.manager( model_input = pdb_inp, build_grm = True) pdb_hierarchy = model.get_hierarchy() sites_cart = model.get_sites_cart() atoms = pdb_hierarchy.atoms() model.setup_riding_h_manager() riding_h_manager = model.get_riding_h_manager() h_para = riding_h_manager.h_parameterization diagnostics = riding_h_manager.diagnostics( sites_cart = sites_cart, threshold = 0.05) h_distances = diagnostics.h_distances type_list = diagnostics.type_list number_h = model.get_hd_selection().count(True) number_h_para = len(h_para) - h_para.count(None) assert (number_h_para == number_h-2), 'Not all H atoms are parameterized' for ih in h_distances: labels = atoms[ih].fetch_labels() assert (h_distances[ih] < 0.2), \ 'distance too large: %s atom: %s (%s) residue: %s ' \ % (h_para[ih].htype, atoms[ih].name, ih, labels.resseq.strip()) for type1, type2 in zip(type_list, type_list_known): assert (type1 == type2)
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 remediate_model_object(self, model, convert_to_new): self.residues_dict = {} non_v3_atoms_count = 0 pdb_hierarchy = model.get_hierarchy() #pdb_hierarchy.atoms().reset_i_seq() residue_groups = pdb_hierarchy.residue_groups() for residue_group in residue_groups: atom_groups = residue_group.atom_groups() for atom_group in atom_groups: res_name = atom_group.resname if res_name in self.dna_bases or res_name in self.na_bases: self.remediate_na_atom_group(atom_group, convert_to_new) res_name = atom_group.resname for atom in atom_group.atoms(): if not res_name in self.residues_dict: self.residues_dict[ res_name] = self.build_hash_from_chem_components( res_name, convert_to_new=convert_to_new, build_all_atoms=False) atom_exch_dict = self.residues_dict[res_name] #print(atom.name + str(atom.name+" "+res_name in atom_exch_dict)) if atom.name + " " + res_name in atom_exch_dict: new_entry = atom_exch_dict.get(atom.name + " " + res_name) atom.set_name(new_entry[0:4]) non_v3_atoms_count = non_v3_atoms_count + 1 pdb_hierarchy.sort_atoms_in_place() pdb_hierarchy.atoms_reset_serial() return model
def run(args, out=sys.stdout): for file_name in args: try: assert os.path.exists(file_name) print >> out, "Converting %s to mmCIF format." %file_name pdb_input = iotbx.pdb.pdb_input(file_name=file_name) model = mmtbx.model.manager( model_input = pdb_input) # hierarchy = pdb_input.construct_hierarchy(sort_atoms=False) hierarchy = model.get_hierarchy() pdb_atoms = hierarchy.atoms() pdb_atoms.set_chemical_element_simple_if_necessary() elements = pdb_atoms.extract_element().strip() if (not elements.all_ne("")) : n_missing = elements.count("") raise RuntimeError("Missing element symbol for %d atoms." % n_missing) basename = os.path.splitext(os.path.basename(file_name))[0] txt = model.model_as_mmcif() with open(basename+".cif", 'w') as f: f.write(txt) print >> out, " wrote %s.cif" % basename # except IOError, e: # debugging variant, to see traceback except Exception, e: print >> out, "Error converting %s to mmCIF format:" %file_name print >> out, " ", str(e) continue
def test_mixed(): pdb_inp = iotbx.pdb.input(lines=mixed_ca_and_full.split("\n"), source_info=None) model = mmtbx.model.manager(model_input=pdb_inp) pdb_hierarchy = model.get_hierarchy() twos_count_no_ca = count_residue_sets(pdb_hierarchy=pdb_hierarchy, length=2, allow_poly_ca=False) threes_count_no_ca = count_residue_sets(pdb_hierarchy=pdb_hierarchy, length=3, allow_poly_ca=False) fives_count_no_ca = count_residue_sets(pdb_hierarchy=pdb_hierarchy, length=5, allow_poly_ca=False) twos_count_ca = count_residue_sets(pdb_hierarchy=pdb_hierarchy, length=2, allow_poly_ca=True) threes_count_ca = count_residue_sets(pdb_hierarchy=pdb_hierarchy, length=3, allow_poly_ca=True) fives_count_ca = count_residue_sets(pdb_hierarchy=pdb_hierarchy, length=5, allow_poly_ca=True) print("Check allow_poly_ca=True on ca-only chain") print("twos:%i threes:%i fives:%i" % (twos_count_no_ca, threes_count_no_ca, fives_count_no_ca)) assert [twos_count_no_ca, threes_count_no_ca, fives_count_no_ca] == [3, 1, 0] print("OK") print("Check allow_poly_ca=True on mixed chain") print("twos:%i threes:%i fives:%i" % (twos_count_ca, threes_count_ca, fives_count_ca)) assert [twos_count_ca, threes_count_ca, fives_count_ca] == [11, 8, 4] print("OK")
def exercise_set_b_iso(): pdb_str = """ ATOM 104 N SER A 8 14.526 19.060 18.223 1.00 5.10 N ANISOU 104 N SER A 8 500 632 808 -107 58 104 N ATOM 105 CA SER A 8 14.099 17.792 18.758 1.00 5.95 C """ cs = crystal.symmetry((5.01, 5.01, 5.47, 90, 90, 120), "P 62 2 2") uc = cs.unit_cell() pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) model = mmtbx.model.manager(model_input=pdb_inp, crystal_symmetry=cs) # m = model.deep_copy() bs = flex.double([10, 20]) m.set_b_iso(values=bs) assert approx_equal(m.get_hierarchy().atoms().extract_b(), [-1.0, 20.0]) us = m.get_hierarchy().atoms().extract_uij() assert approx_equal(us[0], model.get_hierarchy().atoms().extract_uij()[0]) assert approx_equal(us[1], (-1.0, -1.0, -1.0, -1.0, -1.0, -1.0)) u_iso_1 = m.get_xray_structure().scatterers().extract_u_iso() b_iso_2 = m.get_hierarchy().atoms().extract_b() assert approx_equal(u_iso_1[0], -1) assert approx_equal(b_iso_2[0], -1) assert approx_equal(adptbx.u_as_b(u_iso_1[1]), 20) assert approx_equal(b_iso_2[1], 20) # m = model.deep_copy() m.convert_to_isotropic() bs = flex.double([10, 20]) sel = flex.bool([True, False]) m.set_b_iso(values=bs, selection=sel) assert approx_equal(m.get_b_iso(), [10, 5.95])
def exercise_06(): """ Test that when building bio-molecule and then finding NCS relations from it, we get the same rotation and translation""" pdb_strings = [pdb_str_4, pdb_str_5] for method, pdb_string in enumerate(pdb_strings): pdb_inp = pdb.input(source_info=None, lines=pdb_string) model = mmtbx.model.manager(pdb_inp, expand_with_mtrix=False) crystal_symmetry = model.crystal_symmetry() # The exact transforms from pdb_string r1_expected = matrix.sqr([ 0.309017, -0.951057, 0.0, 0.951057, 0.309017, -0.0, 0.0, 0.0, 1.0 ]) r2_expected = matrix.sqr([ -0.809017, -0.587785, 0.0, 0.587785, -0.809017, -0.0, 0.0, 0.0, 1.0 ]) t1_expected = matrix.col([0, 0, 7]) t2_expected = matrix.col([0, 0, 0]) # Look at biomt records retrieved from PDB file if method == 0: rec = model._model_input.process_BIOMT_records() model.expand_with_BIOMT_records() h = model.get_hierarchy() else: rec = model._model_input.process_MTRIX_records() model.expand_with_MTRIX_records() h = model.get_hierarchy() r1 = rec.r[1] r2 = rec.r[2] t1 = rec.t[1] t2 = rec.t[2] assert approx_equal(r1, r1_expected, eps=0.001) assert approx_equal(t1, t1_expected, eps=0.1) assert approx_equal(r2, r2_expected, eps=0.001) assert approx_equal(t2, t2_expected, eps=0.1) # Look at the rotation and translation found by the NCS search s = h.as_pdb_string(crystal_symmetry=crystal_symmetry) ncs_obj = ncs.input(hierarchy=pdb.input(source_info=None, lines=s).construct_hierarchy()) nrgl = ncs_obj.get_ncs_restraints_group_list() assert approx_equal(r1_expected, nrgl[0].copies[0].r, eps=0.001) assert approx_equal(t1_expected, nrgl[0].copies[0].t, eps=0.1) assert approx_equal(r2_expected, nrgl[0].copies[1].r, eps=0.001) assert approx_equal(t2_expected, nrgl[0].copies[1].t, eps=0.1) if method == 0: assert nrgl.get_n_groups() == 1 elif method == 1: assert nrgl.get_n_groups() == 2
def exercise_biomt(): inp = iotbx.pdb.input(lines=biomt_txt+ss_txt+atoms_txt, source_info=None) model = mmtbx.model.manager( model_input = inp) assert model.get_number_of_atoms() == 300, model.get_number_of_atoms() assert model.get_hierarchy().atoms_size() == 300 assert model.get_xray_structure().scatterers().size() == 300 ss_ann = model.get_ss_annotation() assert ss_ann.get_n_helices() == 2 assert ss_ann.get_n_sheets() == 1 model.expand_with_BIOMT_records() assert model.get_number_of_atoms() == 900, model.get_number_of_atoms() assert model.get_hierarchy().atoms_size() == 900 assert model.get_xray_structure().scatterers().size() == 900, model.get_xray_structure().scatterers().size() ss_ann = model.get_ss_annotation() assert ss_ann.get_n_helices() == 6 assert ss_ann.get_n_sheets() == 3