Skip to content

Commit

Permalink
Add bpi method (issue #168)
Browse files Browse the repository at this point in the history
o Add bpi,OnDiskMsnExp method. Parameter initial allows to specify whether the
  original data from the mzML file is returned, or whether the values are
  calculated.
o Modify tic,OnDiskMSnExp method to always return the initial TIC from the
  mzML's header, unless parameter initial = FALSE
o Add unit tests for both methods.

From: jotsetung <johannes.rainer@gmail.com>

git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/MSnbase@124448 bc3139a8-67e5-0310-9ffc-ced21a209358
  • Loading branch information
Laurent Gatto committed Nov 24, 2016
1 parent 25a325a commit 9083669
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 42 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ exportMethods(updateObject,
intensity,
mz,
tic,
bpi,
ionCount,
fromFile,
centroided, "centroided<-",
Expand Down
2 changes: 2 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,5 @@ setGeneric("filterFile", function (object, ...) standardGeneric("filterFile"))
setGeneric("filterAcquisitionNum", function (object, ...) standardGeneric("filterAcquisitionNum"))

## isolationWindow generic is in mzR

setGeneric("bpi", function(object, ...) standardGeneric("bpi"))
62 changes: 35 additions & 27 deletions R/methods-OnDiskMSnExp.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,33 +252,41 @@ setMethod("rtime", "OnDiskMSnExp",
## Get the total ion Current (from the featureData, thus it will not
## be recalculated).
setMethod("tic", "OnDiskMSnExp",
function(object, BPPARAM = bpparam()) {
skipFun <- c("pickPeaks")
if (length(object@spectraProcessingQueue) > 0) {
recalc <- any(unlist(lapply(object@spectraProcessingQueue,
function(z) {
if (any(z@FUN == skipFun))
return(FALSE)
return(TRUE)
})))
} else {
## No need to calculate the peaks count; we can use the
## information from the feature data.
recalc <- FALSE
}
## Also enforce re-calculation if we've got totIonCurrent of 0!
if (any(fData(object)$totIonCurrent == 0))
recalc <- TRUE
if (recalc) {
vals <- unlist(spectrapply(object,
FUN = tic,
BPPARAM = BPPARAM))
} else {
vals <- fData(object)$totIonCurrent
}
names(vals) <- featureNames(object)
return(vals)
})
function(object, initial = TRUE, BPPARAM = bpparam()) {
if (initial) {
vals <- fData(object)$totIonCurrent
} else {
## Calculate the value.
vals <- unlist(spectrapply(object, FUN = tic,
BPPARAM = BPPARAM))
}
names(vals) <- featureNames(object)
return(vals)
})

############################################################
## bpi
##
## Get the base peak intensity from each spectrum. If initial = TRUE
## the basePeakIntensity from the feaureData is returned, i.e. the value
## extracted from the header of the raw mzML file. If initial = FALSE the
## actual base peak intensity will be calculated for each spectrum by first
## applying all eventual data manipulation operations.
setMethod("bpi", "OnDiskMSnExp",
function(object, initial = TRUE, BPPARAM = bpparam()) {
if (initial) {
vals <- fData(object)$basePeakIntensity
} else {
## Calculate the value.
vals <- unlist(spectrapply(object,
FUN = function(z) {
return(max(intensity(z)))
}, BPPARAM = BPPARAM))
}
names(vals) <- featureNames(object)
return(vals)
})


############################################################
## collisionEnergy
Expand Down
16 changes: 8 additions & 8 deletions inst/scripts/torture.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
library(MSnbase)
library(msdata)
f <- msdata::proteomics(full.names = TRUE, pattern = "TMT_Erwinia")
f <- msdata::proteomics(full.names = TRUE)

## readMSData
for (i in 1:10000) {
if (i %% 200 == 0)
cat(i, "\n")
res <- readMSData(f)
sp <- spectra(res)
res <- readMSData(f[1], verbose = FALSE, msLevel. = 1)
res <- readMSData(f[1], verbose = FALSE, msLevel. = 2)
sp <- res[[12]]
sp <- spectra(res)
}

## readMSData2
for (i in 1:7500) {
if (i %% 200 == 0)
cat(i, "\n")
res <- readMSData2(f, verbose = FALSE)
sp <- spectra(res)
sp <- res[[12]]
res <- readMSData2(mzf, verbose = FALSE)
res <- readMSData2(f[1], verbose = FALSE)
sp <- res[[6]]
res <- readMSData2(f[2], verbose = FALSE)
sp <- spectra(res)
sp <- res[[12]]
}

37 changes: 30 additions & 7 deletions man/OnDiskMSnExp-class.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
\alias{scanIndex,OnDiskMSnExp-method}
\alias{spectra,OnDiskMSnExp-method}
\alias{tic,OnDiskMSnExp-method}
\alias{bpi,OnDiskMSnExp-method}
\alias{precScanNum,OnDiskMSnExp-method}
\alias{featureNames<-,OnDiskMSnExp-method}
\alias{featureNames<-,OnDiskMSnExp,ANY-method}
Expand Down Expand Up @@ -400,17 +401,39 @@ Class \code{"\linkS4class{Versioned}"}, by class "pSet", distance 5.
}

\item{tic}{
\code{tic(signature(object="OnDiskMSnExp"))}:
get the total ion current of each spectrum in the data
set. This information is extracted from the object's
\code{featureData} and represents the tic provided in the original
raw data files.
\code{tic(signature(object="OnDiskMSnExp"), initial = TRUE,
BPPARAM = bpparam())}:
get the total ion current (TIC) of each spectrum in the data
set. If \code{initial = TRUE}, the information is extracted from
the object's \code{featureData} and represents the tic provided in
the header of the original raw data files. For \code{initial =
FALSE}, the TIC is calculated from the actual intensity values in
each spectrum after applying all eventual data manipulation methods.
\code{BPPARAM} parameter: see \code{spectra} method above.
Returns a numeric vector with names corresponding to the spectrum
names.
}
\item{bpi}{
\code{bpi(signature(object="OnDiskMSnExp"), initial = TRUE,
BPPARAM = bpparam())}:
get the base peak intensity (BPI), i.e. the maximum intensity from
each spectrum in the data set. If \code{initial = TRUE}, the
information is extracted from the object's \code{featureData} and
represents the tic provided in the header of the original raw data
files. For \code{initial = FALSE}, the TIC is calculated from the
actual intensity values in each spectrum after applying all
eventual data manipulation methods.

\code{BPPARAM} parameter: see \code{spectra} method above.

Returns a numeric vector with names corresponding to the spectrum
names.
}

\item{featureNames}{
\item{featureNames}{
\code{tic(signature(object="OnDiskMSnExp"))}: return a
\code{character} of length \code{length(object)} containing the
feature names. A replacement method is also available.
Expand Down Expand Up @@ -494,7 +517,7 @@ Class \code{"\linkS4class{Versioned}"}, by class "pSet", distance 5.
\item{\code{as(from, "MSnExp")}}{Converts the \code{OnDiskMSnExp}
object \code{from}, to an in-memory \code{MSnExp}. Also available
as an S3 method \code{as.MSnExp()}.}

}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/testthat/test_OnDiskMSnExp_other_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,34 @@ test_that("Test precursor* for OnDiskMSnExp", {
names(pcn) <- names(pcn2) <- NULL
expect_identical(pcn, pcn2)
})

############################################################
## bpi
test_that("bpi,OnDiskMSnExp", {
## Get the "initial" one.
basepi <- bpi(ondisk)
expect_identical(unname(basepi), fData(ondisk)$basePeakIntensity)
expect_identical(names(basepi), featureNames(ondisk))
## Calculate:
basepi <- bpi(ondisk, initial = FALSE)
expect_identical(names(basepi), featureNames(ondisk))
sp <- spectra(ondisk)
basepi_calc <- unlist(lapply(sp, FUN = function(z) max(intensity(z))))
expect_identical(basepi, basepi_calc)
})


############################################################
## tic
test_that("tic,OnDiskMSnExp", {
## Get the "initial" one.
totic <- tic(ondisk)
expect_identical(unname(totic), fData(ondisk)$totIonCurrent)
expect_identical(names(totic), featureNames(ondisk))
## Calculate:
totic <- tic(ondisk, initial = FALSE)
expect_identical(names(totic), featureNames(ondisk))
sp <- spectra(ondisk)
totic_calc <- unlist(lapply(sp, FUN = tic))
expect_identical(totic, totic_calc)
})

0 comments on commit 9083669

Please sign in to comment.