diff --git a/DESCRIPTION b/DESCRIPTION index 7006652c..c233fc3c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 @@ -41,7 +41,7 @@ Depends: R (>= 4.0.0), S4Vectors, BiocParallel, - ProtGenerics (>= 1.35.4) + ProtGenerics (>= 1.37.1) Imports: methods, IRanges, diff --git a/NAMESPACE b/NAMESPACE index c2a95ca3..0a9fbcf9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,7 +17,6 @@ export(concatenateSpectra) export(coreSpectraVariables) export(countIdentifications) export(deisotopeSpectra) -export(estimatePrecursorIntensity) export(estimatePrecursorMz) export(filterPeaksRanges) export(filterPrecursorIsotopes) @@ -84,6 +83,7 @@ exportMethods(dataStorage) exportMethods(dataStorageBasePath) exportMethods(dropNaSpectraVariables) exportMethods(entropy) +exportMethods(estimatePrecursorIntensity) exportMethods(export) exportMethods(filterAcquisitionNum) exportMethods(filterDataOrigin) @@ -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) diff --git a/NEWS.md b/NEWS.md index 953f9468..49e8e289 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/Spectra-functions.R b/R/Spectra-functions.R index 8bb9042d..517452c4 100644 --- a/R/Spectra-functions.R +++ b/R/Spectra-functions.R @@ -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**. #' diff --git a/R/Spectra.R b/R/Spectra.R index a20a44ba..76a467c5 100644 --- a/R/Spectra.R +++ b/R/Spectra.R @@ -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) -} \ No newline at end of file +} + +#' @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) + }) diff --git a/man/estimatePrecursorIntensity.Rd b/man/estimatePrecursorIntensity.Rd index e4a7efd9..97a2cde2 100644 --- a/man/estimatePrecursorIntensity.Rd +++ b/man/estimatePrecursorIntensity.Rd @@ -1,21 +1,21 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/Spectra-functions.R -\name{estimatePrecursorIntensity} -\alias{estimatePrecursorIntensity} +% Please edit documentation in R/Spectra.R +\name{estimatePrecursorIntensity,Spectra-method} +\alias{estimatePrecursorIntensity,Spectra-method} \title{Estimate Precursor Intensities} \usage{ -estimatePrecursorIntensity( - x, +\S4method{estimatePrecursorIntensity}{Spectra}( + object, ppm = 20, tolerance = 0, method = c("previous", "interpolation"), msLevel. = 2L, - f = dataOrigin(x), + f = dataOrigin(object), BPPARAM = bpparam() ) } \arguments{ -\item{x}{\code{Spectra} with MS1 and MS2 spectra.} +\item{object}{\code{Spectra} with MS1 and MS2 spectra.} \item{ppm}{\code{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 diff --git a/tests/testthat/test_Spectra-functions.R b/tests/testthat/test_Spectra-functions.R index 7fabfbb5..8df50d71 100644 --- a/tests/testthat/test_Spectra-functions.R +++ b/tests/testthat/test_Spectra-functions.R @@ -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))) diff --git a/tests/testthat/test_Spectra.R b/tests/testthat/test_Spectra.R index 5169b2fe..e81efdcb 100644 --- a/tests/testthat/test_Spectra.R +++ b/tests/testthat/test_Spectra.R @@ -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")) -}) \ No newline at end of file +}) + +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)]) +})