Skip to content

Commit

Permalink
add: method for plaintextparam
Browse files Browse the repository at this point in the history
  • Loading branch information
philouail committed May 15, 2024
1 parent 729bac0 commit 08fd5b2
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 58 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ importFrom("stats", "aov", "approx", "convolve", "cor", "deriv3",
"rnorm", "runif", "dbeta", "resid")
importFrom("utils", "flush.console", "head", "object.size",
"packageVersion", "read.csv", "tail", "write.csv",
"write.table", "capture.output", "data")
"write.table", "capture.output", "data", "read.table")

importFrom("MassSpecWavelet", "peakDetectionCWT", "tuneInPeakInfo")

Expand Down
3 changes: 0 additions & 3 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -1496,9 +1496,6 @@ setGeneric("levelplot", function(x, data, ...) standardGeneric("levelplot"))
#' For specific examples, see the help pages of the individual parameter classes
#' listed above.
#'
#' @param file `MsExperiment` or `XcmsExperiment` 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 `MzTabParam`.
Expand Down
119 changes: 86 additions & 33 deletions R/PlainTextParam.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@
#' are going to be stored/ should be loaded from. The default will be
#' `tempdir()`.
#'
#' @param 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.
#'
#' @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.
#'
#' @inheritParams storeResults
#'
#' @return for `PlainTextParam`: a `PlainTextParam` class. `storeResults` does
Expand All @@ -70,6 +80,12 @@
#'
#' @importFrom jsonlite serializeJSON write_json unserializeJSON read_json
#'
#' @importFrom utils read.table write.table
#'
#' @importFrom MsExperiment MsExperiment readMsExperiment
#'
#' @importFrom MsCoreUtils common_path
#'
#' @examples
#' ## Load test data set of class `MsExperiment`
#' library(MsExperiment)
Expand Down Expand Up @@ -105,10 +121,14 @@ NULL

#' @noRd
setClass("PlainTextParam",
slots = c(path = "character"),
slots = c(path = "character",
spectraExport = "logical",
spectraFilePath = "character"),
contains = "Param",
prototype = prototype(
path = character()),
path = character(),
spectraExport = logical(),
spectraFilePath = character()),
validity = function(object) {
msg <- NULL
if (length(object@path) != 1)
Expand All @@ -119,8 +139,11 @@ setClass("PlainTextParam",
#' @rdname PlainTextParam
#'
#' @export
PlainTextParam <- function(path = tempdir()) {
new("PlainTextParam", path = path)
PlainTextParam <- function(path = tempdir(), spectraExport = FALSE,
spectraFilePath = character()) { # i'm not sure what to put
new("PlainTextParam", path = path,
spectraExport = spectraExport,
spectraFilePath = spectraFilePath)
}

### methods
Expand All @@ -132,7 +155,8 @@ setMethod("storeResults",
dir.create(path = param@path,
recursive = TRUE,
showWarnings = TRUE)
.store_msexperiment(x = object, path = param@path)
.store_msexperiment(x = object, path = param@path,
spectraExport = param@spectraExport)
}
)

Expand All @@ -142,46 +166,57 @@ setMethod("storeResults",
param = "PlainTextParam"),
function(object, param){
callNextMethod()
.store_xcmsexperiment(x = object, path = param@path)
.store_xcmsexperiment(x = object, path = param@path,
spectraExport = param@spectraExport)
}
)

#' @rdname PlainTextParam
setMethod("loadResults",
signature(param = "PlainTextParam"),
function(param){
res <- .load_msexperiment(path = param@path)
fl <- file.path(path, "chrom_peaks.txt")
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)
res <- .load_xcmsexperiment(res, path = param@path,
spectraExport = param@spectraExport)
validObject(res)
res
}
)

