Skip to content

Commit

Permalink
addition of òbject =` and other fix
Browse files Browse the repository at this point in the history
  • Loading branch information
philouail committed May 23, 2024
1 parent e753de4 commit 5ea7b2a
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 98 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 11 additions & 6 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -1519,18 +1524,18 @@ 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")
#' 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)
#'
#' @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"))
Expand Down
76 changes: 37 additions & 39 deletions R/PlainTextParam.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
}
)

Expand All @@ -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")
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down
23 changes: 19 additions & 4 deletions R/RDataParam.R
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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

Expand Down Expand Up @@ -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
}
)

35 changes: 17 additions & 18 deletions man/PlainTextParam.Rd

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

12 changes: 9 additions & 3 deletions man/RDataParam.Rd

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

Loading

0 comments on commit 5ea7b2a

Please sign in to comment.