Skip to content

Commit

Permalink
Adds asDataFrame() function (close #325)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgatto committed Jul 16, 2024
1 parent 4a7522e commit 4ad59d1
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ BugReports: https://github.com/RforMassSpectrometry/Spectra/issues
URL: https://github.com/RforMassSpectrometry/Spectra
biocViews: Infrastructure, Proteomics, MassSpectrometry, Metabolomics
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Roxygen: list(markdown=TRUE)
Collate:
'hidden_aliases.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(MsBackendMemory)
export(MsBackendMzR)
export(PrecursorMzParam)
export(applyProcessing)
export(asDataFrame)
export(chunkapply)
export(combinePeaksData)
export(combineSpectra)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*MetaboCoreUtils* directly through their namespace (`MsCoreUtils::`) to avoid
errors if performed in parallel on Windows machines or if called on a
re-loaded object.
- New `asDataFrame()` function to convert a (small) `Spectra` object
into a long `DataFrame`.

## Changes in 1.15.2

Expand Down
25 changes: 24 additions & 1 deletion R/Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,9 @@ NULL
#' window left and right of a peak where to remove fourier transform
#' artefacts.
#'
#' @param i For `[`: `integer`, `logical` or `character` to subset the object.
#' @param i For `[`: `integer`, `logical` or `character` to subset the
#' object. For `asDataFrame()` an `numeric` indicating which scans to coerce
#' to a `DataFrame` (default is `seq_along(object)`).
#'
#' @param j For `[`: not supported.
#'
Expand Down Expand Up @@ -2831,3 +2833,24 @@ setReplaceMethod("dataStorageBasePath", "Spectra", function(object, value) {
dataStorageBasePath(object@backend) <- value
object
})

#' @export
#' @rdname Spectra
#'
#' @param spectraVars `character()` indicating what spectra variables to add to
#' the `DataFrame`. Default is `spectraVariables(object)`, i.e. all
#' available variables.
#'
#' @examples
#'
#' ## Convert a subset of the Spectra object to a long DataFrame.
#' asDataFrame(sciex, i = 1:3, spectraVars = c("rtime", "msLevel"))
asDataFrame <- function(object, i = seq_along(object),
spectraVars = spectraVariables(object)) {
stopifnot(inherits(object, "Spectra"))
object <- object[i]
n <- sapply(peaksData(object), nrow)
v <- spectraData(object)[rep(seq_along(object), n), spectraVars]
p <- do.call(rbind, as.list(peaksData(object)))
cbind(p, v)
}
18 changes: 17 additions & 1 deletion man/Spectra.Rd

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

21 changes: 21 additions & 0 deletions tests/testthat/test_Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -1908,3 +1908,24 @@ test_that("dataStorageBasePath,dataStorageBasePath<-,MsBackendMzR works", {
#' errors
expect_error(dataStorageBasePath(tmp) <- "some path", "Provided path")
})


test_that("asDataFrame works", {
sciex_file <- normalizePath(
dir(system.file("sciex", package = "msdata"), full.names = TRUE))
sp <- Spectra(sciex_file)
## Full dataframe
df <- asDataFrame(sp)
expect_identical(nrow(df), sum(sapply(peaksData(sp), nrow)))
expect_identical(ncol(df), length(spectraVariables(sp)) + 2L)
expect_identical(names(df), c("mz", "intensity", spectraVariables(sp)))
## Three first scans and 2 spectra variables
df <- asDataFrame(sp, i = 1:3, spectraVars = c("msLevel", "rtime"))
expect_identical(nrow(df), sum(sapply(peaksData(sp[1:3]), nrow)))
expect_identical(ncol(df), 2L + 2L)
## Three first scans and no spectra variables
df <- asDataFrame(sp, i = 1:3, spectraVars = NULL)
expect_identical(nrow(df), sum(sapply(peaksData(sp[1:3]), nrow)))
expect_identical(ncol(df), 2L)
expect_identical(names(df), c("mz", "intensity"))
})

0 comments on commit 4ad59d1

Please sign in to comment.