rformassspectrometry / Spectra

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

manipulating spectraData clears peaksData #146

Closed stanstrup closed 3 years ago

stanstrup commented 3 years ago
spd <- DataFrame(msLevel = c(1L, 2L), rtime = c(1.1, 1.2))
spd$mz <- list(c(100, 103.2, 104.3, 106.5), c(45.6, 120.4, 190.2))
spd$intensity <- list(c(200, 400, 34.2, 17), c(12.3, 15.2, 6.8))

data <- Spectra(spd)

peaksData(data) %>% as.list()
[[1]]
        mz intensity
[1,] 100.0     200.0
[2,] 103.2     400.0
[3,] 104.3      34.2
[4,] 106.5      17.0

[[2]]
        mz intensity
[1,]  45.6      12.3
[2,] 120.4      15.2
[3,] 190.2       6.8

Now changing spectraData and peaksData becomes empty:

spectraData(data) <- spectraData(data)
peaksData(data) %>% as.list()
[[1]]
     mz intensity

[[2]]
     mz intensity

I assume this is not intended?

jorainer commented 3 years ago

That's a tricky one. Yes, I agree that it is not intuitive that this function removes m/z and intensity values since spectraData does not return these values. I will fix that by internally re-adding the mz and intensity columns if they are not present in value. This can however cause problems if the new spectra data is in a different order than before!

Thus, if possible, I suggest replacing individual spectra variables/columns instead of replacing the full data at once. I will update the documentation accordingly.

stanstrup commented 3 years ago

OK. Thanks. So this should be the way to go?:

data@backend@spectraData$msLevel <- c(3,4)
jorainer commented 3 years ago

No, please don't access the backend directly. It would be msLevel(data) <- c(3, 4) or data$msLevel <- c(3, 4). All operations should be possible on the Spectra object, thus the normal users should never access the backend.

jorainer commented 3 years ago

Anyway, what you reported is a bug, because spectraData <- on a Spectra object should not remove m/z and intensity values.

stanstrup commented 3 years ago

Aha. OK. I forgot about directly access the spectraData with $.

jorainer commented 3 years ago

And you can also add additional spectra variables with that, e.g. data$new_col <- "a" would add a new spectra variable "new_col".

jorainer commented 3 years ago

It's fixed with the new version. Closing the issue now - feel free to reopen if needed @stanstrup