Ejemplo n.º 1
0
    def _pick_precursor_scan_peaks(self, precursor_scan, chosen_envelopes=None):
        """Pick peaks from the given precursor scan

        Parameters
        ----------
        precursor_scan: Scan
            Scan to pick peaks from
        chosen_envelopes: list, optional
            list of m/z intervals to pick peaks for

        Returns
        -------
        PeakSet
        """
        if precursor_scan.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'
        prec_mz, prec_intensity = precursor_scan.arrays
        if not self.pick_only_tandem_envelopes and self.envelope_selector is None:
            prec_peaks = pick_peaks(prec_mz, prec_intensity, peak_mode=peak_mode, **self.ms1_peak_picking_args)
        else:
            if chosen_envelopes is None:
                chosen_envelopes = self._get_envelopes(precursor_scan)
            prec_peaks = pick_peaks(prec_mz, prec_intensity, peak_mode=peak_mode,
                                    target_envelopes=chosen_envelopes,
                                    **self.ms1_peak_picking_args)
        return prec_peaks
Ejemplo n.º 2
0
    def pick_precursor_scan_peaks(self, precursor_scan):
        logger.info("Picking Precursor Scan Peaks: %r", precursor_scan)
        if precursor_scan.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'
        prec_mz, prec_intensity = precursor_scan.arrays
        if not self.pick_only_tandem_envelopes and self.envelope_selector is None:
            prec_peaks = pick_peaks(prec_mz,
                                    prec_intensity,
                                    peak_mode=peak_mode,
                                    **self.ms1_peak_picking_args)
        else:
            if self.envelope_selector is None:
                chosen_envelopes = [
                    s.precursor_information
                    for s in precursor_scan.product_scans
                ]
                chosen_envelopes = sorted([(p.mz - 5, p.mz + 10)
                                           for p in chosen_envelopes])
            else:
                chosen_envelopes = self.envelope_selector(precursor_scan)
            prec_peaks = pick_peaks(prec_mz,
                                    prec_intensity,
                                    peak_mode=peak_mode,
                                    target_envelopes=chosen_envelopes,
                                    **self.ms1_peak_picking_args)

        precursor_scan.peak_set = prec_peaks
        return prec_peaks
Ejemplo n.º 3
0
    def _pick_precursor_scan_peaks(self,
                                   precursor_scan,
                                   chosen_envelopes=None):
        """Pick peaks from the given precursor scan

        Parameters
        ----------
        precursor_scan: Scan
            Scan to pick peaks from
        chosen_envelopes: list, optional
            list of m/z intervals to pick peaks for

        Returns
        -------
        PeakSet
        """
        if precursor_scan.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'
        prec_mz, prec_intensity = precursor_scan.arrays
        if not self.pick_only_tandem_envelopes and self.envelope_selector is None:
            prec_peaks = pick_peaks(prec_mz,
                                    prec_intensity,
                                    peak_mode=peak_mode,
                                    **self.ms1_peak_picking_args)
        else:
            if chosen_envelopes is None:
                chosen_envelopes = self._get_envelopes(precursor_scan)
            prec_peaks = pick_peaks(prec_mz,
                                    prec_intensity,
                                    peak_mode=peak_mode,
                                    target_envelopes=chosen_envelopes,
                                    **self.ms1_peak_picking_args)
        return prec_peaks
Ejemplo n.º 4
0
    def test_empty(self):
        peaks = []
        x, y = reprofile(peaks)
        repicked = pick_peaks(x, y)
        assert len(repicked) == 0

        peaks = self.make_data()
        peaks[0].full_width_at_half_max = 10
        x, y = reprofile(peaks, max_fwhm=1.0)
        repicked = pick_peaks(x, y)
        assert len(repicked) == 0
Ejemplo n.º 5
0
    def pick_peaks(self, *args, **kwargs):
        """A wrapper around :func:`ms_peak_picker.pick_peaks` which will populate the
        :attr:`peak_set` attribute of this scan.

        Parameters
        ----------
        *args :
            Passed along to :func:`ms_peak_picker.pick_peaks`
        **kwargs : TYPE
            Passed along to :func:`ms_peak_picker.pick_peaks`

        Returns
        -------
        self
        """
        mzs, intensities = self.arrays
        if self.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'

        kwargs['peak_mode'] = peak_mode

        self.peak_set = pick_peaks(mzs, intensities, *args, **kwargs)
        return self
