Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: creation of new generic function so save XcmsExperiment object #698

Merged
merged 8 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
philouail marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @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).
philouail marked this conversation as resolved.
Show resolved Hide resolved
#'
#' - `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
philouail marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @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
#'
philouail marked this conversation as resolved.
Show resolved Hide resolved
#' @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 <- ...
philouail marked this conversation as resolved.
Show resolved Hide resolved
#'
#' ## 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)
21 changes: 9 additions & 12 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 @@ -633,7 +622,7 @@ test_that(".xmse_filter_peaks_intensities works", {
res <- .xmse_filter_peaks_intensities(xmse, nValues = 1, threshold = 50000)
expect_equal(res, unname(chromPeaks(xmse)[, "maxo"] >= 50000))

res <- .xmse_filter_peaks_intensities(xmse, nValues = 1, , msLeve = 2L)
res <- .xmse_filter_peaks_intensities(xmse, nValues = 1, , msLevel = 2L)
expect_true(length(res) == 0)
philouail marked this conversation as resolved.
Show resolved Hide resolved
philouail marked this conversation as resolved.
Show resolved Hide resolved
})

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"))
})
Loading