def exercise_1(): pdb_file = libtbx.env.find_in_repositories( relative_path="phenix_regression/pdb/1yjp_h.pdb", test=os.path.isfile) mtz_file = libtbx.env.find_in_repositories( relative_path="phenix_regression/reflection_files/1yjp.mtz", test=os.path.isfile) if (None in [pdb_file, mtz_file]) : print "phenix_regression not found, skipping test" return False pdb_in = file_reader.any_file(pdb_file) hierarchy = pdb_in.file_object.hierarchy hierarchy.atoms().reset_i_seq() xrs = pdb_in.file_object.xray_structure_simple() mtz_in = file_reader.any_file(mtz_file) f_obs = mtz_in.file_server.miller_arrays[0] r_free = mtz_in.file_server.miller_arrays[1] r_free = r_free.customized_copy(data=(r_free.data()==1)) fmodel = mmtbx.utils.fmodel_simple( f_obs=f_obs, r_free_flags=r_free, xray_structures=[xrs], scattering_table="n_gaussian") map_stats = real_space_correlation.map_statistics_for_fragment( fragment=hierarchy, fmodel=fmodel) assert approx_equal(map_stats.cc, 0.960, eps=0.01) edm = fmodel.electron_density_map() map1_coeffs = edm.map_coefficients("2mFo-DFc") map1 = map1_coeffs.fft_map( resolution_factor=0.25).apply_sigma_scaling().real_map() map2_coeffs = edm.map_coefficients("Fmodel") map2 = map2_coeffs.fft_map( resolution_factor=0.25).apply_sigma_scaling().real_map() xray_structure = fmodel.xray_structure map_stats2 = real_space_correlation.map_statistics_for_atom_selection( atom_selection=flex.bool(xrs.sites_cart().size(), True), map1=map1, map2=map2, xray_structure=xrs) assert approx_equal(map_stats2.cc, map_stats.cc, 0.01) # XXX other code outside cctbx depends on the current API - do not simply # change the test if this breaks! results = real_space_correlation.simple( fmodel=fmodel, pdb_hierarchy=hierarchy, log=null_out()) assert isinstance(results, list) assert isinstance(results[0], group_args) assert (results[0].n_atoms == 1) assert (results[0].id_str == " A GLY 1 N ") return True
def exercise_1(): pdb_file = libtbx.env.find_in_repositories( relative_path="phenix_regression/pdb/1yjp_h.pdb", test=os.path.isfile) mtz_file = libtbx.env.find_in_repositories( relative_path="phenix_regression/reflection_files/1yjp.mtz", test=os.path.isfile) if (None in [pdb_file, mtz_file]): print "phenix_regression not found, skipping test" return False pdb_in = file_reader.any_file(pdb_file) hierarchy = pdb_in.file_object.hierarchy hierarchy.atoms().reset_i_seq() xrs = pdb_in.file_object.xray_structure_simple() mtz_in = file_reader.any_file(mtz_file) f_obs = mtz_in.file_server.miller_arrays[0] r_free = mtz_in.file_server.miller_arrays[1] r_free = r_free.customized_copy(data=(r_free.data() == 1)) fmodel = mmtbx.utils.fmodel_simple(f_obs=f_obs, r_free_flags=r_free, xray_structures=[xrs], scattering_table="n_gaussian") map_stats = real_space_correlation.map_statistics_for_fragment( fragment=hierarchy, fmodel=fmodel) assert approx_equal(map_stats.cc, 0.960, eps=0.01) edm = fmodel.electron_density_map() map1_coeffs = edm.map_coefficients("2mFo-DFc") map1 = map1_coeffs.fft_map( resolution_factor=0.25).apply_sigma_scaling().real_map() map2_coeffs = edm.map_coefficients("Fmodel") map2 = map2_coeffs.fft_map( resolution_factor=0.25).apply_sigma_scaling().real_map() xray_structure = fmodel.xray_structure map_stats2 = real_space_correlation.map_statistics_for_atom_selection( atom_selection=flex.bool(xrs.sites_cart().size(), True), map1=map1, map2=map2, xray_structure=xrs) assert approx_equal(map_stats2.cc, map_stats.cc, 0.01) # XXX other code outside cctbx depends on the current API - do not simply # change the test if this breaks! overall_cc, results = real_space_correlation.simple( fmodel=fmodel, pdb_hierarchy=hierarchy, log=null_out()) assert isinstance(overall_cc, float) assert isinstance(results, list) assert isinstance(results[0], group_args) assert (results[0].n_atoms == 1) assert (results[0].id_str == " A GLY 1 N ") return True
def exercise_rejoin(): from mmtbx.building.alternate_conformations.tst_build_simple import pdb_raw pdb_in = iotbx.pdb.hierarchy.input(pdb_string=pdb_raw) hierarchy = pdb_in.hierarchy params = alternate_conformations.rejoin_phil.extract() n_modified = alternate_conformations.rejoin_split_single_conformers( pdb_hierarchy=hierarchy, params=params, model_error_ml=0.5, log=null_out()) assert (n_modified == 3), n_modified # Gln5 # split residue 6 without changing coordinates, set occupancy very low chain = hierarchy.only_model().chains()[0] rg6 = chain.residue_groups()[5] ag = rg6.only_atom_group().detached_copy() for atom in ag.atoms(): atom.occ = 0.05 for atom in rg6.atoms(): atom.occ = 0.95 rg6.only_atom_group().altloc = 'A' ag.altloc = 'B' rg6.append_atom_group(ag) n_modified = alternate_conformations.rejoin_split_single_conformers( pdb_hierarchy=hierarchy.deep_copy(), params=params, model_error_ml=0.5, log=null_out()) assert (n_modified == 1), n_modified # now with higher B-factors for all atoms for atom in hierarchy.atoms(): atom.b = atom.b * 10 n_modified = alternate_conformations.rejoin_split_single_conformers( pdb_hierarchy=hierarchy, params=params, log=null_out()) assert (n_modified == 1), n_modified
def exercise_map_utils () : # # UNSTABLE # hierarchy, fmodel = get_1yjp_pdb_and_fmodel() sel_cache = hierarchy.atom_selection_cache() sele = sel_cache.selection("resseq 5 and (name CD or name OE1 or name NE2)") sele_all = sel_cache.selection("resseq 5") fmodel.xray_structure.scale_adp(factor=0.5, selection=sele) fmodel.update_xray_structure(update_f_calc=True) two_fofc_map, fofc_map = building.get_difference_maps(fmodel) map_stats = building.local_density_quality( fofc_map=fofc_map, two_fofc_map=two_fofc_map, atom_selection=sele_all, xray_structure=fmodel.xray_structure) out = StringIO() map_stats.show_atoms_outside_density(out=out, two_fofc_cutoff=3.0) pdb_strs = [ l.split(":")[0] for l in out.getvalue().splitlines() ] assert len(pdb_strs) > 0 # XXX there seems to be a stochastic effect here #assert (pdb_strs == ['pdb=" CA GLN A 5 "', 'pdb=" CB GLN A 5 "', # 'pdb=" CD GLN A 5 "', 'pdb=" NE2 GLN A 5 "']), \ # pdb_strs fc_map = fmodel.map_coefficients(map_type="Fc").fft_map( resolution_factor=0.25).apply_sigma_scaling().real_map_unpadded() assert (map_stats.number_of_atoms_below_fofc_map_level() == 3) assert (map_stats.fraction_of_nearby_grid_points_above_cutoff() > 0.0) stats = building.get_model_map_stats( selection=sele_all, target_map=two_fofc_map, model_map=fc_map, unit_cell=fmodel.xray_structure.unit_cell(), sites_cart=fmodel.xray_structure.sites_cart(), pdb_atoms=hierarchy.atoms())
def load_pdb_structure(id, format="pdb", allow_unknowns=False, local_cache=None): """ Simple utility method to load the PDB hierarchy and xray structure objects directly (without intermediate files). """ data = fetch(id=id, format=format, log=null_out(), local_cache=local_cache) import iotbx.pdb.hierarchy pdb_in = iotbx.pdb.hierarchy.input(pdb_string=data.read()) hierarchy = pdb_in.hierarchy hierarchy.atoms().reset_i_seq() # XXX enable_scattering_type_unknown can be modified here because the PDB # (unfortunately) contains many unknowns which would crash this xray_structure = pdb_in.input.xray_structure_simple( enable_scattering_type_unknown=allow_unknowns) return hierarchy, xray_structure
def exercise_map_utils () : # # UNSTABLE 2x # hierarchy, fmodel = get_1yjp_pdb_and_fmodel() sel_cache = hierarchy.atom_selection_cache() sele = sel_cache.selection("resseq 5 and (name CD or name OE1 or name NE2)") sele_all = sel_cache.selection("resseq 5") fmodel.xray_structure.scale_adp(factor=0.5, selection=sele) fmodel.update_xray_structure(update_f_calc=True) two_fofc_map, fofc_map = building.get_difference_maps(fmodel) map_stats = building.local_density_quality( fofc_map=fofc_map, two_fofc_map=two_fofc_map, atom_selection=sele_all, xray_structure=fmodel.xray_structure) out = StringIO() map_stats.show_atoms_outside_density(out=out, two_fofc_cutoff=3.0) pdb_strs = [ l.split(":")[0] for l in out.getvalue().splitlines() ] assert len(pdb_strs) > 0 # XXX there seems to be a stochastic effect here #assert (pdb_strs == ['pdb=" CA GLN A 5 "', 'pdb=" CB GLN A 5 "', # 'pdb=" CD GLN A 5 "', 'pdb=" NE2 GLN A 5 "']), \ # pdb_strs fc_map = fmodel.map_coefficients(map_type="Fc").fft_map( resolution_factor=0.25).apply_sigma_scaling().real_map_unpadded() assert (map_stats.number_of_atoms_below_fofc_map_level() == 3) assert (map_stats.fraction_of_nearby_grid_points_above_cutoff() > 0.0) stats = building.get_model_map_stats( selection=sele_all, target_map=two_fofc_map, model_map=fc_map, unit_cell=fmodel.xray_structure.unit_cell(), sites_cart=fmodel.xray_structure.sites_cart(), pdb_atoms=hierarchy.atoms())
def load_pdb_structure (id, format="pdb", allow_unknowns=False, local_cache=None) : """ Simple utility method to load the PDB hierarchy and xray structure objects directly (without intermediate files). """ data = fetch(id=id, format=format, log=null_out(), local_cache=local_cache) import iotbx.pdb.hierarchy pdb_in = iotbx.pdb.hierarchy.input(pdb_string=data.read()) hierarchy = pdb_in.hierarchy hierarchy.atoms().reset_i_seq() # XXX enable_scattering_type_unknown can be modified here because the PDB # (unfortunately) contains many unknowns which would crash this xray_structure = pdb_in.input.xray_structure_simple( enable_scattering_type_unknown=allow_unknowns) return hierarchy, xray_structure
def exercise () : from mmtbx.building.alternate_conformations import conformer_generation from mmtbx.monomer_library import server import iotbx.pdb.hierarchy generate_inputs() params = master_phil().extract() mon_lib_srv = server.server() pdb_in = iotbx.pdb.hierarchy.input(file_name="shear_frag_single.pdb") hierarchy = pdb_in.hierarchy pdb_atoms = hierarchy.atoms() pdb_atoms.reset_i_seq() sites_cart = pdb_atoms.extract_xyz() xrs = pdb_in.input.xray_structure_simple() models = [] prev_res = next_res = next_next_res = None for chain in hierarchy.only_model().chains() : residue_groups = chain.residue_groups() n_rg = len(residue_groups) # should be 4 for i_res, residue_group in enumerate(residue_groups) : sites_orig = sites_cart.deep_copy() next_res = next_next_res = None if (i_res < (n_rg - 1)) : next_res = residue_groups[i_res+1].atom_groups()[0] if (i_res < (n_rg - 2)) : next_next_res = residue_groups[i_res+2].atom_groups()[0] atom_groups = residue_group.atom_groups() primary_conf = atom_groups[0] out = StringIO() confs = [] for conf in conformer_generation.generate_single_residue_confs( atom_group=primary_conf, sites_cart=sites_cart.deep_copy(), mon_lib_srv=mon_lib_srv, params=params.torsion_search, prev_residue=prev_res, next_residue=next_res, next_next_residue=next_next_res, backrub=False, shear=True) : conf.show_summary(out=out) confs.append(conf) prev_res = primary_conf if (confs is None) : continue if (i_res == 1) : assert (""" A ILE 7 None 4.0 mt""") for conf in confs : sites_new = sites_cart.set_selected(conf.sites_selection, conf.sites_selected()) pdb_atoms.set_xyz(sites_new) models.append(hierarchy.only_model().detached_copy()) new_hierarchy = iotbx.pdb.hierarchy.root() for i_model, conf in enumerate(models) : conf.id = str(i_model + 1) new_hierarchy.append_model(conf) open("shear_frag_naive_ensemble.pdb", "w").write( new_hierarchy.as_pdb_string())
def exercise_set_charge () : from iotbx import file_reader input_pdb = """ ATOM 1 CL CL X 1 0.000 0.000 0.000 1.00 20.00 CL END """ open("tmp_cl.pdb", "w").write(input_pdb) easy_run.call('phenix.pdbtools tmp_cl.pdb charge_selection="element Cl" charge=-1 --quiet') pdb_in = file_reader.any_file("tmp_cl.pdb_modified.pdb").file_object hierarchy = pdb_in.hierarchy xrs = pdb_in.xray_structure_simple() assert (xrs.scatterers()[0].scattering_type == 'Cl1-') assert (hierarchy.atoms()[0].charge == '1-')
def exercise_set_charge(): from iotbx import file_reader input_pdb = """ ATOM 1 CL CL X 1 0.000 0.000 0.000 1.00 20.00 CL END """ open("tmp_cl.pdb", "w").write(input_pdb) easy_run.call( 'phenix.pdbtools tmp_cl.pdb charge_selection="element Cl" charge=-1 --quiet' ) pdb_in = file_reader.any_file("tmp_cl.pdb_modified.pdb").file_object hierarchy = pdb_in.hierarchy xrs = pdb_in.xray_structure_simple() assert (xrs.scatterers()[0].scattering_type == 'Cl1-') assert (hierarchy.atoms()[0].charge == '1-')
def realign (self, atom_selection_string) : self.atom_selection_string = atom_selection_string ref_atoms = self.reference_hierarchy.atoms() ref_atoms.reset_i_seq() if (self.atom_selection_string is not None) : assert (not self.calpha_only) and (self.backbone_only != True) elif (self.calpha_only) : self.atom_selection_string = "name CA" elif (self.backbone_only) : self.atom_selection_string = \ "name CA or name CB or name C or name N or name O" else : self.atom_selection_string = "all" from scitbx.array_family import flex sel_cache = self.reference_hierarchy.atom_selection_cache() self.atom_selection = sel_cache.selection(self.atom_selection_string) assert (self.atom_selection.count(True) > 0) self.atoms_ref = [] ref_sel = flex.size_t() ref_chain = self.reference_hierarchy.only_model().only_chain() for residue_group in ref_chain.residue_groups() : rg_atoms = residue_group.only_atom_group().atoms() for atom in rg_atoms : if (not self.atom_selection[atom.i_seq]) : continue resid = residue_group.resid() self.atoms_ref.append("%s %s" % (resid, atom.name.strip())) ref_sel.append(atom.i_seq) assert (len(ref_sel) > 0) sites_ref = ref_atoms.extract_xyz() self.reference_sites = sites_ref.select(ref_sel) sites_moved = easy_mp.pool_map( iterable=range(len(self.related_chains)), fixed_func=self.align_model, processes=self.nproc) self.selection_moved = [] for k, lsq_fit in enumerate(sites_moved) : hierarchy = self.related_chains[k].pdb_hierarchy if (lsq_fit is None) : print >> self.log, "No LSQ fit for model %s:%s" % \ (self.related_chains[k].source_info, self.related_chains[k].chain_id) continue self.selection_moved.append(k) pdb_atoms = hierarchy.atoms() sites_cart = lsq_fit.r.elems * pdb_atoms.extract_xyz() + lsq_fit.t.elems pdb_atoms.set_xyz(sites_cart)
def realign(self, atom_selection_string): self.atom_selection_string = atom_selection_string ref_atoms = self.reference_hierarchy.atoms() ref_atoms.reset_i_seq() if (self.atom_selection_string is not None): assert (not self.calpha_only) and (self.backbone_only != True) elif (self.calpha_only): self.atom_selection_string = "name CA" elif (self.backbone_only): self.atom_selection_string = \ "name CA or name CB or name C or name N or name O" else : self.atom_selection_string = "all" from scitbx.array_family import flex sel_cache = self.reference_hierarchy.atom_selection_cache() self.atom_selection = sel_cache.selection(self.atom_selection_string) assert (self.atom_selection.count(True) > 0) self.atoms_ref = [] ref_sel = flex.size_t() ref_chain = self.reference_hierarchy.only_model().only_chain() for residue_group in ref_chain.residue_groups(): rg_atoms = residue_group.only_atom_group().atoms() for atom in rg_atoms : if (not self.atom_selection[atom.i_seq]): continue resid = residue_group.resid() self.atoms_ref.append("%s %s" % (resid, atom.name.strip())) ref_sel.append(atom.i_seq) assert (len(ref_sel) > 0) sites_ref = ref_atoms.extract_xyz() self.reference_sites = sites_ref.select(ref_sel) sites_moved = easy_mp.pool_map( iterable=range(len(self.related_chains)), fixed_func=self.align_model, processes=self.nproc) self.selection_moved = [] for k, lsq_fit in enumerate(sites_moved): hierarchy = self.related_chains[k].pdb_hierarchy if (lsq_fit is None): print("No LSQ fit for model %s:%s" % \ (self.related_chains[k].source_info, self.related_chains[k].chain_id), file=self.log) continue self.selection_moved.append(k) pdb_atoms = hierarchy.atoms() sites_cart = lsq_fit.r.elems * pdb_atoms.extract_xyz() + lsq_fit.t.elems pdb_atoms.set_xyz(sites_cart)
def exercise_rejoin () : from mmtbx.building.alternate_conformations.tst_build_simple import pdb_raw pdb_in = iotbx.pdb.hierarchy.input(pdb_string=pdb_raw) hierarchy = pdb_in.hierarchy params = alternate_conformations.rejoin_phil.extract() n_modified = alternate_conformations.rejoin_split_single_conformers( pdb_hierarchy=hierarchy, params=params, model_error_ml=0.5, log=null_out()) assert (n_modified == 3), n_modified # Gln5 # split residue 6 without changing coordinates, set occupancy very low chain = hierarchy.only_model().chains()[0] rg6 = chain.residue_groups()[5] ag = rg6.only_atom_group().detached_copy() for atom in ag.atoms() : atom.occ = 0.05 for atom in rg6.atoms() : atom.occ = 0.95 rg6.only_atom_group().altloc = 'A' ag.altloc = 'B' rg6.append_atom_group(ag) n_modified = alternate_conformations.rejoin_split_single_conformers( pdb_hierarchy=hierarchy.deep_copy(), params=params, model_error_ml=0.5, log=null_out()) assert (n_modified == 1), n_modified # now with higher B-factors for all atoms for atom in hierarchy.atoms() : atom.b = atom.b * 10 n_modified = alternate_conformations.rejoin_split_single_conformers( pdb_hierarchy=hierarchy, params=params, log=null_out()) assert (n_modified == 1), n_modified
def __init__( self, hierarchy=None, # XXX warning, ncs_phil_groups can be changed inside... ncs_phil_groups=None, params=None, log=None, ): """ TODO: 1. Transfer get_ncs_info_as_spec() to ncs/ncs.py:ncs Select method to build ncs_group_object order of implementation: 1) ncs_phil_groups - user-supplied definitions are filtered 2) hierarchy only - Performing NCS search Args: ----- ncs_phil_groups: iotbx.phil.parse(ncs_group_phil_str).extract().ncs_group chain_max_rmsd (float): limit of rms difference between chains to be considered as copies min_percent (float): Threshold for similarity between chains similarity define as: (number of matching res) / (number of res in longer chain) chain_similarity_threshold (float): min similarity between matching chains residue_match_radius (float): max allow distance difference between pairs of matching atoms of two residues """ self.number_of_ncs_groups = 0 # consider removing/replacing with function self.ncs_restraints_group_list = class_ncs_restraints_group_list() # keep hierarchy for writing (To have a source of atoms labels) self.hierarchy = hierarchy # residues common to NCS copies. Used for .spec representation self.common_res_dict = {} # Collect messages, recommendation and errors self.messages = '' # Not used outside... self.old_i_seqs = None self.original_hierarchy = None self.truncated_hierarchy = None self.truncated_h_asc = None self.chains_info = None extension = '' # set search parameters self.params = params if self.params is None: self.params = input.get_default_params().ncs_search # if log is None: self.log = sys.stdout else: self.log = log if hierarchy: # for a in hierarchy.atoms(): # print "oo", a.i_seq, a.id_str() # print "=====" hierarchy.atoms().reset_i_seq() self.original_hierarchy = hierarchy.deep_copy() self.original_hierarchy.reset_atom_i_seqs() if self.params.exclude_selection is not None: # pdb_hierarchy_inp.hierarchy.write_pdb_file("in_ncs_pre_before.pdb") cache = hierarchy.atom_selection_cache() sel = cache.selection("not (%s)" % self.params.exclude_selection) self.truncated_hierarchy = hierarchy.select(sel) else: # this could be to save iseqs but I'm not sure self.truncated_hierarchy = hierarchy.select( flex.size_t_range(hierarchy.atoms_size())) self.old_i_seqs = self.truncated_hierarchy.atoms().extract_i_seq() # print "self.old_i_seqs", list(self.old_i_seqs) # self.truncated_hierarchy.atoms().reset_i_seq() self.truncated_hierarchy.reset_atom_i_seqs() self.truncated_h_asc = self.truncated_hierarchy.atom_selection_cache( ) # self.truncated_hierarchy.write_pdb_file("in_ncs_pre_after.pdb") self.chains_info = ncs_search.get_chains_info( self.truncated_hierarchy) if self.truncated_hierarchy.atoms_size() == 0: return # # print "ncs_groups before validation", ncs_phil_groups validated_ncs_phil_groups = None validated_ncs_phil_groups = self.validate_ncs_phil_groups( pdb_h=self.truncated_hierarchy, ncs_phil_groups=ncs_phil_groups, asc=self.truncated_h_asc) if validated_ncs_phil_groups is None: # print "Last chance, building from hierarchy" self.build_ncs_obj_from_pdb_asu(pdb_h=self.truncated_hierarchy, asc=self.truncated_h_asc) # error handling if self.ncs_restraints_group_list.get_n_groups() == 0: print >> self.log, '========== WARNING! ============\n' print >> self.log, ' No NCS relation were found !!!\n' print >> self.log, '================================\n' if self.messages != '': print >> self.log, self.messages
def exercise(): from mmtbx.building.alternate_conformations import density_sampling from mmtbx.utils import fmodel_simple from mmtbx.monomer_library import server from iotbx import file_reader import iotbx.pdb.hierarchy generate_inputs() fmodel_params = """ high_resolution = 1.2 r_free_flags_fraction = 0.1 add_sigmas = True pdb_file = ser_frag.pdb output { label = F type = *real complex file_name = ser_frag.mtz } fmodel.k_sol = 0.3 fmodel.b_sol = 20 """ open("ser_frag_fmodel.eff", "w").write(fmodel_params) assert (easy_run.fully_buffered("phenix.fmodel ser_frag_fmodel.eff"). raise_if_errors().return_code == 0) assert os.path.isfile("ser_frag.mtz") mtz_in = file_reader.any_file("ser_frag.mtz") f_obs = mtz_in.file_server.miller_arrays[0] flags = mtz_in.file_server.miller_arrays[1] flags = flags.customized_copy(data=(flags.data() == 1)) mon_lib_srv = server.server() pdb_in = iotbx.pdb.hierarchy.input(file_name="ser_frag_single.pdb") hierarchy = pdb_in.hierarchy pdb_atoms = hierarchy.atoms() pdb_atoms.reset_i_seq() sites_cart = pdb_atoms.extract_xyz() xrs = pdb_in.input.xray_structure_simple() fmodel = fmodel_simple(f_obs=f_obs, xray_structures=[xrs], scattering_table="n_gaussian", r_free_flags=flags, skip_twin_detection=True) models = [] prev_res = next_res = next_next_res = None for chain in hierarchy.only_model().chains(): residue_groups = chain.residue_groups() n_rg = len(residue_groups) for i_res, residue_group in enumerate(residue_groups): sites_orig = sites_cart.deep_copy() next_res = next_next_res = None if (i_res < (n_rg - 1)): next_res = residue_groups[i_res + 1].atom_groups()[0] if (i_res < (n_rg - 2)): next_next_res = residue_groups[i_res + 2].atom_groups()[0] atom_groups = residue_group.atom_groups() primary_conf = atom_groups[0] out = StringIO() confs = density_sampling.screen_residue( residue=primary_conf, prev_residue=prev_res, next_residue=next_res, next_next_residue=next_next_res, sites_cart=sites_cart, fmodel=fmodel, mon_lib_srv=mon_lib_srv, params=None, backrub=True, shear=False, verbose=True, out=out) prev_res = primary_conf if (confs is None): continue # TODO tweak density sampling to allow a backrubbed conformer with a # chi1 t rotamer for Ser 99 if (i_res == 1): assert (""" A SER 99 20.0 None t""" in out.getvalue()) for conf in confs: sites_new = sites_cart.set_selected(conf.sites_selection, conf.sites_selected()) pdb_atoms.set_xyz(sites_new) models.append(hierarchy.only_model().detached_copy()) confs = density_sampling.screen_residue( residue=primary_conf, prev_residue=prev_res, next_residue=next_res, next_next_residue=next_next_res, sites_cart=sites_cart, fmodel=fmodel, mon_lib_srv=mon_lib_srv, params=None, backrub=False, out=out) if (i_res == 1): print len(confs) new_hierarchy = iotbx.pdb.hierarchy.root() for i_model, conf in enumerate(models): conf.id = str(i_model + 1) new_hierarchy.append_model(conf) open("ser_frag_guided_ensemble.pdb", "w").write(new_hierarchy.as_pdb_string())
reference_sites=sites_cart_pentagon, other_sites=sites_cart) sites_cart_out = sup.other_sites_best_fit() for site_label,site_cart in zip(atom_names, sites_cart_out): atom = iotbx.pdb.hierarchy.atom() atom.name = " %-3s" % site_label atom.xyz = matrix.col(site_cart) + matrix.col((0,0,i_stack*1.5)) atom.occ = 1 atom.b = 20 atom.element = " " + site_label[0] atom_group.append_atom(atom) residue_group = iotbx.pdb.hierarchy.residue_group( resseq="%4d" % (i_stack+1), icode=" ") residue_group.append_atom_group(atom_group) chain.append_residue_group(residue_group) hierarchy.atoms().reset_serial() pdb_str = hierarchy.as_pdb_string(append_end=True) file_name = "puckers.pdb" print "Writing file:", file_name open(file_name, "w").write("""\ REMARK random_puckers.py REMARK 1 = 3' REMARK 2 = 2' REMARK 3 = A REMARK 4 = B """ + pdb_str) # print "OK" if (__name__ == "__main__"): run(args=sys.argv[1:])
def exercise () : from mmtbx.building.alternate_conformations import density_sampling from mmtbx.utils import fmodel_simple from mmtbx.monomer_library import server from iotbx import file_reader import iotbx.pdb.hierarchy generate_inputs() fmodel_params = """ high_resolution = 1.2 r_free_flags_fraction = 0.1 add_sigmas = True pdb_file = shear_frag.pdb output { label = F type = *real complex file_name = shear_frag.mtz } fmodel.k_sol = 0.3 fmodel.b_sol = 20 """ open("shear_frag_fmodel.eff", "w").write(fmodel_params) assert (easy_run.fully_buffered("phenix.fmodel shear_frag_fmodel.eff" ).raise_if_errors().return_code == 0) assert os.path.isfile("shear_frag.mtz") mtz_in = file_reader.any_file("shear_frag.mtz") f_obs = mtz_in.file_server.miller_arrays[0] flags = mtz_in.file_server.miller_arrays[1] flags = flags.customized_copy(data=(flags.data()==1)) mon_lib_srv = server.server() pdb_in = iotbx.pdb.hierarchy.input(file_name="shear_frag_single.pdb") hierarchy = pdb_in.hierarchy pdb_atoms = hierarchy.atoms() pdb_atoms.reset_i_seq() sites_cart = pdb_atoms.extract_xyz() xrs = pdb_in.input.xray_structure_simple() fmodel = fmodel_simple( f_obs=f_obs, xray_structures=[xrs], scattering_table="n_gaussian", r_free_flags=flags, skip_twin_detection=True) models = [] prev_res = next_res = next_next_res = None for chain in hierarchy.only_model().chains() : residue_groups = chain.residue_groups() n_rg = len(residue_groups) # should be 4 for i_res, residue_group in enumerate(residue_groups) : sites_orig = sites_cart.deep_copy() next_res = next_next_res = None if (i_res < (n_rg - 1)) : next_res = residue_groups[i_res+1].atom_groups()[0] if (i_res < (n_rg - 2)) : next_next_res = residue_groups[i_res+2].atom_groups()[0] atom_groups = residue_group.atom_groups() primary_conf = atom_groups[0] out = StringIO() confs = density_sampling.screen_residue( residue=primary_conf, prev_residue=prev_res, next_residue=next_res, next_next_residue=next_next_res, sites_cart=sites_cart, fmodel=fmodel, mon_lib_srv=mon_lib_srv, params=None, backrub=True, shear=True, verbose=True, out=out) prev_res = primary_conf if (confs is None) : continue if (i_res == 1) : assert (""" A ILE 7 None 4.0 mt""") for conf in confs : sites_new = sites_cart.set_selected(conf.sites_selection, conf.sites_selected()) pdb_atoms.set_xyz(sites_new) models.append(hierarchy.only_model().detached_copy()) new_hierarchy = iotbx.pdb.hierarchy.root() for i_model, conf in enumerate(models) : conf.id = str(i_model + 1) new_hierarchy.append_model(conf) open("shear_frag_naive_ensemble.pdb", "w").write( new_hierarchy.as_pdb_string())
def run(args): assert args in [[], ["--verbose"]] if (len(args) != 0): cout = sys.stdout else: cout = null_out() edge_list_bonds = [(0, 1), (0, 4), (1, 2), (2, 3), (3, 4)] bond_list = [(("C1*", "C2*"), 1.529), (("C1*", "O4*"), 1.412), (("C2*", "C3*"), 1.526), (("C3*", "C4*"), 1.520), (("C4*", "O4*"), 1.449)] angle_list = [ (("C1*", "C2*", "C3*"), 101.3), (("C2*", "C3*", "C4*"), 102.3), (("C3*", "C4*", "O4*"), 104.2), (("C4*", "O4*", "C1*"), 110.0) ] sites_cart, geo_manager = cctbx.geometry_restraints.manager \ .construct_non_crystallographic_conserving_bonds_and_angles( sites_cart=sites_cart_3p, edge_list_bonds=edge_list_bonds, edge_list_angles=[]) for bond_atom_names, distance_ideal in bond_list: i, j = [atom_names.index(atom_name) for atom_name in bond_atom_names] bond_params = geo_manager.bond_params_table[i][j] assert approx_equal(bond_params.distance_ideal, distance_ideal, eps=1.e-2) bond_params.distance_ideal = distance_ideal bond_params.weight = 1 / 0.02**2 assert geo_manager.angle_proxies is None geo_manager.angle_proxies = cctbx.geometry_restraints.shared_angle_proxy() for angle_atom_names, angle_ideal in angle_list: i_seqs = [ atom_names.index(atom_name) for atom_name in angle_atom_names ] geo_manager.angle_proxies.append( cctbx.geometry_restraints.angle_proxy(i_seqs=i_seqs, angle_ideal=angle_ideal, weight=1 / 3**2)) geo_manager.show_sorted(site_labels=atom_names, sites_cart=sites_cart, f=cout) def lbfgs(sites_cart): for i_lbfgs_restart in range(3): minimized = cctbx.geometry_restraints.lbfgs.lbfgs( sites_cart=sites_cart, geometry_restraints_manager=geo_manager) assert is_below_limit(value=minimized.final_target_value, limit=1e-10) return minimized lbfgs(sites_cart=sites_cart_3p) lbfgs(sites_cart=sites_cart_2p) conformer_counts = [0] * 4 sites_cart = sites_cart.deep_copy() mt = flex.mersenne_twister(seed=0) for i_trial in range(20): while True: for i in range(sites_cart.size()): sites_cart[i] = mt.random_double_point_on_sphere() try: lbfgs(sites_cart=sites_cart) except RuntimeError as e: if (not str(e).startswith( "Bond distance > max_reasonable_bond_distance: ")): raise else: break rmsd_list = flex.double() for reference_sites in [ sites_cart_3p, sites_cart_2p, sites_cart_a, sites_cart_b ]: sup = scitbx.math.superpose.least_squares_fit( reference_sites=reference_sites, other_sites=sites_cart) rmsd = reference_sites.rms_difference(sup.other_sites_best_fit()) rmsd_list.append(rmsd) oline = " ".join(["%.3f" % rmsd for rmsd in rmsd_list]) print(oline, file=cout) assert is_below_limit(min(rmsd_list), 1e-3) conformer_counts[flex.min_index(rmsd_list)] += 1 print("conformer_counts:", conformer_counts) # if (libtbx.env.has_module("iotbx")): import iotbx.pdb.hierarchy hierarchy = iotbx.pdb.hierarchy.root() model = iotbx.pdb.hierarchy.model(id="") chain = iotbx.pdb.hierarchy.chain(id="A") model.append_chain(chain) hierarchy.append_model(model) # sites_cart_pentagon = pentagon_sites_cart() for i_stack, sites_cart in enumerate( [sites_cart_3p, sites_cart_2p, sites_cart_a, sites_cart_b]): atom_group = iotbx.pdb.hierarchy.atom_group(resname=" U", altloc="") sup = scitbx.math.superpose.least_squares_fit( reference_sites=sites_cart_pentagon, other_sites=sites_cart) sites_cart_out = sup.other_sites_best_fit() for site_label, site_cart in zip(atom_names, sites_cart_out): atom = iotbx.pdb.hierarchy.atom() atom.name = " %-3s" % site_label atom.xyz = matrix.col(site_cart) + matrix.col( (0, 0, i_stack * 1.5)) atom.occ = 1 atom.b = 20 atom.element = " " + site_label[0] atom_group.append_atom(atom) residue_group = iotbx.pdb.hierarchy.residue_group(resseq="%4d" % (i_stack + 1), icode=" ") residue_group.append_atom_group(atom_group) chain.append_residue_group(residue_group) hierarchy.atoms().reset_serial() pdb_str = hierarchy.as_pdb_string(append_end=True) file_name = "puckers.pdb" print("Writing file:", file_name) open(file_name, "w").write("""\ REMARK random_puckers.py REMARK 1 = 3' REMARK 2 = 2' REMARK 3 = A REMARK 4 = B """ + pdb_str) # print("OK")
def combine_ensembles (self, pdb_hierarchy) : """ Iterate over all residues covered by the group, and pick the alternate conformers out for each residue to combine into a continous fragment. If additional conformers are needed, the previously existing atom group will be duplicated as necessary. """ from mmtbx import building from scitbx.array_family import flex hierarchy = pdb_hierarchy.deep_copy() pdb_atoms = hierarchy.atoms() pdb_atoms.reset_i_seq() n_atoms = len(pdb_atoms) covered_selection = flex.bool(pdb_atoms.size(), False) sites_cart = pdb_atoms.extract_xyz().deep_copy() residue_groups = [] n_max = self.n_confs_max() selection = self.get_entire_selection() central_selection = self.get_central_residues_selection() sub_hierarchy = hierarchy.deep_copy().select(selection) sub_hierarchy.atoms().reset_i_seq() sub_iselection = pdb_atoms.extract_i_seq().select(selection) sub_central_selection = flex.bool(pdb_atoms.size(), False).set_selected(central_selection, True).select(sub_iselection) fragment_atom_groups = [] fragment_i_seqs = [] for residue_group in mmtbx.building.iter_residue_groups(sub_hierarchy) : new_atom_groups = [] n_confs = 0 atom_group = residue_group.only_atom_group() ag_new_isel = atom_group.atoms().extract_i_seq() ag_old_isel = sub_iselection.select(ag_new_isel) fragment_i_seqs.append(ag_old_isel) original_sel = flex.bool(n_atoms, False).set_selected(ag_old_isel, True) def new_atom_group (trial, selection) : sites_new = sites_cart.deep_copy() sites_new.set_selected(selection, trial.sites_cart) new_ag = atom_group.detached_copy() new_ag.atoms().set_xyz(sites_new.select(original_sel)) #for k, atom in enumerate(new_ag.atoms()) : # atom.i_seq = ag_old_isel[k] return new_ag if (sub_central_selection.select(ag_new_isel).all_eq(True)) : # residue is the center of the window for a single ensemble, so use # the coordinates from that enemble found_residue = False for ens in self.ensembles : if (original_sel.select(ens.residue_selection).all_eq(True)): assert (not found_residue) for trial in self.sites_trials : new_atom_groups.append(new_atom_group(trial, ens.selection)) n_confs += 1 found_residue = True assert (found_residue) else : # adjacent residue only, so we take the best n_max out of all # conformations (padding with the first conf if necessary) all_trials = [] ens_selection = None for ens in self.ensembles : ens_selection_ = flex.bool(n_atoms, False).set_selected( ens.selection, True) if (ens_selection_.select(ag_old_isel).all_eq(True)) : all_trials.extend(ens.sites_trials) ens_selection = ens.selection break if (len(all_trials) == 0) : raise RuntimeError("No matching atoms found for %s" % atom_group.id_str()) # XXX sort by maximum deviation, or rmsd? all_trials.sort(lambda a,b: cmp(b.max_dev, a.max_dev)) k = 0 while (n_confs < n_max) and (k < len(all_trials)) : trial = all_trials[k] new_atom_groups.append(new_atom_group(trial, ens_selection)) n_confs += 1 k += 1 # fill in the remaining conformations with copies of the first while (n_confs < n_max) : new_ag = atom_group.detached_copy() new_atom_groups.append(new_ag) n_confs += 1 fragment_atom_groups.append(new_atom_groups) return fragment_atom_groups, fragment_i_seqs
cmdline = mmtbx.utils.cmdline_load_pdb_and_data( args=args, master_phil=master_phil, process_pdb_file=False, scattering_table="n_gaussian") params = cmdline.params working_phil = master_phil.format(python_object=params) master_phil.fetch_diff(source=working_phil).show(out=out) fmodel = cmdline.fmodel hierarchy = cmdline.pdb_hierarchy sele_cache = hierarchy.atom_selection_cache() assert (params.selection is not None) selection = sele_cache.selection(params.selection) assert (selection.count(True) > 0) have_results = False pdb_atoms = hierarchy.atoms() sites_cart = pdb_atoms.extract_xyz() ensembles = [] t1 = time.time() for chain in hierarchy.only_model().chains() : prev_residue = next_residue = None residue_groups = chain.residue_groups() n_groups = len(residue_groups) for i_res, residue_group in enumerate(residue_groups) : if (i_res < n_groups - 1) : next_residue = residue_groups[i_res+1].atom_groups()[0] i_seqs = residue_group.atoms().extract_i_seq() if (selection.select(i_seqs).all_eq(True)) : atom_group = residue_group.only_atom_group() if (get_class(atom_group.resname) != "common_amino_acid") : continue
def combine_ensembles(self, pdb_hierarchy): """ Iterate over all residues covered by the group, and pick the alternate conformers out for each residue to combine into a continous fragment. If additional conformers are needed, the previously existing atom group will be duplicated as necessary. """ from mmtbx import building from scitbx.array_family import flex hierarchy = pdb_hierarchy.deep_copy() pdb_atoms = hierarchy.atoms() pdb_atoms.reset_i_seq() n_atoms = len(pdb_atoms) covered_selection = flex.bool(pdb_atoms.size(), False) sites_cart = pdb_atoms.extract_xyz().deep_copy() residue_groups = [] n_max = self.n_confs_max() selection = self.get_entire_selection() central_selection = self.get_central_residues_selection() sub_hierarchy = hierarchy.deep_copy().select(selection) sub_hierarchy.atoms().reset_i_seq() sub_iselection = pdb_atoms.extract_i_seq().select(selection) sub_central_selection = flex.bool(pdb_atoms.size(), False).set_selected(central_selection, True).select(sub_iselection) fragment_atom_groups = [] fragment_i_seqs = [] for residue_group in mmtbx.building.iter_residue_groups(sub_hierarchy): new_atom_groups = [] n_confs = 0 atom_group = residue_group.only_atom_group() ag_new_isel = atom_group.atoms().extract_i_seq() ag_old_isel = sub_iselection.select(ag_new_isel) fragment_i_seqs.append(ag_old_isel) original_sel = flex.bool(n_atoms, False).set_selected(ag_old_isel, True) def new_atom_group(trial, selection): sites_new = sites_cart.deep_copy() sites_new.set_selected(selection, trial.sites_cart) new_ag = atom_group.detached_copy() new_ag.atoms().set_xyz(sites_new.select(original_sel)) #for k, atom in enumerate(new_ag.atoms()): # atom.i_seq = ag_old_isel[k] return new_ag if (sub_central_selection.select(ag_new_isel).all_eq(True)): # residue is the center of the window for a single ensemble, so use # the coordinates from that enemble found_residue = False for ens in self.ensembles : if (original_sel.select(ens.residue_selection).all_eq(True)): assert (not found_residue) for trial in self.sites_trials : new_atom_groups.append(new_atom_group(trial, ens.selection)) n_confs += 1 found_residue = True assert (found_residue) else : # adjacent residue only, so we take the best n_max out of all # conformations (padding with the first conf if necessary) all_trials = [] ens_selection = None for ens in self.ensembles : ens_selection_ = flex.bool(n_atoms, False).set_selected( ens.selection, True) if (ens_selection_.select(ag_old_isel).all_eq(True)): all_trials.extend(ens.sites_trials) ens_selection = ens.selection break if (len(all_trials) == 0): raise RuntimeError("No matching atoms found for %s" % atom_group.id_str()) # XXX sort by maximum deviation, or rmsd? all_trials.sort(key=operator.attrgetter("max_dev"), reverse=True) k = 0 while (n_confs < n_max) and (k < len(all_trials)): trial = all_trials[k] new_atom_groups.append(new_atom_group(trial, ens_selection)) n_confs += 1 k += 1 # fill in the remaining conformations with copies of the first while (n_confs < n_max): new_ag = atom_group.detached_copy() new_atom_groups.append(new_ag) n_confs += 1 fragment_atom_groups.append(new_atom_groups) return fragment_atom_groups, fragment_i_seqs
def run(args, out=None): if (out is None): out = sys.stdout from mmtbx.building.alternate_conformations import density_sampling import mmtbx.maps.utils import mmtbx.utils from mmtbx.monomer_library import server import iotbx.pdb.hierarchy get_class = iotbx.pdb.common_residue_names_get_class mon_lib_srv = server.server() cmdline = mmtbx.utils.cmdline_load_pdb_and_data( args=args, master_phil=master_phil, process_pdb_file=False, scattering_table="n_gaussian") params = cmdline.params working_phil = master_phil.format(python_object=params) master_phil.fetch_diff(source=working_phil).show(out=out) fmodel = cmdline.fmodel hierarchy = cmdline.pdb_hierarchy sele_cache = hierarchy.atom_selection_cache() assert (params.selection is not None) selection = sele_cache.selection(params.selection) assert (selection.count(True) > 0) have_results = False pdb_atoms = hierarchy.atoms() sites_cart = pdb_atoms.extract_xyz() ensembles = [] t1 = time.time() for chain in hierarchy.only_model().chains(): prev_residue = next_residue = None residue_groups = chain.residue_groups() n_groups = len(residue_groups) for i_res, residue_group in enumerate(residue_groups): if (i_res < n_groups - 1): next_residue = residue_groups[i_res + 1].atom_groups()[0] i_seqs = residue_group.atoms().extract_i_seq() if (selection.select(i_seqs).all_eq(True)): atom_group = residue_group.only_atom_group() if (get_class(atom_group.resname) != "common_amino_acid"): continue confs = density_sampling.screen_residue( residue=atom_group, prev_residue=prev_residue, next_residue=next_residue, sites_cart=sites_cart, fmodel=fmodel, mon_lib_srv=mon_lib_srv, params=params, map_file_name="map_coeffs.mtz", out=out) ensemble = [] for conf in confs: conf_atoms = conf.set_atom_sites(pdb_atoms) pdb_lines = [] for atom in conf_atoms: pdb_lines.append(atom.format_atom_record()) ensemble.append("\n".join(pdb_lines)) ensembles.append(ensemble) prev_residue = residue_group.atom_groups()[0] if (len(ensembles) == 0): raise Sorry("No alternate conformations found.") t2 = time.time() print("search time: %.1fs" % (t2 - t1), file=out) for i_ens, ensemble in enumerate(ensembles): ensemble_hierarchy = iotbx.pdb.hierarchy.root() for k, model_str in enumerate(ensemble): input = iotbx.pdb.hierarchy.input(pdb_string=model_str) model = input.hierarchy.only_model().detached_copy() model.id = str(k + 1) ensemble_hierarchy.append_model(model) f = open("ensemble_%d.pdb" % (i_ens + 1), "w") f.write(ensemble_hierarchy.as_pdb_string()) f.close() print("wrote ensemble_%d.pdb" % (i_ens + 1)) if (params.coot): easy_run.call( "coot --pdb %s --auto map_coeffs.mtz --pdb ensemble.pdb" % params.input.pdb.file_name[0])
def run (args, out=None ): if (out is None) : out = sys.stdout from mmtbx.building.alternate_conformations import density_sampling import mmtbx.maps.utils import mmtbx.utils from mmtbx.monomer_library import server import iotbx.pdb.hierarchy get_class = iotbx.pdb.common_residue_names_get_class mon_lib_srv = server.server() cmdline = mmtbx.utils.cmdline_load_pdb_and_data( args=args, master_phil=master_phil, process_pdb_file=False, scattering_table="n_gaussian") params = cmdline.params working_phil = master_phil.format(python_object=params) master_phil.fetch_diff(source=working_phil).show(out=out) fmodel = cmdline.fmodel hierarchy = cmdline.pdb_hierarchy sele_cache = hierarchy.atom_selection_cache() assert (params.selection is not None) selection = sele_cache.selection(params.selection) assert (selection.count(True) > 0) have_results = False pdb_atoms = hierarchy.atoms() sites_cart = pdb_atoms.extract_xyz() ensembles = [] t1 = time.time() for chain in hierarchy.only_model().chains() : prev_residue = next_residue = None residue_groups = chain.residue_groups() n_groups = len(residue_groups) for i_res, residue_group in enumerate(residue_groups) : if (i_res < n_groups - 1) : next_residue = residue_groups[i_res+1].atom_groups()[0] i_seqs = residue_group.atoms().extract_i_seq() if (selection.select(i_seqs).all_eq(True)) : atom_group = residue_group.only_atom_group() if (get_class(atom_group.resname) != "common_amino_acid") : continue confs = density_sampling.screen_residue( residue=atom_group, prev_residue=prev_residue, next_residue=next_residue, sites_cart=sites_cart, fmodel=fmodel, mon_lib_srv=mon_lib_srv, params=params, map_file_name="map_coeffs.mtz", out=out) ensemble = [] for conf in confs : conf_atoms = conf.set_atom_sites(pdb_atoms) pdb_lines = [] for atom in conf_atoms : pdb_lines.append(atom.format_atom_record()) ensemble.append("\n".join(pdb_lines)) ensembles.append(ensemble) prev_residue = residue_group.atom_groups()[0] if (len(ensembles) == 0) : raise Sorry("No alternate conformations found.") t2 = time.time() print >> out, "search time: %.1fs" % (t2-t1) for i_ens, ensemble in enumerate(ensembles) : ensemble_hierarchy = iotbx.pdb.hierarchy.root() for k, model_str in enumerate(ensemble) : input = iotbx.pdb.hierarchy.input(pdb_string=model_str) model = input.hierarchy.only_model().detached_copy() model.id = str(k+1) ensemble_hierarchy.append_model(model) f = open("ensemble_%d.pdb" % (i_ens+1), "w") f.write(ensemble_hierarchy.as_pdb_string()) f.close() print "wrote ensemble_%d.pdb" % (i_ens+1) if (params.coot) : easy_run.call("coot --pdb %s --auto map_coeffs.mtz --pdb ensemble.pdb" % params.input.pdb.file_name[0])