Ejemplo n.º 6
0
    def pick_product_scan_peaks(self, product_scan):
        """Pick the peaks of product scan

        Parameter
        ---------
        product_scan: :class:`~.Scan`
            The scan to pick peaks from.

        Returns
        -------
        PeakSet
        """
        if product_scan.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'
        product_mz, product_intensity = product_scan.arrays
        peaks = pick_peaks(product_mz, product_intensity, peak_mode=peak_mode, **self.msn_peak_picking_args)

        if peaks is None:
            raise EmptyScanError(
                "Could not pick peaks for empty product scan", self)

        product_scan.peak_set = peaks
        return peaks
Ejemplo n.º 7
0
    def pick_product_scan_peaks(self, product_scan):
        """Pick the peaks of product scan

        Parameters
        ----------
        product_scan: :class:`~.Scan`
            The scan to pick peaks from.

        Returns
        -------
        PeakSet
        """
        if product_scan.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'
        product_mz, product_intensity = product_scan.arrays
        peaks = pick_peaks(product_mz,
                           product_intensity,
                           peak_mode=peak_mode,
                           **self.msn_peak_picking_args)

        if peaks is None:
            raise EmptyScanError("Could not pick peaks for empty product scan",
                                 self)

        product_scan.peak_set = peaks
        return peaks
Ejemplo n.º 8
0
    def pick_peaks(self, *args, **kwargs):
        """A wrapper around :func:`ms_peak_picker.pick_peaks` which will populate the
        :attr:`peak_set` attribute of this scan.

        Parameters
        ----------
        *args :
            Passed along to :func:`ms_peak_picker.pick_peaks`
        **kwargs : TYPE
            Passed along to :func:`ms_peak_picker.pick_peaks`

        Returns
        -------
        self
        """
        mzs, intensities = self.arrays
        if self.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'

        kwargs['peak_mode'] = peak_mode

        self.peak_set = pick_peaks(mzs, intensities, *args, **kwargs)
        return self
Ejemplo n.º 9
0
 def pick_product_scan_peaks(self, product_scan):
     if product_scan.is_profile:
         peak_mode = 'profile'
     else:
         peak_mode = 'centroid'
     product_mz, product_intensity = product_scan.arrays
     peaks = pick_peaks(product_mz, product_intensity, peak_mode=peak_mode, **self.msn_peak_picking_args)
     product_scan.peak_set = peaks
     return peaks
Ejemplo n.º 10
0
 def _get_centroids(self, scan):
     mzs, intensities = self.read_spectrum(scan.frame.id, scan.start_scan,
                                           scan.end_scan)
     sort_mask = np.argsort(mzs)
     mzs = mzs[sort_mask]
     intensities = intensities[sort_mask]
     centroids = pick_peaks(mzs, intensities, peak_mode="centroid")
     if centroids is None:
         centroids = PeakIndex(np.array([], float), np.array([], float), [])
     return centroids
Ejemplo n.º 11
0
 def pick_product_scan_peaks(self, product_scan):
     if product_scan.is_profile:
         peak_mode = 'profile'
     else:
         peak_mode = 'centroid'
     product_mz, product_intensity = product_scan.arrays
     peaks = pick_peaks(product_mz,
                        product_intensity,
                        peak_mode=peak_mode,
                        **self.msn_peak_picking_args)
     product_scan.peak_set = peaks
     return peaks
Ejemplo n.º 12
0
    def pick_precursor_scan_peaks(self, precursor_scan):
        logger.info("Picking Precursor Scan Peaks: %r", precursor_scan)
        if precursor_scan.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'
        prec_mz, prec_intensity = precursor_scan.arrays
        if not self.pick_only_tandem_envelopes and self.envelope_selector is None:
            prec_peaks = pick_peaks(prec_mz, prec_intensity, peak_mode=peak_mode, **self.ms1_peak_picking_args)
        else:
            if self.envelope_selector is None:
                chosen_envelopes = [s.precursor_information for s in precursor_scan.product_scans]
                chosen_envelopes = sorted([(p.mz - 5, p.mz + 10) for p in chosen_envelopes])
            else:
                chosen_envelopes = self.envelope_selector(precursor_scan)
            prec_peaks = pick_peaks(prec_mz, prec_intensity, peak_mode=peak_mode,
                                    target_envelopes=chosen_envelopes,
                                    **self.ms1_peak_picking_args)

        precursor_scan.peak_set = prec_peaks
        return prec_peaks
