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)
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()
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 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 __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])
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 secondary_structure_from_sequence(pdb_str, sequence=None, pdb_hierarchy_template=None, rotamer_manager=None): """ Return pdb.hierarchy with secondary structure according to sequence or reference hierarcy. If reference hierarchy provided, the resulting hierarchy will be rigid body aligned to it. Residue numbers will start from 1. pdb_str - "ideal" structure at least 2 residues long. sequence - string with sequence (one-letter codes) pdb_hierarchy_template - reference hierarchy. """ if rotamer_manager is None: rotamer_manager = RotamerEval() pht = pdb_hierarchy_template assert [sequence, pht].count(None) == 1 if pht is not None: lk = len(pht.altloc_indices().keys()) if lk == 0: raise Sorry( "Hierarchy template in secondary_structure_from_sequence is empty" ) else: if len(pht.altloc_indices().keys()) != 1: raise Sorry("Alternative conformations are not supported") number_of_residues = len(sequence) if sequence!=None else \ len(pht.models()[0].chains()[0].conformers()[0].residues()) if number_of_residues < 1: raise Sorry('sequence should contain at least one residue.') ideal_res_dict = idealized_aa.residue_dict() real_res_list = None if pht: real_res_list = pht.models()[0].chains()[0].residue_groups() pdb_hierarchy = iotbx.pdb.input(source_info=None, lines=pdb_str).\ construct_hierarchy() pdb_hierarchy.truncate_to_poly_gly() chain = pdb_hierarchy.models()[0].chains()[0] current_gly_ag = chain.residue_groups()[0].atom_groups()[0] new_chain = iotbx.pdb.hierarchy.chain(id="A") new_chain.pre_allocate_residue_groups(number_of_additional_residue_groups=\ number_of_residues) r, t = get_r_t_matrices_from_structure(pdb_str) for j in range(number_of_residues): # put ALA rg = iotbx.pdb.hierarchy.residue_group(icode="") rg.resseq = j + 1 new_chain.append_residue_group(residue_group=rg) ag_to_place = current_gly_ag.detached_copy() rg.append_atom_group(atom_group=ag_to_place) current_gly_ag.atoms().set_xyz(r.elems * current_gly_ag.atoms().extract_xyz() + t.elems) current_reference_ag = real_res_list[j].atom_groups()[0] if pht else \ ideal_res_dict[three_one[sequence[j]].lower()].models()[0].chains()[0].\ residue_groups()[0].atom_groups()[0] side_chain_placement(ag_to_place, current_reference_ag, rotamer_manager) new_pdb_h = iotbx.pdb.hierarchy.new_hierarchy_from_chain(new_chain) # align to real if pht != None: fixed_sites, moving_sites = get_matching_sites_cart_in_both_h( pht, new_pdb_h) assert len(fixed_sites) == len(moving_sites) lsq_fit_obj = superpose.least_squares_fit(reference_sites=fixed_sites, other_sites=moving_sites) new_pdb_h.atoms().set_xyz(lsq_fit_obj.r.elems * new_pdb_h.atoms().extract_xyz() + lsq_fit_obj.t.elems) return new_pdb_h
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']
def __init__( self, restraints_manager, pdb_hierarchy, correct_special_position_tolerance, riding_h_manager=None, ncs_restraints_group_list=[], # These are actually for NCS CONSTRAINTS! max_number_of_iterations=500, number_of_macro_cycles=5, selection=None, bond=False, nonbonded=False, angle=False, dihedral=False, chirality=False, planarity=False, parallelity=False, rmsd_bonds_termination_cutoff=0, rmsd_angles_termination_cutoff=0, alternate_nonbonded_off_on=False, cdl=False, rdl=False, correct_hydrogens=False, fix_rotamer_outliers=True, allow_allowed_rotamers=True, states_collector=None, log=None, mon_lib_srv=None, ias_selection=None, ): self.log = log if self.log is None: self.log = sys.stdout self.pdb_hierarchy = pdb_hierarchy self.ias_selection = ias_selection self.minimized = None self.mon_lib_srv = mon_lib_srv if self.mon_lib_srv is None: self.mon_lib_srv = monomer_library.server.server() self.restraints_manager = restraints_manager assert max_number_of_iterations + number_of_macro_cycles > 0 assert [ bond, nonbonded, angle, dihedral, chirality, planarity, parallelity ].count(False) < 7 self.cdl_proxies = None self.rdl_proxies = None self.rotamer_manager = None if fix_rotamer_outliers: from mmtbx.rotamer.rotamer_eval import RotamerEval self.rotamer_manager = RotamerEval(mon_lib_srv=self.mon_lib_srv) if (cdl): from mmtbx.conformation_dependent_library.cdl_setup import setup_restraints self.cdl_proxies = setup_restraints( self.restraints_manager.geometry) self.correct_hydrogens = correct_hydrogens if (alternate_nonbonded_off_on and number_of_macro_cycles % 2 != 0): number_of_macro_cycles += 1 import scitbx.lbfgs lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations=max_number_of_iterations) exception_handling_params = scitbx.lbfgs.exception_handling_parameters( ignore_line_search_failed_step_at_lower_bound=True) geometry_restraints_flags = geometry_restraints.flags.flags( bond=bond, nonbonded=nonbonded, angle=angle, dihedral=dihedral, chirality=chirality, planarity=planarity, parallelity=parallelity, reference_coordinate=True, reference_dihedral=True, bond_similarity=True, ramachandran_restraints=True) self.update_cdl_restraints() self.show() for i_macro_cycle in range(number_of_macro_cycles): print(" macro-cycle:", i_macro_cycle, file=self.log) self.restraints_manager.geometry.update_ramachandran_restraints_phi_psi_targets( hierarchy=self.pdb_hierarchy) if (alternate_nonbonded_off_on and i_macro_cycle <= number_of_macro_cycles / 2): geometry_restraints_flags.nonbonded = bool(i_macro_cycle % 2) self.update_cdl_restraints(macro_cycle=i_macro_cycle) if (fix_rotamer_outliers): junk, self.restraints_manager = add_rotamer_restraints( pdb_hierarchy=self.pdb_hierarchy, restraints_manager=self.restraints_manager, selection=selection, sigma=10, mode="fix_outliers", accept_allowed=allow_allowed_rotamers, mon_lib_srv=self.mon_lib_srv, rotamer_manager=self.rotamer_manager) sites_cart = self.pdb_hierarchy.atoms().extract_xyz() if rdl: self.updaterdl(prefix="Update RDL restraints") if (ncs_restraints_group_list is not None and len(ncs_restraints_group_list)) > 0: # do ncs minimization print("Using NCS constraints.", file=self.log) xrs = self.pdb_hierarchy.extract_xray_structure( ).deep_copy_scatterers() refine_selection = flex.size_t(range(xrs.scatterers().size())) tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\ target_function_and_grads_geometry_minimization( xray_structure=xrs, ncs_restraints_group_list=ncs_restraints_group_list, # CONSTRAINTS refine_selection=refine_selection, restraints_manager=self.restraints_manager.geometry, refine_sites=True, refine_transformations=False, ) minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs( target_and_grads_object=tfg_obj, xray_structure=xrs, ncs_restraints_group_list= ncs_restraints_group_list, # CONSTRAINTS refine_selection=refine_selection, finite_grad_differences_test=False, max_iterations=max_number_of_iterations, refine_sites=True, refine_transformations=False) self.pdb_hierarchy.adopt_xray_structure(xrs) else: sites_cart_orig = sites_cart.deep_copy() if ias_selection is not None and ias_selection.count(True) > 0: sites_cart = sites_cart.select(~ias_selection) self.minimized = lbfgs( sites_cart=sites_cart, riding_h_manager=riding_h_manager, correct_special_position_tolerance= correct_special_position_tolerance, geometry_restraints_manager=restraints_manager.geometry, geometry_restraints_flags=geometry_restraints_flags, lbfgs_termination_params=lbfgs_termination_params, lbfgs_exception_handling_params=exception_handling_params, sites_cart_selection=selection, rmsd_bonds_termination_cutoff=rmsd_bonds_termination_cutoff, rmsd_angles_termination_cutoff= rmsd_angles_termination_cutoff, states_collector=states_collector, site_labels=None) if (ias_selection is not None): for i_seq, ias_s in enumerate( ias_selection ): # assumes that IAS appended to the back if (not ias_s): sites_cart_orig[i_seq] = sites_cart[i_seq] else: sites_cart_orig = sites_cart self.pdb_hierarchy.atoms().set_xyz(sites_cart_orig) self.show() self.log.flush() geometry_restraints_flags.nonbonded = nonbonded lbfgs_termination_params = scitbx.lbfgs.termination_parameters( max_iterations=max_number_of_iterations)
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()
def __init__(self, pdb_hierarchy, params=None, secondary_structure_annotation=None, reference_map=None, crystal_symmetry=None, grm=None, rama_manager=None, rotamer_manager=None, log=null_out(), verbose=False): if len(pdb_hierarchy.models()) > 1: raise Sorry("Multi-model files are not supported") self.original_pdb_h = pdb_hierarchy self.secondary_structure_annotation = secondary_structure_annotation asc = pdb_hierarchy.atom_selection_cache() self.xrs = pdb_hierarchy.extract_xray_structure( crystal_symmetry=crystal_symmetry) self.reference_map = reference_map self.resulting_pdb_h = pdb_hierarchy.deep_copy() self.resulting_pdb_h.reset_atom_i_seqs() self.params = self.process_params(params) self.log = log self.verbose = verbose self.grm = grm self.r = rama_manager if self.r is None: self.r = rama_eval() self.rotamer_manager = rotamer_manager if self.rotamer_manager is None: self.rotamer_manager = RotamerEval() ram = ramalyze.ramalyze(pdb_hierarchy=pdb_hierarchy) self.p_initial_rama_outliers = ram.out_percent self.p_before_minimization_rama_outliers = None self.p_after_minimiaztion_rama_outliers = None n_inputs = [reference_map, crystal_symmetry].count(None) if not (n_inputs == 0 or n_inputs == 2): print >> log, "Need to have both map and symmetry info. Not using map." self.reference_map = None berkeley_count = utils.list_rama_outliers_h( self.resulting_pdb_h).count("\n") self.berkeley_p_before_minimization_rama_outliers = \ berkeley_count/float(self.resulting_pdb_h.overall_counts().n_residues)*100 # self.berkeley_p_before_minimization_rama_outliers = None self.berkeley_p_after_minimiaztion_rama_outliers = self.berkeley_p_before_minimization_rama_outliers self.ref_exclusion_selection = "" number_of_ccd_trials = 0 # print "logic expr outcome:", (number_of_ccd_trials < 10 and self.berkeley_p_before_minimization_rama_outliers > 0.001) # print number_of_ccd_trials < 10 # print "berkeley before rama out:", self.berkeley_p_before_minimization_rama_outliers if self.berkeley_p_before_minimization_rama_outliers <= 0.001: print >> self.log, "No ramachandran outliers, skipping CCD step." if not self.params.enabled: print >> self.log, "Loop idealization is not enabled, use 'enabled=True'." while (number_of_ccd_trials < self.params.number_of_ccd_trials and self.berkeley_p_after_minimiaztion_rama_outliers > 0.001 and self.params.enabled): print "CCD try number, outliers:", number_of_ccd_trials, self.berkeley_p_before_minimization_rama_outliers number_of_ccd_trials += 1 processed_chain_ids = [] for chain in self.resulting_pdb_h.only_model().chains(): print >> self.log, "Idealizing chain %s" % chain.id if chain.id not in processed_chain_ids: processed_chain_ids.append(chain.id) else: continue selection = "protein and chain %s and (name N or name CA or name C or name O)" % chain.id sel = asc.selection("chain %s" % chain.id) chain_h = self.resulting_pdb_h.select(sel) m = chain_h.only_model() i = 0 cutted_chain_h = None for c in m.chains(): if i == 0: cutted_chain_h = iotbx.pdb.hierarchy.new_hierarchy_from_chain( c) else: print >> self.log, "WARNING!!! Duplicating chain ids! Only the first chain will be processed." print >> self.log, " Removing chain %s with %d residues" % ( c.id, len(c.residues())) m.remove_chain(c) i += 1 exclusions, ch_h = self.idealize_chain( hierarchy=(cutted_chain_h if cutted_chain_h else chain_h)) if ch_h is not None: set_xyz_smart( # dest_h=self.resulting_pdb_h, dest_h=chain, source_h=ch_h) for resnum in exclusions: selection += " and not resseq %s" % resnum self.ref_exclusion_selection += "(%s) or " % selection # # dumping and reloading hierarchy to do proper rounding of coordinates self.resulting_pdb_h = iotbx.pdb.input( source_info=None, lines=self.resulting_pdb_h.as_pdb_string( )).construct_hierarchy() berkeley_count = utils.list_rama_outliers_h( self.resulting_pdb_h).count("\n") self.berkeley_p_before_minimization_rama_outliers = \ berkeley_count/float(self.resulting_pdb_h.overall_counts().n_residues)*100 if len(self.ref_exclusion_selection) > 0: self.ref_exclusion_selection = self.ref_exclusion_selection[: -3] # self.resulting_pdb_h.write_pdb_file(file_name="%s_before_minimization.pdb" % self.params.output_prefix) ram = ramalyze.ramalyze(pdb_hierarchy=self.resulting_pdb_h) self.p_before_minimization_rama_outliers = ram.out_percent duke_count = ram.get_outliers_count_and_fraction()[0] if berkeley_count != duke_count: print >> self.log, "Discrepancy between berkeley and duke after ccd:", berkeley_count, duke_count if self.params.minimize_whole: print >> self.log, "minimizing whole thing..." print >> self.log, "self.ref_exclusion_selection", self.ref_exclusion_selection # print >> sel if self.reference_map is None: minimize_wrapper_for_ramachandran( hierarchy=self.resulting_pdb_h, xrs=self.xrs, original_pdb_h=self.original_pdb_h, excl_string_selection=self.ref_exclusion_selection, grm=self.grm, log=None, ss_annotation=self.secondary_structure_annotation) else: mwwm = minimize_wrapper_with_map( pdb_h=self.resulting_pdb_h, xrs=self.xrs, target_map=self.reference_map, grm=self.grm, ss_annotation=self.secondary_structure_annotation, log=self.log) # self.resulting_pdb_h.write_pdb_file(file_name="%s_all_minized.pdb" % self.params.output_prefix) ram = ramalyze.ramalyze(pdb_hierarchy=self.resulting_pdb_h) self.p_after_minimiaztion_rama_outliers = ram.out_percent berkeley_count = utils.list_rama_outliers_h( self.resulting_pdb_h).count("\n") duke_count = ram.get_outliers_count_and_fraction()[0] self.berkeley_p_after_minimiaztion_rama_outliers = \ berkeley_count/float(self.resulting_pdb_h.overall_counts().n_residues)*100 if berkeley_count != duke_count: print >> self.log, "Discrepancy between berkeley and duke after min:", berkeley_count, duke_count else: print >> self.log, "Number of Rama outliers after min:", berkeley_count
def minimize_wrapper_for_ramachandran( hierarchy, xrs, original_pdb_h, excl_string_selection, grm=None, log=None, ncs_restraints_group_list=[], ss_annotation=None, mon_lib_srv=None, ener_lib=None, rotamer_manager=None, reference_rotamers=True, number_of_cycles=1, run_first_minimization_without_reference=False, oldfield_weight_scale=3, oldfield_plot_cutoff=0.03, nonbonded_weight=500, reference_sigma=0.7): """ Wrapper around geometry minimization specifically tuned for eliminating Ramachandran outliers. """ try: import cPickle as pickle except ImportError: import pickle from time import time from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str from mmtbx.geometry_restraints import reference from mmtbx.command_line.geometry_minimization import \ get_geometry_restraints_manager from mmtbx.geometry_restraints.torsion_restraints.reference_model import \ reference_model, reference_model_params from libtbx.utils import null_out from scitbx.array_family import flex if log is None: log = null_out() # assert hierarchy.atoms_size()==xrs.scatterers().size(), "%d %d" % ( # hierarchy.atoms_size(), xrs.scatterers().size()) params_line = grand_master_phil_str params = iotbx.phil.parse(input_string=params_line, process_includes=True).extract() params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None params.pdb_interpretation.peptide_link.ramachandran_restraints = True params.pdb_interpretation.peptide_link.oldfield.weight_scale = oldfield_weight_scale params.pdb_interpretation.peptide_link.oldfield.plot_cutoff = oldfield_plot_cutoff params.pdb_interpretation.nonbonded_weight = nonbonded_weight params.pdb_interpretation.c_beta_restraints = True params.pdb_interpretation.max_reasonable_bond_distance = None params.pdb_interpretation.peptide_link.apply_peptide_plane = True params.pdb_interpretation.ncs_search.enabled = True params.pdb_interpretation.restraints_library.rdl = True processed_pdb_files_srv = mmtbx.utils.\ process_pdb_file_srv( crystal_symmetry= xrs.crystal_symmetry(), pdb_interpretation_params = params.pdb_interpretation, stop_for_unknowns = False, log=log, cif_objects=None) processed_pdb_file, junk = processed_pdb_files_srv.\ process_pdb_files(raw_records=flex.split_lines(hierarchy.as_pdb_string())) mon_lib_srv = processed_pdb_files_srv.mon_lib_srv ener_lib = processed_pdb_files_srv.ener_lib ncs_restraints_group_list = [] if processed_pdb_file.ncs_obj is not None: ncs_restraints_group_list = processed_pdb_file.ncs_obj.get_ncs_restraints_group_list( ) if grm is None: grm = get_geometry_restraints_manager(processed_pdb_file, xrs, params=params) else: grm.geometry.pair_proxies(sites_cart=hierarchy.atoms().extract_xyz()) if grm.geometry.ramachandran_manager is not None: grm.geometry.ramachandran_manager.update_phi_psi_targets( sites_cart=hierarchy.atoms().extract_xyz()) if reference_rotamers and original_pdb_h is not None: # make selection excluding rotamer outliers from mmtbx.rotamer.rotamer_eval import RotamerEval # print "Excluding rotamer outliers" if rotamer_manager is None: rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv) non_rot_outliers_selection = flex.bool(hierarchy.atoms_size(), False) for model in original_pdb_h.models(): for chain in model.chains(): for conf in chain.conformers(): for res in conf.residues(): ev = rotamer_manager.evaluate_residue_2(res) if ev != "OUTLIER" or ev is None: for a in res.atoms(): non_rot_outliers_selection[a.i_seq] = True # else: # print " ", res.id_str() rm_params = reference_model_params.extract() rm_params.reference_model.enabled = True rm_params.reference_model.strict_rotamer_matching = False rm_params.reference_model.main_chain = False rm = reference_model(processed_pdb_file=processed_pdb_file, reference_file_list=None, reference_hierarchy_list=[original_pdb_h], mon_lib_srv=mon_lib_srv, ener_lib=ener_lib, has_hd=None, params=rm_params.reference_model, selection=non_rot_outliers_selection, log=log) rm.show_reference_summary(log=log) grm.geometry.adopt_reference_dihedral_manager(rm) # dealing with SS if ss_annotation is not None: from mmtbx.secondary_structure import manager ss_manager = manager(pdb_hierarchy=hierarchy, geometry_restraints_manager=grm.geometry, sec_str_from_pdb_file=ss_annotation, params=None, mon_lib_srv=mon_lib_srv, verbose=-1, log=log) grm.geometry.set_secondary_structure_restraints(ss_manager=ss_manager, hierarchy=hierarchy, log=log) # grm pickle-unpickle # t0 = time() # prefix="grm" # pklfile = open("%s.pkl" % prefix, 'wb') # pickle.dump(grm.geometry, pklfile) # pklfile.close() # t1 = time() # pklfile = open("%s.pkl" % prefix, 'rb') # grm_from_file = pickle.load(pklfile) # pklfile.close() # t2 = time() # print "Time pickling/unpickling: %.4f, %.4f" % (t1-t0, t2-t1) # grm.geometry=grm_from_file if run_first_minimization_without_reference: obj = run2(restraints_manager=grm, pdb_hierarchy=hierarchy, correct_special_position_tolerance=1.0, ncs_restraints_group_list=ncs_restraints_group_list, max_number_of_iterations=300, number_of_macro_cycles=number_of_cycles, bond=True, nonbonded=True, angle=True, dihedral=True, chirality=True, planarity=True, fix_rotamer_outliers=True, log=log) if original_pdb_h is not None: if len(excl_string_selection) == 0: excl_string_selection = "all" asc = original_pdb_h.atom_selection_cache() sel = asc.selection( "(%s) and (name CA or name C or name N or name O)" % excl_string_selection) grm.geometry.append_reference_coordinate_restraints_in_place( reference.add_coordinate_restraints( sites_cart=original_pdb_h.atoms().extract_xyz().select(sel), selection=sel, sigma=reference_sigma, top_out_potential=True)) # grm.geometry.write_geo_file( # sites_cart=hierarchy.atoms().extract_xyz(), # site_labels=[atom.id_str() for atom in hierarchy.atoms()], # file_name="last_gm.geo") obj = run2(restraints_manager=grm, pdb_hierarchy=hierarchy, correct_special_position_tolerance=1.0, ncs_restraints_group_list=ncs_restraints_group_list, max_number_of_iterations=300, number_of_macro_cycles=number_of_cycles, bond=True, nonbonded=True, angle=True, dihedral=True, chirality=True, planarity=True, fix_rotamer_outliers=True, log=log) grm.geometry.reference_dihedral_manager = None
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"
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_files = work_params.file_name from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str params = iotbx.phil.parse(input_string=grand_master_phil_str, process_includes=True).extract() params.pdb_interpretation.clash_guard.nonbonded_distance_threshold = None mon_lib_srv = mmtbx.monomer_library.server.server() ppf_srv = mmtbx.utils.process_pdb_file_srv( crystal_symmetry=None, pdb_interpretation_params=params.pdb_interpretation, stop_for_unknowns=False, log=log, cif_objects=None, # need to figure out how to get them mon_lib_srv=mon_lib_srv, ener_lib=None, use_neutron_distances=False) processed_pdb_file, pdb_inp = ppf_srv.process_pdb_files( pdb_file_names=pdb_files) cs = ppf_srv.crystal_symmetry xrs = processed_pdb_file.xray_structure(show_summary=True) if (xrs is None): raise Sorry("Cannot extract xray_structure.") grm = get_geometry_restraints_manager( processed_pdb_file=processed_pdb_file, xray_structure=xrs, log=log) pdb_h = processed_pdb_file.all_chain_proxies.pdb_hierarchy fixed_pdb_h = pdb_h.deep_copy() fixed_pdb_h.reset_atom_i_seqs() rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv) fixed_pdb_h = fix_rotamer_outliers(pdb_hierarchy=fixed_pdb_h, grm=grm.geometry, xrs=xrs, radius=work_params.radius, mon_lib_srv=mon_lib_srv, rotamer_manager=rotamer_manager, asc=None) iotbx.pdb.write_whole_pdb_file(file_name="%s.pdb" % work_params.output_prefix, processed_pdb_file=processed_pdb_file, pdb_hierarchy=fixed_pdb_h, crystal_symmetry=cs)
def minimize_wrapper_for_ramachandran( hierarchy, xrs, original_pdb_h, excl_string_selection, log=None, ss_annotation = None, reference_rotamers = True, run_first_minimization_without_reference=False, oldfield_weight_scale=3, oldfield_plot_cutoff=0.03, nonbonded_weight=500, reference_sigma=0.7): """ Wrapper around geometry minimization specifically tuned for eliminating Ramachandran outliers. """ import pickle from time import time from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str from mmtbx.geometry_restraints import reference from mmtbx.command_line.geometry_minimization import \ get_geometry_restraints_manager from mmtbx.geometry_restraints.torsion_restraints.reference_model import \ reference_model, reference_model_params from libtbx.utils import null_out from scitbx.array_family import flex if log is None: log = null_out() params_line = grand_master_phil_str params = iotbx.phil.parse( input_string=params_line, process_includes=True).extract() params.pdb_interpretation.clash_guard.nonbonded_distance_threshold=None params.pdb_interpretation.peptide_link.ramachandran_restraints = True params.pdb_interpretation.peptide_link.oldfield.weight_scale=oldfield_weight_scale params.pdb_interpretation.peptide_link.oldfield.plot_cutoff=oldfield_plot_cutoff params.pdb_interpretation.nonbonded_weight = nonbonded_weight params.pdb_interpretation.c_beta_restraints=True params.pdb_interpretation.max_reasonable_bond_distance = None params.pdb_interpretation.peptide_link.apply_peptide_plane = True params.pdb_interpretation.ncs_search.enabled = True params.pdb_interpretation.restraints_library.rdl = True processed_pdb_files_srv = mmtbx.utils.\ process_pdb_file_srv( crystal_symmetry= xrs.crystal_symmetry(), pdb_interpretation_params = params.pdb_interpretation, stop_for_unknowns = False, log=log, cif_objects=None) processed_pdb_file, junk = processed_pdb_files_srv.\ process_pdb_files(raw_records=flex.split_lines(hierarchy.as_pdb_string())) mon_lib_srv = processed_pdb_files_srv.mon_lib_srv ener_lib = processed_pdb_files_srv.ener_lib ncs_restraints_group_list = [] if processed_pdb_file.ncs_obj is not None: ncs_restraints_group_list = processed_pdb_file.ncs_obj.get_ncs_restraints_group_list() grm = get_geometry_restraints_manager( processed_pdb_file, xrs, params=params) if reference_rotamers and original_pdb_h is not None: # make selection excluding rotamer outliers from mmtbx.rotamer.rotamer_eval import RotamerEval rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv) non_rot_outliers_selection = flex.bool([False]*hierarchy.atoms().size()) for model in original_pdb_h.models(): for chain in model.chains(): for conf in chain.conformers(): for res in conf.residues(): ev = rotamer_manager.evaluate_residue_2(res) if ev != "OUTLIER" or ev is None: for a in res.atoms(): non_rot_outliers_selection[a.i_seq] = True rm_params = reference_model_params.extract() rm_params.reference_model.enabled=True rm_params.reference_model.strict_rotamer_matching=False rm_params.reference_model.main_chain=False rm = reference_model( processed_pdb_file=processed_pdb_file, reference_file_list=None, reference_hierarchy_list=[original_pdb_h], mon_lib_srv=mon_lib_srv, ener_lib=ener_lib, has_hd=None, params=rm_params.reference_model, selection=non_rot_outliers_selection, log=log) rm.show_reference_summary(log=log) grm.geometry.adopt_reference_dihedral_manager(rm) # dealing with SS if ss_annotation is not None: from mmtbx.secondary_structure import manager ss_manager = manager( pdb_hierarchy=hierarchy, geometry_restraints_manager=grm.geometry, sec_str_from_pdb_file=ss_annotation, params=None, mon_lib_srv=mon_lib_srv, verbose=-1, log=log) grm.geometry.set_secondary_structure_restraints( ss_manager=ss_manager, hierarchy=hierarchy, log=log) # grm pickle-unpickle # t0 = time() # prefix="grm" # pklfile = open("%s.pkl" % prefix, 'wb') # pickle.dump(grm.geometry, pklfile) # pklfile.close() # t1 = time() # pklfile = open("%s.pkl" % prefix, 'rb') # grm_from_file = pickle.load(pklfile) # pklfile.close() # t2 = time() # print "Time pickling/unpickling: %.4f, %.4f" % (t1-t0, t2-t1) # grm.geometry=grm_from_file if run_first_minimization_without_reference: obj = run2( restraints_manager=grm, pdb_hierarchy=hierarchy, correct_special_position_tolerance=1.0, ncs_restraints_group_list=ncs_restraints_group_list, max_number_of_iterations=300, number_of_macro_cycles=5, bond=True, nonbonded=True, angle=True, dihedral=True, chirality=True, planarity=True, fix_rotamer_outliers=True, log=log) if original_pdb_h is not None: if len(excl_string_selection) == 0: excl_string_selection = "all" asc = original_pdb_h.atom_selection_cache() sel = asc.selection("(%s) and (name CA or name C or name N or name O)" % excl_string_selection) grm.geometry.append_reference_coordinate_restraints_in_place( reference.add_coordinate_restraints( sites_cart = original_pdb_h.atoms().extract_xyz().select(sel), selection = sel, sigma = reference_sigma, top_out_potential=True)) obj = run2( restraints_manager = grm, pdb_hierarchy = hierarchy, correct_special_position_tolerance = 1.0, ncs_restraints_group_list=ncs_restraints_group_list, max_number_of_iterations = 300, number_of_macro_cycles = 5, bond = True, nonbonded = True, angle = True, dihedral = True, chirality = True, planarity = True, fix_rotamer_outliers = True, log = log)
from __future__ import absolute_import, division, print_function import time from mmtbx import monomer_library import mmtbx.refinement.real_space.fit_residue import iotbx.pdb from mmtbx.rotamer.rotamer_eval import RotamerEval import mmtbx.utils from scitbx.matrix import rotate_point_around_axis import libtbx.load_env mon_lib_srv = monomer_library.server.server() rotamer_eval = RotamerEval() rotamer_manager = mmtbx.idealized_aa_residues.rotamer_manager.load( rotamers="favored") pdb_files = [ "ala.pdb", "asn.pdb", "asp.pdb", "cys.pdb", "gln.pdb", "glu.pdb", "gly.pdb", "his.pdb", "ile.pdb", "leu.pdb", "met.pdb", "mse.pdb", # is ignored with rotamer named None "phe.pdb", "pro.pdb", # BAD all-rotamers files "ser.pdb",
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"
def __init__(self, pdb_h, xrs, target_map, grm=None, ncs_restraints_group_list=[], mon_lib_srv=None, rotamer_manager=None, ss_annotation=None, refine_ncs_operators=False, number_of_cycles=1, log=None): from mmtbx.refinement.geometry_minimization import add_rotamer_restraints from mmtbx.model_statistics import geometry_no_grm self.pdb_h = pdb_h self.xrs = xrs self.log = log self.cs = self.xrs.crystal_symmetry() print >> self.log, "Minimizing using reference map..." self.log.flush() self.grm = grm # create a new one # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py: # minimize_wrapper_for_ramachandran if self.grm is None: from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str from mmtbx.geometry_restraints import reference from mmtbx.command_line.geometry_minimization import \ get_geometry_restraints_manager from libtbx.utils import null_out from scitbx.array_family import flex import mmtbx.utils if self.log is None: self.log = null_out() params_line = grand_master_phil_str import iotbx.phil params = iotbx.phil.parse( input_string=params_line, process_includes=True).extract() params.pdb_interpretation.clash_guard.nonbonded_distance_threshold=None params.pdb_interpretation.peptide_link.ramachandran_restraints = True params.pdb_interpretation.peptide_link.oldfield.weight_scale=3 params.pdb_interpretation.peptide_link.oldfield.plot_cutoff=0.03 params.pdb_interpretation.nonbonded_weight = 500 params.pdb_interpretation.c_beta_restraints=True params.pdb_interpretation.max_reasonable_bond_distance = None params.pdb_interpretation.peptide_link.apply_peptide_plane = True params.pdb_interpretation.ncs_search.enabled = True params.pdb_interpretation.restraints_library.rdl = True processed_pdb_files_srv = mmtbx.utils.\ process_pdb_file_srv( crystal_symmetry= self.cs, pdb_interpretation_params = params.pdb_interpretation, stop_for_unknowns = False, log=self.log, cif_objects=None) processed_pdb_file, junk = processed_pdb_files_srv.\ process_pdb_files(raw_records=flex.split_lines(self.pdb_h.as_pdb_string())) mon_lib_srv = processed_pdb_files_srv.mon_lib_srv ener_lib = processed_pdb_files_srv.ener_lib ncs_restraints_group_list = [] if processed_pdb_file.ncs_obj is not None: ncs_restraints_group_list = processed_pdb_file.ncs_obj.get_ncs_restraints_group_list() grm = get_geometry_restraints_manager( processed_pdb_file, xrs, params=params) # dealing with SS if ss_annotation is not None: from mmtbx.secondary_structure import manager ss_manager = manager( pdb_hierarchy=self.pdb_h, geometry_restraints_manager=grm.geometry, sec_str_from_pdb_file=ss_annotation, params=None, mon_lib_srv=mon_lib_srv, verbose=-1, log=self.log) grm.geometry.set_secondary_structure_restraints( ss_manager=ss_manager, hierarchy=self.pdb_h, log=self.log) else: self.grm.geometry.pair_proxies( sites_cart=self.pdb_h.atoms().extract_xyz()) if self.grm.geometry.ramachandran_manager is not None: self.grm.geometry.ramachandran_manager.update_phi_psi_targets( sites_cart=self.pdb_h.atoms().extract_xyz()) ncs_groups=None if len(ncs_restraints_group_list) > 0: ncs_groups=ncs_restraints_group_list if rotamer_manager is None: from mmtbx.rotamer.rotamer_eval import RotamerEval rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv) self.pdb_h.write_pdb_file(file_name="rsr_before_rot_fix.pdb", crystal_symmetry=self.xrs.crystal_symmetry()) # STOP() selection_real_space = xrs.backbone_selection() # selection_real_space = None import mmtbx.refinement.real_space.weight self.w = None for x in xrange(number_of_cycles): print >> self.log, " Updating rotamer restraints..." self.pdb_h, grm = add_rotamer_restraints( pdb_hierarchy = self.pdb_h, restraints_manager = grm, selection = None, sigma = 5, mode = "fix_outliers", accept_allowed = False, mon_lib_srv = mon_lib_srv, rotamer_manager = rotamer_manager) self.xrs = self.pdb_h.extract_xray_structure(crystal_symmetry=self.cs) self.pdb_h.write_pdb_file(file_name="rsr_after_rot_fix.pdb", crystal_symmetry=self.xrs.crystal_symmetry()) # if True: if ncs_restraints_group_list is None or len(ncs_restraints_group_list)==0: #No NCS if self.w is None: print >> self.log, " Determining weight..." self.log.flush() self.weight = mmtbx.refinement.real_space.weight.run( map_data = target_map, xray_structure = self.xrs, pdb_hierarchy = self.pdb_h, geometry_restraints_manager = grm, rms_bonds_limit = 0.015, rms_angles_limit = 1.0) # division is to put more weight onto restraints. Checked. Works. self.w = self.weight.weight/3.0 # self.w = self.weight.weight # self.w =2 # print >> self.log, self.w for s in self.weight.msg_strings: print >> self.log, s print >> self.log, " Minimizing..." print >> self.log, " with weight %f" % self.w self.log.flush() refine_object = simple( target_map = target_map, selection = None, max_iterations = 150, geometry_restraints_manager = grm.geometry, selection_real_space = selection_real_space, states_accumulator = None, ncs_groups = ncs_groups) refine_object.refine(weight = self.w, xray_structure = self.xrs) self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds() print >> log, "RMSDS:", self.rmsd_bonds_final, self.rmsd_angles_final # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers()) self.xrs=self.xrs.replace_sites_cart( new_sites=refine_object.sites_cart(), selection=None) # print >> log, "sizes", self.xrs.scatterers() else: # Yes NCS # copy-paste from macro_cycle_real_space.py import mmtbx.ncs.ncs_utils as nu nu.get_list_of_best_ncs_copy_map_correlation( ncs_groups = ncs_restraints_group_list, xray_structure = self.xrs, map_data = target_map, d_min = 3) if self.w is None: print >> self.log, " Determining weight... (NCS)", self.weight = mmtbx.refinement.real_space.weight.run( map_data = target_map, xray_structure = self.xrs,#.select(sel_master), pdb_hierarchy = self.pdb_h,#.select(sel_master), geometry_restraints_manager = grm, rms_bonds_limit = 0.01, rms_angles_limit = 1.0, ncs_groups = ncs_restraints_group_list) # division supposed to put more weight onto restraints. Need checking. self.w = self.weight.weight/3.0 for s in self.weight.msg_strings: print >> self.log, s print >> self.log, " Minimizing... (NCS)" actions = [[True, False], ] if refine_ncs_operators: actions = [[False, True], [True, False]] for action in actions: refine_sites, refine_transformations = action tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\ target_function_and_grads_real_space( map_data = target_map, xray_structure = self.xrs, ncs_restraints_group_list = ncs_restraints_group_list, refine_selection = None, real_space_gradients_delta = 1, restraints_manager = grm, data_weight = self.w, refine_sites = refine_sites, refine_transformations = refine_transformations) minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs( target_and_grads_object = tfg_obj, xray_structure = self.xrs, ncs_restraints_group_list = ncs_restraints_group_list, refine_selection = None, finite_grad_differences_test = False, max_iterations = 100, refine_sites = refine_sites, refine_transformations = refine_transformations) self.xrs = tfg_obj.xray_structure # self.structure_monitor.update( # xray_structure = tfg_obj.xray_structure, # accept_as_is = True) self.pdb_h.adopt_xray_structure(self.xrs) # ms = geometry_no_grm( # pdb_hierarchy=self.pdb_h, # molprobity_scores=True) # print >> self.log, ms.format_molprobity_scores(prefix=" ") # print >> log, "pdb_h", self.pdb_h.atoms_size() self.pdb_h.write_pdb_file("after_map_min.pdb")
def __init__(self, pdb_h, xrs, target_map, grm=None, ncs_restraints_group_list=[], mon_lib_srv=None, rotamer_manager=None, ss_annotation=None, refine_ncs_operators=False, number_of_cycles=1, log=None): assert grm is not None from mmtbx.refinement.geometry_minimization import add_rotamer_restraints from mmtbx.model_statistics import geometry_no_grm from mmtbx.refinement.minimization_monitor import minimization_monitor self.pdb_h = pdb_h self.xrs = xrs self.log = log # self.cs = self.xrs.crystal_symmetry() self.cs = crystal.symmetry( unit_cell=self.xrs.crystal_symmetry().unit_cell(), space_group=1) print >> self.log, "Minimizing using reference map..." self.log.flush() self.grm = grm # copy-paste from cctbx_project/mmtbx/refinement/geometry_minimization.py: # minimize_wrapper_for_ramachandran self.grm.geometry.pair_proxies( sites_cart=self.pdb_h.atoms().extract_xyz()) if self.grm.geometry.ramachandran_manager is not None: self.grm.geometry.ramachandran_manager.update_phi_psi_targets( sites_cart=self.pdb_h.atoms().extract_xyz()) ncs_groups=None if len(ncs_restraints_group_list) > 0: ncs_groups=ncs_restraints_group_list if rotamer_manager is None: from mmtbx.rotamer.rotamer_eval import RotamerEval rotamer_manager = RotamerEval(mon_lib_srv=mon_lib_srv) # self.pdb_h.write_pdb_file(file_name="rsr_before_rot_fix.pdb", # crystal_symmetry=self.xrs.crystal_symmetry()) # STOP() # selection_real_space = xrs.backbone_selection() # XXX What is it??? min_monitor = minimization_monitor( number_of_cycles=number_of_cycles, max_number_of_cycles=20, mode="no_outliers") selection_real_space = None import mmtbx.refinement.real_space.weight self.w = 1 print >> log, "number_of_cycles", number_of_cycles print >> log, "Stats before minimization:" ms = geometry_no_grm( pdb_hierarchy=self.pdb_h, molprobity_scores=True) print >> self.log, ms.format_molprobity_scores(prefix=" ") while min_monitor.need_more_cycles(): # for x in xrange(number_of_cycles): print >> self.log, "Cycle number", min_monitor.get_current_cycle_n() print >> self.log, " Updating rotamer restraints..." self.pdb_h, grm = add_rotamer_restraints( pdb_hierarchy = self.pdb_h, restraints_manager = grm, selection = None, sigma = 5, mode = "fix_outliers", accept_allowed = False, mon_lib_srv = mon_lib_srv, rotamer_manager = rotamer_manager) self.xrs = self.pdb_h.extract_xray_structure(crystal_symmetry=self.cs) # self.pdb_h.write_pdb_file(file_name="rsr_after_rot_fix.pdb", # crystal_symmetry=self.xrs.crystal_symmetry()) # if True: if ncs_restraints_group_list is None or len(ncs_restraints_group_list)==0: #No NCS if min_monitor.need_weight_optimization(): # if self.w is None: print >> self.log, " Determining weight..." self.log.flush() self.weight = mmtbx.refinement.real_space.weight.run( map_data = target_map, xray_structure = self.xrs, pdb_hierarchy = self.pdb_h, geometry_restraints_manager = grm, rms_bonds_limit = 0.015, rms_angles_limit = 1.0) # for s in self.weight.msg_strings: # print >> self.log, s # division is to put more weight onto restraints. Checked. Works. self.w = self.weight.weight/3.0 # self.w = self.weight.weight/15.0 # self.w = 0 # self.w = self.weight.weight # print >> self.log, self.w print >> self.log, " Minimizing..." print >> self.log, " with weight %f" % self.w self.log.flush() refine_object = simple( target_map = target_map, selection = None, max_iterations = 150, geometry_restraints_manager = grm.geometry, selection_real_space = selection_real_space, states_accumulator = None, ncs_groups = ncs_groups) refine_object.refine(weight = self.w, xray_structure = self.xrs) self.rmsd_bonds_final, self.rmsd_angles_final = refine_object.rmsds() print >> log, "RMSDS:", self.rmsd_bonds_final, self.rmsd_angles_final # print >> log, "sizes:", len(refine_object.sites_cart()), len(self.xrs.scatterers()) self.xrs=self.xrs.replace_sites_cart( new_sites=refine_object.sites_cart(), selection=None) # print >> log, "sizes", self.xrs.scatterers() else: # Yes NCS # copy-paste from macro_cycle_real_space.py import mmtbx.ncs.ncs_utils as nu nu.get_list_of_best_ncs_copy_map_correlation( ncs_groups = ncs_restraints_group_list, xray_structure = self.xrs, map_data = target_map, d_min = 3) if min_monitor.need_weight_optimization(): # if self.w is None: print >> self.log, " Determining weight... (NCS)", self.weight = mmtbx.refinement.real_space.weight.run( map_data = target_map, xray_structure = self.xrs,#.select(sel_master), pdb_hierarchy = self.pdb_h,#.select(sel_master), geometry_restraints_manager = grm, rms_bonds_limit = 0.01, rms_angles_limit = 1.0, ncs_groups = ncs_restraints_group_list) # division supposed to put more weight onto restraints. Need checking. self.w = self.weight.weight/3.0 for s in self.weight.msg_strings: print >> self.log, s print >> self.log, " Minimizing... (NCS)" tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\ target_function_and_grads_real_space( map_data = target_map, xray_structure = self.xrs, ncs_restraints_group_list = ncs_restraints_group_list, refine_selection = None, real_space_gradients_delta = 1, restraints_manager = grm, data_weight = self.w, refine_sites = True) minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs( target_and_grads_object = tfg_obj, xray_structure = self.xrs, ncs_restraints_group_list = ncs_restraints_group_list, refine_selection = None, finite_grad_differences_test = False, max_iterations = 100, refine_sites = True) self.xrs = tfg_obj.xray_structure # self.structure_monitor.update( # xray_structure = tfg_obj.xray_structure, # accept_as_is = True) self.pdb_h.adopt_xray_structure(self.xrs) ms = geometry_no_grm( pdb_hierarchy=self.pdb_h, molprobity_scores=True) min_monitor.save_cycle_results(geometry=ms) print >> self.log, ms.format_molprobity_scores(prefix=" ")