#' @noRd
.store_msexperiment <- function(x, path = tempdir()) {
.store_msexperiment <- function(x, path = tempdir(),
spectraExport = logical()) {
.export_sample_data(as.data.frame(sampleData(x)),
file.path(path, "sample_data.txt"))
.export_spectra_files(x, path = path)
.export_spectra_processing_queue(spectra(x), path = path)
if (spectraExport == TRUE){
.export_spectra_files(x, path = path)
.export_spectra_processing_queue(spectra(x), path = path)
}
}

#' @noRd
.load_msexperiment <- function(path = character()) {
.load_msexperiment <- function(path = character(), spectraExport = logical(),
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)){
sf <- .import_spectra_files(fl)
if (file.exists(fl) && spectraExport == TRUE){
sf <- .import_spectra_files(fl, spectraFilePath = spectraFilePath)
res <- readMsExperiment(spectraFiles = sf, sampleData = sd)
fl <- file.path(path, "spectra_processing_queue.json")
if (file.exists(fl))
res <- .import_processing_queue(res, fl)
} else stop("No \"spectra_files.txt\" file found in ", path, "Spectra ",
"data will not be restored")
res <- .import_spectra_processing_queue(res, fl)
} else {
res <- MsExperiment(sampleData = sd)
warning("Spectra data will not be restored")
}
res
}