Ejemplo n.º 13
0
    def pick_peaks(self, *args, **kwargs):
        """A wrapper around :func:`ms_peak_picker.pick_peaks` which will populate the
        :attr:`peak_set` attribute of this scan.

        Parameters
        ----------
        fit_type : str, optional
            The name of the peak model to use. One of "quadratic", "gaussian", "lorentzian", or "apex"
        signal_to_noise_threshold : int, optional
            Minimum signal-to-noise measurement to accept a peak
        intensity_threshold : int, optional
            Minimum intensity measurement to accept a peak
        threshold_data : bool, optional
            Whether to apply thresholds to the data
        target_envelopes : list, optional
            A sequence of (start m/z, end m/z) pairs, limiting peak picking to only those intervals
        transforms : list, optional
            A list of :class:`scan_filter.FilterBase` instances or callable that
            accepts (mz_array, intensity_array) and returns (mz_array, intensity_array) or
            `str` matching one of the premade names in `scan_filter.filter_register`
        verbose : bool, optional
            Whether to log extra information while picking peaks
        start_mz : float, optional
            A minimum m/z value to start picking peaks from
        stop_mz : float, optional
            A maximum m/z value to stop picking peaks after
        *args :
            Passed along to :func:`ms_peak_picker.pick_peaks`
        **kwargs :
            Passed along to :func:`ms_peak_picker.pick_peaks`

        Returns
        -------
        Scan
            Returns self
        """
        mzs, intensities = self.arrays
        if len(mzs) == 0:
            self.peak_set = PeakIndex(mzs, intensities, PeakSet([]))
            return self
        if self.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'

        kwargs.setdefault('peak_mode', peak_mode)

        self.peak_set = pick_peaks(mzs, intensities, *args, **kwargs)
        return self
Ejemplo n.º 14
0
    def pick_product_scan_peaks(self, product_scan):
        if product_scan.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'
        product_mz, product_intensity = product_scan.arrays
        peaks = pick_peaks(product_mz,
                           product_intensity,
                           peak_mode=peak_mode,
                           **self.msn_peak_picking_args)

        if peaks is None:
            raise EmptyScanError("Could not pick peaks for empty product scan",
                                 self)

        product_scan.peak_set = peaks
        return peaks
Ejemplo n.º 15
0
 def _scan_arrays(self, scan):
     if scan.is_combined():
         mzs, intensities = self.read_spectrum(
             scan.frame.id, scan.start_scan, scan.end_scan)
         if len(mzs) == 0:
             return np.array([], dtype=float), np.array([], dtype=float)
         sort_mask = np.argsort(mzs)
         mzs = mzs[sort_mask]
         intensities = intensities[sort_mask]
         centroids = pick_peaks(mzs, intensities, peak_mode="centroid")
         if centroids is None:
             return np.array([], dtype=float), np.array([], dtype=float)
         mzs, intensities = reprofile(
             centroids, dx=self._scan_merging_parameters['dx'],
             override_fwhm=self._scan_merging_parameters['fwhm'])
         return mzs, intensities
     else:
         mzs, intensities = self.read_spectrum(scan.frame.id, scan.start_scan, scan.end_scan)
         return mzs, intensities
Ejemplo n.º 16
0
    def _average_ms1(self, precursor_scan):
        """Average signal from :attr:`self.ms1_averaging` scans from
        before and after ``precursor_scan`` and pick peaks from the
        averaged arrays.

        Parameters
        ----------
        precursor_scan: Scan
            The scan to use as a point of reference

        Returns
        -------
        PeakSet
        """
        new_scan = precursor_scan.average(self.ms1_averaging)
        prec_peaks = pick_peaks(*new_scan.arrays,
                                target_envelopes=self._get_envelopes(precursor_scan),
                                **self.ms1_peak_picking_args)
        return prec_peaks
