def find_map_peaks(params, strip_xrs, log): #Adapted from mmtbx fine_peaks.py and #phenix find_peaks_holes.py, simplified to just give #coords and map levels for clustered peaks in difference map print >> log, "_" * 79 print >> log, "Finding Difference Map Peaks:" map_coeff = reflection_file_utils.extract_miller_array_from_file( file_name=params.input.input_map.map_coeff_file, label=params.input.input_map.map_diff_label, type="complex", log=null_log) peaks_result = find_peaks.manager( fmodel=fake_fmodel(strip_xrs), map_type=None, map_cutoff=params.input.parameters.peak_pick_cutoff, params=params.peak_search, use_all_data=True, map_coeffs=map_coeff, log=log) peaks_result.peaks_mapped() #cluter/arrange found peaks? peaks = peaks_result.peaks() #returns heights,coords(frac) unit_cell = strip_xrs.unit_cell() #need cell for cartesian peaks.sites = unit_cell.orthogonalize(peaks.sites) return peaks
def correct_drifted_waters(self, map_cutoff): self.fmodel.update_xray_structure( xray_structure=self.model.get_xray_structure(), update_f_calc=True) find_peaks_params_drifted = find_peaks.master_params.extract() find_peaks_params_drifted.map_next_to_model.min_model_peak_dist = 0.01 find_peaks_params_drifted.map_next_to_model.min_peak_peak_dist = 0.01 find_peaks_params_drifted.map_next_to_model.max_model_peak_dist = 0.5 find_peaks_params_drifted.peak_search.min_cross_distance = 0.5 find_peaks_params_drifted.resolution_factor = \ self.find_peaks_params.resolution_factor peaks = find_peaks.manager(fmodel=self.fmodel, map_type="2mFobs-DFmodel", map_cutoff=map_cutoff, params=find_peaks_params_drifted, log=self.log).peaks_mapped() if (peaks is not None and self.fmodel.r_work() > 0.01): sites_frac, heights = peaks.sites, peaks.heights model_sites_frac = self.model.get_xray_structure().sites_frac() solvent_selection = self.model.solvent_selection() mmtbx.utils.correct_drifted_waters( sites_frac_all=model_sites_frac, sites_frac_peaks=sites_frac, water_selection=solvent_selection, unit_cell=self.model.get_xray_structure().unit_cell()) self.model.get_xray_structure().set_sites_frac( sites_frac=model_sites_frac) self.fmodel.update_xray_structure( xray_structure=self.model.get_xray_structure(), update_f_calc=True)
def correct_drifted_waters(self, map_cutoff): self.fmodel.update_xray_structure( xray_structure = self.model.xray_structure, update_f_calc = True) find_peaks_params_drifted = find_peaks.master_params.extract() find_peaks_params_drifted.map_next_to_model.min_model_peak_dist=0.01 find_peaks_params_drifted.map_next_to_model.min_peak_peak_dist=0.01 find_peaks_params_drifted.map_next_to_model.max_model_peak_dist=0.5 find_peaks_params_drifted.peak_search.min_cross_distance=0.5 find_peaks_params_drifted.resolution_factor = \ self.find_peaks_params.resolution_factor peaks = find_peaks.manager( fmodel = self.fmodel, map_type = "2mFobs-DFmodel", map_cutoff = map_cutoff, params = find_peaks_params_drifted, log = self.log).peaks_mapped() if(peaks is not None and self.fmodel.r_work() > 0.01): sites_frac, heights = peaks.sites, peaks.heights model_sites_frac = self.model.xray_structure.sites_frac() solvent_selection = self.model.solvent_selection() mmtbx.utils.correct_drifted_waters( sites_frac_all = model_sites_frac, sites_frac_peaks = sites_frac, water_selection = solvent_selection, unit_cell = self.model.xray_structure.unit_cell()) self.model.xray_structure.set_sites_frac(sites_frac = model_sites_frac) self.fmodel.update_xray_structure( xray_structure = self.model.xray_structure, update_f_calc = True)
def find_peaks(self, map_type, map_cutoff): self.fmodel.update_xray_structure( xray_structure=self.model.get_xray_structure(), update_f_calc=True) return find_peaks.manager(fmodel=self.fmodel, map_type=map_type, map_cutoff=map_cutoff, params=self.find_peaks_params, log=self.log)
def find_hydrogen_peaks(fmodel, pdb_atoms, params, log): fp_manager = find_peaks.manager(fmodel=fmodel, map_type=params.map_type, map_cutoff=params.map_cutoff, params=params, log=log) result = fp_manager.peaks_mapped() fp_manager.show_mapped(pdb_atoms=pdb_atoms) return result
def find_peaks(self, map_type, map_cutoff): self.fmodel.update_xray_structure( xray_structure = self.model.xray_structure, update_f_calc = True) return find_peaks.manager( fmodel = self.fmodel, map_type = map_type, map_cutoff = map_cutoff, params = self.find_peaks_params, log = self.log)
def find_hydrogen_peaks(fmodel, pdb_atoms, params, log): fp_manager = find_peaks.manager(fmodel = fmodel, map_type = params.map_type, map_cutoff = params.map_cutoff, params = params, log = log) result = fp_manager.peaks_mapped() fp_manager.show_mapped(pdb_atoms = pdb_atoms) return result
def find_peaks(self, map_type, map_cutoff): self.fmodel.update_xray_structure( xray_structure=self.model.xray_structure, update_f_calc=False) #N.B. essential that 'use_all_data = False' so only working reflections are used if self.verbose > 0: silent = False else: silent = True return find_peaks.manager(fmodel=self.fmodel, map_type=map_type, map_cutoff=map_cutoff, params=self.fpp, use_all_data=False, silent=silent, log=self.log)
def find_peaks(self, map_type, map_cutoff): self.fmodel.update_xray_structure( xray_structure = self.model.xray_structure, update_f_calc = False) #N.B. essential that 'use_all_data = False' so only working reflections are used if self.verbose > 0: silent = False else: silent = True return find_peaks.manager(fmodel = self.fmodel, map_type = map_type, map_cutoff = map_cutoff, params = self.fpp, use_all_data = False, silent = silent, log = self.log)
def find_peaks_holes(fmodel, pdb_hierarchy, params=None, map_cutoff=3.0, anom_map_cutoff=3.0, filter_peaks_by_2fofc=None, use_phaser_if_available=True, return_llg_map=False, include_peaks_near_model=False, out=None): """ Find peaks and holes in mFo-DFc map, plus flag solvent atoms with suspiciously high mFo-DFc values, plus anomalous peaks if anomalous data are present. Returns a pickle-able object storing all this information (with the ability to write out a PDB file with the sites of interest). """ if (out is None): out = sys.stdout if (params is None): params = master_phil.fetch().extract().find_peaks if (include_peaks_near_model): params.map_next_to_model.min_model_peak_dist = 0 pdb_atoms = pdb_hierarchy.atoms() unit_cell = fmodel.xray_structure.unit_cell() from mmtbx import find_peaks from cctbx import maptbx f_map = None if (filter_peaks_by_2fofc is not None): f_map_ = fmodel.electron_density_map().fft_map( resolution_factor=params.resolution_factor, symmetry_flags=maptbx.use_space_group_symmetry, map_type="2mFo-DFc", use_all_data=True) f_map_.apply_sigma_scaling() f_map = f_map_.real_map() make_header("Positive difference map peaks", out=out) peaks_result = find_peaks.manager(fmodel=fmodel, map_type="mFo-DFc", map_cutoff=map_cutoff, params=params, log=out) peaks_result.peaks_mapped() peaks_result.show_mapped(pdb_atoms) peaks = peaks_result.peaks() if (filter_peaks_by_2fofc is not None): n_removed = peaks.filter_by_secondary_map( map=f_map, min_value=filter_peaks_by_2fofc) print >> out, "" print >> out, "%d peaks remaining after 2mFo-DFc filtering" % \ len(peaks.sites) # very important - sites are initially fractional coordinates! peaks.sites = unit_cell.orthogonalize(peaks.sites) print >> out, "" out.flush() make_header("Negative difference map holes", out=out) holes_result = find_peaks.manager(fmodel=fmodel, map_type="mFo-DFc", map_cutoff=-map_cutoff, params=params, log=out) holes_result.peaks_mapped() holes_result.show_mapped(pdb_atoms) holes = holes_result.peaks() # XXX is this useful? #if (filter_peaks_by_2fofc is not None) : # holes.filter_by_secondary_map( # map=f_map, # min_value=filter_peaks_by_2fofc) holes.sites = unit_cell.orthogonalize(holes.sites) print >> out, "" out.flush() anom = None anom_map_coeffs = None if (fmodel.f_obs().anomalous_flag()): make_header("Anomalous difference map peaks", out=out) anom_map_type = "anom_residual" if ((use_phaser_if_available) and (libtbx.env.has_module("phaser")) and (not fmodel.twin)): import mmtbx.map_tools print >> out, "Will use Phaser LLG map" anom_map_type = None anom_map_coeffs = mmtbx.map_tools.get_phaser_sad_llg_map_coefficients( fmodel=fmodel, pdb_hierarchy=pdb_hierarchy, log=out) anom_result = find_peaks.manager(fmodel=fmodel, map_type=anom_map_type, map_coeffs=anom_map_coeffs, map_cutoff=anom_map_cutoff, params=params, log=out) anom_result.peaks_mapped() anom_result.show_mapped(pdb_atoms) anom = anom_result.peaks() if (filter_peaks_by_2fofc is not None): anom.filter_by_secondary_map(map=f_map, min_value=filter_peaks_by_2fofc) print >> out, "" print >> out, "%d peaks remaining after 2mFo-DFc filtering" % \ len(anom.sites) anom.sites = unit_cell.orthogonalize(anom.sites) print >> out, "" out.flush() anom_map = None cache = pdb_hierarchy.atom_selection_cache() sites_frac = fmodel.xray_structure.sites_frac() water_isel = cache.selection( "resname HOH and not (element H or element D)").iselection() waters_out = [None, None] if (len(water_isel) > 0): map_types = ["mFo-DFc"] map_cutoffs = [map_cutoff] if (fmodel.f_obs().anomalous_flag()): map_types.append("anomalous") map_cutoffs.append(anom_map_cutoff) for k, map_type in enumerate(map_types): fft_map = None # re-use Phaser LLG map if it was previously calculated if (map_type == "anomalous") and (anom_map_coeffs is not None): fft_map = anom_map_coeffs.fft_map( resolution_factor=params.resolution_factor, symmetry_flags=maptbx.use_space_group_symmetry) else: fft_map = fmodel.electron_density_map().fft_map( resolution_factor=params.resolution_factor, symmetry_flags=maptbx.use_space_group_symmetry, map_type=map_type, use_all_data=True) real_map = fft_map.apply_sigma_scaling().real_map_unpadded() if (map_type == "anomalous"): anom_map = real_map suspicious_waters = [] for i_seq in water_isel: atom = pdb_atoms[i_seq] rho = real_map.tricubic_interpolation(sites_frac[i_seq]) if (rho >= map_cutoffs[k]): peak = water_peak(id_str=atom.id_str(), xyz=atom.xyz, peak_height=rho, map_type=map_type) suspicious_waters.append(peak) if (len(suspicious_waters) > 0): make_header("Water molecules with %s peaks" % map_type, out=out) for peak in suspicious_waters: peak.show(out=out) print >> out, "" waters_out[k] = suspicious_waters non_water_anom_peaks = None if (fmodel.f_obs().anomalous_flag()): non_water_anom_peaks = [] if (anom_map is None): fft_map = fmodel.electron_density_map().fft_map( resolution_factor=params.resolution_factor, symmetry_flags=maptbx.use_space_group_symmetry, map_type="anom", use_all_data=True) anom_map = fft_map.apply_sigma_scaling().real_map_unpadded() non_water_non_H_i_sel = cache.selection( "not (resname HOH or element H or element D)").iselection() for i_seq in non_water_non_H_i_sel: rho = anom_map.tricubic_interpolation(sites_frac[i_seq]) if (rho >= anom_map_cutoff): atom = pdb_atoms[i_seq] peak = water_peak(id_str=atom.id_str(), xyz=atom.xyz, peak_height=rho, map_type="anomalous") non_water_anom_peaks.append(peak) all_results = peaks_holes_container( peaks=peaks, holes=holes, anom_peaks=anom, map_cutoff=map_cutoff, anom_map_cutoff=anom_map_cutoff, water_peaks=waters_out[0], water_anom_peaks=waters_out[1], non_water_anom_peaks=non_water_anom_peaks) all_results.show_summary(out=out) if (return_llg_map): return all_results, anom_map_coeffs return all_results
def write_map_file(self): # output MTZ file with map coefficients class map_coeffs_mtz_label_manager: def __init__(self, amplitudes, phases): self._amplitudes = amplitudes self._phases = phases def amplitudes(self): return self._amplitudes def phases(self, root_label, anomalous_sign=None): assert anomalous_sign is None or not anomalous_sign return self._phases mtz_history_buffer = flex.std_string() lbl_mgr = map_coeffs_mtz_label_manager(amplitudes="FoFo", phases="PHFc") mtz_dataset = self.map_coeff.as_mtz_dataset( column_root_label=lbl_mgr.amplitudes(), label_decorator=lbl_mgr) mtz_history_buffer.append("> column label %s = phenix %s" % (lbl_mgr.amplitudes(), "FoFoPHFc")) if self.output_file is not None: file_name = self.output_file else: file_name = "FoFoPHFc.mtz" mtz_history_buffer.append("file name %s" % file_name) mtz_object = mtz_dataset.mtz_object() mtz_object.add_history(mtz_history_buffer) mtz_object.write(file_name=file_name) self.file_names = [file_name] if (self.peak_search): from mmtbx.command_line import find_peaks_holes from mmtbx import find_peaks peak_search_log = self.log if (self.silent): peak_search_log = null_out() fmodel = self.fmodel peaks = find_peaks.manager(fmodel=fmodel, map_type=None, map_coeffs=self.map_coeff, map_cutoff=self.map_cutoff, params=self.peak_search_params, log=peak_search_log).peaks_mapped() peaks.sites = fmodel.xray_structure.unit_cell().orthogonalize( peaks.sites) holes = find_peaks.manager(fmodel=fmodel, map_type=None, map_coeffs=self.map_coeff, map_cutoff=-self.map_cutoff, params=self.peak_search_params, log=peak_search_log).peaks_mapped() holes.sites = fmodel.xray_structure.unit_cell().orthogonalize( holes.sites) result = find_peaks_holes.peaks_holes_container( peaks=peaks, holes=holes, map_cutoff=self.map_cutoff) pdb_out = os.path.splitext(file_name)[0] + "_peaks.pdb" result.save_pdb_file(file_name=pdb_out, include_anom=False, include_water=False, log=peak_search_log) self.file_names.append(pdb_out) return self.file_names
def write_map_file (self) : # output MTZ file with map coefficients class map_coeffs_mtz_label_manager: def __init__(self, amplitudes, phases): self._amplitudes = amplitudes self._phases = phases def amplitudes(self): return self._amplitudes def phases(self, root_label, anomalous_sign=None): assert anomalous_sign is None or not anomalous_sign return self._phases mtz_history_buffer = flex.std_string() lbl_mgr = map_coeffs_mtz_label_manager(amplitudes = "FoFo", phases = "PHFc") mtz_dataset = self.map_coeff.as_mtz_dataset( column_root_label=lbl_mgr.amplitudes(), label_decorator=lbl_mgr) mtz_history_buffer.append("> column label %s = phenix %s" % ( lbl_mgr.amplitudes(), "FoFoPHFc")) if self.output_file is not None : file_name = self.output_file else : file_name = "FoFoPHFc.mtz" mtz_history_buffer.append("file name %s"%file_name) mtz_object = mtz_dataset.mtz_object() mtz_object.add_history(mtz_history_buffer) mtz_object.write(file_name=file_name) self.file_names = [ file_name ] if (self.peak_search) : from mmtbx.command_line import find_peaks_holes from mmtbx import find_peaks peak_search_log = self.log if (self.silent) : peak_search_log = null_out() fmodel = self.fmodel peaks = find_peaks.manager( fmodel=fmodel, map_type=None, map_coeffs=self.map_coeff, map_cutoff=self.map_cutoff, params=self.peak_search_params, log=peak_search_log).peaks_mapped() peaks.sites = fmodel.xray_structure.unit_cell().orthogonalize(peaks.sites) holes = find_peaks.manager( fmodel=fmodel, map_type=None, map_coeffs=self.map_coeff, map_cutoff=-self.map_cutoff, params=self.peak_search_params, log=peak_search_log).peaks_mapped() holes.sites = fmodel.xray_structure.unit_cell().orthogonalize(holes.sites) result = find_peaks_holes.peaks_holes_container( peaks=peaks, holes=holes, map_cutoff=self.map_cutoff) pdb_out = os.path.splitext(file_name)[0] + "_peaks.pdb" result.save_pdb_file( file_name=pdb_out, include_anom=False, include_water=False, log=peak_search_log) self.file_names.append(pdb_out) return self.file_names
def find_peaks_holes ( fmodel, pdb_hierarchy, params=None, map_cutoff=3.0, anom_map_cutoff=3.0, filter_peaks_by_2fofc=None, use_phaser_if_available=True, return_llg_map=False, include_peaks_near_model=False, out=None) : """ Find peaks and holes in mFo-DFc map, plus flag solvent atoms with suspiciously high mFo-DFc values, plus anomalous peaks if anomalous data are present. Returns a pickle-able object storing all this information (with the ability to write out a PDB file with the sites of interest). """ if (out is None) : out = sys.stdout if (params is None) : params = master_phil.fetch().extract().find_peaks if (include_peaks_near_model) : params.map_next_to_model.min_model_peak_dist = 0 pdb_atoms = pdb_hierarchy.atoms() unit_cell = fmodel.xray_structure.unit_cell() from mmtbx import find_peaks from cctbx import maptbx f_map = None if (filter_peaks_by_2fofc is not None) : f_map_ = fmodel.electron_density_map().fft_map( resolution_factor=params.resolution_factor, symmetry_flags=maptbx.use_space_group_symmetry, map_type="2mFo-DFc", use_all_data=True) f_map_.apply_sigma_scaling() f_map = f_map_.real_map() make_header("Positive difference map peaks", out=out) peaks_result = find_peaks.manager( fmodel=fmodel, map_type="mFo-DFc", map_cutoff=map_cutoff, params=params, log=out) peaks_result.peaks_mapped() peaks_result.show_mapped(pdb_atoms) peaks = peaks_result.peaks() if (filter_peaks_by_2fofc is not None) : n_removed = peaks.filter_by_secondary_map( map=f_map, min_value=filter_peaks_by_2fofc) print >> out, "" print >> out, "%d peaks remaining after 2mFo-DFc filtering" % \ len(peaks.sites) # very important - sites are initially fractional coordinates! peaks.sites = unit_cell.orthogonalize(peaks.sites) print >> out, "" out.flush() make_header("Negative difference map holes", out=out) holes_result = find_peaks.manager( fmodel=fmodel, map_type="mFo-DFc", map_cutoff=-map_cutoff, params=params, log=out) holes_result.peaks_mapped() holes_result.show_mapped(pdb_atoms) holes = holes_result.peaks() # XXX is this useful? #if (filter_peaks_by_2fofc is not None) : # holes.filter_by_secondary_map( # map=f_map, # min_value=filter_peaks_by_2fofc) holes.sites = unit_cell.orthogonalize(holes.sites) print >> out, "" out.flush() anom = None anom_map_coeffs = None if (fmodel.f_obs().anomalous_flag()) : make_header("Anomalous difference map peaks", out=out) anom_map_type = "anom_residual" if ((use_phaser_if_available) and (libtbx.env.has_module("phaser")) and (not fmodel.twin)) : import mmtbx.map_tools print >> out, "Will use Phaser LLG map" anom_map_type = None anom_map_coeffs = mmtbx.map_tools.get_phaser_sad_llg_map_coefficients( fmodel=fmodel, pdb_hierarchy=pdb_hierarchy, log=out) anom_result = find_peaks.manager( fmodel=fmodel, map_type=anom_map_type, map_coeffs=anom_map_coeffs, map_cutoff=anom_map_cutoff, params=params, log=out) anom_result.peaks_mapped() anom_result.show_mapped(pdb_atoms) anom = anom_result.peaks() if (filter_peaks_by_2fofc is not None) : anom.filter_by_secondary_map( map=f_map, min_value=filter_peaks_by_2fofc) print >> out, "" print >> out, "%d peaks remaining after 2mFo-DFc filtering" % \ len(anom.sites) anom.sites = unit_cell.orthogonalize(anom.sites) print >> out, "" out.flush() anom_map = None cache = pdb_hierarchy.atom_selection_cache() sites_frac = fmodel.xray_structure.sites_frac() water_isel = cache.selection( "resname HOH and not (element H or element D)").iselection() waters_out = [None, None] if (len(water_isel) > 0) : map_types = ["mFo-DFc"] map_cutoffs = [ map_cutoff ] if (fmodel.f_obs().anomalous_flag()) : map_types.append("anomalous") map_cutoffs.append(anom_map_cutoff) for k, map_type in enumerate(map_types) : fft_map = None # re-use Phaser LLG map if it was previously calculated if (map_type == "anomalous") and (anom_map_coeffs is not None) : fft_map = anom_map_coeffs.fft_map( resolution_factor=params.resolution_factor, symmetry_flags=maptbx.use_space_group_symmetry) else : fft_map = fmodel.electron_density_map().fft_map( resolution_factor=params.resolution_factor, symmetry_flags=maptbx.use_space_group_symmetry, map_type=map_type, use_all_data=True) real_map = fft_map.apply_sigma_scaling().real_map_unpadded() if (map_type == "anomalous") : anom_map = real_map suspicious_waters = [] for i_seq in water_isel : atom = pdb_atoms[i_seq] rho = real_map.tricubic_interpolation(sites_frac[i_seq]) if (rho >= map_cutoffs[k]) : peak = water_peak( id_str=atom.id_str(), xyz=atom.xyz, peak_height=rho, map_type=map_type) suspicious_waters.append(peak) if (len(suspicious_waters) > 0) : make_header("Water molecules with %s peaks" % map_type, out=out) for peak in suspicious_waters : peak.show(out=out) print >> out, "" waters_out[k] = suspicious_waters non_water_anom_peaks = None if (fmodel.f_obs().anomalous_flag()) : non_water_anom_peaks = [] if (anom_map is None) : fft_map = fmodel.electron_density_map().fft_map( resolution_factor=params.resolution_factor, symmetry_flags=maptbx.use_space_group_symmetry, map_type="anom", use_all_data=True) anom_map = fft_map.apply_sigma_scaling().real_map_unpadded() non_water_non_H_i_sel = cache.selection( "not (resname HOH or element H or element D)").iselection() for i_seq in non_water_non_H_i_sel : rho = anom_map.tricubic_interpolation(sites_frac[i_seq]) if (rho >= anom_map_cutoff) : atom = pdb_atoms[i_seq] peak = water_peak( id_str=atom.id_str(), xyz=atom.xyz, peak_height=rho, map_type="anomalous") non_water_anom_peaks.append(peak) all_results = peaks_holes_container( peaks=peaks, holes=holes, anom_peaks=anom, map_cutoff=map_cutoff, anom_map_cutoff=anom_map_cutoff, water_peaks=waters_out[0], water_anom_peaks=waters_out[1], non_water_anom_peaks=non_water_anom_peaks) all_results.show_summary(out=out) if (return_llg_map) : return all_results, anom_map_coeffs return all_results