Skip to content

Commit

Permalink
refactor: change estimatePrecursorIntensity to a method
Browse files Browse the repository at this point in the history
- Change `estimatePrecursorIntensity()` to a method to avoid overrides by the
  implementation in *xcms*.
  • Loading branch information
jorainer committed Jul 31, 2024
1 parent d0ea0b1 commit e93fa63
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 98 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: Spectra
Title: Spectra Infrastructure for Mass Spectrometry Data
Version: 1.15.6
Version: 1.15.7
Description: The Spectra package defines an efficient infrastructure
for storing and handling mass spectrometry spectra and functionality to
subset, process, visualize and compare spectra data. It provides different
Expand Down Expand Up @@ -41,7 +41,7 @@ Depends:
R (>= 4.0.0),
S4Vectors,
BiocParallel,
ProtGenerics (>= 1.35.4)
ProtGenerics (>= 1.37.1)
Imports:
methods,
IRanges,
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export(concatenateSpectra)
export(coreSpectraVariables)
export(countIdentifications)
export(deisotopeSpectra)
export(estimatePrecursorIntensity)
export(estimatePrecursorMz)
export(filterPeaksRanges)
export(filterPrecursorIsotopes)
Expand Down Expand Up @@ -84,6 +83,7 @@ exportMethods(dataStorage)
exportMethods(dataStorageBasePath)
exportMethods(dropNaSpectraVariables)
exportMethods(entropy)
exportMethods(estimatePrecursorIntensity)
exportMethods(export)
exportMethods(filterAcquisitionNum)
exportMethods(filterDataOrigin)
Expand Down Expand Up @@ -248,6 +248,7 @@ importMethodsFrom(ProtGenerics,collisionEnergy)
importMethodsFrom(ProtGenerics,compareSpectra)
importMethodsFrom(ProtGenerics,dataOrigin)
importMethodsFrom(ProtGenerics,dataStorage)
importMethodsFrom(ProtGenerics,estimatePrecursorIntensity)
importMethodsFrom(ProtGenerics,filterAcquisitionNum)
importMethodsFrom(ProtGenerics,filterDataOrigin)
importMethodsFrom(ProtGenerics,filterDataStorage)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Spectra 1.15

## Changes in 1.15.7

- Change `estimatePrecursorIntensity()` to a method to avoid overrides/clashes
with the same-named implementation in *xcms*.

## Changes in 1.15.6

- Fix in `selectSpectraVariables()` for `MsBackendMzR`: ensure peaks variables
Expand Down
76 changes: 0 additions & 76 deletions R/Spectra-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -690,82 +690,6 @@ processingLog <- function(x) {
x@processing
}

