From 5ea7b2a95962eda746f410ac22e852272c330ee8 Mon Sep 17 00:00:00 2001 From: Philippine Louail Date: Thu, 23 May 2024 14:27:24 +0200 Subject: [PATCH] =?UTF-8?q?addition=20of=20=C3=B2bject=20=3D`=20and=20othe?= =?UTF-8?q?r=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NAMESPACE | 2 +- R/AllGenerics.R | 17 ++++--- R/PlainTextParam.R | 76 ++++++++++++++-------------- R/RDataParam.R | 23 +++++++-- man/PlainTextParam.Rd | 35 +++++++------ man/RDataParam.Rd | 12 +++-- man/loadResults.Rd | 17 ++++--- tests/testthat/test_PlainTextParam.R | 23 +++------ tests/testthat/test_XcmsExperiment.R | 9 ++-- 9 files changed, 116 insertions(+), 98 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index d039123b5..28c6e7c1c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -566,7 +566,7 @@ importMethodsFrom("Spectra", "precursorMz") importMethodsFrom("Spectra", "$") importMethodsFrom("Spectra", "uniqueMsLevels") importMethodsFrom("Spectra", "backendBpparam") -importFrom("Spectra", "MsBackendMemory") +importFrom("Spectra", "MsBackendMemory", "dataStorageBasePath") ## MsExperiment things importClassesFrom("MsExperiment", "MsExperiment") diff --git a/R/AllGenerics.R b/R/AllGenerics.R index 888892d4a..16b850e99 100644 --- a/R/AllGenerics.R +++ b/R/AllGenerics.R @@ -1486,16 +1486,21 @@ setGeneric("levelplot", function(x, data, ...) standardGeneric("levelplot")) #' #' Supported `param` objects are: #' -#' - [`RDataParam`]: Import an .RData format file.TBD +#' - [`RDataParam`]: Import an `MsExperiment` or `XcmsExperiment` object from +#' an .RData file. #' -#' - [`PlainTextParam`]: Import/create an `XcmsExperiment` object from a folder -#' of text files. +#' - [`PlainTextParam`]: Import an `MsExperiment` or `XcmsExperiment` object +#' from a folder of text files. #' #' - `MzTabParam`: Load a MzTab-m file (to be defined). #' #' For specific examples, see the help pages of the individual parameter classes #' listed above. #' +#' @param object Define the class of the object to be imported. It can be one +#' of the following classes: [`XcmsExperiment()`], [`MsExperiment()`] for now, +#' more class will be supported in the future. +#' #' @param param The parameter object selecting and configuring the format for #' saving. It can be one of the following classes: [`RDataParam`], #' [`PlainTextParam`], or `MzTabParam`. @@ -1519,7 +1524,7 @@ setGeneric("levelplot", function(x, data, ...) standardGeneric("levelplot")) #' storeResults(object = faahko_sub, param = param) #' #' ## Load this saved dataset -#' xcmse <- loadResults(param = param) +#' xcmse <- loadResults(object = XcmsExperiment(),param = param) #' #' ## Save as a collection of plain text files #' pth = file.path(tempdir(), "test") @@ -1527,10 +1532,10 @@ setGeneric("levelplot", function(x, data, ...) standardGeneric("levelplot")) #' storeResults(object = faahko_sub, param = param) #' #' ## Load this saved dataset -#' faahko_load <- loadResults(param = param) +#' faahko_load <- loadResults(object= XcmsExperiment(), param = param) #' #' @md -setGeneric("loadResults", function(param, ...) standardGeneric("loadResults")) +setGeneric("loadResults", function(object, param,...) standardGeneric("loadResults")) setGeneric("localAlignment", function(object) standardGeneric("localAlignment")) setGeneric("localAlignment<-", function(object, value) standardGeneric("localAlignment<-")) setGeneric("loadRaw", function(object, ...) standardGeneric("loadRaw")) diff --git a/R/PlainTextParam.R b/R/PlainTextParam.R index 5545860f9..1769785d6 100644 --- a/R/PlainTextParam.R +++ b/R/PlainTextParam.R @@ -73,10 +73,10 @@ #' `spectraExport = TRUE`, the import should be done in a file system similar #' to the one used for the export. #' -#' @param spectraFilePath for `PlainTextParam` `character(1)`, defining the -#' absolute path where the spectra files should be exported to when storing the -#' object or imported from when loading the object. The default will be set -#' using the common file path of all the spectra files when exporting. +#' @param spectraFilePath for `loadResults` `character(1)`, defining the +#' absolute path where the spectra files should be imported from when loading +#' the object. The default will be set using the common file path of all the +#' spectra files when exporting. #' #' @inheritParams storeResults #' @@ -123,20 +123,16 @@ #' storeResults(object = faahko_sub, param = param) #' #' ## Load this saved dataset -#' faahko_load <- loadResults(param = param) +#' faahko_load <- loadResults(object = XcmsExperiment(), param = param) #' NULL #' @noRd setClass("PlainTextParam", - slots = c(path = "character", - spectraExport = "logical", - spectraFilePath = "character"), + slots = c(path = "character"), contains = "Param", prototype = prototype( - path = character(), - spectraExport = logical(), - spectraFilePath = character()), + path = character()), validity = function(object) { msg <- NULL if (length(object@path) != 1) @@ -147,11 +143,8 @@ setClass("PlainTextParam", #' @rdname PlainTextParam #' #' @export -PlainTextParam <- function(path = tempdir(), spectraExport = FALSE, - spectraFilePath = character()) { # i'm not sure what to put - new("PlainTextParam", path = path, - spectraExport = spectraExport, - spectraFilePath = spectraFilePath) +PlainTextParam <- function(path = tempdir()) { + new("PlainTextParam", path = path) } ### methods @@ -163,8 +156,7 @@ setMethod("storeResults", dir.create(path = param@path, recursive = TRUE, showWarnings = TRUE) - .store_msexperiment(x = object, path = param@path, - spectraExport = param@spectraExport) + .store_msexperiment(x = object, path = param@path) } ) @@ -174,47 +166,53 @@ setMethod("storeResults", param = "PlainTextParam"), function(object, param){ callNextMethod() - .store_xcmsexperiment(x = object, path = param@path, - spectraExport = param@spectraExport) + .store_xcmsexperiment(x = object, path = param@path) } ) #' @rdname PlainTextParam setMethod("loadResults", - signature(param = "PlainTextParam"), - function(param){ + signature(object = "MsExperiment", + param = "PlainTextParam"), + function(object, param, spectraFilePath){ res <- .load_msexperiment(path = param@path, - spectraExport = param@spectraExport, - spectraFilePath = param@spectraFilePath) - fl <- file.path(param@path, "chrom_peaks.txt") - if (file.exists(fl)) - res <- .load_xcmsexperiment(res, path = param@path, - spectraExport = param@spectraExport) + spectraFilePath = spectraFilePath) validObject(res) res } ) +#' @rdname PlainTextParam +setMethod("loadResults", + signature(object = "XcmsExperiment", + param = "PlainTextParam"), + function(object, param, spectraFilePath){ + res <- callNextMethod() + res <- .load_xcmsexperiment(res, path = param@path, + spectraFilePath = spectraFilePath) # not sure if the spectraFilePath is necessary here + validObject(res) + res + } +) + #' @noRd -.store_msexperiment <- function(x, path = tempdir(), - spectraExport = logical()) { +.store_msexperiment <- function(x, path = tempdir()) { .export_sample_data(as.data.frame(sampleData(x)), file.path(path, "sample_data.txt")) - if (spectraExport == TRUE){ - .export_spectra_files(x, path = path) - .export_spectra_processing_queue(spectra(x), path = path) - } + .export_spectra_files(x, path = path) + .export_spectra_processing_queue(spectra(x), path = path) } #' @noRd -.load_msexperiment <- function(path = character(), spectraExport = logical(), +.load_msexperiment <- function(path = character(), spectraFilePath = character()) { fl <- file.path(path, "sample_data.txt") if (file.exists(fl)) sd <- .import_sample_data(fl) else stop("No \"sample_data.txt\" file found in ", path) fl <- file.path(path, "spectra_files.txt") - if (file.exists(fl) && spectraExport == TRUE){ + if (file.exists(fl)){ + spec <- spectra(fl) sf <- .import_spectra_files(fl, spectraFilePath = spectraFilePath) res <- readMsExperiment(spectraFiles = sf, sampleData = sd) fl <- file.path(path, "spectra_processing_queue.json") @@ -251,7 +249,7 @@ setMethod("loadResults", pth <- MsCoreUtils::common_path(fileNames(x)) if (nchar(pth) > 0) pth <- paste0(pth, "/") - fnames <- gsub("\\\\", "/", fileNames(x)) # to fix error with windows + fnames <- gsub("\\\\", "/", fileNames(x)) fnames <- sub(pth, "", fixed = TRUE, fnames) con <- file(file.path(path, "spectra_files.txt"), open = "wt") on.exit(close(con)) @@ -294,7 +292,7 @@ setMethod("loadResults", .export_process_history(x, path = path) if (hasChromPeaks(x)) .export_chrom_peaks(x, path) - if (hasAdjustedRtime(x) && spectraExport == TRUE) + if (hasAdjustedRtime(x)) .export_adjusted_rtime(x, path) if (hasFeatures(x)) .export_features(x, path) @@ -311,7 +309,7 @@ setMethod("loadResults", x <- .import_process_history(x, fl) else stop("No \"process_history.json\" file found in ", path) fl <- file.path(path, "rtime_adjusted.txt") - if (file.exists(fl) && spectraExport == TRUE) + if (file.exists(fl)) x <- .import_adjusted_rtime(x, fl) fl <- file.path(path, "feature_definitions.txt") if (file.exists(fl)) diff --git a/R/RDataParam.R b/R/RDataParam.R index eaaa49b2b..8d3a99a96 100644 --- a/R/RDataParam.R +++ b/R/RDataParam.R @@ -17,8 +17,18 @@ #' @param fileName for `RDataParam` `character(1)`, defining the file name. The #' default will be `tempfile()`. #' +#' @param spectraFilePath for `loadResults` `character(1)`, defining the +#' absolute path where the spectra files should be imported from when loading +#' the object. The default will be set using the common file path of all the +#' spectra files when exporting. This is only supported if the backend of the +#' object loaded is `MsBackendMzr()` +#' #' @inheritParams storeResults #' +#' @inheritParams loadResults +#' +#' @importFrom Spectra dataStorageBasePath +#' #' @return for `RDataParam`: a `RDataParam` class. `storeResults` does not #' return anything but saves the object to a RData file. `loadResults` returns #' an object of class `XcmsExperiment` @@ -37,7 +47,7 @@ #' storeResults(object = faahko_sub, param = param) #' #' ## Load this saved dataset -#' xcmse <- loadResults(param = param) +#' xcmse <- loadResults(object = XcmsExperiment(), param = param) #' NULL @@ -72,9 +82,14 @@ setMethod("storeResults", #' @rdname RDataParam setMethod("loadResults", - signature(param = "RDataParam"), - function(param){ - load(file = param@fileName) + signature(object = "XcmsExperiment", + param = "RDataParam"), + function(object, param, spectraFilePath){ + res <- load(file = param@fileName) + if (!length(spectraFilePath) == 0 && + inherits(s@backend, "MsBackendMzR")) + dataStorageBasePath(spectra(res)) <- spectraFilePath + res } ) diff --git a/man/PlainTextParam.Rd b/man/PlainTextParam.Rd index e98c47f53..c06cdb398 100644 --- a/man/PlainTextParam.Rd +++ b/man/PlainTextParam.Rd @@ -4,43 +4,42 @@ \alias{PlainTextParam} \alias{storeResults,MsExperiment,PlainTextParam-method} \alias{storeResults,XcmsExperiment,PlainTextParam-method} -\alias{loadResults,PlainTextParam-method} +\alias{loadResults,MsExperiment,PlainTextParam-method} +\alias{loadResults,XcmsExperiment,PlainTextParam-method} \title{Store contents of `MsExperiment` and `XcmsExperiment` objects as plain text files} \usage{ -PlainTextParam( - path = tempdir(), - spectraExport = FALSE, - spectraFilePath = character() -) +PlainTextParam(path = tempdir()) \S4method{storeResults}{MsExperiment,PlainTextParam}(object, param) \S4method{storeResults}{XcmsExperiment,PlainTextParam}(object, param) -\S4method{loadResults}{PlainTextParam}(param) +\S4method{loadResults}{MsExperiment,PlainTextParam}(object, param, spectraFilePath) + +\S4method{loadResults}{XcmsExperiment,PlainTextParam}(object, param, spectraFilePath) } \arguments{ \item{path}{for `PlainTextParam` `character(1)`, defining where the files are going to be stored/ should be loaded from. The default will be `tempdir()`.} -\item{spectraExport}{for `PlainTextParam` `logical(1)`, defining whether the -spectra data should be exported/imported. The default is `FALSE`. If -`spectraExport = TRUE`, the import should be done in a file system similar -to the one used for the export.} - -\item{spectraFilePath}{for `PlainTextParam` `character(1)`, defining the -absolute path where the spectra files should be exported to when storing the -object or imported from when loading the object. The default will be set -using the common file path of all the spectra files when exporting.} - \item{object}{\code{MsExperiment} or \code{XcmsExperiment} The data object that needs to be saved.} \item{param}{The parameter object selecting and configuring the format for saving. It can be one of the following classes: \code{\link{RDataParam}}, \code{\link{PlainTextParam}}, or \code{MzTabMParam}.} + +\item{spectraFilePath}{for `loadResults` `character(1)`, defining the +absolute path where the spectra files should be imported from when loading +the object. The default will be set using the common file path of all the +spectra files when exporting.} + +\item{spectraExport}{for `PlainTextParam` `logical(1)`, defining whether the +spectra data should be exported/imported. The default is `FALSE`. If +`spectraExport = TRUE`, the import should be done in a file system similar +to the one used for the export.} } \value{ for `PlainTextParam`: a `PlainTextParam` class. `storeResults` does @@ -132,7 +131,7 @@ param <- PlainTextParam(path = pth, spectraExport = TRUE) storeResults(object = faahko_sub, param = param) ## Load this saved dataset -faahko_load <- loadResults(param = param) +faahko_load <- loadResults(object = XcmsExperiment(), param = param) } \seealso{ diff --git a/man/RDataParam.Rd b/man/RDataParam.Rd index a3830f0b8..d1fb809fe 100644 --- a/man/RDataParam.Rd +++ b/man/RDataParam.Rd @@ -3,14 +3,14 @@ \name{RDataParam} \alias{RDataParam} \alias{storeResults,XcmsExperiment,RDataParam-method} -\alias{loadResults,RDataParam-method} +\alias{loadResults,XcmsExperiment,RDataParam-method} \title{Store/Load an `XcmsExperiment` object as/from .RData file} \usage{ RDataParam(fileName = tempfile()) \S4method{storeResults}{XcmsExperiment,RDataParam}(object, param) -\S4method{loadResults}{RDataParam}(param) +\S4method{loadResults}{XcmsExperiment,RDataParam}(object, param, spectraFilePath) } \arguments{ \item{fileName}{for `RDataParam` `character(1)`, defining the file name. The @@ -22,6 +22,12 @@ to be saved.} \item{param}{The parameter object selecting and configuring the format for saving. It can be one of the following classes: \code{\link{RDataParam}}, \code{\link{PlainTextParam}}, or \code{MzTabMParam}.} + +\item{spectraFilePath}{for `loadResults` `character(1)`, defining the +absolute path where the spectra files should be imported from when loading +the object. The default will be set using the common file path of all the +spectra files when exporting. This is only supported if the backend of the +object loaded is `MsBackendMzr()`} } \value{ for `RDataParam`: a `RDataParam` class. `storeResults` does not @@ -48,7 +54,7 @@ param <- RDataParam(fileName = "example_xcms_object") storeResults(object = faahko_sub, param = param) ## Load this saved dataset -xcmse <- loadResults(param = param) +xcmse <- loadResults(object = XcmsExperiment(), param = param) } \seealso{ diff --git a/man/loadResults.Rd b/man/loadResults.Rd index 8e6e897a6..c63947488 100644 --- a/man/loadResults.Rd +++ b/man/loadResults.Rd @@ -4,9 +4,13 @@ \alias{loadResults} \title{Import various file format as XcmsExperiment object} \usage{ -loadResults(param, ...) +loadResults(object, param, ...) } \arguments{ +\item{object}{Define the class of the object to be imported. It can be one +of the following classes: \code{\link[=XcmsExperiment]{XcmsExperiment()}}, \code{\link[=MsExperiment]{MsExperiment()}} for now, +more class will be supported in the future.} + \item{param}{The parameter object selecting and configuring the format for saving. It can be one of the following classes: \code{\link{RDataParam}}, \code{\link{PlainTextParam}}, or \code{MzTabParam}.} @@ -20,9 +24,10 @@ defined by the \code{param} argument. Supported \code{param} objects are: \itemize{ -\item \code{\link{RDataParam}}: Import an .RData format file.TBD -\item \code{\link{PlainTextParam}}: Import/create an \code{XcmsExperiment} object from a folder -of text files. +\item \code{\link{RDataParam}}: Import an \code{MsExperiment} or \code{XcmsExperiment} object from +an .RData file. +\item \code{\link{PlainTextParam}}: Import an \code{MsExperiment} or \code{XcmsExperiment} object +from a folder of text files. \item \code{MzTabParam}: Load a MzTab-m file (to be defined). } @@ -42,7 +47,7 @@ param <- RDataParam(fileName = "example_xcms_object") storeResults(object = faahko_sub, param = param) ## Load this saved dataset -xcmse <- loadResults(param = param) +xcmse <- loadResults(object = XcmsExperiment(),param = param) ## Save as a collection of plain text files pth = file.path(tempdir(), "test") @@ -50,7 +55,7 @@ param <- PlainTextParam(path = pth, spectraExport = TRUE) storeResults(object = faahko_sub, param = param) ## Load this saved dataset -faahko_load <- loadResults(param = param) +faahko_load <- loadResults(object= XcmsExperiment(), param = param) } \author{ diff --git a/tests/testthat/test_PlainTextParam.R b/tests/testthat/test_PlainTextParam.R index f4c7c6356..86cb2652d 100644 --- a/tests/testthat/test_PlainTextParam.R +++ b/tests/testthat/test_PlainTextParam.R @@ -39,37 +39,26 @@ test_that("storeResults,PlainTextParam,XcmsExperiment works", { test_that("loadResults, PlainTextParam works", { ## test for MsExperiment object only - ## no spectra pth = file.path(tempdir(), "test3") - param <- PlainTextParam(path = pth, spectraExport = FALSE) + param <- PlainTextParam(path = pth) storeResults(mse, param = param) - load_mse <- loadResults(param) - expect_true(inherits(load_mse, "MsExperiment")) - expect_equal(mse, load_mse) # does nto work but make sense because no spectra data. ideas for other checks ? - expect_equal(sampleData(mse), sampleData(load_mse)) - #with spectra ? - param <- PlainTextParam(path = pth, spectraExport = TRUE) - storeResults(mse, param = param) - load_mse <- loadResults(param) + load_mse <- loadResults(object = MsExperiment(), param) expect_true(inherits(load_mse, "MsExperiment")) expect_equal(mse, load_mse) ## test for XcmsExperiment object - ## no spectra pth = file.path(tempdir(), "test4") - param <- PlainTextParam(path = pth, spectraExport = FALSE) + param <- PlainTextParam(path = pth) storeResults(xmse_full, param = param) - load_xmse <- loadResults(param) + load_xmse <- loadResults(object = XcmsExperiment(), param) expect_true(inherits(load_xmse, "XcmsExperiment")) - expect_equal(xmse_full, load_xmse) # also fail probably because of spectra missing + expect_equal(xmse_full, load_xmse) expect_equal(processHistory(xmse_full), processHistory(load_xmse)) #fail why ? expect_equal(xmse_full@featureDefinitions, load_xmse@featureDefinitions) expect_equal(adjustedRtime(xmse_full), adjustedRtime(load_xmse)) - param <- PlainTextParam(path = pth, spectraExport = TRUE) - storeResults(xmse_full, param = param) - load_xmse <- loadResults(param) expect_equal(xmse_full, load_xmse) # not sure how to check for `spectraFilePath` }) + diff --git a/tests/testthat/test_XcmsExperiment.R b/tests/testthat/test_XcmsExperiment.R index d538a7623..640aa5ea2 100644 --- a/tests/testthat/test_XcmsExperiment.R +++ b/tests/testthat/test_XcmsExperiment.R @@ -1413,15 +1413,16 @@ test_that("setAs,XcmsExperiment,xcmsSet works", { expect_equal(peaks(res), chromPeaks(xmseg)) }) -test_that("storeResults,RDataParam works", { +test_that("storeResults,loadResults, RDataParam works", { param <- RDataParam(fileName = "test") param2 <- RDataParam() expect_false(is.null(param2)) storeResults(xmse, param = param) expect_true(file.exists("test")) - load("test") - expect_s4_class(object, "XcmsExperiment") - expect_equal(object, xmse) + res <- loadResults(object = XcmsExperiment(), param, + spectraFilePath = character()) #not sure how to test for spectraFilePath not empty + expect_s4_class(res, "XcmsExperiment") + expect_equal(res, xmse) }) test_that("fillChromPeaks,XcmsExperiment works with verboseBetaColumns", {