Skip to content

Commit

Permalink
feat: creation of new generic function so save XcmsExperiment object
Browse files Browse the repository at this point in the history
  • Loading branch information
philouail committed Oct 26, 2023
1 parent 05d5bea commit 66fd4aa
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 11 deletions.
45 changes: 45 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,51 @@ setGeneric("stitch", function(object, lockMass, ...) standardGeneric("stitch"))
setGeneric("stitch.xml", function(object, lockMass) standardGeneric("stitch.xml"))
setGeneric("stitch.netCDF", function(object, lockMass) standardGeneric("stitch.netCDF"))
setGeneric("stitch.netCDF.new", function(object, lockMass) standardGeneric("stitch.netCDF.new"))

#' @title Save Xcms objects in a specified format
#'
#' @description
#'
#' The `storeResults` function saves an `object` resulting from processing with
#' the `xcms` package (mainly `XcmsExperiment`). Multiple formats for storing
#' and exporting are available and can be defined by the `param` argument.
#'
#' Supported `param` objects are:
#'
#' - `RDataParam`: Save in an .RData format file.
#'
#' - `PlainTextParam`: Save in a plain text format (to be defined).
#'
#' - `MzTabMParam`: Save in MzTab format (to be defined).
#'
#' For specific examples, see the help pages of the individual parameter classes
#' listed above.
#'
#' @param object The data object that needs to be saved.
#'
#' @param param The parameter object selecting and configuring the format for
#' saving. It can be one of the following classes: `RDataParam`,
#' `PlainTextParam`, or `MzTabMParam`.
#'
#' @param ... Optional parameters.
#'
#' @name storeResults
#'
#' @author Philippine Louail, Johannes Rainer
#'
#' @example
#'
#' ## Load a test `XcmsExperiment` object
#' x <-
#'
#' Set up parameter to save as .RData file
#' param <- RDataParam(fileName = "example_xcms_results")
#'
#' save as .RData
#' storeResults(object = x, param = param)
#'
#' @md
setGeneric("storeResults", function(object, param, ...) standardGeneric("storeResults"))
setGeneric("subset<-", function(object, value) standardGeneric("subset<-"))
setGeneric("subsetAdjust", function(object, ...) standardGeneric("subsetAdjust"))
setGeneric("subsetAdjust<-", function(object, value) standardGeneric("subsetAdjust<-"))
Expand Down
2 changes: 2 additions & 0 deletions R/PlainTextParam.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#'
#'PlainTextParam
70 changes: 70 additions & 0 deletions R/RDataParam.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#' @title Store `XcmsExperiment` object as .RData file
#'
#' @name RDataParam
#'
#' @export
#'
#' @description
#' The `RDataParam` class and method allow users to save an `XcmsExperiment`
#' object as an .RData file with a chosen filename. This new `param` class and
#' method are part of the possible dispatch of the generic function
#' `storeResults`.
#'
#' Other available `param` classes and linked methods include:
#'
#' - `PlainTextParam`: ...
#'
#' - `MzTabMParam`: ...
#'
#' @param param A parameter defining the format in which the object should be
#' saved. For the `RDataParam`, it has one slot for the `fileName` in which the
#' object is going to be saved. The default will be `tempfile()`.
#'
#' @param object An object of class `XcmsExperiment` that will be saved as an
#' .RData file.
#'
#' @return The saved object as an .RData file.
#'
#' @author Philippine Louail
#'
#' @examples
#'
#'## Get an `XcmsExperiment` object
#' x <- ...
#'
#' ## Define param
#' param <- RDataParam(fileName = "example_xcms_object")
#'
#' ## Save as RData
#' storeResults(object = x, param = param)
#'
NULL

#' @noRd
setClass("RDataParam",
slots = c(fileName = "character"),
contains = "Param",
prototype = prototype(
fileName = character()),
validity = function(object) {
msg <- NULL
if (length(object@fileName) != 1)
msg <- c("'fileName' has to be a character string of length 1")
msg
})

#' @rdname RDataParam
#'
#' @export
RDataParam <- function(fileName = tempfile()) {
new("RDataParam", fileName = fileName)
}

#' @rdname RDataParam
setMethod("storeResults",
signature(object = "XcmsExperiment",
param = "RDataParam"),
function(object, param){
save(object, file = param@fileName)
}
)
11 changes: 11 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ pest_dda <- findChromPeaks(pest_dda, param = cwp)
## sciex_data <- readMSData(fl, mode = "onDisk")
## sciex_data <- pickPeaks(sciex_data)

library(MsExperiment)
fls <- normalizePath(faahko_3_files)
df <- data.frame(mzML_file = basename(fls),
dataOrigin = fls,
sample = c("ko15", "ko16", "ko18"))
mse <- readMsExperiment(spectraFiles = fls, sampleData = df)
p <- CentWaveParam(noise = 10000, snthresh = 40, prefilter = c(3, 10000))
xmse <- findChromPeaks(mse, param = p)
pdp <- PeakDensityParam(sampleGroups = rep(1, 3))
xmseg <- groupChromPeaks(xmse, param = pdp, add = FALSE)

test_check("xcms")

bpstop(prm)
19 changes: 8 additions & 11 deletions tests/testthat/test_XcmsExperiment.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
library(MsExperiment)
fls <- normalizePath(faahko_3_files)
df <- data.frame(mzML_file = basename(fls),
dataOrigin = fls,
sample = c("ko15", "ko16", "ko18"))
mse <- readMsExperiment(spectraFiles = fls, sampleData = df)
p <- CentWaveParam(noise = 10000, snthresh = 40, prefilter = c(3, 10000))
xmse <- findChromPeaks(mse, param = p)
pdp <- PeakDensityParam(sampleGroups = rep(1, 3))
xmseg <- groupChromPeaks(xmse, param = pdp, add = FALSE)

fl <- system.file("TripleTOF-SWATH", "PestMix1_SWATH.mzML", package = "msdata")
mse_dia <- readMsExperiment(fl)

Expand Down Expand Up @@ -1326,3 +1315,11 @@ test_that("setAs,XcmsExperiment,xcmsSet works", {
expect_s4_class(res, "xcmsSet")
expect_equal(peaks(res), chromPeaks(xmseg))
})

test_that("storeResults,RDataParam works", {
param <- RDataParam(fileName ="test")
param2 <- RDataParam()
expect_false(is.null(param2))
storeResults(xmse, param = param)
expect_true(file.exists("test"))
})

0 comments on commit 66fd4aa

Please sign in to comment.