diff --git a/DESCRIPTION b/DESCRIPTION index 431584b9..7727f9b8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: Spectra Title: Spectra Infrastructure for Mass Spectrometry Data -Version: 1.15.12 +Version: 1.15.13 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 diff --git a/NAMESPACE b/NAMESPACE index df65fbe1..8d8185f9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -60,6 +60,7 @@ exportMethods("msLevel<-") exportMethods("mz<-") exportMethods("peaksData<-") exportMethods("polarity<-") +exportMethods("precursorMz<-") exportMethods("rtime<-") exportMethods("smoothed<-") exportMethods("spectraData<-") @@ -237,6 +238,7 @@ importMethodsFrom(ProtGenerics,"msLevel<-") importMethodsFrom(ProtGenerics,"mz<-") importMethodsFrom(ProtGenerics,"peaksData<-") importMethodsFrom(ProtGenerics,"polarity<-") +importMethodsFrom(ProtGenerics,"precursorMz<-") importMethodsFrom(ProtGenerics,"rtime<-") importMethodsFrom(ProtGenerics,"smoothed<-") importMethodsFrom(ProtGenerics,"spectraData<-") diff --git a/NEWS.md b/NEWS.md index d21a1714..b3d0404d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # Spectra 1.15 +## Changes in 1.15.13 + +- Add `precursorMz<-` method [issue #336](https://github.com/rformassspectrometry/Spectra/issues/336). + ## Changes in 1.15.12 - Add generic `backendRequiredSpectraVariables()` to allow definition of diff --git a/R/MsBackend.R b/R/MsBackend.R index f2a86d0b..186f26c6 100644 --- a/R/MsBackend.R +++ b/R/MsBackend.R @@ -1699,6 +1699,18 @@ setMethod("precursorMz", "MsBackend", function(object) { stop("Not implemented for ", class(object), ".") }) +#' @exportMethod precursorMz<- +#' +#' @importMethodsFrom ProtGenerics precursorMz<- +#' +#' @rdname MsBackend +#' +#' @export +setReplaceMethod("precursorMz", "MsBackend", function(object, ..., value) { + object$precursorMz <- value + object +}) + #' @exportMethod peaksData<- #' #' @importMethodsFrom ProtGenerics peaksData<- diff --git a/R/MsBackendCached.R b/R/MsBackendCached.R index dfb4aa40..e2f4d4d2 100644 --- a/R/MsBackendCached.R +++ b/R/MsBackendCached.R @@ -464,7 +464,6 @@ setMethod("centroided", "MsBackendCached", function(object) { #' @rdname MsBackendCached setReplaceMethod("centroided", "MsBackendCached", function(object, value) { object$centroided <- value - validObject(object) object }) @@ -476,7 +475,6 @@ setMethod("collisionEnergy", "MsBackendCached", function(object) { #' @rdname MsBackendCached setReplaceMethod("collisionEnergy", "MsBackendCached", function(object, value) { object$collisionEnergy <- value - validObject(object) object }) @@ -488,7 +486,6 @@ setMethod("dataOrigin", "MsBackendCached", function(object) { #' @rdname MsBackendCached setReplaceMethod("dataOrigin", "MsBackendCached", function(object, value) { object$dataOrigin <- value - validObject(object) object }) @@ -525,7 +522,6 @@ setMethod("isolationWindowLowerMz", "MsBackendCached", function(object) { setReplaceMethod("isolationWindowLowerMz", "MsBackendCached", function(object, value) { object$isolationWindowLowerMz <- value - validObject(object) object }) @@ -538,7 +534,6 @@ setMethod("isolationWindowTargetMz", "MsBackendCached", function(object) { setReplaceMethod("isolationWindowTargetMz", "MsBackendCached", function(object, value) { object$isolationWindowTargetMz <- value - validObject(object) object }) @@ -551,7 +546,6 @@ setMethod("isolationWindowUpperMz", "MsBackendCached", function(object) { setReplaceMethod("isolationWindowUpperMz", "MsBackendCached", function(object, value) { object$isolationWindowUpperMz <- value - validObject(object) object }) @@ -574,7 +568,6 @@ setMethod("polarity", "MsBackendCached", function(object) { setReplaceMethod("polarity", "MsBackendCached", function(object, value) { if (is.numeric(value)) value <- as.integer(value) object$polarity <- value - validObject(object) object }) @@ -601,7 +594,6 @@ setMethod("rtime", "MsBackendCached", function(object) { #' @rdname MsBackendCached setReplaceMethod("rtime", "MsBackendCached", function(object, value) { object$rtime <- value - validObject(object) object }) @@ -618,6 +610,5 @@ setMethod("smoothed", "MsBackendCached", function(object) { #' @rdname MsBackendCached setReplaceMethod("smoothed", "MsBackendCached", function(object, value) { object$smoothed <- value - validObject(object) object }) diff --git a/R/Spectra.R b/R/Spectra.R index 7564b0a4..14ebbf2c 100644 --- a/R/Spectra.R +++ b/R/Spectra.R @@ -1244,6 +1244,12 @@ setMethod("precursorMz", "Spectra", function(object) { precursorMz(object@backend) }) +#' @rdname spectraData +setReplaceMethod("precursorMz", "Spectra", function(object, ..., value) { + precursorMz(object@backend) <- value + object +}) + #' @rdname spectraData setMethod("rtime", "Spectra", function(object) { rtime(object@backend) diff --git a/man/MsBackend.Rd b/man/MsBackend.Rd index 16b5e782..279576a5 100644 --- a/man/MsBackend.Rd +++ b/man/MsBackend.Rd @@ -84,6 +84,7 @@ \alias{precursorCharge,MsBackend-method} \alias{precursorIntensity,MsBackend-method} \alias{precursorMz,MsBackend-method} +\alias{precursorMz<-,MsBackend-method} \alias{peaksData<-,MsBackend-method} \alias{reset,MsBackend-method} \alias{rtime,MsBackend-method} @@ -244,6 +245,8 @@ \S4method{precursorMz}{MsBackend}(object) +\S4method{precursorMz}{MsBackend}(object, ...) <- value + \S4method{peaksData}{MsBackend}(object) <- value \S4method{reset}{MsBackend}(object) diff --git a/man/spectraData.Rd b/man/spectraData.Rd index 49d2bee3..2aad735f 100644 --- a/man/spectraData.Rd +++ b/man/spectraData.Rd @@ -62,6 +62,7 @@ \alias{precursorCharge,Spectra-method} \alias{precursorIntensity,Spectra-method} \alias{precursorMz,Spectra-method} +\alias{precursorMz<-,Spectra-method} \alias{rtime,Spectra-method} \alias{rtime<-,Spectra-method} \alias{scanIndex,Spectra-method} @@ -154,6 +155,8 @@ coreSpectraVariables() \S4method{precursorMz}{Spectra}(object) +\S4method{precursorMz}{Spectra}(object, ...) <- value + \S4method{rtime}{Spectra}(object) \S4method{rtime}{Spectra}(object) <- value diff --git a/tests/testthat/test_MsBackend.R b/tests/testthat/test_MsBackend.R index 01fa65c2..5e91a0fe 100644 --- a/tests/testthat/test_MsBackend.R +++ b/tests/testthat/test_MsBackend.R @@ -58,6 +58,7 @@ test_that("MsBackend methods throw errors", { expect_error(dm$a <- "a", "implemented for") expect_error(extractByIndex(dm, 1), "implemented for") expect_equal(backendRequiredSpectraVariables(dm), character()) + expect_error(precursorMz(dm) <- 12.3, "implemented for") }) test_that("extractByIndex not implemented fallback", { diff --git a/tests/testthat/test_MsBackendCached.R b/tests/testthat/test_MsBackendCached.R index 86bd8639..e547b190 100644 --- a/tests/testthat/test_MsBackendCached.R +++ b/tests/testthat/test_MsBackendCached.R @@ -302,3 +302,10 @@ test_that("lengths,MsBackendCached works", { res <- lengths(be) expect_true(all(res == 0)) }) + +test_that("precursorMz<-,MsBackendCached works", { + be <- backendInitialize(MsBackendCached(), nspectra = 4) + expect_true(all(is.na(precursorMz(be)))) + precursorMz(be) <- c(1.1, 1.2, 1.3, 1.34) + expect_equal(precursorMz(be), c(1.1, 1.2, 1.3, 1.34)) +}) diff --git a/tests/testthat/test_MsBackendMzR.R b/tests/testthat/test_MsBackendMzR.R index d8a83227..44d38cd2 100644 --- a/tests/testthat/test_MsBackendMzR.R +++ b/tests/testthat/test_MsBackendMzR.R @@ -603,3 +603,9 @@ test_that("backendRequiredSpectraVariables,MsBackendMzR works", { expect_equal(backendRequiredSpectraVariables(tmp), c("dataStorage", "scanIndex")) }) + +test_that("precursorMz<-,MsbackendMzR works", { + a <- sciex_mzr[1:3] + precursorMz(a) <- c(12.2, 1.2, 1.4) + expect_equal(precursorMz(a), c(12.2, 1.2, 1.4)) +}) diff --git a/tests/testthat/test_Spectra.R b/tests/testthat/test_Spectra.R index 43638a4d..4cc721d9 100644 --- a/tests/testthat/test_Spectra.R +++ b/tests/testthat/test_Spectra.R @@ -1986,3 +1986,9 @@ test_that("estimatePrecursorIntensity works", { res_both <- estimatePrecursorIntensity(both) expect_equal(res_second, res_both[510:length(res_both)]) }) + +test_that("precursorMz<-,Spectra works", { + a <- sps_dda[1:3] + precursorMz(a) <- c(12.3, 1.1, 34.3) + expect_equal(precursorMz(a), c(12.3, 1.1, 34.3)) +}) diff --git a/vignettes/MsBackend.Rmd b/vignettes/MsBackend.Rmd index 5192084e..c74f82e8 100644 --- a/vignettes/MsBackend.Rmd +++ b/vignettes/MsBackend.Rmd @@ -1677,6 +1677,21 @@ This method thus retrieves first the MS levels of all spectra and then calls operation by selecting the unique MS levels directly using an SQL call. +### `precursorMz<-` + +Replace the values for the *precursor m/z* spectra +variable. Parameter `value` has to be of type `numeric` (`NA_real_` missing +values are supported, e.g. for MS1 spectra). The default implementation uses the +`$<-` method: + +```{r} +setReplaceMethod("precursorMz", "MsBackend", function(object, ..., value) { + object$precursorMz <- value + object +}) +``` + + ### `ionCount()` The `ionCount()` method should return a `numeric` (length equal to the number of