Skip to content

Commit

Permalink
feat: add precursorMz<- replacement method (issue #336)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed Oct 25, 2024
1 parent 424e9ce commit 5f3a02c
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion 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.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
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ exportMethods("msLevel<-")
exportMethods("mz<-")
exportMethods("peaksData<-")
exportMethods("polarity<-")
exportMethods("precursorMz<-")
exportMethods("rtime<-")
exportMethods("smoothed<-")
exportMethods("spectraData<-")
Expand Down Expand Up @@ -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<-")
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions R/MsBackend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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<-
Expand Down
9 changes: 0 additions & 9 deletions R/MsBackendCached.R
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,6 @@ setMethod("centroided", "MsBackendCached", function(object) {
#' @rdname MsBackendCached
setReplaceMethod("centroided", "MsBackendCached", function(object, value) {
object$centroided <- value
validObject(object)
object
})

Expand All @@ -476,7 +475,6 @@ setMethod("collisionEnergy", "MsBackendCached", function(object) {
#' @rdname MsBackendCached
setReplaceMethod("collisionEnergy", "MsBackendCached", function(object, value) {
object$collisionEnergy <- value
validObject(object)
object
})

Expand All @@ -488,7 +486,6 @@ setMethod("dataOrigin", "MsBackendCached", function(object) {
#' @rdname MsBackendCached
setReplaceMethod("dataOrigin", "MsBackendCached", function(object, value) {
object$dataOrigin <- value
validObject(object)
object
})

Expand Down Expand Up @@ -525,7 +522,6 @@ setMethod("isolationWindowLowerMz", "MsBackendCached", function(object) {
setReplaceMethod("isolationWindowLowerMz", "MsBackendCached",
function(object, value) {
object$isolationWindowLowerMz <- value
validObject(object)
object
})

Expand All @@ -538,7 +534,6 @@ setMethod("isolationWindowTargetMz", "MsBackendCached", function(object) {
setReplaceMethod("isolationWindowTargetMz", "MsBackendCached",
function(object, value) {
object$isolationWindowTargetMz <- value
validObject(object)
object
})

Expand All @@ -551,7 +546,6 @@ setMethod("isolationWindowUpperMz", "MsBackendCached", function(object) {
setReplaceMethod("isolationWindowUpperMz", "MsBackendCached",
function(object, value) {
object$isolationWindowUpperMz <- value
validObject(object)
object
})

Expand All @@ -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
})

Expand All @@ -601,7 +594,6 @@ setMethod("rtime", "MsBackendCached", function(object) {
#' @rdname MsBackendCached
setReplaceMethod("rtime", "MsBackendCached", function(object, value) {
object$rtime <- value
validObject(object)
object
})

Expand All @@ -618,6 +610,5 @@ setMethod("smoothed", "MsBackendCached", function(object) {
#' @rdname MsBackendCached
setReplaceMethod("smoothed", "MsBackendCached", function(object, value) {
object$smoothed <- value
validObject(object)
object
})
6 changes: 6 additions & 0 deletions R/Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions man/MsBackend.Rd

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

3 changes: 3 additions & 0 deletions man/spectraData.Rd

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

1 change: 1 addition & 0 deletions tests/testthat/test_MsBackend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test_MsBackendCached.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
6 changes: 6 additions & 0 deletions tests/testthat/test_MsBackendMzR.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
6 changes: 6 additions & 0 deletions tests/testthat/test_Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
15 changes: 15 additions & 0 deletions vignettes/MsBackend.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5f3a02c

Please sign in to comment.