Closed michaelwitting closed 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?
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).
Ah, well, embarassing - we have already a bin,Spectra
method ...
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)
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...
That will work perfectly. Will give it a try later! Thank you!
It works exactly the way I need it!
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.RBest,
Michael