def exclude_outliers_from_reference_restraints_selection( pdb_hierarchy, restraints_selection): from mmtbx.validation.ramalyze import ramalyze # the import below is SLOW!!! from mmtbx.rotamer.rotamer_eval import RotamerEval assert restraints_selection is not None # ramachandran plot outliers rama_outlier_selection = ramalyze(pdb_hierarchy=pdb_hierarchy, outliers_only=False).outlier_selection() rama_outlier_selection = flex.bool(restraints_selection.size(), rama_outlier_selection) # rotamer outliers rota_outlier_selection = flex.size_t() rotamer_manager = RotamerEval() # SLOW!!! for model in pdb_hierarchy.models(): for chain in model.chains(): for residue_group in chain.residue_groups(): conformers = residue_group.conformers() if(len(conformers)>1): continue for conformer in residue_group.conformers(): residue = conformer.only_residue() if(rotamer_manager.evaluate_residue(residue)=="OUTLIER"): rota_outlier_selection.extend(residue.atoms().extract_i_seq()) rota_outlier_selection = flex.bool(restraints_selection.size(), rota_outlier_selection) outlier_selection = rama_outlier_selection | rota_outlier_selection return restraints_selection & (~outlier_selection)
class load(object): def __init__(self, rotamers, residues=None): if(residues is not None): for i, residue in enumerate(residues): residues[i] = residue.lower() assert rotamers in ["favored", "favored_allowed"] self.rotamer_evaluator = RotamerEval() path=libtbx.env.find_in_repositories("chem_data/rotamer_chi_angles") self.result = {} for aa_code in aa_codes: if(residues is not None): if(not aa_code in residues): continue try: if(rotamers=="favored"): f = "%s_favored.pkl"%aa_code elif(rotamers=="favored_allowed"): f = "%s_favored_allowed.pkl"%aa_code else: raise RuntimeError("Not implemented: rotamers=%s"%rotamers) chi_angles = easy_pickle.load(file_name = "/".join([path,f])) except Exception: chi_angles = None self.result.setdefault(aa_code,[]).extend([chi_angles]) def get_chi_angles(self, resname): resname = resname.strip().lower() assert resname in aa_codes return self.result[resname][0] def rotamer(self, residue): return self.rotamer_evaluator.evaluate_residue(residue)
class load(object): def __init__(self, rotamers="all"): assert rotamers in ["all", "favored", "allowed"] self.rotamer_evaluator = RotamerEval() path=libtbx.env.find_in_repositories("mmtbx/idealized_aa_residues/data") self.result = {} for aa_code in aa_codes: try: if( rotamers=="all"): f = "%s-coarse_step10.pickle"%aa_code elif(rotamers=="favored"): f = "%s-coarse_step10_favored.pickle"%aa_code else: raise RuntimeError("Not implemented: rotamers=%s"%rotamers) chi_angles = easy_pickle.load(file_name = "/".join([path,f])) # XXX Fix later chi_angles_ = [] for chi in chi_angles: chi_ = [] for ch in chi: chi_.append(ch*math.pi/180) chi_angles_.append(chi_) chi_angles = chi_angles_ # XXX Fix later except Exception: chi_angles = None self.result.setdefault(aa_code,[]).extend([chi_angles]) def get_chi_angles(self, resname): resname = resname.strip().lower() assert resname in aa_codes return self.result[resname][0] def rotamer(self, residue): return self.rotamer_evaluator.evaluate_residue(residue)
class load(object): def __init__(self): self.rotamer_evaluator = RotamerEval() path = libtbx.env.find_in_repositories( "mmtbx/idealized_aa_residues/data") self.result = {} for aa_code in aa_codes: try: chi_angles = easy_pickle.load(file_name="/".join( [path, "%s-coarse_step10.pickle" % aa_code])) # XXX Fix later chi_angles_ = [] for chi in chi_angles: chi_ = [] for ch in chi: chi_.append(ch * math.pi / 180) chi_angles_.append(chi_) chi_angles = chi_angles_ # XXX Fix later except Exception: chi_angles = None self.result.setdefault(aa_code, []).extend([chi_angles]) def get_chi_angles(self, resname): resname = resname.strip().lower() assert resname in aa_codes return self.result[resname][0] def rotamer(self, residue): return self.rotamer_evaluator.evaluate_residue(residue)
def show(pdb_hierarchy, tm, xrs, grm, prefix): map = compute_map(target_map=tm, xray_structure=xrs) cc = flex.linear_correlation(x=map.as_1d(), y=tm.data.as_1d()).coefficient() es = grm.energies_sites(sites_cart=xrs.sites_cart()) rmsd_a = es.angle_deviations()[2] rmsd_b = es.bond_deviations()[2] print "%s: overall CC: %6.4f rmsd_bonds=%6.3f rmsd_angles=%6.3f" % ( prefix, cc, rmsd_b, rmsd_a) pdb_hierarchy.adopt_xray_structure(xrs) rotamer_manager = RotamerEval() for model in pdb_hierarchy.models(): for chain in model.chains(): for residue in chain.residues(): sites_cart = residue.atoms().extract_xyz() sel = maptbx.grid_indices_around_sites( unit_cell=xrs.unit_cell(), fft_n_real=map.focus(), fft_m_real=map.all(), sites_cart=sites_cart, site_radii=flex.double(sites_cart.size(), 2)) ccr = flex.linear_correlation( x=map.select(sel).as_1d(), y=tm.data.select(sel).as_1d()).coefficient() fmt = "%s: %4s %10s CC: %6.4f" print fmt % (prefix, residue.resname, rotamer_manager.evaluate_residue(residue), ccr)
def exclude_outliers_from_reference_restraints_selection( pdb_hierarchy, restraints_selection): from mmtbx.validation.ramalyze import ramalyze # the import below is SLOW!!! from mmtbx.rotamer.rotamer_eval import RotamerEval assert restraints_selection is not None # ramachandran plot outliers rama_outlier_selection = ramalyze(pdb_hierarchy=pdb_hierarchy, outliers_only=False).outlier_selection() rama_outlier_selection = flex.bool(restraints_selection.size(), rama_outlier_selection) # rotamer outliers rota_outlier_selection = flex.size_t() rotamer_manager = RotamerEval() # SLOW!!! for model in pdb_hierarchy.models(): for chain in model.chains(): for residue_group in chain.residue_groups(): conformers = residue_group.conformers() if (len(conformers) > 1): continue for conformer in residue_group.conformers(): residue = conformer.only_residue() if (rotamer_manager.evaluate_residue(residue) == "OUTLIER" ): rota_outlier_selection.extend( residue.atoms().extract_i_seq()) rota_outlier_selection = flex.bool(restraints_selection.size(), rota_outlier_selection) outlier_selection = rama_outlier_selection | rota_outlier_selection return restraints_selection & (~outlier_selection)
class load(object): def __init__(self): self.rotamer_evaluator = RotamerEval() path=libtbx.env.find_in_repositories("mmtbx/idealized_aa_residues/data") self.result = {} for aa_code in aa_codes: try: chi_angles = easy_pickle.load( file_name="/".join([path,"%s-coarse_step10.pickle"%aa_code])) # XXX Fix later chi_angles_ = [] for chi in chi_angles: chi_ = [] for ch in chi: chi_.append(ch*math.pi/180) chi_angles_.append(chi_) chi_angles = chi_angles_ # XXX Fix later except Exception: chi_angles = None self.result.setdefault(aa_code,[]).extend([chi_angles]) def get_chi_angles(self, resname): resname = resname.strip().lower() assert resname in aa_codes if(resname == "mse"): return self.result["met"][0] # alias return self.result[resname][0] def rotamer(self, residue): return self.rotamer_evaluator.evaluate_residue(residue)
def show(pdb_hierarchy, tm, xrs, grm, prefix): map = compute_map(target_map=tm, xray_structure=xrs) cc = flex.linear_correlation( x=map.as_1d(), y=tm.data.as_1d()).coefficient() es = grm.energies_sites(sites_cart = xrs.sites_cart()) rmsd_a = es.angle_deviations()[2] rmsd_b = es.bond_deviations()[2] print "%s: overall CC: %6.4f rmsd_bonds=%6.3f rmsd_angles=%6.3f"%( prefix, cc, rmsd_b, rmsd_a) pdb_hierarchy.adopt_xray_structure(xrs) rotamer_manager = RotamerEval() for model in pdb_hierarchy.models(): for chain in model.chains(): for residue in chain.residues(): sites_cart = residue.atoms().extract_xyz() sel = maptbx.grid_indices_around_sites( unit_cell = xrs.unit_cell(), fft_n_real = map.focus(), fft_m_real = map.all(), sites_cart = sites_cart, site_radii = flex.double(sites_cart.size(), 2)) ccr = flex.linear_correlation( x=map.select(sel).as_1d(), y=tm.data.select(sel).as_1d()).coefficient() fmt = "%s: %4s %10s CC: %6.4f" print fmt%(prefix, residue.resname, rotamer_manager.evaluate_residue(residue),ccr)
def run(pdb_str, expected_ids): get_class = iotbx.pdb.common_residue_names_get_class mon_lib_srv = mmtbx.monomer_library.server.server() rotamer_manager = RotamerEval() pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) pdb_hierarchy = pdb_inp.construct_hierarchy() result_ids = [] for residue_group in pdb_hierarchy.residue_groups(): for conformer in residue_group.conformers(): for residue in conformer.residues(): sites_cart = residue.atoms().extract_xyz() rotamer_name = rotamer_manager.evaluate_residue( residue=residue) print residue.resname, residue.resseq, rotamer_name result_ids.append(rotamer_name) if (get_class(residue.resname) == "common_amino_acid"): rotamer_iterator = mon_lib_srv.rotamer_iterator( fine_sampling=True, comp_id=residue.resname, atom_names=residue.atoms().extract_name(), sites_cart=sites_cart) if (rotamer_iterator is None or rotamer_iterator.problem_message is not None or rotamer_iterator.rotamer_info is None): rotamer_iterator = None if (rotamer_iterator is not None): d1_min, d2_min = 1.e+9, 1.e+9 for r, rotamer_sites_cart in rotamer_iterator: sites_cart_rot = rotamer_manager.nearest_rotamer_sites_cart( residue=residue) d1 = flex.mean( flex.sqrt((sites_cart - sites_cart_rot).dot())) d2 = flex.mean( flex.sqrt( (sites_cart - rotamer_sites_cart).dot())) if (d1 < d1_min): d1_min = d1 if (d2 < d2_min): d2_min = d2 assert approx_equal(d1_min, d2_min) assert result_ids == expected_ids
def run(pdb_str, expected_ids): get_class = iotbx.pdb.common_residue_names_get_class mon_lib_srv = mmtbx.monomer_library.server.server() rotamer_manager = RotamerEval() pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_str) pdb_hierarchy = pdb_inp.construct_hierarchy() result_ids = [] for residue_group in pdb_hierarchy.residue_groups(): for conformer in residue_group.conformers(): for residue in conformer.residues(): sites_cart = residue.atoms().extract_xyz() rotamer_name = rotamer_manager.evaluate_residue(residue=residue) print residue.resname, residue.resseq, rotamer_name result_ids.append(rotamer_name) if(get_class(residue.resname) == "common_amino_acid"): rotamer_iterator = mon_lib_srv.rotamer_iterator( fine_sampling = True, comp_id = residue.resname, atom_names = residue.atoms().extract_name(), sites_cart = sites_cart) if(rotamer_iterator is None or rotamer_iterator.problem_message is not None or rotamer_iterator.rotamer_info is None): rotamer_iterator = None if(rotamer_iterator is not None): d1_min, d2_min = 1.e+9, 1.e+9 for r, rotamer_sites_cart in rotamer_iterator: sites_cart_rot = rotamer_manager.nearest_rotamer_sites_cart( residue=residue) d1= flex.mean(flex.sqrt((sites_cart - sites_cart_rot).dot())) d2= flex.mean(flex.sqrt((sites_cart - rotamer_sites_cart).dot())) if(d1 < d1_min): d1_min = d1 if(d2 < d2_min): d2_min = d2 assert approx_equal(d1_min, d2_min) assert result_ids == expected_ids
out = StringIO() r.show_old_output(out=out, verbose=False) assert (out.getvalue() == """\ A 47 PRO:1.00:86.4:329.3:41.3:324.9::Favored:Cg_exo A 48 MSE:0.70:0.3:287.6:214.8:138.3::OUTLIER:OUTLIER A 49 LYS:1.00:0.1:288.6:263.2:251.7:233.0:OUTLIER:OUTLIER """), out.getvalue() try: r = rotalyze.rotalyze(pdb_hierarchy=hierarchy, data_version="9000") except ValueError: pass else: raise Exception_expected from mmtbx.rotamer.rotamer_eval import RotamerEval rotamer_manager = RotamerEval() results = [] for model in hierarchy.models(): for chain in model.chains(): for residue in chain.residues(): cur_rot = rotamer_manager.evaluate_residue(residue) results.append(cur_rot) assert results == ['Cg_exo', 'OUTLIER', 'OUTLIER'] if (__name__ == "__main__"): exercise_rotalyze() exercise_2() print "OK"
class structure_monitor(object): def __init__(self, pdb_hierarchy, xray_structure, target_map_object=None, geometry_restraints_manager=None): adopt_init_args(self, locals()) self.unit_cell = self.xray_structure.unit_cell() self.xray_structure = xray_structure.deep_copy_scatterers() self.xray_structure_start = xray_structure.deep_copy_scatterers() self.states_collector = mmtbx.utils.states( pdb_hierarchy = self.pdb_hierarchy, xray_structure = self.xray_structure, counter = 1) self.states_collector.add(sites_cart = self.xray_structure.sites_cart()) self.rotamer_manager = RotamerEval() self.assert_pdb_hierarchy_xray_structure_sync() # self.five_cc = None self.map_cc_whole_unit_cell = None self.map_cc_around_atoms = None self.map_cc_per_atom = None self.rmsd_b = None self.rmsd_a = None self.dist_from_start = 0 self.dist_from_previous = 0 self.number_of_rotamer_outliers = 0 self.residue_monitors = None self.stats_evaluations = [] # self.initialize() def assert_pdb_hierarchy_xray_structure_sync(self): return #XXX sc1 = self.xray_structure.sites_cart() sc2 = self.pdb_hierarchy.atoms().extract_xyz() assert approx_equal(sc1, sc2, 1.e-3) def initialize(self): self.assert_pdb_hierarchy_xray_structure_sync() # residue monitors self.residue_monitors = [] backbone_atoms = ["N","CA","C","O","CB"] get_class = iotbx.pdb.common_residue_names_get_class sites_cart = self.xray_structure.sites_cart() current_map = self.compute_map(xray_structure = self.xray_structure) for model in self.pdb_hierarchy.models(): for chain in model.chains(): for residue_group in chain.residue_groups(): conformers = residue_group.conformers() if(len(conformers)>1): continue for conformer in residue_group.conformers(): residue = conformer.only_residue() id_str="%s%s%s"%(chain.id,residue.resname,residue.resseq.strip()) if(get_class(residue.resname) == "common_amino_acid"): residue_i_seqs_backbone = flex.size_t() residue_i_seqs_sidechain = flex.size_t() residue_i_seqs_all = flex.size_t() residue_i_seqs_c = flex.size_t() residue_i_seqs_n = flex.size_t() for atom in residue.atoms(): an = atom.name.strip() bb = an in backbone_atoms residue_i_seqs_all.append(atom.i_seq) if(bb): residue_i_seqs_backbone.append(atom.i_seq) else: residue_i_seqs_sidechain.append(atom.i_seq) if(an == "C"): residue_i_seqs_c.append(atom.i_seq) if(an == "N"): residue_i_seqs_n.append(atom.i_seq) sca = sites_cart.select(residue_i_seqs_all) scs = sites_cart.select(residue_i_seqs_sidechain) scb = sites_cart.select(residue_i_seqs_backbone) if(scs.size()==0): ccs = None else: ccs = self.map_cc(sites_cart=scs, other_map = current_map) if(sca.size()==0): cca = None else: cca = self.map_cc(sites_cart=sca, other_map = current_map) if(scb.size()==0): ccb = None else: ccb = self.map_cc(sites_cart=scb, other_map = current_map) self.residue_monitors.append(residue_monitor( residue = residue, id_str = id_str, selection_sidechain = residue_i_seqs_sidechain, selection_backbone = residue_i_seqs_backbone, selection_all = residue_i_seqs_all, selection_c = residue_i_seqs_c, selection_n = residue_i_seqs_n, map_cc_sidechain = ccs, map_cc_backbone = ccb, map_cc_all = cca, rotamer_status= self.rotamer_manager.evaluate_residue(residue))) else: residue_i_seqs_all = residue.atoms().extract_i_seq() sca = sites_cart.select(residue_i_seqs_all) cca = self.map_cc(sites_cart=sca, other_map = current_map) self.residue_monitors.append(residue_monitor( residue = residue, id_str = id_str, selection_all = residue_i_seqs_all, map_cc_all = cca)) # globals self.five_cc = five_cc( map = self.target_map_object.map_data, xray_structure = self.xray_structure, d_min = self.target_map_object.d_min) self.map_cc_whole_unit_cell = self.map_cc(other_map = current_map) self.map_cc_around_atoms = self.map_cc(other_map = current_map, sites_cart = sites_cart) self.map_cc_per_atom = self.map_cc(other_map = current_map, sites_cart = sites_cart, per_atom = True) if(self.geometry_restraints_manager is not None): es = self.geometry_restraints_manager.energies_sites(sites_cart=sites_cart) self.rmsd_a = es.angle_deviations()[2] self.rmsd_b = es.bond_deviations()[2] self.dist_from_start = flex.mean(self.xray_structure_start.distances( other = self.xray_structure)) self.number_of_rotamer_outliers = 0 for r in self.residue_monitors: if(r.rotamer_status == "OUTLIER"): self.number_of_rotamer_outliers += 1 self.assert_pdb_hierarchy_xray_structure_sync() def compute_map(self, xray_structure): self.assert_pdb_hierarchy_xray_structure_sync() mc = self.target_map_object.miller_array.structure_factors_from_scatterers( xray_structure = xray_structure).f_calc() fft_map = miller.fft_map( crystal_gridding = self.target_map_object.crystal_gridding, fourier_coefficients = mc) fft_map.apply_sigma_scaling() return fft_map.real_map_unpadded() def map_cc_histogram_per_atom(self, radius=2, n_slots=10): self.assert_pdb_hierarchy_xray_structure_sync() from mmtbx.maps import correlation current_map = self.compute_map(xray_structure = self.xray_structure) return correlation.histogram_per_atom( map_1 = current_map, map_2 = self.target_map_object.map_data, sites_cart = self.xray_structure.sites_cart(), unit_cell = self.xray_structure.unit_cell(), radius = radius, n_slots = n_slots) def map_cc(self, other_map, sites_cart=None, atom_radius=2, per_atom=False): self.assert_pdb_hierarchy_xray_structure_sync() from mmtbx.maps import correlation if(sites_cart is not None): if(per_atom): result = correlation.from_map_map_atoms_per_atom( map_1 = other_map, map_2 = self.target_map_object.map_data, sites_cart = sites_cart, unit_cell = self.xray_structure.unit_cell(), radius = atom_radius) else: result = correlation.from_map_map_atoms( map_1 = other_map, map_2 = self.target_map_object.map_data, sites_cart = sites_cart, unit_cell = self.xray_structure.unit_cell(), radius = atom_radius) else: result = correlation.from_map_map( map_1 = other_map, map_2 = self.target_map_object.map_data) return result def show(self, prefix="", log=None): self.assert_pdb_hierarchy_xray_structure_sync() if(log is None): log = sys.stdout fmt = """%s CC_mask: %-6.3f %s CC_volume: %-6.3f %s CC_peaks: %-6.3f %s rmsd (bonds): %-s %s rmsd (angles): %-s %s Dist. moved from start: %-6.3f %s Dist. moved from previous: %-6.3f %s All-atom clashscore %-s %s Ramachandran plot: %s outliers: %-s %% %s allowed: %-s %% %s favored: %-s %% %s Omega angle: %s cis-proline: %-s %% %s twisted proline: %-s %% %s cis-general: %-s %% %s twisted-general: %-s %% %s CaBLAM analysis: %s outliers: %-s %% %s disfavored: %-s %% %s ca outliers: %-s %% %s Rotamer outliers: %-s %% %s C-beta deviations: %-s %% """ mso = None try: if self.geometry_restraints_manager is not None and False: # XXX False at the end is intentional, because currently I want to # disable this 'if' branch. Reason is - nothing from extended # model_statistics (with GRM) is being used, so no reason to spend # time calculating statistics over various restraints. mso = mmtbx.model.statistics.geometry( pdb_hierarchy = self.pdb_hierarchy, molprobity_scores = libtbx.env.has_module("probe"), restraints_manager = self.geometry_restraints_manager) else: mso = mmtbx.model.statistics.geometry_no_grm( pdb_hierarchy = self.pdb_hierarchy, molprobity_scores = libtbx.env.has_module("probe")) except Exception: # some part of validation failed pass self.stats_evaluations.append( group_args( cc = group_args( cc_mask = self.five_cc.result.cc_mask, cc_volume = self.five_cc.result.cc_volume, cc_peaks = self.five_cc.result.cc_peaks), geometry = mso, rmsd_a = self.rmsd_a, rmsd_b = self.rmsd_b)) if mso is not None and self.five_cc is not None: print(fmt%( # prefix, self.map_cc_whole_unit_cell, # prefix, self.map_cc_around_atoms, prefix, self.five_cc.cc_mask, prefix, self.five_cc.cc_volume, prefix, self.five_cc.cc_peaks, prefix, format_value("%-6.2f", self.rmsd_b).strip(), prefix, format_value("%-6.2f", self.rmsd_a).strip(), prefix, self.dist_from_start, prefix, self.dist_from_previous, prefix, format_value("%-6.2f", mso.clashscore), prefix, prefix, format_value("%-5.2f", mso.ramachandran_outliers), prefix, format_value("%-5.2f", mso.ramachandran_allowed), prefix, format_value("%-5.2f", mso.ramachandran_favored), prefix, prefix, format_value("%-5.2f", mso.cis_proline), prefix, format_value("%-5.2f", mso.twisted_proline), prefix, format_value("%-5.2f", mso.cis_general), prefix, format_value("%-5.2f", mso.twisted_general), prefix, prefix, format_value("%-5.2f", mso.cablam_outliers), prefix, format_value("%-5.2f", mso.cablam_disfavored), prefix, format_value("%-5.2f", mso.cablam_ca_outliers), prefix, format_value("%6.2f", mso.rotamer_outliers).strip(), prefix, format_value("%-5.2f", mso.c_beta_dev_percent)), file=log) def show_residues(self, map_cc_all=0.8, map_cc_sidechain=0.8, log=None): self.assert_pdb_hierarchy_xray_structure_sync() if(log is None): log = sys.stdout header_printed = True for r in self.residue_monitors: i1=r.map_cc_all < map_cc_all i2=r.rotamer_status == "OUTLIER" i4=r.map_cc_sidechain is not None and r.map_cc_sidechain<map_cc_sidechain if([i1,i2,i4].count(True)>0): if(header_printed): print("Residue CC CC CC Rotamer", file=log) print(" id all backbone sidechain id", file=log) header_printed = False print(r.format_info_string(), file=log) def update(self, xray_structure, accept_as_is=True): if(not accept_as_is): current_map = self.compute_map(xray_structure = xray_structure) sites_cart = xray_structure.sites_cart() sites_cart_ = self.xray_structure.sites_cart() for r in self.residue_monitors: sca = sites_cart.select(r.selection_all) scs = sites_cart.select(r.selection_sidechain) scb = sites_cart.select(r.selection_backbone) map_cc_all = self.map_cc(sites_cart = sca, other_map = current_map) map_cc_sidechain = self.map_cc(sites_cart = scs, other_map = current_map) map_cc_backbone = self.map_cc(sites_cart = scb, other_map = current_map) flag = map_cc_all >= r.map_cc_all and \ map_cc_backbone >= r.map_cc_backbone and \ map_cc_sidechain>= r.map_cc_sidechain if(flag): residue_sites_cart_new = sites_cart.select(r.selection_all) sites_cart_ = sites_cart_.set_selected(r.selection_all, residue_sites_cart_new) xray_structure = xray_structure.replace_sites_cart(sites_cart_) # re-initialize monitor self.dist_from_previous = flex.mean(self.xray_structure.distances( other = xray_structure)) self.xray_structure = xray_structure self.pdb_hierarchy.adopt_xray_structure(xray_structure) self.initialize() self.states_collector.add(sites_cart = xray_structure.sites_cart()) self.assert_pdb_hierarchy_xray_structure_sync()
def exercise_2(): pdb_str = """\ ATOM 2527 N LEU A 261 -31.022 -24.808 107.479 1.00 28.22 N ATOM 2528 CA LEU A 261 -30.054 -23.719 107.237 1.00 21.77 C ATOM 2529 C LEU A 261 -30.582 -22.773 106.168 1.00 27.64 C ATOM 2530 O LEU A 261 -29.841 -21.977 105.561 1.00 26.70 O ATOM 2531 CB LEU A 261 -28.696 -24.276 106.874 1.00 22.58 C ATOM 2532 CG LEU A 261 -28.135 -25.066 108.060 1.00 40.89 C ATOM 2533 CD1 LEU A 261 -26.892 -25.858 107.664 1.00 46.72 C ATOM 2534 CD2 LEU A 261 -27.806 -24.109 109.202 1.00 38.88 C ATOM 2535 H LEU A 261 -31.201 -25.277 106.781 1.00 33.87 H ATOM 2536 HA LEU A 261 -29.950 -23.204 108.064 1.00 26.12 H ATOM 2537 HB2 LEU A 261 -28.781 -24.874 106.115 1.00 27.10 H ATOM 2538 HB3 LEU A 261 -28.088 -23.548 106.670 1.00 27.10 H ATOM 2539 HG LEU A 261 -28.806 -25.693 108.373 1.00 49.07 H ATOM 2540 HD11 LEU A 261 -26.570 -26.338 108.430 1.00 56.07 H ATOM 2541 HD12 LEU A 261 -27.124 -26.473 106.965 1.00 56.07 H ATOM 2542 HD13 LEU A 261 -26.219 -25.247 107.353 1.00 56.07 H ATOM 2543 HD21 LEU A 261 -28.608 -23.653 109.468 1.00 46.66 H ATOM 2544 HD22 LEU A 261 -27.455 -24.612 109.941 1.00 46.66 H ATOM 2545 HD23 LEU A 261 -27.153 -23.474 108.899 1.00 46.66 H ATOM 2546 N GLY A 262 -31.887 -22.863 105.948 1.00 23.68 N ATOM 2547 CA GLY A 262 -32.572 -21.935 105.075 1.00 21.87 85 C ATOM 2548 C GLY A 262 -33.718 -22.620 104.386 1.00 27.32 C ATOM 2549 O GLY A 262 -33.943 -23.822 104.556 1.00 23.10 O ATOM 2550 H GLY A 262 -32.399 -23.459 106.298 1.00 28.42 H ATOM 2551 HA2 GLY A 262 -32.916 -21.189 105.591 1.00 26.25 85 H ATOM 2552 HA3 GLY A 262 -31.958 -21.598 104.405 1.00 26.25 85 H ATOM 2553 N SER A 263 -34.460 -21.830 103.628 1.00 24.62 N ATOM 2554 CA SER A 263 -35.631 -22.290 102.921 1.00 27.15 C ATOM 2555 C SER A 263 -35.594 -21.761 101.492 1.00 22.14 C ATOM 2556 O SER A 263 -34.723 -20.945 101.159 1.00 21.01 O ATOM 2557 CB SER A 263 -36.839 -21.713 103.619 1.00 25.73 C ATOM 2558 OG SER A 263 -36.907 -22.232 104.922 1.00 26.84 O ATOM 2559 H SER A 263 -34.296 -20.995 103.507 1.00 29.54 H ATOM 2560 HA SER A 263 -35.680 -23.269 102.917 1.00 32.58 H ATOM 2561 HB2 SER A 263 -36.754 -20.747 103.661 1.00 30.87 H ATOM 2562 HB3 SER A 263 -37.641 -21.960 103.132 1.00 30.87 H ATOM 2563 HG SER A 263 -37.560 -21.925 105.312 1.00 32.20 H """ pdb_str2 = """ ATOM 453 N PRO A 47 8.633 6.370 5.022 1.00 13.79 N ATOM 454 CA PRO A 47 7.915 7.571 5.496 1.00 14.61 C ATOM 455 C PRO A 47 7.612 7.481 6.994 1.00 15.06 C ATOM 456 O PRO A 47 7.289 6.377 7.439 1.00 14.39 O ATOM 457 CB PRO A 47 6.639 7.559 4.651 1.00 16.24 C ATOM 458 CG PRO A 47 7.089 6.901 3.338 1.00 15.52 C ATOM 459 CD PRO A 47 7.990 5.773 3.833 1.00 14.40 C ATOM 460 N MSE A 48 7.754 8.528 7.779 1.00 15.13 N ATOM 461 CA MSE A 48 7.482 8.456 9.201 1.00 16.17 C ATOM 462 C MSE A 48 6.040 8.750 9.517 1.00 15.23 C ATOM 463 O MSE A 48 5.417 9.418 8.735 1.00 14.77 O ATOM 464 CB MSE A 48 8.165 9.538 10.023 1.00 19.62 C ATOM 465 CG MSE A 48 9.630 9.466 10.238 1.00 21.70 C ATOM 466 SE MSE A 48 10.022 10.161 12.050 0.70 37.95 SE ATOM 467 CE MSE A 48 11.268 8.720 12.235 1.00 28.72 C ATOM 468 N LYS A 49 5.519 8.291 10.645 1.00 13.93 N ATOM 469 CA LYS A 49 4.167 8.624 11.045 1.00 13.79 C ATOM 470 C LYS A 49 4.022 10.138 11.202 1.00 14.66 C ATOM 471 O LYS A 49 5.011 10.853 11.351 1.00 15.69 O ATOM 472 CB LYS A 49 3.797 7.915 12.349 1.00 13.33 C ATOM 473 CG LYS A 49 3.593 6.416 12.204 1.00 14.35 C ATOM 474 CD LYS A 49 2.121 6.071 12.044 1.00 16.45 C ATOM 475 CE LYS A 49 1.571 5.402 13.292 1.00 18.19 C ATOM 476 NZ LYS A 49 0.899 4.110 12.980 1.00 19.97 N """ pdb_io = pdb.input(source_info=None, lines=pdb_str) hierarchy = pdb_io.construct_hierarchy() try : rotalyze.rotalyze(pdb_hierarchy=hierarchy) except Sorry as e : assert ("GLY A 262" in str(e)) else : raise Exception_expected pdb_io = pdb.input(source_info=None, lines=pdb_str2) hierarchy = pdb_io.construct_hierarchy() r = rotalyze.rotalyze(pdb_hierarchy=hierarchy) out = StringIO() r.show_old_output(out=out, verbose=False) output = out.getvalue() assert output == """\ A 47 PRO:1.00:86.4:329.3:41.3:324.9::Favored:Cg_exo A 48 MSE:0.70:0.3:287.6:214.8:138.3::OUTLIER:OUTLIER A 49 LYS:1.00:0.1:288.6:263.2:251.7:233.0:OUTLIER:OUTLIER """, output r = rotalyze.rotalyze(pdb_hierarchy=hierarchy, data_version="8000") out = StringIO() r.show_old_output(out=out, verbose=False) assert (out.getvalue() == """\ A 47 PRO:1.00:86.4:329.3:41.3:324.9::Favored:Cg_exo A 48 MSE:0.70:0.3:287.6:214.8:138.3::OUTLIER:OUTLIER A 49 LYS:1.00:0.1:288.6:263.2:251.7:233.0:OUTLIER:OUTLIER """), out.getvalue() try : r = rotalyze.rotalyze(pdb_hierarchy=hierarchy, data_version="9000") except ValueError : pass else : raise Exception_expected from mmtbx.rotamer.rotamer_eval import RotamerEval rotamer_manager = RotamerEval() results = [] for model in hierarchy.models(): for chain in model.chains(): for residue in chain.residues(): cur_rot = rotamer_manager.evaluate_residue(residue) results.append(cur_rot) assert results == ['Cg_exo', 'OUTLIER', 'OUTLIER']
out = StringIO() r.show_old_output(out=out, verbose=False) assert (out.getvalue() == """\ A 47 PRO:1.00:86.4:329.3:41.3:324.9::Favored:Cg_exo A 48 MSE:0.70:0.3:287.6:214.8:138.3::OUTLIER:OUTLIER A 49 LYS:1.00:0.1:288.6:263.2:251.7:233.0:OUTLIER:OUTLIER """), out.getvalue() try : r = rotalyze.rotalyze(pdb_hierarchy=hierarchy, data_version="9000") except ValueError : pass else : raise Exception_expected from mmtbx.rotamer.rotamer_eval import RotamerEval rotamer_manager = RotamerEval() results = [] for model in hierarchy.models(): for chain in model.chains(): for residue in chain.residues(): cur_rot = rotamer_manager.evaluate_residue(residue) results.append(cur_rot) assert results == ['Cg_exo', 'OUTLIER', 'OUTLIER'] if (__name__ == "__main__") : exercise_rotalyze() exercise_2() print "OK"
class structure_monitor(object): def __init__(self, pdb_hierarchy, xray_structure, target_map_object=None, geometry_restraints_manager=None): adopt_init_args(self, locals()) self.unit_cell = self.xray_structure.unit_cell() self.xray_structure = xray_structure.deep_copy_scatterers() self.xray_structure_start = xray_structure.deep_copy_scatterers() self.states_collector = mmtbx.utils.states( pdb_hierarchy = self.pdb_hierarchy, xray_structure = self.xray_structure, counter = 1) self.states_collector.add(sites_cart = self.xray_structure.sites_cart()) self.rotamer_manager = RotamerEval() self.assert_pdb_hierarchy_xray_structure_sync() # self.map_cc_whole_unit_cell = None self.map_cc_around_atoms = None self.map_cc_per_atom = None self.rmsd_b = None self.rmsd_a = None self.dist_from_start = 0 self.dist_from_previous = 0 self.number_of_rotamer_outliers = 0 self.residue_monitors = None # self.initialize() def assert_pdb_hierarchy_xray_structure_sync(self): return #XXX sc1 = self.xray_structure.sites_cart() sc2 = self.pdb_hierarchy.atoms().extract_xyz() assert approx_equal(sc1, sc2, 1.e-3) def initialize(self): self.assert_pdb_hierarchy_xray_structure_sync() # residue monitors self.residue_monitors = [] backbone_atoms = ["N","CA","C","O","CB"] get_class = iotbx.pdb.common_residue_names_get_class sites_cart = self.xray_structure.sites_cart() current_map = self.compute_map(xray_structure = self.xray_structure) for model in self.pdb_hierarchy.models(): for chain in model.chains(): for residue_group in chain.residue_groups(): conformers = residue_group.conformers() if(len(conformers)>1): continue for conformer in residue_group.conformers(): residue = conformer.only_residue() id_str="%s%s%s"%(chain.id,residue.resname,residue.resseq.strip()) if(get_class(residue.resname) == "common_amino_acid"): residue_i_seqs_backbone = flex.size_t() residue_i_seqs_sidechain = flex.size_t() residue_i_seqs_all = flex.size_t() residue_i_seqs_c = flex.size_t() residue_i_seqs_n = flex.size_t() for atom in residue.atoms(): an = atom.name.strip() bb = an in backbone_atoms residue_i_seqs_all.append(atom.i_seq) if(bb): residue_i_seqs_backbone.append(atom.i_seq) else: residue_i_seqs_sidechain.append(atom.i_seq) if(an == "C"): residue_i_seqs_c.append(atom.i_seq) if(an == "N"): residue_i_seqs_n.append(atom.i_seq) sca = sites_cart.select(residue_i_seqs_all) scs = sites_cart.select(residue_i_seqs_sidechain) scb = sites_cart.select(residue_i_seqs_backbone) if(scs.size()==0): ccs = None else: ccs = self.map_cc(sites_cart=scs, other_map = current_map) if(sca.size()==0): cca = None else: cca = self.map_cc(sites_cart=sca, other_map = current_map) if(scb.size()==0): ccb = None else: ccb = self.map_cc(sites_cart=scb, other_map = current_map) self.residue_monitors.append(residue_monitor( residue = residue, id_str = id_str, selection_sidechain = residue_i_seqs_sidechain, selection_backbone = residue_i_seqs_backbone, selection_all = residue_i_seqs_all, selection_c = residue_i_seqs_c, selection_n = residue_i_seqs_n, map_cc_sidechain = ccs, map_cc_backbone = ccb, map_cc_all = cca, rotamer_status= self.rotamer_manager.evaluate_residue(residue))) else: residue_i_seqs_all = residue.atoms().extract_i_seq() sca = sites_cart.select(residue_i_seqs_all) cca = self.map_cc(sites_cart=sca, other_map = current_map) self.residue_monitors.append(residue_monitor( residue = residue, id_str = id_str, selection_all = residue_i_seqs_all, map_cc_all = cca)) # globals self.map_cc_whole_unit_cell = self.map_cc(other_map = current_map) self.map_cc_around_atoms = self.map_cc(other_map = current_map, sites_cart = sites_cart) self.map_cc_per_atom = self.map_cc(other_map = current_map, sites_cart = sites_cart, per_atom = True) if(self.geometry_restraints_manager is not None): es = self.geometry_restraints_manager.energies_sites(sites_cart=sites_cart) self.rmsd_a = es.angle_deviations()[2] self.rmsd_b = es.bond_deviations()[2] self.dist_from_start = flex.mean(self.xray_structure_start.distances( other = self.xray_structure)) self.number_of_rotamer_outliers = 0 for r in self.residue_monitors: if(r.rotamer_status == "OUTLIER"): self.number_of_rotamer_outliers += 1 self.assert_pdb_hierarchy_xray_structure_sync() def compute_map(self, xray_structure): self.assert_pdb_hierarchy_xray_structure_sync() mc = self.target_map_object.miller_array.structure_factors_from_scatterers( xray_structure = xray_structure).f_calc() fft_map = miller.fft_map( crystal_gridding = self.target_map_object.crystal_gridding, fourier_coefficients = mc) fft_map.apply_sigma_scaling() return fft_map.real_map_unpadded() def map_cc_histogram_per_atom(self, radius=2, n_slots=10): self.assert_pdb_hierarchy_xray_structure_sync() from mmtbx.maps import correlation current_map = self.compute_map(xray_structure = self.xray_structure) return correlation.histogram_per_atom( map_1 = current_map, map_2 = self.target_map_object.map_data, sites_cart = self.xray_structure.sites_cart(), unit_cell = self.xray_structure.unit_cell(), radius = radius, n_slots = n_slots) def map_cc(self, other_map, sites_cart=None, atom_radius=2, per_atom=False): self.assert_pdb_hierarchy_xray_structure_sync() from mmtbx.maps import correlation if(sites_cart is not None): if(per_atom): result = correlation.from_map_map_atoms_per_atom( map_1 = other_map, map_2 = self.target_map_object.map_data, sites_cart = sites_cart, unit_cell = self.xray_structure.unit_cell(), radius = atom_radius) else: result = correlation.from_map_map_atoms( map_1 = other_map, map_2 = self.target_map_object.map_data, sites_cart = sites_cart, unit_cell = self.xray_structure.unit_cell(), radius = atom_radius) else: result = correlation.from_map_map( map_1 = other_map, map_2 = self.target_map_object.map_data) return result def show(self, prefix="", log=None): self.assert_pdb_hierarchy_xray_structure_sync() if(log is None): log = sys.stdout fmt = """%s Map CC (whole unit cell): %-6.3f %s Map CC (around atoms): %-6.3f %s rmsd (bonds): %-s %s rmsd (angles): %-s %s Dist. moved from start: %-6.3f %s Dist. moved from previous: %-6.3f %s All-atom clashscore %-s %s Ramachandran plot: %s outliers: %-s %% %s allowed: %-s %% %s favored: %-s %% %s Rotamer outliers: %-s %% %s C-beta deviations: %-s """ if(self.geometry_restraints_manager is not None): mso = model_statistics.geometry( pdb_hierarchy = self.pdb_hierarchy, molprobity_scores = libtbx.env.has_module("probe"), restraints_manager = self.geometry_restraints_manager) print >> log, fmt%( prefix, self.map_cc_whole_unit_cell, prefix, self.map_cc_around_atoms, prefix, format_value("%-6.2f", self.rmsd_b).strip(), prefix, format_value("%-6.2f", self.rmsd_a).strip(), prefix, self.dist_from_start, prefix, self.dist_from_previous, prefix, format_value("%-6.2f", mso.clashscore), prefix, prefix, format_value("%-5.2f", mso.ramachandran_outliers), prefix, format_value("%-5.2f", mso.ramachandran_allowed), prefix, format_value("%-5.2f", mso.ramachandran_favored), prefix, format_value("%6.2f", mso.rotamer_outliers).strip(), prefix, format_value("%-3d", mso.c_beta_dev)) else: print >> log, fmt%( prefix, self.map_cc_whole_unit_cell, prefix, self.map_cc_around_atoms, prefix, "None", prefix, "None", prefix, self.dist_from_start, prefix, self.dist_from_previous, prefix, "None", prefix, prefix, "None", prefix, "None", prefix, "None", prefix, "None", prefix, "None") def show_residues(self, map_cc_all=0.8, map_cc_sidechain=0.8, log=None): self.assert_pdb_hierarchy_xray_structure_sync() if(log is None): log = sys.stdout header_printed = True for r in self.residue_monitors: i1=r.map_cc_all < map_cc_all i2=r.rotamer_status == "OUTLIER" i4=r.map_cc_sidechain is not None and r.map_cc_sidechain<map_cc_sidechain if([i1,i2,i4].count(True)>0): if(header_printed): print >> log, "Residue CC CC CC Rotamer" print >> log, " id all backbone sidechain id" header_printed = False print >> log, r.format_info_string() def update(self, xray_structure, accept_as_is=True): if(not accept_as_is): current_map = self.compute_map(xray_structure = xray_structure) sites_cart = xray_structure.sites_cart() sites_cart_ = self.xray_structure.sites_cart() for r in self.residue_monitors: sca = sites_cart.select(r.selection_all) scs = sites_cart.select(r.selection_sidechain) scb = sites_cart.select(r.selection_backbone) map_cc_all = self.map_cc(sites_cart = sca, other_map = current_map) map_cc_sidechain = self.map_cc(sites_cart = scs, other_map = current_map) map_cc_backbone = self.map_cc(sites_cart = scb, other_map = current_map) flag = map_cc_all >= r.map_cc_all and \ map_cc_backbone >= r.map_cc_backbone and \ map_cc_sidechain>= r.map_cc_sidechain if(flag): residue_sites_cart_new = sites_cart.select(r.selection_all) sites_cart_ = sites_cart_.set_selected(r.selection_all, residue_sites_cart_new) xray_structure = xray_structure.replace_sites_cart(sites_cart_) # re-initialize monitor self.dist_from_previous = flex.mean(self.xray_structure.distances( other = xray_structure)) self.xray_structure = xray_structure self.pdb_hierarchy.adopt_xray_structure(xray_structure) self.initialize() self.states_collector.add(sites_cart = xray_structure.sites_cart()) self.assert_pdb_hierarchy_xray_structure_sync()