Ejemplo n.º 17
0
    def _average_ms1(self, precursor_scan):
        """Average signal from :attr:`self.ms1_averaging` scans from
        before and after ``precursor_scan`` and pick peaks from the
        averaged arrays.

        Parameters
        ----------
        precursor_scan: Scan
            The scan to use as a point of reference

        Returns
        -------
        PeakSet
        """
        new_scan = precursor_scan.average(self.ms1_averaging)
        prec_peaks = pick_peaks(
            *new_scan.arrays,
            target_envelopes=self._get_envelopes(precursor_scan),
            **self.ms1_peak_picking_args)
        return prec_peaks
Ejemplo n.º 18
0
def pick_peaks_from_file(path):
    reader = PointListReader(path)
    return pick_peaks(reader.mzs, reader.intensities, "lorenztian")
Ejemplo n.º 19
0
def pick_peaks_from_file(path):
    reader = PointListReader(path)
    return pick_peaks(reader.mzs, reader.intensities, "lorenztian")
Ejemplo n.º 20
0
 def test_reprofile(self):
     peaks = self.make_data()
     x, y = reprofile(peaks)
     repicked = pick_peaks(x, y)
     diff = repicked[0].mz - peaks[0].mz
     assert abs(diff) < 1e-3
Ejemplo n.º 21
0
    def pick_peaks(self, *args, **kwargs):
        """A wrapper around :func:`ms_peak_picker.pick_peaks` which will populate the
        :attr:`peak_set` attribute of this scan.

        Parameters
        ----------
        fit_type : str, optional
            The name of the peak model to use. One of "quadratic", "gaussian", "lorentzian", or "apex"
        signal_to_noise_threshold : int, optional
            Minimum signal-to-noise measurement to accept a peak
        intensity_threshold : int, optional
            Minimum intensity measurement to accept a peak
        threshold_data : bool, optional
            Whether to apply thresholds to the data
        target_envelopes : list, optional
            A sequence of (start m/z, end m/z) pairs, limiting peak picking to only those intervals
        transforms : list, optional
            A list of :class:`scan_filter.FilterBase` instances or callable that
            accepts (mz_array, intensity_array) and returns (mz_array, intensity_array) or
            `str` matching one of the premade names in `scan_filter.filter_register`
        verbose : bool, optional
            Whether to log extra information while picking peaks
        start_mz : float, optional
            A minimum m/z value to start picking peaks from
        stop_mz : float, optional
            A maximum m/z value to stop picking peaks after
        *args :
            Passed along to :func:`ms_peak_picker.pick_peaks`
        **kwargs :
            Passed along to :func:`ms_peak_picker.pick_peaks`

        Returns
        -------
        Scan
            Returns self
        """
        # Check to see if the user requested one of the ms_peak_picker fits or wanted
        # to use the vendor peak picker if provided.
        fit_type_k = kwargs.get("fit_type")
        if len(args) > 0:
            fit_type_a = args[0]
        else:
            fit_type_a = None
        if fit_type_k == 'vendor' or fit_type_a == 'vendor':
            try:
                peaks = self.source._pick_peaks_vendor(self._data, *args,
                                                       **kwargs)
                self.peak_set = peaks
                return self
            except NotImplementedError:
                pass
        # Prepare the peak picking parameters
        mzs, intensities = self.arrays
        if len(mzs) == 0:
            self.peak_set = PeakIndex(mzs, intensities, PeakSet([]))
            return self
        if self.is_profile:
            peak_mode = 'profile'
        else:
            peak_mode = 'centroid'

        kwargs.setdefault('peak_mode', peak_mode)

        self.peak_set = pick_peaks(mzs, intensities, *args, **kwargs)
        return self
Ejemplo n.º 22
0
 def test_denoise(self):
     x, y = self.make_data()
     denoised_x, denoised_y = denoise(x, y, window_size=2.0)
     assert y.mean() >= 100.0
     assert denoised_y.mean() < 1.0
     assert len(pick_peaks(denoised_x, denoised_y)) == 1