rformassspectrometry / RforMassSpectrometry

The R for Mass Spectrometry meta-package
https://rformassspectrometry.github.io/RforMassSpectrometry/
16 stars 8 forks source link

RforMassSpectrometry development call 2023-05-19 #32

Open jorainer opened 1 year ago

jorainer commented 1 year ago

Topics to discuss:

jorainer commented 1 year ago

Examples

library(Spectra)

df <- data.frame(rtime = c(1.1, 1.2, 1.3, 1.4),
                 msLevel = 1L)
df$mz <- list(c(13, 14.1, 22, 23, 24, 49),
              c(45.1, 56),
              c(34.3, 134.4, 344, 443),
              c(12.1, 31))
df$intensity <- list(c(100, 300, 30, 120, 12, 34),
                     c(345, 234),
                     c(123, 124, 145, 3),
                     c(122, 421))

#' add some arbitrary information for each peak to the data.frame
df$ann <- list(c("a", NA, "b", "c", "d", NA),
               c("e", "f"),
               c("g", "h", "i", NA),
               c("j", "k"))

B <- Spectra(df, peaksVariables = c("mz", "intensity", "ann"))

#' Peak annotations get stored into @peaksDataFrame
B@backend@peaksDataFrame

peaksVariables(B)
[1] "mz"        "intensity" "ann" 

peaksData(B@backend, columns = peaksVariables(B))[[1L]]
     mz     intensity ann
[1,] "13.0" "100"     "a"
[2,] "14.1" "300"     NA 
[3,] "22.0" " 30"     "b"
[4,] "23.0" "120"     "c"
[5,] "24.0" " 12"     "d"
[6,] "49.0" " 34"     NA 

#' Not ideal because a matrix is returned...
#' In addition, the "generic" access is also supported
B$ann

Problems with filtering:

B2 <- filterMzValues(B, 23, tolerance = 1)
peaksData(B2, columns = c("mz", "intensity"))[[1L]]
     mz intensity
[1,] 22        30
[2,] 23       120
[3,] 24        12

#' Peaks variables were/are not updated properly.
B2$ann[[1L]]
[1] "a" NA  "b" "c" "d" NA 

#' Error extracting peaksData (since a character vector is returned by `peaksData`
peaksData(B2, columns = c("mz", "intensity", "ann"))[[1L]]
Error in x * ppm : non-numeric argument to binary operator