rformassspectrometry / Spectra

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

Function for alignment of Spectra #242

Closed michaelwitting closed 2 years ago

michaelwitting commented 2 years ago

Hi all,

I'm having a problem in which I would need to generate a matrix of all spectra in a Spectra object. This would need the alignment of multiple spectra.

I think there is a similar function already in xcms, but it won't work on Spectra objects. https://rdrr.io/bioc/xcms/src/R/mzClust.R

Best,

Michael

jorainer commented 2 years ago

Hm, I was not aware of these functions in xcms - an easy solution would be to bin the spectra by m/z and then create a matrix for that, each column being one m/z bin, each row one spectrum and the values the intensities. Would this fit your purpose?

jorainer commented 2 years ago

I'm at present working on a function binnedPeaks that takes a Spectra object and returns a matrix with each row representing one spectrum, columns being bins (m/z values) and values the intensities.

As alternative (or in addition?) we could however also define a bin,Spectra method that performs the binning on-the-fly (i.e. actual binning is performed as part of the lazy evaluation queue).

jorainer commented 2 years ago

Ah, well, embarassing - we have already a bin,Spectra method ...

jorainer commented 2 years ago

Maybe this could help to create a matrix of (aligned) peaks from a Spectra object:

sps_bin <- bin(sps, binSize = 0.1)
intmat <- do.call(rbind, intensity(sps_bin))
## remove columns with only 0s
zeros <- colSums(intmat) == 0
intmat <- intmat[, !zeros]
## use the m/z values of the bins as column names
colnames(intmat) <- mz(sps_bin)[[1L]][!zeros]

intmat
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]   [,8]  [,9] [,10] [,11] [,12]
[1,] 0.000 0.000 0.000 0.000 0.000 0.000 0.000  0.000 3.407 0.000 0.000 0.000
[2,] 0.000 0.000 0.000 6.685 4.381 3.022 0.000 16.708 0.000 0.000 0.000 0.000
[3,] 0.459 2.585 2.446 0.000 0.000 0.000 0.508  0.000 0.000 8.968 0.524 0.974
[4,] 0.000 0.000 0.000 0.000 0.000 0.000 0.000  0.000 0.000 3.837 0.000 0.000
     [,13]  [,14] [,15] [,16]   [,17] [,18]   [,19] [,20]  [,21] [,22]
[1,]     0 47.494 3.094 0.000   0.000 0.000 100.000 13.24  0.000     0
[2,]   100  0.000 0.000 4.565   0.000 0.000  40.643  0.00  0.000     0
[3,]     0  0.000 0.000 0.000 100.000 0.000   0.000  0.00 40.994     0
[4,]     0  0.000 0.000 0.000  32.341 1.374   0.000  0.00  0.000   100

And you could do stuff like

heatmap(intmat)
heatmap

This is now also described in the vignette of Spectra.

Would this work for you @michaelwitting ? Doing an actual alignment without binning on a Spectra with length > 2 would be rather difficult because that needs still to be done in a pairwise manner and will most likely depend on the pair the iterative algorithm starts with...

michaelwitting commented 2 years ago

That will work perfectly. Will give it a try later! Thank you!

michaelwitting commented 2 years ago

It works exactly the way I need it!