#' @title Estimate Precursor Intensities
#'
#' @description
#'
#' Some MS instrument manufacturers don't provide precursor intensities for
#' fragment spectra. These can however be estimated, given that also MS1
#' spectra are available. The `estimatePrecursorIntensity()` funtion defines the
#' precursor intensities for MS2 spectra using the intensity of the matching
#' MS1 peak from the closest MS1 spectrum (i.e. the last MS1 spectrum measured
#' before the respective MS2 spectrum). With `method = "interpolation"` it is
#' also possible to calculate the precursor intensity based on an interpolation
#' of intensity values (and retention times) of the matching MS1 peaks from the
#' previous and next MS1 spectrum. See below for an example.
#'
#' @param x `Spectra` with MS1 and MS2 spectra.
#'
#' @param ppm `numeric(1)` with the maximal allowed relative difference of m/z
#' values between the precursor m/z of a spectrum and the m/z of the
#' respective ion on the MS1 scan.
#'
#' @param tolerance `numeric(1)` with the maximal allowed difference of m/z
#' values between the precursor m/z of a spectrum and the m/z of the
#' respective ion on the MS1 scan.
#'
#' @param method `character(1)` defining whether the precursor intensity
#' should be estimated on the previous MS1 spectrum (`method = "previous"`,
#' the default) or based on an interpolation on the previous and next
#' MS1 spectrum (`method = "interpolation"`).
#'
#' @param msLevel. `integer(1)` the MS level for which precursor intensities
#' should be estimated. Defaults to `2L`.
#'
#' @param f `factor` (or vector to be coerced to `factor`) defining which
#' spectra belong to the same original data file (sample).
#' Defaults to `f = dataOrigin(x)`.
#'
#' @param BPPARAM Parallel setup configuration. See [bpparam()] for more
#' information. This is passed directly to the [backendInitialize()] method
#' of the [MsBackend-class].
#'
#' @author Johannes Rainer with feedback and suggestions from Corey Broeckling
#'
#' @export
#'
#' @rdname estimatePrecursorIntensity
#'
#' @examples
#'
#' #' ## Calculating the precursor intensity for MS2 spectra:
#' ##
#' ## Some MS instrument manufacturer don't report the precursor intensities
#' ## for MS2 spectra. The `estimatePrecursorIntensity` function can be used
#' ## in these cases to calculate the precursor intensity on MS1 data. Below
#' ## we load an mzML file from a vendor providing precursor intensities and
#' ## compare the estimated and reported precursor intensities.
#' tmt <- Spectra(msdata::proteomics(full.names = TRUE)[5],
#' backend = MsBackendMzR())
#' pmi <- estimatePrecursorIntensity(tmt)
#' plot(pmi, precursorIntensity(tmt))
#'
#' ## We can also replace the original precursor intensity values with the
#' ## newly calculated ones
#' tmt$precursorIntensity <- pmi
estimatePrecursorIntensity <- function(x, ppm = 20, tolerance = 0,
method = c("previous", "interpolation"),
msLevel. = 2L, f = dataOrigin(x),
BPPARAM = bpparam()) {
if (is.factor(f))
f <- as.character(f)
f <- factor(f, levels = unique(f))
BPPARAM <- backendBpparam(x@backend, BPPARAM)
unlist(bplapply(split(x, f), FUN = .estimate_precursor_intensity, ppm = ppm,
tolerance = tolerance, method = method, msLevel = msLevel.,
BPPARAM = BPPARAM), use.names = FALSE)
}

#' estimate precursor intensities based on MS1 peak intensity. This function
#' assumes that `x` is a `Spectra` with data **from a single file/sample**.
#'
Expand Down
83 changes: 82 additions & 1 deletion R/Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -2859,4 +2859,85 @@ asDataFrame <- function(object, i = seq_along(object),
v <- spectraData(object)[rep(seq_along(object), n), spectraVars]
p <- do.call(rbind, as.list(peaksData(object)))
cbind(p, v)
}
}

