Skip to content

Commit

Permalink
refactor: $,Spectra correctly returns peak variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed May 25, 2023
1 parent f0d13c6 commit dc02d60
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
variables (in addition to `"mz"` and `"intensity"`) are requested. For
`columns = c("mz", "intensity")` (the default) a `list` of `matrix` is
returned.
- `peaksData,Spectra` returns either a `matrix` or `data.frame` and ensures
the peak data is correctly subset based on the lazy evaluation processing
queue.
- `$,Spectra` to access peak variables ensures the lazy evaluation queue is
applied prior to extracting the values.

## Changes in 1.11.2

Expand Down
28 changes: 18 additions & 10 deletions R/Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,15 @@ NULL
#' spectra, each element a `numeric` vector with the m/z values of
#' one spectrum.
#'
#' - `peaksData`: gets the *peaks* matrices for all spectra in `object`. The
#' function returns a [SimpleList()] of matrices, each `matrix` with columns
#' `"mz"` and `"intensity"` with the m/z and intensity values for all peaks of
#' a spectrum. Optional parameter `columns` is passed to the backend's
#' `peaksData` function to allow selection of specific (or additional) peaks
#' variables (columns) that should be extracted (if available). Importantly,
#' - `peaksData`: gets the *peaks* data for all spectra in `object`. Peaks data
#' consist of the m/z and intensity values as well as possible additional
#' annotations (variables) of all peaks of each spectrum. The function
#' returns a [SimpleList()] of two dimensional arrays (either `matrix` or
#' `data.frame`), with each array providing the values for the requested
#' *peak variables* (by default `"mz"` and `"intensity"`). Optional parameter
#' `columns` is passed to the backend's `peaksData` function to allow
#' selection of specific (or additional) peaks variables (columns) that
#' should be extracted (if available). Importantly,
#' it is **not** guaranteed that each backend supports this parameter (while
#' each backend must support extraction of `"mz"` and `"intensity"` columns).
#' Parameter `columns` defaults to `c("mz", "intensity")` but any value
Expand All @@ -246,8 +249,8 @@ NULL
#' the backend. Default peak variables are `"mz"` and `"intensity"` (which
#' all backends need to support and provide), but some backends might provide
#' additional variables.
#' These variables correspond to the column names of the `numeric` `matrix`
#' representing the peak data (returned by `peaksData`).
#' These variables correspond to the column names of the peak data array
#' returned by `peaksData`.
#'
#' - `polarity`, `polarity<-`: gets or sets the polarity for each
#' spectrum. `polarity` returns an `integer` vector (length equal
Expand Down Expand Up @@ -1901,8 +1904,13 @@ setMethod("$", "Spectra", function(x, name) {
mz(x)
else if (name == "intensity")
intensity(x)
else
do.call("$", list(x@backend, name))
else {
if (length(x@processingQueue) && name %in% peaksVariables(x))
.peaksapply(x, FUN = function(z, ...) z[, name],
columns = c("mz", "intensity", name))
else
do.call("$", list(x@backend, name))
}
})

#' @rdname Spectra
Expand Down
19 changes: 11 additions & 8 deletions man/Spectra.Rd

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

2 changes: 2 additions & 0 deletions tests/testthat/test_Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ test_that("peaksData,Spectra works", {
expect_true(is.data.frame(res[[1L]]))
expect_equal(res[[1L]][, "ann"], c("b", "c", "d"))
expect_equal(colnames(res[[1L]]), c("ann", "mz"))

expect_equal(s$ann[[1L]], c("b", "c", "d"))
})

test_that("lengths,Spectra works", {
Expand Down

0 comments on commit dc02d60

Please sign in to comment.