lgatto / MSnbase

Base Classes and Functions for Mass Spectrometry and Proteomics
http://lgatto.github.io/MSnbase/
124 stars 50 forks source link

Slot for scan rate in Msnbase object #532

Closed jmorim closed 3 years ago

jmorim commented 3 years ago

Would it be a good idea to make a slot for scan rate for an MS experiment? Like object@experimentData$scanRate

In the simplest case it can be calculated as

rts = object@featureData@data$retentionTime
nScans = length(rts)
object@experimentData$scanRate = nScans / (max(rts) - min(rts))
lgatto commented 3 years ago

I don't want to add a new dedicated slot explicitly, which would break backwards compatibility. Your solution, that adds it to experimentData is a step in the right direction - the @other slot is a list that accepts any additional/new data. You could then wrap everything in a function

addScanRate <- function(x) {
    stopifnot(inherits(x, "MSnExp"))
    rts <- rtime(x)
    nScans<-  length(rts)
    x@experimentData@other$scanRate <- nScans / (max(rts) - min(rts))
    x
}

And use it:

> odmse <- addScanRate(odmse)
> experimentData(odmse)@other$scanRate
[1] 10.02938

You might however want to improve the function by calculating a per-file scan rate, when multiple files are read at once.

jmorim commented 3 years ago

I think I got it.


addScanRate <- function(x){
   stopifnot(inherits(x, 'MSnExp'))
  rts.by.file = split(rtime(x), fromFile(x))
  nScans = lapply(rts.by.file, length)
   x@experimentData@other$scanRate = lapply(seq_along(nScans), function(x){
    nScans[[x]] / (max(rts.by.file[[x]]) - min(rts.by.file[[x]]))
  })
  x
}