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.rmsd_b = None self.rmsd_a = None self.dist_from_start = 0 self.dist_from_previous = 0 self.stats_evaluations = [] self.cc_mask = None self.cc_box = None # self.initialize()
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 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 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 exercise(pdb_poor_str, d_min = 1.0, resolution_factor = 0.25): # Fit one residue in many-residues model # # answer pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_answer) pdb_inp.write_pdb_file(file_name = "answer.pdb") xrs_answer = pdb_inp.xray_structure_simple() f_calc = xrs_answer.structure_factors(d_min = d_min).f_calc() fft_map = f_calc.fft_map(resolution_factor=resolution_factor) fft_map.apply_sigma_scaling() target_map = fft_map.real_map_unpadded() mtz_dataset = f_calc.as_mtz_dataset(column_root_label = "FCmap") mtz_object = mtz_dataset.mtz_object() mtz_object.write(file_name = "answer.mtz") # take TYR9 sites_answer = list( pdb_inp.construct_hierarchy().residue_groups())[1].atoms().extract_xyz() # poor mon_lib_srv = monomer_library.server.server() master_params = iotbx.phil.parse( input_string=mmtbx.monomer_library.pdb_interpretation.master_params_str, process_includes=True).extract() master_params.link_distance_cutoff=999 processed_pdb_file = monomer_library.pdb_interpretation.process( mon_lib_srv = mon_lib_srv, params = master_params, ener_lib = monomer_library.server.ener_lib(), raw_records = flex.std_string(pdb_poor_str.splitlines()), strict_conflict_handling = True, force_symmetry = True, log = None) pdb_hierarchy_poor = processed_pdb_file.all_chain_proxies.pdb_hierarchy xrs_poor = processed_pdb_file.xray_structure() sites_cart_poor = xrs_poor.sites_cart() pdb_hierarchy_poor.write_pdb_file(file_name = "poor.pdb") # rotamer_manager = RotamerEval() get_class = iotbx.pdb.common_residue_names_get_class for model in pdb_hierarchy_poor.models(): for chain in model.chains(): for residue in chain.only_conformer().residues(): if(get_class(residue.resname) == "common_amino_acid" and int(residue.resseq)==9): # take TYR9 t0 = time.time() ro = mmtbx.refinement.real_space.fit_residue.run_with_minimization( target_map = target_map, residue = residue, xray_structure = xrs_poor, mon_lib_srv = mon_lib_srv, rotamer_manager = rotamer_manager, real_space_gradients_delta = d_min*resolution_factor, geometry_restraints_manager = processed_pdb_file.geometry_restraints_manager(show_energies=False)) sites_final = residue.atoms().extract_xyz() t1 = time.time()-t0 pdb_hierarchy_poor.adopt_xray_structure(ro.xray_structure) pdb_hierarchy_poor.write_pdb_file(file_name = "refined.pdb") dist = flex.mean(flex.sqrt((sites_answer - sites_final).dot())) # Highly unstable test assert dist < 0.9
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])
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 __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 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: assert len(pht.altloc_indices().keys()) == 1, \ "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() truncate_to_poly_gly(pdb_hierarchy) 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 __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, tried_rama_angles={}, tried_final_rama_angles={}, n_run=0): 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 self.ideal_res_dict = idealized_aa.residue_dict() self.n_run = n_run 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 # here we are recording what CCD solutions were used to fix particular # outliers to not use the same in the next CCD try. # Nested dict. First level: # key: chain id, value: dict # key: resid (string), value: list of tried variants. self.tried_rama_angles = tried_rama_angles self.tried_final_rama_angles = tried_final_rama_angles 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 n_bad_omegas = utils.n_bad_omegas(self.resulting_pdb_h) self.berkeley_p_after_minimiaztion_rama_outliers = self.berkeley_p_before_minimization_rama_outliers self.ref_exclusion_selection = "" self.number_of_ccd_trials = 0 # print "logic expr outcome:", (self.number_of_ccd_trials < 10 and self.berkeley_p_before_minimization_rama_outliers > 0.001) # print self.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 and (n_bad_omegas<1 and self.params.make_all_trans)): print >> self.log, "No ramachandran outliers, skipping CCD step." print "n_bad_omegas", n_bad_omegas print "self.params.make_all_trans",self.params.make_all_trans if not self.params.enabled: print >> self.log, "Loop idealization is not enabled, use 'enabled=True'." while (self.number_of_ccd_trials < self.params.number_of_ccd_trials and (self.berkeley_p_after_minimiaztion_rama_outliers > 0.001 or (n_bad_omegas>=1 and self.params.make_all_trans)) and self.params.enabled): print >> self.log, "CCD try number, outliers:", self.number_of_ccd_trials, self.berkeley_p_before_minimization_rama_outliers processed_chain_ids = [] for chain in self.resulting_pdb_h.only_model().chains(): if chain.id not in self.tried_rama_angles.keys(): self.tried_rama_angles[chain.id] = {} if chain.id not in self.tried_final_rama_angles.keys(): self.tried_final_rama_angles[chain.id] = {} 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), tried_rama_angles_for_chain=self.tried_rama_angles[chain.id], tried_final_rama_angles_for_chain=self.tried_final_rama_angles[chain.id]) 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 print "self.tried_rama_angles", self.tried_rama_angles print "self.tried_final_rama_angles", self.tried_final_rama_angles # # 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] 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 self.resulting_pdb_h.write_pdb_file(file_name="%d%s_discrepancy.pdb" % (self.number_of_ccd_trials, self.params.output_prefix)) if self.params.debug: self.resulting_pdb_h.write_pdb_file( file_name="%d%s_all_not_minized.pdb" % (self.number_of_ccd_trials, self.params.output_prefix)) if self.params.minimize_whole: print >> self.log, "minimizing whole chain..." print >> self.log, "self.ref_exclusion_selection", self.ref_exclusion_selection # print >> sel # XXX but first let's check and fix rotamers... print >> self.log, "Fixing/checking rotamers in loop idealization..." excl_sel = self.ref_exclusion_selection if len(excl_sel) == 0: excl_sel = None non_outliers_for_check = asc.selection("(%s)" % self.ref_exclusion_selection) pre_result_h = mmtbx.utils.fix_rotamer_outliers( pdb_hierarchy=self.resulting_pdb_h, grm=self.grm.geometry, xrs=self.xrs, map_data=self.reference_map, radius=5, mon_lib_srv=None, rotamer_manager=self.rotamer_manager, backrub_range=None, # don't sample backrub at this point non_outliers_to_check=non_outliers_for_check, # bool selection asc=asc, verbose=True, log=self.log) 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, number_of_cycles=Auto, log=self.log) if self.params.debug: self.resulting_pdb_h.write_pdb_file( file_name="%d%s_all_minized.pdb" % (self.number_of_ccd_trials, 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] n_bad_omegas = utils.n_bad_omegas(self.resulting_pdb_h) 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 print >> self.log, "Number of bad omegas:", n_bad_omegas self.number_of_ccd_trials += 1
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 xrange(number_of_macro_cycles): print >> self.log, " macro-cycle:", i_macro_cycle self.restraints_manager.geometry.update_ramachandran_restraints_phi_psi_targets( sites_cart=self.pdb_hierarchy.atoms().extract_xyz()) 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): self.pdb_hierarchy, 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 >> self.log, "Using NCS constraints." xrs = self.pdb_hierarchy.extract_xray_structure( ).deep_copy_scatterers() refine_selection = flex.size_t(xrange(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)
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, 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
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
from __future__ import division import time import mmtbx.refinement.real_space.fit_residues import mmtbx.refinement.real_space from mmtbx.rotamer.rotamer_eval import RotamerEval rotamer_eval = RotamerEval() pdb_poor = """\ CRYST1 36.039 33.133 33.907 90.00 90.00 90.00 P 1 ATOM 369 N GLN A 23 12.323 18.703 12.553 1.00 0.00 N ATOM 370 CA GLN A 23 12.181 19.413 11.273 1.00 0.00 C ATOM 371 C GLN A 23 12.652 18.596 10.060 1.00 0.00 C ATOM 372 O GLN A 23 12.591 19.108 8.942 1.00 0.00 O ATOM 373 CB GLN A 23 12.912 20.768 11.283 1.00 0.00 C ATOM 374 CG GLN A 23 12.633 21.683 12.491 1.00 0.00 C ATOM 375 CD GLN A 23 11.920 22.989 12.156 1.00 0.00 C ATOM 376 OE1 GLN A 23 10.721 23.090 12.304 1.00 0.00 O ATOM 377 NE2 GLN A 23 12.465 23.786 11.261 1.00 0.00 N ATOM 378 H GLN A 23 13.024 19.067 13.190 1.00 0.00 H ATOM 379 HA GLN A 23 11.130 19.627 11.113 1.00 0.00 H ATOM 380 HB2 GLN A 23 13.985 20.581 11.232 1.00 0.00 H ATOM 381 HB3 GLN A 23 12.646 21.297 10.366 1.00 0.00 H ATOM 382 HG2 GLN A 23 12.047 21.165 13.248 1.00 0.00 H ATOM 383 HG3 GLN A 23 13.584 21.937 12.941 1.00 0.00 H ATOM 384 HE21 GLN A 23 13.373 23.615 10.881 1.00 0.00 H ATOM 385 HE22 GLN A 23 11.850 24.502 10.909 1.00 0.00 H ATOM 386 N LYS A 24 13.104 17.339 10.235 1.00 0.00 N ATOM 387 CA LYS A 24 13.177 16.380 9.120 1.00 0.00 C ATOM 388 C LYS A 24 12.362 15.085 9.207 1.00 0.00 C ATOM 389 O LYS A 24 12.509 14.250 8.322 1.00 0.00 O ATOM 390 CB LYS A 24 14.585 16.121 8.540 1.00 0.00 C
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=" ")
def substitute_ss(real_h, xray_structure, ss_annotation, params = None, grm=None, use_plane_peptide_bond_restr=True, fix_rotamer_outliers=True, cif_objects=None, log=null_out(), rotamer_manager=None, reference_map=None, verbose=False): """ Substitute secondary structure elements in real_h hierarchy with ideal ones _in_place_. Returns reference torsion proxies - the only thing that cannot be restored with little effort outside the procedure. real_h - hierarcy to substitute secondary structure elements. xray_structure - xray_structure - needed to get crystal symmetry (to construct processed_pdb_file and xray_structure is needed to call get_geometry_restraints_manager for no obvious reason). ss_annotation - iotbx.pdb.annotation object. """ t0 = time() if rotamer_manager is None: rotamer_manager = RotamerEval() for model in real_h.models(): for chain in model.chains(): if len(chain.conformers()) > 1: raise Sorry("Alternative conformations are not supported.") processed_params = process_params(params) if not processed_params.enabled: return None expected_n_hbonds = 0 ann = ss_annotation for h in ann.helices: expected_n_hbonds += h.get_n_maximum_hbonds() edited_h = real_h.deep_copy() n_atoms_in_real_h = real_h.atoms_size() selection_cache = real_h.atom_selection_cache() # check the annotation for correctness (atoms are actually in hierarchy) error_msg = "The following secondary structure annotations result in \n" error_msg +="empty atom selections. They don't match the structre: \n" t1 = time() # Checking for SS selections deleted_annotations = ann.remove_empty_annotations( hierarchy=real_h, asc=selection_cache) if not deleted_annotations.is_empty(): if processed_params.skip_empty_ss_elements: if len(deleted_annotations.helices) > 0: print >> log, "Removing the following helices because there are" print >> log, "no corresponding atoms in the model:" for h in deleted_annotations.helices: print >> log, h.as_pdb_str() error_msg += " %s\n" % h if len(deleted_annotations.sheets) > 0: print >> log, "Removing the following sheets because there are" print >> log, "no corresponding atoms in the model:" for sh in deleted_annotations.sheets: print >> log, sh.as_pdb_str() error_msg += " %s\n" % sh.as_pdb_str(strand_id=st.strand_id) else: raise Sorry(error_msg) phil_str = ann.as_restraint_groups() # gathering initial special position atoms special_position_settings = crystal.special_position_settings( crystal_symmetry = xray_structure.crystal_symmetry()) site_symmetry_table = \ special_position_settings.site_symmetry_table( sites_cart = real_h.atoms().extract_xyz(), unconditional_general_position_flags=( real_h.atoms().extract_occ() != 1)) original_spi = site_symmetry_table.special_position_indices() t2 = time() # Actually idelizing SS elements log.write("Replacing ss-elements with ideal ones:\n") log.flush() for h in ann.helices: log.write(" %s\n" % h.as_pdb_str()) log.flush() selstring = h.as_atom_selections() isel = selection_cache.iselection(selstring[0]) all_bsel = flex.bool(n_atoms_in_real_h, False) all_bsel.set_selected(isel, True) sel_h = real_h.select(all_bsel, copy_atoms=True) ideal_h = get_helix(helix_class=h.helix_class, pdb_hierarchy_template=sel_h, rotamer_manager=rotamer_manager) # edited_h.select(all_bsel).atoms().set_xyz(ideal_h.atoms().extract_xyz()) set_xyz_carefully(dest_h=edited_h.select(all_bsel), source_h=ideal_h) for sh in ann.sheets: s = " %s\n" % sh.as_pdb_str() ss = s.replace("\n", "\n ") log.write(ss[:-2]) log.flush() for st in sh.strands: selstring = st.as_atom_selections() isel = selection_cache.iselection(selstring) all_bsel = flex.bool(n_atoms_in_real_h, False) all_bsel.set_selected(isel, True) sel_h = real_h.select(all_bsel, copy_atoms=True) ideal_h = secondary_structure_from_sequence( pdb_str=beta_pdb_str, sequence=None, pdb_hierarchy_template=sel_h, rotamer_manager=rotamer_manager, ) set_xyz_carefully(edited_h.select(all_bsel), ideal_h) # edited_h.select(all_bsel).atoms().set_xyz(ideal_h.atoms().extract_xyz()) t3 = time() pre_result_h = edited_h pre_result_h.reset_i_seq_if_necessary() n_atoms = real_h.atoms_size() bsel = flex.bool(n_atoms, False) helix_selection = flex.bool(n_atoms, False) sheet_selection = flex.bool(n_atoms, False) other_selection = flex.bool(n_atoms, False) ss_for_tors_selection = flex.bool(n_atoms, False) nonss_for_tors_selection = flex.bool(n_atoms, False) selection_cache = real_h.atom_selection_cache() # set all CA atoms to True for other_selection #isel = selection_cache.iselection("name ca") isel = selection_cache.iselection("name ca or name n or name o or name c") other_selection.set_selected(isel, True) n_main_chain_atoms = other_selection.count(True) isel = selection_cache.iselection("name ca or name n or name o or name c") nonss_for_tors_selection.set_selected(isel, True) main_chain_selection_prefix = "(name ca or name n or name o or name c) %s" t4 = time() print >> log, "Preparing selections..." log.flush() # Here we are just preparing selections for h in ann.helices: ss_sels = h.as_atom_selections()[0] selstring = main_chain_selection_prefix % ss_sels isel = selection_cache.iselection(selstring) helix_selection.set_selected(isel, True) other_selection.set_selected(isel, False) isel = selection_cache.iselection(selstring) ss_for_tors_selection.set_selected(isel, True) nonss_for_tors_selection.set_selected(isel, False) for sheet in ann.sheets: for ss_sels in sheet.as_atom_selections(): selstring = main_chain_selection_prefix % ss_sels isel = selection_cache.iselection(selstring) sheet_selection.set_selected(isel, True) other_selection.set_selected(isel, False) isel = selection_cache.iselection(selstring) ss_for_tors_selection.set_selected(isel, True) nonss_for_tors_selection.set_selected(isel, False) t5 = time() isel = selection_cache.iselection( "not name ca and not name n and not name o and not name c") other_selection.set_selected(isel, False) helix_sheet_intersection = helix_selection & sheet_selection if helix_sheet_intersection.count(True) > 0: sheet_selection = sheet_selection & ~helix_sheet_intersection assert ((helix_selection | sheet_selection) & other_selection).count(True)==0 from mmtbx.monomer_library.pdb_interpretation import grand_master_phil_str params_line = grand_master_phil_str params_line += "secondary_structure {%s}" % secondary_structure.sec_str_master_phil_str # print "params_line" # print params_line params = iotbx.phil.parse(input_string=params_line, process_includes=True)#.extract() # This does not the same way for a strange reason. Need to investigate. # The number of resulting hbonds is different later. # w_params = params.extract() # w_params.pdb_interpretation.secondary_structure.protein.remove_outliers = False # w_params.pdb_interpretation.peptide_link.ramachandran_restraints = True # w_params.pdb_interpretation.c_beta_restraints = True # w_params.pdb_interpretation.secondary_structure.enabled = True # params.format(python_object=w_params) # params.show() # print "="*80 # print "="*80 # print "="*80 if grm is None: custom_par_text = "\n".join([ "pdb_interpretation.secondary_structure {protein.remove_outliers = False\n%s}" \ % phil_str, "pdb_interpretation.peptide_link.ramachandran_restraints = True", "c_beta_restraints = True", "pdb_interpretation.secondary_structure.enabled=True", "pdb_interpretation.clash_guard.nonbonded_distance_threshold=None", "pdb_interpretation.max_reasonable_bond_distance=None", # "pdb_interpretation.nonbonded_weight=500", "pdb_interpretation.peptide_link.oldfield.weight_scale=3", "pdb_interpretation.peptide_link.oldfield.plot_cutoff=0.03", "pdb_interpretation.peptide_link.omega_esd_override_value=3", "pdb_interpretation.peptide_link.apply_all_trans=True", ]) if use_plane_peptide_bond_restr: custom_par_text += "\npdb_interpretation.peptide_link.apply_peptide_plane=True" custom_pars = params.fetch( source=iotbx.phil.parse(custom_par_text)).extract() # params.format(python_object=custom_pars) # params.show() # STOP() params = custom_pars # params = w_params t6 = time() import mmtbx.utils processed_pdb_files_srv = mmtbx.utils.\ process_pdb_file_srv( crystal_symmetry= xray_structure.crystal_symmetry(), pdb_interpretation_params = params.pdb_interpretation, log=null_out(), cif_objects=cif_objects) if verbose: print >> log, "Processing file..." log.flush() processed_pdb_file, junk = processed_pdb_files_srv.\ process_pdb_files(raw_records=flex.split_lines(real_h.as_pdb_string())) t7 = time() grm = get_geometry_restraints_manager( processed_pdb_file, xray_structure) t8 = time() else: ss_params = secondary_structure.default_params ss_params.secondary_structure.protein.remove_outliers=False ss_manager = secondary_structure.manager( pdb_hierarchy=real_h, geometry_restraints_manager=grm.geometry, sec_str_from_pdb_file=ss_annotation, params=ss_params.secondary_structure, mon_lib_srv=None, verbose=-1, log=log) grm.geometry.set_secondary_structure_restraints( ss_manager=ss_manager, hierarchy=real_h, log=log) real_h.reset_i_seq_if_necessary() from mmtbx.geometry_restraints import reference if reference_map is None: if verbose: print >> log, "Adding reference coordinate restraints..." grm.geometry.append_reference_coordinate_restraints_in_place( reference.add_coordinate_restraints( sites_cart = real_h.atoms().extract_xyz().select(helix_selection), selection = helix_selection, sigma = processed_params.sigma_on_reference_helix)) grm.geometry.append_reference_coordinate_restraints_in_place( reference.add_coordinate_restraints( sites_cart = real_h.atoms().extract_xyz().select(sheet_selection), selection = sheet_selection, sigma = processed_params.sigma_on_reference_sheet)) grm.geometry.append_reference_coordinate_restraints_in_place( reference.add_coordinate_restraints( sites_cart = real_h.atoms().extract_xyz().select(other_selection), selection = other_selection, sigma = processed_params.sigma_on_reference_non_ss)) if verbose: print >> log, "Adding chi torsion restraints..." grm.geometry.add_chi_torsion_restraints_in_place( pdb_hierarchy = pre_result_h, sites_cart = pre_result_h.atoms().extract_xyz().\ select(ss_for_tors_selection), selection = ss_for_tors_selection, chi_angles_only = False, sigma = processed_params.sigma_on_torsion_ss) grm.geometry.add_chi_torsion_restraints_in_place( pdb_hierarchy = pre_result_h, sites_cart = real_h.atoms().extract_xyz().\ select(nonss_for_tors_selection), selection = nonss_for_tors_selection, chi_angles_only = False, sigma = processed_params.sigma_on_torsion_nonss) real_h.atoms().set_xyz(pre_result_h.atoms().extract_xyz()) # # Check and correct for special positions # special_position_settings = crystal.special_position_settings( crystal_symmetry = xray_structure.crystal_symmetry()) site_symmetry_table = \ special_position_settings.site_symmetry_table( sites_cart = real_h.atoms().extract_xyz(), unconditional_general_position_flags=( real_h.atoms().extract_occ() != 1)) spi = site_symmetry_table.special_position_indices() if spi.size() > 0: print >> log, "Moving atoms from special positions:" for spi_i in spi: if spi_i not in original_spi: new_coords = ( real_h.atoms()[spi_i].xyz[0]+0.2, real_h.atoms()[spi_i].xyz[1]+0.2, real_h.atoms()[spi_i].xyz[2]+0.2) print >> log, " ", real_h.atoms()[spi_i].id_str(), print >> log, tuple(real_h.atoms()[spi_i].xyz), "-->", new_coords real_h.atoms()[spi_i].set_xyz(new_coords) t9 = time() if processed_params.file_name_before_regularization is not None: grm.geometry.pair_proxies(sites_cart=real_h.atoms().extract_xyz()) if grm.geometry.ramachandran_manager is not None: grm.geometry.ramachandran_manager.update_phi_psi_targets( sites_cart=real_h.atoms().extract_xyz()) print >> log, "Outputting model before regularization %s" % processed_params.file_name_before_regularization real_h.write_pdb_file( file_name=processed_params.file_name_before_regularization) geo_fname = processed_params.file_name_before_regularization[:-4]+'.geo' print >> log, "Outputting geo file for regularization %s" % geo_fname grm.write_geo_file( site_labels=[atom.id_str() for atom in real_h.atoms()], file_name=geo_fname) #testing number of restraints assert grm.geometry.get_n_den_proxies() == 0 if reference_map is None: assert grm.geometry.get_n_reference_coordinate_proxies() == n_main_chain_atoms refinement_log = null_out() log.write( "Refining geometry of substituted secondary structure elements...") log.flush() if verbose: refinement_log = log from mmtbx.refinement.geometry_minimization import run2 t10 = time() if reference_map is None: obj = run2( restraints_manager = grm, pdb_hierarchy = real_h, correct_special_position_tolerance = 1.0, max_number_of_iterations = processed_params.n_iter, number_of_macro_cycles = processed_params.n_macro, bond = True, nonbonded = True, angle = True, dihedral = True, chirality = True, planarity = True, fix_rotamer_outliers = fix_rotamer_outliers, log = refinement_log) else: ref_xrs = real_h.extract_xray_structure( crystal_symmetry=xray_structure.crystal_symmetry()) minimize_wrapper_with_map( pdb_h=real_h, xrs=ref_xrs, target_map=reference_map, grm=grm, ncs_restraints_group_list=[], mon_lib_srv=None, ss_annotation=ss_annotation, refine_ncs_operators=False, number_of_cycles=processed_params.n_macro, log=log) real_h.write_pdb_file("after_ss_map_min.pdb") log.write(" Done\n") log.flush() t11 = time() # print >> log, "Initial checking, init : %.4f" % (t1-t0) # print >> log, "Checking SS : %.4f" % (t2-t1) # print >> log, "Initializing selections : %.4f" % (t4-t3) # print >> log, "Looping for selections : %.4f" % (t5-t4) # print >> log, "Finalizing selections : %.4f" % (t6-t5) # print >> log, "PDB interpretation : %.4f" % (t7-t6) # print >> log, "Get GRM : %.4f" % (t8-t7) # print >> log, "Adding restraints to GRM : %.4f" % (t9-t8) # print >> log, "Running GM : %.4f" % (t11-t10) # print_hbond_proxies(grm.geometry,real_h) return grm.geometry.get_chi_torsion_proxies()
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']
rotamers.append(rotamer_manager.evaluate_residue(res)) # print rotamers assert rotamers == ['m-80', 'p'], rotamers def exercise_2(mon_lib_srv, ener_lib, rotamer_manager): pdb_h, grm, xrs = get_necessary_inputs(pdb_str_2, mon_lib_srv, ener_lib) pdb_h = fix_rotamer_outliers(pdb_hierarchy=pdb_h, grm=grm, xrs=xrs, radius=5, mon_lib_srv=mon_lib_srv, rotamer_manager=rotamer_manager, asc=None) rotamers = [] for res in pdb_h.only_chain().only_conformer().residues(): rotamers.append(rotamer_manager.evaluate_residue(res)) assert rotamers == [ 'mtt180', 'tttt', 'm-30', 'pt', 'p', 'mt-10', 'm-40', 'EXCEPTION', 'tt', 'tttt' ], rotamers if (__name__ == "__main__"): mon_lib_srv = mmtbx.monomer_library.server.server() ener_lib = mmtbx.monomer_library.server.ener_lib() rotamer_manager = RotamerEval() exercise_1(mon_lib_srv, ener_lib, rotamer_manager) exercise_2(mon_lib_srv, ener_lib, rotamer_manager) 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")