#' @title Estimate Precursor Intensities
#'
#' @description
#'
#' Some MS instrument manufacturers don't provide precursor intensities for
#' fragment spectra. These can however be estimated, given that also MS1
#' spectra are available. The `estimatePrecursorIntensity()` funtion defines the
#' precursor intensities for MS2 spectra using the intensity of the matching
#' MS1 peak from the closest MS1 spectrum (i.e. the last MS1 spectrum measured
#' before the respective MS2 spectrum). With `method = "interpolation"` it is
#' also possible to calculate the precursor intensity based on an interpolation
#' of intensity values (and retention times) of the matching MS1 peaks from the
#' previous and next MS1 spectrum. See below for an example.
#'
#' @param object `Spectra` with MS1 and MS2 spectra.
#'
#' @param ppm `numeric(1)` with the maximal allowed relative difference of m/z
#' values between the precursor m/z of a spectrum and the m/z of the
#' respective ion on the MS1 scan.
#'
#' @param tolerance `numeric(1)` with the maximal allowed difference of m/z
#' values between the precursor m/z of a spectrum and the m/z of the
#' respective ion on the MS1 scan.
#'
#' @param method `character(1)` defining whether the precursor intensity
#' should be estimated on the previous MS1 spectrum (`method = "previous"`,
#' the default) or based on an interpolation on the previous and next
#' MS1 spectrum (`method = "interpolation"`).
#'
#' @param msLevel. `integer(1)` the MS level for which precursor intensities
#' should be estimated. Defaults to `2L`.
#'
#' @param f `factor` (or vector to be coerced to `factor`) defining which
#' spectra belong to the same original data file (sample).
#' Defaults to `f = dataOrigin(x)`.
#'
#' @param BPPARAM Parallel setup configuration. See [bpparam()] for more
#' information. This is passed directly to the [backendInitialize()] method
#' of the [MsBackend-class].
#'
#' @author Johannes Rainer with feedback and suggestions from Corey Broeckling
#'
#' @importMethodsFrom ProtGenerics estimatePrecursorIntensity
#'
#' @exportMethod estimatePrecursorIntensity
#'
#' @rdname estimatePrecursorIntensity
#'
#' @examples
#'
#' #' ## Calculating the precursor intensity for MS2 spectra:
#' ##
#' ## Some MS instrument manufacturer don't report the precursor intensities
#' ## for MS2 spectra. The `estimatePrecursorIntensity` function can be used
#' ## in these cases to calculate the precursor intensity on MS1 data. Below
#' ## we load an mzML file from a vendor providing precursor intensities and
#' ## compare the estimated and reported precursor intensities.
#' tmt <- Spectra(msdata::proteomics(full.names = TRUE)[5],
#' backend = MsBackendMzR())
#' pmi <- estimatePrecursorIntensity(tmt)
#' plot(pmi, precursorIntensity(tmt))
#'
#' ## We can also replace the original precursor intensity values with the
#' ## newly calculated ones
#' tmt$precursorIntensity <- pmi
setMethod(
"estimatePrecursorIntensity", "Spectra",
function(object, ppm = 20, tolerance = 0,
method = c("previous", "interpolation"),
msLevel. = 2L, f = dataOrigin(object), BPPARAM = bpparam()) {
if (is.factor(f))
f <- as.character(f)
f <- factor(f, levels = unique(f))
BPPARAM <- backendBpparam(object@backend, BPPARAM)
unlist(bplapply(split(object, f),
FUN = .estimate_precursor_intensity, ppm = ppm,
tolerance = tolerance, method = method,
msLevel = msLevel., BPPARAM = BPPARAM),
use.names = FALSE)
})
14 changes: 7 additions & 7 deletions man/estimatePrecursorIntensity.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions tests/testthat/test_Spectra-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -690,16 +690,6 @@ test_that(".estimate_precursor_intensity works", {
expect_true(all(is.na(res)))
})

test_that("estimatePrecursorIntensity works", {
fls <- msdata::proteomics(full.names = TRUE)[c(5, 3)]
second <- Spectra(fls[2], backend = MsBackendMzR())
both <- Spectra(fls, backend = MsBackendMzR())

res_second <- estimatePrecursorIntensity(second)
res_both <- estimatePrecursorIntensity(both)
expect_equal(res_second, res_both[510:length(res_both)])
})

test_that(".chunk_factor works", {
res <- .chunk_factor(10, chunkSize = 3)
expect_equal(res, as.factor(c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4)))
Expand Down
12 changes: 11 additions & 1 deletion tests/testthat/test_Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -1928,4 +1928,14 @@ test_that("asDataFrame works", {
expect_identical(nrow(df), sum(sapply(peaksData(sp[1:3]), nrow)))
expect_identical(ncol(df), 2L)
expect_identical(names(df), c("mz", "intensity"))
})
})

test_that("estimatePrecursorIntensity works", {
fls <- msdata::proteomics(full.names = TRUE)[c(5, 3)]
second <- Spectra(fls[2], backend = MsBackendMzR())
both <- Spectra(fls, backend = MsBackendMzR())

res_second <- estimatePrecursorIntensity(second)
res_both <- estimatePrecursorIntensity(both)
expect_equal(res_second, res_both[510:length(res_both)])
})

0 comments on commit e93fa63

Please sign in to comment.