rformassspectrometry / Spectra

Low level infrastructure to handle MS spectra
https://rformassspectrometry.github.io/Spectra/
34 stars 24 forks source link

removePeaks with relative treshold #126

Closed michaelwitting closed 3 years ago

michaelwitting commented 3 years ago

Hi,

as far as I understand removePeaks is working with an absolute intensity treshold. Is it possible to also have a relative treshold? Best without the need to first convert intensity to relative scale and filter, because then the absolute intensities are lost.

Best regards,

Michael

jorainer commented 3 years ago

Firstly, the function is now called filterIntensity and yes, it's only possible to specify a hard cut-off in that function. What relative threshold would you need?

michaelwitting commented 3 years ago

I would like to filter peaks that are lower than let's say 1% of the base peak intensity.

jorainer commented 3 years ago

The filter call which is applied to each spectrum has its msLevel, centroiding, intensity and mz available. So, filtering based on the intensity values of a spectrum should be doable. We would have to support passing a function to filterIntensity.

What would not be possible right now is to filter peaks in a spectrum based on the e.g. precursor intensity of that spectrum.

jorainer commented 3 years ago

PR #136 adds this functionality. With it you can then add a function to filterIntensity that takes the intensity values of a spectrum as input and filters the spectrum based on that. Example:

my_filter <- function(x) {
    x > max(x, na.rm = TRUE) / 10
}
sps <- filterIntensity(sps, intenity = my_filter)

This will remove all peaks from each spectrum that have an intensity smaller than 10% of the maximum intensity in that spectrum (BPI).

michaelwitting commented 3 years ago

Awesome, seems to work correct!