Skip to contents

The filterSpectriPy() function allows to filter/process a Spectra object using the select_by_intensity, select_by_mz, remove_peaks_around_precursor_mz, and normalize_intensities of the python matchms.filtering module.

Selection and configuration of the algorithm can be performed with one of the parameter objects:

  • select_by_intensity: Keeps only the peaks within defined intensity range (keep if intensity_from >= intensity >= intensity_to).

  • select_by_mz: Keeps only the peaks between mz_from and mz_to (keep if mz_from >= m/z >= mz_to).

  • remove_peaks_around_precursor_mz: Removes the peaks that are within mz_tolerance (in Da) of the precursor mz, exlcuding the precursor peak.

  • normalize_intensities: Normalizes the intensities of peaks (and losses) to unit height.

Usage

select_by_intensity(intensity_from = 10, intensity_to = 200)

select_by_mz(mz_from = 0, mz_to = 1000)

remove_peaks_around_precursor_mz(mz_tolerance = 17)

normalize_intensities()

# S4 method for class 'Spectra,select_by_intensity'
filterSpectriPy(sps, param, ...)

# S4 method for class 'Spectra,select_by_mz'
filterSpectriPy(sps, param, ...)

# S4 method for class 'Spectra,remove_peaks_around_precursor_mz'
filterSpectriPy(sps, param, ...)

# S4 method for class 'Spectra,normalize_intensities'
filterSpectriPy(sps, param, ...)

Arguments

intensity_from

numeric(1): Set lower threshold for peak intensity. Default is 10.

intensity_to

numeric(1): Set upper threshold for peak intensity. Default is 200.

mz_from

numeric(1): Set lower threshold for m/z peak positions. Default is 0.

mz_to

numeric(1): Set upper threshold for m/z peak positions. Default is 1000.

mz_tolerance

numeric(1): Tolerance of m/z values that are not allowed to lie within the precursor mz. Default is 17 Da.

sps

A Spectra::Spectra() object.

param

one of parameter classes listed above (such as select_by_intensity) defining the filter/processing function in python and its parameters.

...

ignored.

Value

filterSpectriPy() returns a Spectra object on which the filtering/processing function has been applied

See also

Spectra::filterIntensity(), Spectra::filterMzRange(), Spectra::scalePeaks() in the Spectra package for pure R implementations of filtering/processing calculations.

Author

Thomas Naake

Examples

library(Spectra)
## create some example Spectra
DF <- DataFrame(
    msLevel = c(2L, 2L, 2L),
    name = c("Caffeine", "Caffeine", "1-Methylhistidine"),
    precursorMz = c(195.0877, 195.0877, 170.0924)
)
DF$intensity <- list(
    c(340.0, 416, 2580, 412),
    c(388.0, 3270, 85, 54, 10111),
    c(3.407, 47.494, 3.094, 100.0, 13.240))
DF$mz <- list(
    c(135.0432, 138.0632, 163.0375, 195.0880),
    c(110.0710, 138.0655, 138.1057, 138.1742, 195.0864),
    c(109.2, 124.2, 124.5, 170.16, 170.52))
sps <- Spectra(DF)

## process Spectra with matchms' select_by_intensity algorithm
## note: the first filterSpectriPy will take longer because the Python
## environment needs to be set up.
filterSpectriPy(sps, param = select_by_intensity(intensity_from=50, intensity_to=400))
#> MSn data (Spectra) with 3 spectra in a MsBackendMemory backend:
#>     msLevel     rtime scanIndex
#>   <integer> <numeric> <integer>
#> 1        NA        NA        NA
#> 2        NA        NA        NA
#> 3        NA        NA        NA
#>  ... 16 more variables/columns.
#> Processing:
#>  Merge 3 Spectra into one [Thu Feb  6 16:06:24 2025] 

## Process Spectra with matchms' select_by_mz algorithm
filterSpectriPy(sps, param = select_by_mz(mz_from=150, mz_to=450))
#> MSn data (Spectra) with 3 spectra in a MsBackendMemory backend:
#>     msLevel     rtime scanIndex
#>   <integer> <numeric> <integer>
#> 1        NA        NA        NA
#> 2        NA        NA        NA
#> 3        NA        NA        NA
#>  ... 16 more variables/columns.
#> Processing:
#>  Merge 3 Spectra into one [Thu Feb  6 16:06:25 2025] 

## Calculate pairwise similarity of all spectra in sps with matchms' 
## remove_peaks_around_precursor_mz algorithm
filterSpectriPy(sps, param = remove_peaks_around_precursor_mz(mz_tolerance=20))
#> MSn data (Spectra) with 3 spectra in a MsBackendMemory backend:
#>     msLevel     rtime scanIndex
#>   <integer> <numeric> <integer>
#> 1        NA        NA        NA
#> 2        NA        NA        NA
#> 3        NA        NA        NA
#>  ... 16 more variables/columns.
#> Processing:
#>  Merge 3 Spectra into one [Thu Feb  6 16:06:25 2025] 

## Calculate pairwise similarity of all spectra in sps with matchms' 
## normalize_intensities algorithm
filterSpectriPy(sps, normalize_intensities())
#> MSn data (Spectra) with 3 spectra in a MsBackendMemory backend:
#>     msLevel     rtime scanIndex
#>   <integer> <numeric> <integer>
#> 1        NA        NA        NA
#> 2        NA        NA        NA
#> 3        NA        NA        NA
#>  ... 16 more variables/columns.
#> Processing:
#>  Merge 3 Spectra into one [Thu Feb  6 16:06:25 2025]