def validate_residues(self): from mmtbx.conformation_dependent_library import generate_protein_threes from mmtbx.rotamer import ramachandran_eval, rotamer_eval # this is so we generate rama_eval only once rama_eval = ramachandran_eval.RamachandranEval() rota_eval = rotamer_eval.RotamerEval() rotamer_id = rotamer_eval.RotamerID() # loads in the rotamer names threes = generate_protein_threes(hierarchy=self.pdb_hierarchy, include_non_linked=True, backbone_only=False, geometry=None) for i, three in enumerate(threes): if i == 0: self.residues.append( ValidationResidue(three, rama_eval, rota_eval, rotamer_id, index=0)) self.residues.append( ValidationResidue(three, rama_eval, rota_eval, rotamer_id)) if three.end: self.residues.append( ValidationResidue(three, rama_eval, rota_eval, rotamer_id, index=2))
def fit_residue_in_box(self, mon_lib_srv=None, rotamer_manager=None, backbone_sample_angle=20): import mmtbx.refinement.real_space.fit_residue if (mon_lib_srv is None): import mmtbx.monomer_library.server mon_lib_srv = mmtbx.monomer_library.server.server() if (rotamer_manager is None): from mmtbx.rotamer import rotamer_eval rotamer_manager = rotamer_eval.RotamerEval(mon_lib_srv=mon_lib_srv) residue = self.only_residue() self.restrain_atoms(selection=self.others_in_box, reference_sigma=0.1) self.real_space_refine() mmtbx.refinement.real_space.fit_residue.run_with_minimization( target_map=self.target_map_box, residue=residue, xray_structure=self.box.xray_structure_box, mon_lib_srv=mon_lib_srv, rotamer_manager=rotamer_manager, geometry_restraints_manager=self.box_restraints_manager.geometry, real_space_gradients_delta=self.d_min * self.resolution_factor, rms_bonds_limit=0.01, rms_angles_limit=1.0, backbone_sample_angle=backbone_sample_angle)
def score_rotamers (hierarchy, selection) : """ Count the number of rotamer outliers from a selection of residues in a PDB hierarchy. """ from mmtbx.rotamer import rotamer_eval r = rotamer_eval.RotamerEval(data_version="8000") n_outliers = 0 sub_hierarchy = hierarchy.select(selection) # XXX probably inefficient for rg in sub_hierarchy.only_model().only_chain().residue_groups() : rotamer_flag = r.evaluate_residue(rg.only_atom_group()) if (rotamer_flag == "OUTLIER") : n_outliers += 1 return n_outliers
def __init__(self, residue, sites_cart, mon_lib_srv, params, prev_residue=None, next_residue=None, next_next_residue=None, evaluate_backbone_callback=None): adopt_init_args(self, locals()) from mmtbx.rotamer import rotamer_eval from mmtbx.rotamer import ramachandran_eval import iotbx.pdb from scitbx.array_family import flex get_class = iotbx.pdb.common_residue_names_get_class assert get_class( residue.resname) == "common_amino_acid", residue.resname self.rotamer_scorer = rotamer_eval.RotamerEval(data_version="8000") self.ramachandran_scorer = ramachandran_eval.RamachandranEval() self.sidechain_clusters = generate_sidechain_clusters( residue=residue, mon_lib_srv=mon_lib_srv) self.sites_start = sites_cart.deep_copy() self.i_seqs_residue = residue.atoms().extract_i_seq() self.i_seqs_sidechain = flex.size_t() for atom in self.residue.atoms(): if (not atom.name.strip() in ["C", "N", "H", "CA", "CB", "self"]): self.i_seqs_sidechain.append(atom.i_seq) self.i_seqs_primary = flex.size_t() if (not None in [prev_residue, next_residue]): self.set_up_backrub() if (next_next_residue is not None): self.set_up_shear() for i_seq in self.shear_i_seqs_primary1: self.i_seqs_primary.append(i_seq) for i_seq in self.shear_i_seqs_primary2: self.i_seqs_primary.append(i_seq) for i_seq in self.shear_i_seqs_middle: self.i_seqs_primary.append(i_seq) else: for i_seq in self.backrub_i_seqs: self.i_seqs_primary.append(i_seq) else: self.i_seqs_primary = self.i_seqs_residue
def __init__(self, pdb_hierarchy, xray_structure, range_start=-10.0, range_stop=10.0, step=1.0, min_angle_between_solutions=0.5, name_hash=None, selection=None, log=None): if (log is None): log = sys.stdout self.log = log self.mon_lib_srv = mmtbx.monomer_library.server.server() self.unit_cell = xray_structure.unit_cell() self.exclude_free_r_reflections = False self.torsion_params = \ fit_rotamers.torsion_search_params().extract().torsion_search self.torsion_params.range_start = range_start self.torsion_params.range_stop = range_stop self.torsion_params.step = step self.torsion_params.min_angle_between_solutions = \ min_angle_between_solutions self.selection = selection if self.selection is None: sites_cart = pdb_hierarchy.atoms().extract_xyz() self.selection = flex.bool(len(sites_cart), True) self.c_alpha_hinges = torsion_utils.get_c_alpha_hinges( pdb_hierarchy=pdb_hierarchy, xray_structure=xray_structure, selection=self.selection) self.name_hash = name_hash if self.name_hash is None: self.name_hash = build_name_hash(pdb_hierarchy) from mmtbx.rotamer.sidechain_angles import SidechainAngles from mmtbx.rotamer import rotamer_eval self.sa = SidechainAngles(False) self.rotamer_id = rotamer_eval.RotamerID() self.rotamer_evaluator = rotamer_eval.RotamerEval( mon_lib_srv=self.mon_lib_srv) self.target_map_data = None self.residual_map_data = None
def update_restraints(hierarchy, geometry, # restraints_manager, current_geometry=None, # xray_structure!! sites_cart=None, rdl_proxies=None, esd_factor=1., exclude_backbone=False, assert_rotamer_found=False, rdl_selection=None, log=None, verbose=False, data_version="8000", ): assert data_version == "8000","data_version not recognized." assert not exclude_backbone loud=True and False if loud: verbose=1 from mmtbx.rotamer.sidechain_angles import SidechainAngles from mmtbx.rotamer import rotamer_eval from mmtbx.validation import rotalyze # def _set_or_reset_angle_restraints(geometry, lookup, use_rdl_weights=False, verbose=False, ): count = 0 for angle_proxy in geometry.angle_proxies: if angle_proxy.i_seqs in lookup: if verbose: print(" i_seqs %-15s initial %12.3f %12.3f" % ( angle_proxy.i_seqs, angle_proxy.angle_ideal, angle_proxy.weight, )) assert angle_proxy.angle_ideal<181 angle_proxy.angle_ideal = lookup[angle_proxy.i_seqs][0] if use_rdl_weights: angle_proxy.weight = esd_factor/lookup[angle_proxy.i_seqs][1]**2 if verbose: print(" i_seqs %-15s final %12.3f %12.3f" % ( angle_proxy.i_seqs, angle_proxy.angle_ideal, angle_proxy.weight, )) count += 1 return count # def _set_or_reset_dihedral_restraints(geometry, lookup, use_rdl_weights=False, verbose=False, ): count = 0 for angle_proxy in geometry.dihedral_proxies: if angle_proxy.i_seqs in lookup: if verbose: print(" i_seqs %-15s initial %12.3f %12.3f %s %d" % ( angle_proxy.i_seqs, angle_proxy.angle_ideal, angle_proxy.weight, angle_proxy.alt_angle_ideals, angle_proxy.periodicity, )) angle_proxy.angle_ideal = lookup[angle_proxy.i_seqs][0] if use_rdl_weights: angle_proxy.weight = esd_factor/lookup[angle_proxy.i_seqs][1]**2 angle_proxy.alt_angle_ideals=None angle_proxy.periodicity = lookup[angle_proxy.i_seqs][2] if verbose: print(" i_seqs %-15s final %12.3f %12.3f %s %d" % ( angle_proxy.i_seqs, angle_proxy.angle_ideal, angle_proxy.weight, angle_proxy.alt_angle_ideals, angle_proxy.periodicity, )) count += 1 return count # t0=time.time() sa = SidechainAngles(False) rotamer_id = rotamer_eval.RotamerID() rotamer_evaluator = rotamer_eval.RotamerEval(data_version=data_version) sites_cart = None if current_geometry: sites_cart = current_geometry.sites_cart() # if rdl_proxies is None: rdl_proxies = [] i_seqs_restraints = {} i_seqs_restraints_reverse = {} # def _alt_loc_atom_generator(residue_group, atom_group): atoms = [] for ag in residue_group.atom_groups(): if ag.altloc.strip()=="" or ag.altloc.strip()==atom_group.altloc.strip(): for atom in ag.atoms(): yield atom # for model in hierarchy.models(): #if verbose: print 'model: "%s"' % model.id for chain in model.chains(): #if verbose: print 'chain: "%s"' % chain.id for residue_group in chain.residue_groups(): all_dict = rotalyze.construct_complete_sidechain(residue_group) for atom_group in residue_group.atom_groups(): if rdl_selection is None: pass elif 'all' in rdl_selection: pass elif atom_group.resname in rdl_selection: pass else: continue rc = get_rotamer_data(atom_group, sa, rotamer_evaluator, rotamer_id, all_dict=all_dict, sites_cart=sites_cart, ) if rc is None: continue rotamer_name, chis, value = rc if verbose: chis_str = "[" if chis: for chi in chis: chis_str += " %6.1f," % chi chis_str = chis_str[:-1]+']' try: print(" %s %s %s %-5s %-60s %0.1f" % ( chain.id, atom_group.resname, residue_group.resseq, rotamer_name, chis_str, value, ), file=log) except TypeError as e: print(" %s %s %s %-5s %-60s %s" % ( chain.id, atom_group.resname, residue_group.resseq, rotamer_name, chis_str, value, ), file=log) if loud: print('exclude_backbone',exclude_backbone) if rotamer_name in ["OUTLIER"]: continue resname_lookup = substitute_residue_lookup.get(atom_group.resname, atom_group.resname, ) if rotamer_name not in rdl_database[resname_lookup]: if assert_rotamer_found: raise Sorry("rotamer %s not found in db" % rotamer_name) else: continue if loud: print('not outlier') if rotamer_name not in rdl_database[resname_lookup]: if loud: print("rotamer_name %s not in RDL db" % rotamer_name) continue if loud: print('rotamer_name %s found' % rotamer_name) restraints = rdl_database[resname_lookup][rotamer_name] defaults = rdl_database[resname_lookup]["default"] rdl_proxies.append(atom_group.id_str()) for names, values in restraints.items(): i_seqs = [] if exclude_backbone and names[1] in ["N", "CA", "C"]: continue for name in names: for atom in _alt_loc_atom_generator(residue_group, atom_group): if name.strip()==atom.name.strip(): i_seqs.append(atom.i_seq) break if len(i_seqs)!=len(names): continue i_seqs_restraints[tuple(i_seqs)] = values i_seqs.reverse() i_seqs_restraints[tuple(i_seqs)] = values if names in defaults: i_seqs_restraints_reverse[tuple(i_seqs)] = defaults[names] i_seqs.reverse() i_seqs_restraints_reverse[tuple(i_seqs)] = defaults[names] # if loud: for i, atom in enumerate(hierarchy.atoms()): print(i, atom.quote()) count = _set_or_reset_dihedral_restraints(geometry, i_seqs_restraints_reverse, #verbose=loud, ) count_d = _set_or_reset_dihedral_restraints(geometry, i_seqs_restraints, verbose=loud, ) count = _set_or_reset_angle_restraints(geometry, i_seqs_restraints_reverse, #verbose=loud, ) count_a = _set_or_reset_angle_restraints(geometry, i_seqs_restraints, verbose=loud, ) # print(" Number of angles, dihedrals RDL adjusted : %d, %d" % ( count_a, count_d, ), file=log) print(" Time to adjust : %0.1fs" % ( time.time()-t0), file=log) return rdl_proxies
def refit_residues(pdb_hierarchy, cif_objects, fmodel, use_rotamers=True, anneal=False, verbose=True, allow_modified_residues=False, out=sys.stdout): """ Use real-space refinement tools to fit newly extended residues. """ from mmtbx.refinement.real_space import fit_residue from mmtbx.rotamer import rotamer_eval import mmtbx.monomer_library.server from mmtbx import building from scitbx.array_family import flex mon_lib_srv = mmtbx.monomer_library.server.server() rotamer_manager = rotamer_eval.RotamerEval() ppdb_out = box_out = out if (not verbose): ppdb_out = null_out() box_out = null_out() make_sub_header("Processing new model", out=ppdb_out) processed_pdb = building.reprocess_pdb( pdb_hierarchy=pdb_hierarchy, crystal_symmetry=fmodel.f_obs().crystal_symmetry(), cif_objects=cif_objects, out=ppdb_out) print("", file=ppdb_out) hierarchy = processed_pdb.all_chain_proxies.pdb_hierarchy xrs = processed_pdb.xray_structure() grm_geometry = processed_pdb.geometry_restraints_manager() make_sub_header("Fitting residues", out=out) target_map = fmodel.map_coefficients( map_type="2mFo-DFc", exclude_free_r_reflections=True).fft_map( resolution_factor=0.25).apply_sigma_scaling().real_map_unpadded() unit_cell = xrs.unit_cell() for chain in hierarchy.only_model().chains(): if (not chain.is_protein()) and (not allow_modified_residues): continue residue_groups = chain.residue_groups() for i_rg, residue_group in enumerate(residue_groups): prev_res = next_res = None atom_groups = residue_group.atom_groups() if (len(atom_groups) > 1): continue residue = atom_groups[0] atoms = residue.atoms() atoms.reset_tmp() segids = atoms.extract_segid() if (segids.all_eq("XXXX")): sites_start = atoms.extract_xyz() def get_two_fofc_mean(residue): sum = 0 n_atoms = 0 for atom in residue.atoms(): if (not atom.element.strip() in ["H", "D"]): site_frac = unit_cell.fractionalize( site_cart=atom.xyz) sum += target_map.eight_point_interpolation( site_frac) n_atoms += 1 assert (n_atoms > 0) return sum / n_atoms sites_start = atoms.extract_xyz().deep_copy() two_fofc_mean_start = get_two_fofc_mean(residue) refit = fit_residue.run_with_minimization( target_map=target_map, residue=residue, xray_structure=xrs, mon_lib_srv=mon_lib_srv, rotamer_manager=rotamer_manager, geometry_restraints_manager=grm_geometry, real_space_gradients_delta=fmodel.f_obs().d_min() * 0.25, rms_bonds_limit=0.01, rms_angles_limit=1.0, backbone_sample_angle=20, allow_modified_residues=allow_modified_residues) two_fofc_mean_end = get_two_fofc_mean(residue) sites_end = atoms.extract_xyz() flag = "" if (two_fofc_mean_end > two_fofc_mean_start): flag = " <-- keep" xrs = refit.xray_structure else: atoms.set_xyz(sites_start) for atom in atoms: atom.tmp = 1 print(" residue '%s' : rmsd=%5.3f 2fofc_start=%5.3f 2fofc_end=%5.3f%s" \ % (residue.id_str(), sites_end.rms_difference(sites_start), two_fofc_mean_start, two_fofc_mean_end, flag), file=out) return hierarchy, xrs
# There's also plenty of room to expand on this to look at # correlations between residues in one ensemble a la MutInf... # # Started by Daniel Keedy in May 2013 # ############################################################## import sys import os import iotbx.pdb.hierarchy from mmtbx.rotamer import rotamer_eval from mmtbx.rotamer import sidechain_angles import iotbx.pdb.amino_acid_codes # These scorers are global variables since they hog memory (I think?) rotamer_scorer = rotamer_eval.RotamerEval() sc_angles_measurer = sidechain_angles.SidechainAngles(show_errs=True) aa_resnames = iotbx.pdb.amino_acid_codes.one_letter_given_three_letter class StructureEnsemble(): def __init__(self, nm): self.name = nm self.conf_ensems = {} # CNIT --> ConformerEnsemble def add_structures(self, dirname): for filename in os.listdir(dirname): self.add_structure(filename) def add_structure(self, filename):
def __init__(self): from mmtbx.rotamer.sidechain_angles import SidechainAngles from mmtbx.rotamer import rotamer_eval self.sa = SidechainAngles(False) self.data_version = "8000" self.r = rotamer_eval.RotamerEval(data_version=self.data_version)
def __init__(self, pdb_hierarchy, data_version="8000", outliers_only=False, show_errors=False, out=sys.stdout, quiet=False): validation.__init__(self) self.n_allowed = 0 self.n_favored = 0 from mmtbx.rotamer.sidechain_angles import SidechainAngles from mmtbx.rotamer import rotamer_eval from mmtbx.rotamer.rotamer_eval import RotamerID from mmtbx.validation import utils self.data_version = data_version # if self.data_version == "500": self.outlier_threshold = 0.01 if self.data_version == "8000": self.outlier_threshold = OUTLIER_THRESHOLD else: raise ValueError( "data_version given to RotamerEval not recognized (%s)." % data_version) sidechain_angles = SidechainAngles(show_errors) rotamer_evaluator = rotamer_eval.RotamerEval( data_version=data_version) rotamer_id = rotamer_eval.RotamerID() # loads in the rotamer names use_segids = utils.use_segids_in_place_of_chainids( hierarchy=pdb_hierarchy) current_rotamers = {} for model in pdb_hierarchy.models(): for chain in model.chains(): if use_segids: chain_id = utils.get_segid_as_chainid(chain=chain) else: chain_id = chain.id for rg in chain.residue_groups(): all_dict = construct_complete_sidechain(rg) for atom_group in rg.atom_groups(): coords = get_center(atom_group) resname = atom_group.resname occupancy = get_occupancy(atom_group) kwargs = { "chain_id" : chain_id, "resseq" : rg.resseq, "icode" : rg.icode, "altloc" : atom_group.altloc, "resname" : resname, "xyz" : coords, "occupancy" : occupancy, } atom_dict = all_dict.get(atom_group.altloc) res_key = get_residue_key(atom_group=atom_group) try: chis = sidechain_angles.measureChiAngles( atom_group, atom_dict)#.get(conformer.altloc)) except AttributeError: if show_errors: kwargs['incomplete'] = True result = rotamer(**kwargs) print >> out, '%s is missing some sidechain atoms' % \ result.id_str() self.results.append(result) continue if (chis is not None): if None in chis: continue cur_res = resname.lower().strip() if cur_res == 'mse': cur_res = 'met' value = rotamer_evaluator.evaluate(cur_res, chis) if value is not None: self.n_total += 1 kwargs['score'] = value * 100 wrap_chis = rotamer_id.wrap_chis(resname.strip(), chis, symmetry=False) sym_chis = wrap_chis[:] sym_chis = rotamer_id.wrap_sym(resname.strip(), sym_chis) evaluation = self.evaluateScore(value) kwargs['evaluation'] = evaluation if evaluation == "OUTLIER": kwargs['outlier'] = True kwargs['rotamer_name'] = evaluation else: kwargs['outlier'] = False kwargs['rotamer_name'] = rotamer_id.identify(resname, wrap_chis) #deal with unclassified rotamers if kwargs['rotamer_name'] == '': kwargs['rotamer_name'] = "UNCLASSIFIED" while (len(wrap_chis) < 4): wrap_chis.append(None) kwargs['chi_angles'] = wrap_chis result = rotamer(**kwargs) if (result.is_outlier()) or (not outliers_only): self.results.append(result) out_count, out_percent = self.get_outliers_count_and_fraction() self.out_percent = out_percent * 100.0
def process_results(pdb_hierarchy, fmodel, residues_in, building_trials, params, verbose=False, log=sys.stdout): assert (len(residues_in) == len(building_trials)) from mmtbx.rotamer import rotamer_eval n_alternates = 0 unit_cell = fmodel.xray_structure.unit_cell() two_fofc_map, fofc_map = building.get_difference_maps(fmodel) rot_eval = rotamer_eval.RotamerEval(data_version="8000") for main_conf, trials in zip(residues_in, building_trials): if (trials is None): print("WARNING: error building %s" % main_conf.id_str(), file=log) continue if (len(trials) == 0): continue res_log = StringIO() print(" %s:" % main_conf.id_str(), file=res_log) main_rotamer = alt_rotamer = None if (not main_conf.resname in ["GLY", "PRO", "ALA"]): main_rotamer = rot_eval.evaluate_residue(main_conf) assert (main_rotamer != "OUTLIER") best_trial = pick_best_alternate(trials=trials, params=params, rotamer=main_rotamer, log=res_log) if (best_trial is None): continue new_conf = best_trial.as_atom_group() changed_rotamer = (best_trial.rotamer != main_rotamer) skip = False flag = "" stats = best_trial.stats # FIXME this needs to be made more consistent with the filtering criteria # in disorder/__init__.py if ((stats.rmsd < params.rmsd_min) and (stats.max_dev < params.rmsd_min) and (not changed_rotamer)): skip = True print(" selected conformer (occ=%.2f):" % \ best_trial.occupancy, file=res_log) res_log2 = StringIO() density_quality = building.residue_density_quality( atom_group=new_conf, unit_cell=unit_cell, two_fofc_map=two_fofc_map, fofc_map=fofc_map) if (main_conf.resname in ["CYS", "MET", "MSE"]): # XXX if we have a heavier atom (S or SE) in the residue, some additional # sanity checks insure that it has slightly stronger density than we # require for lighter elements. multipliers are just guesses, taking # into account rad damage and partial SeMet incorporation. heavy_atom = { "CYS": "SG", "MET": "SD", "MSE": "SE" }[main_conf.resname] mult = {"CYS": 1.6, "MET": 1.6, "MSE": 2.0}[main_conf.resname] map_levels = density_quality.density_at_atom(heavy_atom) if (map_levels is None): # this probably shouldn't even happen skip = True if ((map_levels.fofc < params.map_thresholds.fofc_min * mult) or (map_levels.two_fofc < params.map_thresholds.two_fofc_min * mult)): skip = True n_atoms_outside_density = density_quality.show_atoms_outside_density( two_fofc_cutoff=params.map_thresholds.two_fofc_min, fofc_cutoff=params.map_thresholds.fofc_min, out=res_log2, prefix=" ") fofc_max = density_quality.max_fofc_value() if (n_atoms_outside_density > 0): skip = True if (not skip) and (verbose): flag = " ***" print(" RMSD=%5.3f max. change=%.2f max(Fo-Fc)=%.1f%s" \ % (stats.rmsd, stats.max_dev, fofc_max, flag), file=res_log) if (changed_rotamer): print(" starting rotamer=%s new rotamer=%s" % \ (main_rotamer, best_trial.rotamer), file=res_log) if (n_atoms_outside_density != 0): print(" atoms outside density:", file=res_log) print(res_log2.getvalue(), file=res_log) else: print("", file=res_log) if (not skip) or (verbose): log.write(res_log.getvalue()) if (skip): continue residue_group = main_conf.parent() main_conf.altloc = 'A' new_occ = 0.5 if (params.expected_occupancy is not None): new_occ = max(0.2, min(0.8, params.expected_occupancy)) for atom in main_conf.atoms(): atom.occ = 1.0 - new_occ atom.segid = alt_confs.SEGID_MAIN new_conf = new_conf.detached_copy() new_conf.altloc = 'B' for atom in new_conf.atoms(): atom.segid = alt_confs.SEGID_NEW_REBUILT atom.occ = new_occ residue_group.append_atom_group(new_conf) n_alternates += 1 return n_alternates
def find_alternate_residue(residue, pdb_hierarchy, fmodel, restraints_manager, params, verbose=False, debug=None, log=None): if (log is None): log = null_out() t1 = time.time() from scitbx.array_family import flex selection = flex.size_t() window = building.get_window_around_residue(residue, window_size=params.window_size) for pdb_object in window: selection.extend(pdb_object.atoms().extract_i_seq()) assert (len(selection) > 0) and (not selection.all_eq(0)) occupancies = [] if (params.expected_occupancy is not None): assert (0.0 <= params.expected_occupancy <= 1.0) occupancies = [params.expected_occupancy] else: occupancies = [0.2, 0.3, 0.4, 0.5] trials = [] sites_start_1d = pdb_hierarchy.atoms().extract_xyz().as_double() from mmtbx.rotamer import rotamer_eval rotamer_manager = rotamer_eval.RotamerEval(data_version="8000") id_str = residue.id_str() delete_selection = None if (params.omit_waters): delete_selection = building.get_nearby_water_selection( pdb_hierarchy=pdb_hierarchy, xray_structure=fmodel.xray_structure, selection=selection) for occupancy in occupancies: prefix = "%s_%.2f" % (id_str.replace(" ", "_"), occupancy) map_file_name = None if (debug > 1): map_file_name = prefix + ".mtz" two_fofc_map, fofc_map = alt_confs.get_partial_omit_map( fmodel=fmodel.deep_copy(), selection=selection, selection_delete=delete_selection, negate_surrounding=True, map_file_name=map_file_name, partial_occupancy=1.0 - occupancy) rebuild = rebuild_residue( target_map=fofc_map, pdb_hierarchy=pdb_hierarchy, xray_structure=fmodel.xray_structure, geometry_restraints_manager=restraints_manager, rotamer_eval=rotamer_manager, d_min=fmodel.f_obs().d_min()) new_hierarchy = rebuild( atom_group=residue, window_size=params.window_size, backbone_sample_angle=params.backbone_sample_angle, anneal=params.anneal, annealing_temperature=params.annealing_temperature, use_chi1_sampling=params.simple_chi1_sampling, log=log) trial = residue_trial(residue=residue, new_hierarchy=new_hierarchy, occupancy=occupancy, rotamer_eval=rotamer_manager, fmodel=fmodel, two_fofc_map=two_fofc_map, fofc_map=fofc_map) trials.append(trial) if (debug > 1): open("%s.pdb" % prefix, "w").write(trial.new_hierarchy.as_pdb_string()) sites_end_1d = pdb_hierarchy.atoms().extract_xyz().as_double() assert sites_start_1d.all_eq(sites_end_1d) t2 = time.time() if (debug > 1): print(" %d build trials (%s): %.3fs" % (len(occupancies), residue.id_str(), t2 - t1), file=log) return trials
def __init__(self, fmodel, params, mp_params, pdb_hierarchy, processed_pdb_file, selection=None, cif_objects=(), verbose=True, debug=False, out=None): if (out is None) : out = sys.stdout adopt_init_args(self, locals()) self.asynchronous_output = False from mmtbx.rotamer import rotamer_eval from scitbx.array_family import flex assert (processed_pdb_file is not None) or (len(pdb_file_names) > 0) assert (0 < self.params.window_radius <= 4) self.pdb_hierarchy = pdb_hierarchy self.processed_pdb_file = processed_pdb_file self.get_processed_pdb_file(log=out) self.sites_cart = self.pdb_hierarchy.atoms().extract_xyz().deep_copy() if (self.selection is None): self.selection = flex.bool(self.sites_cart.size(), True) self.min_required_deviation = self.params.min_deviation if (self.min_required_deviation is Auto): self.min_required_deviation = fmodel.f_obs().d_min() / 2 self._ensembles = [] self.nproc_1 = self.nproc_2 = 1 two_fofc_map, fofc_map = mmtbx.building.get_difference_maps(fmodel=fmodel) windows = [] r = rotamer_eval.RotamerEval(data_version="8000") exclude_resnames = [] if (params.exclude_resnames is not None): exclude_resnames = [ n.upper() for n in params.exclude_resnames ] for chain in self.pdb_hierarchy.only_model().chains(): if not chain.is_protein(): continue residues = chain.residue_groups() fragments = alt_confs.fragment_single_conformer_chain(residues) for fragment_residues in fragments : start = params.window_radius end = - params.window_radius for i_res, residue in enumerate(fragment_residues[start:end]): j_res = i_res + start atom_groups = residue.atom_groups() main_conf = atom_groups[0] if (main_conf.resname.upper() in exclude_resnames): continue residue_id = main_conf.id_str() ag_i_seqs = main_conf.atoms().extract_i_seq() if (not self.selection.select(ag_i_seqs).all_eq(True)): continue if (len(atom_groups) != 1): if (self.verbose): print(" residue %s already has multiple conformations"%\ residue_id, file=out) continue ag_i_seqs_no_hd = flex.size_t() for atom in main_conf.atoms(): if (atom.element.strip() not in ["H","D"]): ag_i_seqs_no_hd.append(atom.i_seq) # XXX this is probably not optimal; what should I do about the # adjacent residues? it would be good to check Ramachandran plot too if (self.params.prefilter.rotameric_only): n_outliers = alt_confs.score_rotamers(hierarchy=hierarchy, selection=ag_i_seqs) if (n_outliers > 0): if (self.verbose): print(" residue %s is a rotamer outlier" % residue_id, file=out) continue if (self.params.prefilter.use_difference_map): map_stats = building.local_density_quality( fofc_map=fofc_map, two_fofc_map=two_fofc_map, atom_selection=ag_i_seqs_no_hd, xray_structure=fmodel.xray_structure, radius=self.params.prefilter.sampling_radius) if ((map_stats.number_of_atoms_in_difference_holes() == 0) and (map_stats.fraction_of_nearby_grid_points_above_cutoff()==0)): if (self.verbose): print(" no difference density for %s" % residue_id, file=out) continue window_selection = flex.size_t() offset = - self.params.window_radius while (offset <= self.params.window_radius): adjacent_group = fragment_residues[j_res+offset].atom_groups()[0] window_selection.extend(adjacent_group.atoms().extract_i_seq()) offset += 1 windows.append(residue_window( residue_id_str=residue_id, selection=window_selection, residue_selection=ag_i_seqs_no_hd, sites_reference=self.sites_cart.select(selection), window_radius=self.params.window_radius)) if (len(windows) == 0): raise Sorry("No peptide segments meeting the filtering criteria could "+ "be extracted from the selected atoms.") else : print("%d fragments will be refined." % len(windows), file=out) if (self.mp_params.nproc == 1): pass elif (self.mp_params.technology == "multiprocessing"): if (self.params.n_trials == 1) and (len(self.params.partial_occupancy) == 1): # only one refinement per window, so parallelize residue iteration self.nproc_1 = self.mp_params.nproc else : # multiple refinements per window, so parallelize at that level # FIXME actually, this needs to be smarter - if the number of # available processors is greater than the number of refinements per # window, it will be more efficient to parallelize the window loop self.nproc_2 = self.mp_params.nproc else : # queuing system, so we can only parallelize residue iteration self.nproc_1 = self.mp_params.nproc self.out = null_out() self.processed_pdb_file = None print("", file=out) alt_confs.print_trial_header(out) ensembles = [] if (self.nproc_1 == 1): self.asynchronous_output = True for window in windows : ens = self.refine_window(window) ensembles.append(ens) else : ensembles = easy_mp.parallel_map( func=self.refine_window, iterable=windows, processes=self.nproc_1, qsub_command=mp_params.qsub_command, method=mp_params.technology) self._ensembles = [ e for e in ensembles if (e is not None) ] # XXX reassert order print("", file=out) if (len(self._ensembles) == 0): print("WARNING: no ensembles passed filtering step", file=out) print("", file=out) self._ensembles.sort(lambda a,b: a.selection[0] < b.selection[0]) self.processed_pdb_file = processed_pdb_file if (debug): for k, ens in enumerate(filtered): pdb_out = ens.dump_pdb_file( pdb_hierarchy=pdb_hierarchy, crystal_symmetry=fmodel.f_obs()) print("wrote %s" % pdb_out, file=out)
't60': None, }, 'PHE': { 'm-10': { lookup['PHE'][2]: [-14.7, 19.8, 2] }, }, 'TYR': { 'm-10': { lookup['PHE'][2]: [-14.7, 20.2, 2] }, }, 'VAL': 1, } rotamer_evaluator = rotamer_eval.RotamerEval(data_version='8000') rotamer_id = rotamer_eval.RotamerID() # loads in the rotamer names def generate_chis( chis=None, n=2, step=10, depth=0, starting_chis=None, ): if chis is None: if starting_chis: chis = [] for c in starting_chis: chis.append(c)