Skip to content

Commit

Permalink
fix: selectSpectraVariables to not drop peaks variables
Browse files Browse the repository at this point in the history
- Ensure `selectSpectraVariables()` for `MsBackendMzR` is not removing peaks
  variables `"mz"` and `"intensity"` by default.
  • Loading branch information
jorainer committed Jul 23, 2024
1 parent 681f27d commit d0ea0b1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 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.5
Version: 1.15.6
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
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.6

- Fix in `selectSpectraVariables()` for `MsBackendMzR`: ensure peaks variables
`"mz"` and `"intensity"` are not by default removed.

## Changes in 1.15.5

- Add new `filterPeaksRanges()` function to filter mass peaks by ranges on
Expand Down
4 changes: 2 additions & 2 deletions R/MsBackendDataFrame.R
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,15 @@ setMethod("selectSpectraVariables", "MsBackendDataFrame",
spectraVariables(object))],
collapse = ", "), " not available")
keep <- spectraVariables[spectraVariables %in%
colnames(object@spectraData)]
colnames(object@spectraData)]
if (length(keep))
object@spectraData <- object@spectraData[, keep,
drop = FALSE]
msg <- .valid_spectra_data_required_columns(object@spectraData)
if (length(msg))
stop(msg)
object@peaksVariables <- intersect(object@peaksVariables,
colnames(object@spectraData))
spectraVariables)
validObject(object)
object
})
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test_MsBackendDataFrame.R
Original file line number Diff line number Diff line change
Expand Up @@ -622,15 +622,32 @@ test_that("selectSpectraVariables,MsBackendDataFrame works", {
be <- backendInitialize(MsBackendDataFrame(), df)

res <- selectSpectraVariables(be, c("dataStorage", "other_col"))

expect_equal(res@peaksVariables, be@peaksVariables)
expect_equal(colnames(res@spectraData), c("dataStorage", "other_col"))
expect_equal(msLevel(res), c(NA_integer_, NA_integer_))

res <- selectSpectraVariables(be, c("dataStorage", "rtime"))
expect_equal(colnames(res@spectraData), c("dataStorage", "rtime"))
expect_equal(res@peaksVariables, be@peaksVariables)

expect_error(selectSpectraVariables(be, "rtime"), "dataStorage is/are missing")
expect_error(selectSpectraVariables(be, "something"),
"something not available")

df$mz <- list(c(1.2, 1.4), c(5.3, 34.5, 52.1))
df$intensity <- list(c(123, 121.1), c(1231.1, 343.1, 21.1))
be <- backendInitialize(MsBackendDataFrame(), df)
res <- selectSpectraVariables(be, c("dataStorage", "other_col"))
expect_equal(colnames(res@spectraData), c("dataStorage", "other_col"))
expect_equal(msLevel(res), c(NA_integer_, NA_integer_))
expect_equal(res@peaksVariables, character())

be <- backendInitialize(MsBackendDataFrame(), df)
res <- selectSpectraVariables(be, c("dataStorage", "mz", "intensity"))
expect_equal(colnames(res@spectraData), c("dataStorage", "mz", "intensity"))
expect_equal(msLevel(res), c(NA_integer_, NA_integer_))
expect_equal(res@peaksVariables, c("mz", "intensity"))
})

test_that("$,$<-,MsBackendDataFrame works", {
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test_MsBackendMzR.R
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ test_that("selectSpectraVariables,MsBackendMzR works", {
"scanIndex"))
expect_equal(colnames(res@spectraData), c("dataStorage", "msLevel", "rtime",
"scanIndex"))
expect_equal(res@peaksVariables, character())

res <- selectSpectraVariables(be, c("dataStorage", "msLevel", "rtime",
"scanIndex", "mz", "intensity"))
expect_equal(colnames(res@spectraData), c("dataStorage", "msLevel", "rtime",
"scanIndex"))
expect_equal(res@peaksVariables, c("mz", "intensity"))

expect_error(selectSpectraVariables(be, c("dataStorage", "msLevel")),
"scanIndex is/are missing")
})
Expand Down Expand Up @@ -559,6 +567,7 @@ test_that("dropNaSpectraVariables works with MsBackendMzR", {
expect_equal(mz(res[1]), mz(sciex_mzr[1]))
expect_true(length(spectraVariables(res)) <
length(spectraVariables(sciex_mzr)))
expect_equal(res@peaksVariables, sciex_mzr@peaksVariables)
})

test_that("supportsSetBackend,MsBackendMzR", {
Expand Down

0 comments on commit d0ea0b1

Please sign in to comment.