def ion_centroids(self, sf, adduct): """ Args ---- sf : str adduct: str Returns ------- : list of tuples """ try: pyisocalc.parseSumFormula( sf + adduct) # tests is the sf and adduct compatible iso_pattern = isotopePattern(str(sf + adduct)) iso_pattern.addCharge(int(self.charge)) fwhm = self.sigma * SIGMA_TO_FWHM resolving_power = iso_pattern.masses[0] / fwhm instrument_model = InstrumentModel('tof', resolving_power) centr = iso_pattern.centroids(instrument_model) mzs = np.array(centr.masses) ints = 100. * np.array(centr.intensities) mzs, ints = self._trim(mzs, ints, ISOTOPIC_PEAK_N) return mzs, ints except Exception as e: logger.warning('%s %s - %s', sf, adduct, e) return None, None
def get_isotope_pattern(sf, resolving_power=100000, instrument_type='tof', at_mz=None, cutoff_perc=0.1, charge=None, pts_per_mz=10000, **kwargs): # layer of compatibility with the original pyMSpec.pyisocalc module if charge is None: charge = 1 cutoff = cutoff_perc / 100.0 abs_charge = max(1, abs(charge)) p = isotopePattern(str(sf), cutoff / 10.0) p.addCharge(charge) mzs = np.arange( min(p.masses) / abs_charge - 1, max(p.masses) / abs_charge + 1, 1.0 / pts_per_mz) instr = InstrumentModel(instrument_type, resolving_power) intensities = np.asarray(p.envelope(instr)(mzs * abs_charge)) intensities *= 100.0 / intensities.max() ms = MassSpectrum() ms.add_spectrum(mzs, intensities) p = ProfileSpectrum(mzs, intensities).centroids(5) p.removeIntensitiesBelow(cutoff) p.sortByMass() ms.add_centroids(p.masses, np.array(p.intensities)) return ms
def __init__(self, sm_instance, ds_name=None, ds_id=None, tmp_dir=gettempdir(), max_size=100e9): ds_id = ds_id or sm_instance.dataset(name=ds_name).id ds_name = ds_name or sm_instance.dataset(id=ds_id).name self.imzml_fn, self.imzb_fn = _download(sm_instance, ds_id, tmp_dir, max_size) self.imzb = ImzbReader(self.imzb_fn) img = self.imzb.get_mz_image(0, 0) # strip empty rows/columns from the image self._first_col = min(np.where(img.sum(axis=0) > -img.shape[0])[0]) self._first_row = min(np.where(img.sum(axis=1) > -img.shape[1])[0]) self._bins = None self._meta = json.loads(sm_instance.dataset(id=ds_id).metadata.json) rp = self._meta['MS_Analysis']['Detector_Resolving_Power'] analyzer = self.analyzer.lower() if 'orbitrap' in analyzer: analyzer = 'orbitrap' self._instrument = InstrumentModel(analyzer, float(rp['Resolving_Power']), float(rp['mz'])) self._sm = sm_instance self._name = ds_name self._id = ds_id
def centroids(self, formula): """ Args ----- formula : str Returns ----- list[tuple] """ try: pyisocalc.parseSumFormula( formula) # tests that formula is parsable iso_pattern = isotopePattern(str(formula)) iso_pattern.addCharge(int(self.charge)) fwhm = self.sigma * SIGMA_TO_FWHM resolving_power = iso_pattern.masses[0] / fwhm instrument_model = InstrumentModel('tof', resolving_power) centr = iso_pattern.centroids(instrument_model) mzs_ = np.array(centr.masses) ints_ = 100. * np.array(centr.intensities) mzs_, ints_ = self._trim(mzs_, ints_, self.n_peaks) n = len(mzs_) mzs = np.zeros(self.n_peaks) mzs[:n] = np.array(mzs_) ints = np.zeros(self.n_peaks) ints[:n] = ints_ return mzs, ints except Exception as e: logger.warning('%s - %s', formula, e) return None, None
def on_get(self, req, res, ion, instr, res_power, at_mz, charge): try: isotopes = isotopePattern(ion) isotopes.addCharge(int(charge)) instrument = InstrumentModel(instr, float(res_power), float(at_mz)) centroids = Centroids(isotopes, instrument) self.on_success(res, centroids.spectrum_chart()) except Exception as e: LOG.warning('(%s, %s, %s, %s, %s) - %s', ion, instr, res_power, at_mz, charge, e)
def test_centroiding(f, resolution): p = isotopePattern(f, 0.9999999) instr = InstrumentModel('tof', resolution) p1 = p.centroids(instr, points_per_fwhm=500) min_mz = min(p1.masses) max_mz = max(p1.masses) step = 5e-5 n_pts = int((max_mz + 2 - min_mz) / step) mzs = [min_mz - 1 + step * i for i in range(n_pts)] p2 = ProfileSpectrum(mzs, p.envelope(instr)(mzs)).centroids(window_size=15) assert_patterns_almost_equal(p1, p2, instr)
def test_thresholding(f, threshold): instr = InstrumentModel('tof', 100000) p = isotopePattern(f).centroids(instr, min_abundance=threshold) for a in p.intensities: assert a >= threshold
def generate(ion, instr, res_power, at_mz, charge): isotopes = isotopePattern(ion) isotopes.addCharge(int(charge)) instrument = InstrumentModel(instr, float(res_power), float(at_mz)) centroids = Centroids(isotopes, instrument) return centroids.spectrum_chart()