#' Sample data
Expand All @@ -192,7 +227,9 @@ setMethod("loadResults",

#' @noRd
.import_sample_data <- function(file = character()) {
read.table(file)
x <- read.table(file)
rownames(x) <- NULL #read.table force numbering of rownames
x
}

#' Spectra file
Expand All @@ -203,15 +240,27 @@ setMethod("loadResults",
warning("Spectra data will not be exported, backend need to be of ",
"class 'MsBackendMzR'")
else {
fls <- fileNames(x)
write.table(fls, file = file.path(path, "spectra_files.txt"),
row.names = FALSE, col.names = FALSE)
pth <- MsCoreUtils::common_path(fileNames(x))
if (nchar(pth) > 0)
pth <- paste0(pth, "/")
fnames <- gsub("\\\\", "/", fileNames(x)) # to fix error with windows
fnames <- sub(pth, "", fixed = TRUE, fnames)
con <- file(file.path(path, "spectra_files.txt"), open = "wt")
on.exit(close(con))
writeLines(paste0("spectraFilePath = ", pth), con = con)
writeLines(fnames, con = con)
}
}

#' @noRd
.import_spectra_files <- function(file = character()) {
as.character(read.table(file)[, 1L])
.import_spectra_files <- function(file = character(),
spectraFilePath = character()) {
if (!length(spectraFilePath) > 0){
spectraFilePath <- readLines(file, n = 1L)
spectraFilePath <- sub("spectraFilePath = ", "", spectraFilePath)
}
fls <- readLines(file, n = -1L)[-1]
fls <- paste0(spectraFilePath, fls)
}

#' Processing queue
Expand All @@ -232,30 +281,34 @@ setMethod("loadResults",
}

#' @noRd
.store_xcmsexperiment <- function(x, path = tempdir()) {
.store_xcmsexperiment <- function(x, path = tempdir(),
spectraExport = logical()) {
.export_process_history(x, path = path)
if (hasChromPeaks(x))
.export_chrom_peaks(x, path)
if (hasAdjustedRtime(x))
if (hasAdjustedRtime(x) && spectraExport == TRUE)
.export_adjusted_rtime(x, path)
if (hasFeatures(x))
.export_features(x, path)
}

#' @noRd
.load_xcmsexperiment <- function(x, path = character()){
res <- as(res, "XcmsExperiment")
res <- .import_chrom_peaks(res, path)
.load_xcmsexperiment <- function(x, path = character(),
spectraExport = logical()){
x <- as(x, "XcmsExperiment")
fl <- file.path(path, "chrom_peaks.txt")
x <- .import_chrom_peaks(x, path)
fl <- file.path(path, "process_history.json")
if (file.exists(fl))
res <- .import_process_history(res, fl)
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))
res <- .import_adjusted_rtime(res, fl)
if (file.exists(fl) && spectraExport == TRUE)
x <- .import_adjusted_rtime(x, fl)
fl <- file.path(path, "feature_definitions.txt")
if (file.exists(fl))
res <- .import_features(res, path)
x <- .import_features(x, path)
x
}

#' Processing history
Expand Down
4 changes: 3 additions & 1 deletion R/XcmsExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,9 @@ setMethod("dropAdjustedRtime", "XcmsExperiment", function(object) {

#' @rdname XcmsExperiment
setMethod("hasAdjustedRtime", "MsExperiment", function(object) {
any(spectraVariables(spectra(object)) == "rtime_adjusted")
if (length(spectra(object)))
any(spectraVariables(spectra(object)) == "rtime_adjusted")
else FALSE
})

#' @rdname XcmsExperiment
Expand Down
20 changes: 17 additions & 3 deletions man/PlainTextParam.Rd

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

3 changes: 0 additions & 3 deletions man/loadResults.Rd

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

53 changes: 39 additions & 14 deletions tests/testthat/test_PlainTextParam.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ xmse_full <- loadXcmsData("xmse")

test_that("storeResults,PlainTextParam,MsExperiment works", {
pth <- file.path(tempdir(), "test")
param <- PlainTextParam(path = pth)
param <- PlainTextParam(path = pth, spectraExport = TRUE)
param2 <- PlainTextParam()
expect_false(is.null(param2))
expect_error(new("PlainTextParam", path = c(tempdir(), tempdir())))
Expand All @@ -16,10 +16,10 @@ test_that("storeResults,PlainTextParam,MsExperiment works", {

test_that("storeResults,PlainTextParam,XcmsExperiment works", {
pth = file.path(tempdir(), "test")
param <- PlainTextParam(path = pth)
param <- PlainTextParam(path = pth, spectraExport = TRUE)
param2 <- PlainTextParam()
expect_false(is.null(param2))
mse <- filterMzRange(xmse_full, c(200, 500))
xmse_full <- filterMzRange(xmse_full, c(200, 500))
storeResults(xmse_full, param = param)
expect_true(dir.exists(pth))
expect_true(file.exists(file.path(param@path, "sample_data.txt")))
Expand All @@ -31,17 +31,42 @@ test_that("storeResults,PlainTextParam,XcmsExperiment works", {
expect_true(file.exists(file.path(param@path, "rtime_adjusted.txt")))
expect_true(file.exists(file.path(param@path, "feature_definitions.txt")))
expect_true(file.exists(file.path(param@path, "feature_peak_index.txt")))
pth = file.path(tempdir(), "test2")
param <- PlainTextParam(path = pth, spectraExport = FALSE)
storeResults(xmse_full, param = param)
expect_false(file.exists(file.path(param@path, "spectra_files.txt")))
})

test_that("loadResults, PlainTextParam works", {
pth = file.path(tempdir(), "test")
param <- PlainTextParam(path = pth)
storeResults(xmse_full, param = param)
load_xmse <- loadResults(param)
expect_true(inherits(load_xmse, "XcmsExperiment"))
expect_equal(xmse_full, load_xmse)
expect_equal(processHistory(xmse_full), processHistory(load_xmse))
expect_equal(xmse_full@featureDefinitions,
load_xmse@featureDefinitions)
expect_equal(adjustedRtime(xmse_full), adjustedRtime(load_xmse))
})
## test for MsExperiment object only
## no spectra
pth = file.path(tempdir(), "test3")
param <- PlainTextParam(path = pth, spectraExport = FALSE)
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. 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)
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)
storeResults(xmse_full, param = param)
load_xmse <- loadResults(param)
expect_true(inherits(load_xmse, "XcmsExperiment"))
expect_equal(xmse_full, load_xmse)
expect_equal(processHistory(xmse_full), processHistory(load_xmse))
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)
})

0 comments on commit 08fd5b2

Please sign in to comment.