diff --git a/DESCRIPTION b/DESCRIPTION index 78796aa8..df989c20 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -63,7 +63,7 @@ Imports: MsCoreUtils (>= 1.15.7), MsFeatures, MsExperiment (>= 1.5.4), - Spectra (>= 1.13.7), + Spectra (>= 1.15.3), progress, jsonlite, RColorBrewer, diff --git a/R/PlainTextParam.R b/R/PlainTextParam.R index acdd12eb..060450ac 100644 --- a/R/PlainTextParam.R +++ b/R/PlainTextParam.R @@ -1,4 +1,5 @@ #' @include XcmsExperiment.R +#' #' @title Store contents of `MsExperiment` and `XcmsExperiment` objects as #' plain text files #' @@ -9,76 +10,79 @@ #' @family xcms result export and import formats. #' #' @description -#' The `PlainTextParam` class and method enable users to save/load an -#' `MsExperiment` or `XcmsExperiment` object as a collections of plain text -#' files in/from a specified folder. -#' Note that, while for all xcms results within the `XcmsExperiment` can -#' and will be exported, the full raw MS data (of the object's `Spectra` object) -#' will currently not be exported in plain text format. For `Spectra` using the -#' [MsBackendMzR()] backend, the names of the raw data files will however be -#' exported (which enables to *load* the full `Spectra` respectively -#' `MsExperiment` objects). -#' -#' For an `MsExperiment` object, the exported files include: #' -#' - The [sampleData()] stored as a text file named *sample_data.txt*. +#' The `storeResults()` and `loadResults()` methods with the `PlainTextParam` +#' option enable users to save/load an `MsExperiment` or `XcmsExperiment` +#' object as a collections of plain text files in/from a specified folder. This +#' folder, defined with the `path` parameter, will be created by the +#' `storeResults()` function. Any previous exports eventually present in that +#' folder will be overwritten. #' -#' For a `Spectra` object, the exported files include: +#' For an `MsExperiment` object, the exported files, stored into the directory +#' specifyied with the `path` parameter, include: #' -#' - The [spectraData()] stored in a tabular format in a text file named -#' *backend_data.txt*. -#' -#' - The `processingQueueVariables`, `processing`, [processingChunkSize()] and -#' `backend` class information of the object stored in a text file named -#' *spectra_slots.txt*. -#' -#' - Processing queue of the `Spectra` object, ensuring that any spectra data -#' modifications are retained. It is stored in a `json` file named -#' *spectra_processing_queue.json*. -#' -#' Note : The Spectra object will only be exported if it uses a -#' [MsBackendMzR()] backend. Other backends are no supported as of now. +#' - The [sampleData()] stored as a text file named *sample_data.txt*. #' #' For an `XcmsExperiment` object, the exported files are the same as those #' for an `MsExperiment` object, with the addition of the following: #' #' - The [processHistory()] information of the object, stored in a `json` file -#' named *process_history.json*. +#' named *process_history.json*. #' #' - The chromatographic peak information obtained with [chromPeaks()] and -#' [chromPeaksData()], stored in tabular format in the text files -#' *chrom_peaks.txt* and *chrom_peak_data.txt* respectively. +#' [chromPeaksData()], stored in tabular format in the text files +#' *chrom_peaks.txt* and *chrom_peak_data.txt* respectively. #' #' - The retention time information obtained with [adjustedRtime()] stored -#' in the text file named *rtime_adjusted.txt*. +#' in a text file named *rtime_adjusted.txt*. #' #' - The [featureDefinitions()] stored in a text file named -#' *feature_definitions.txt*. Additionally, a second file named -#' *feature_peak_index.txt* is generated to connect the features' definitions -#' with their names. +#' *feature_definitions.txt*. Additionally, a second file named +#' *feature_peak_index.txt* is generated to connect the features' definitions +#' with their names. #' -#' This `param` class and method are part of the possible dispatch of the -#' generic functions `storeResults()` and `loadResults()`. -#' The folder defined in the `path` parameter will be created by calling -#' `storeResults`. If the folder already exists, previous exports in that -#' folder might get overwritten. +#' For a `Spectra` object, the exported files include: #' -#' @param path for `PlainTextParam` `character(1)`, defining where the files -#' are going to be stored/ should be loaded from. The default will be -#' `tempdir()`. +#' - The `processingQueueVariables`, `processing`, [processingChunkSize()] and +#' `backend` class information of the object stored in a text file named +#' *spectra_slots.txt*. +#' +#' - The processing queue of the `Spectra` object, ensuring that any spectra +#' data modifications are retained. It is stored in a `json` file named +#' *spectra_processing_queue.json*. +#' +#' Import/export of the MS data depends on the respective implementation of +#' the respective `MsBackend` object. For `MsBackendMzR`, the exported data +#' and related text files are: +#' +#' - The backend's [spectraData()] stored in a tabular format in a text file +#' named *backend_data.txt*. #' -#' @param spectraPath 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. +#' @note +#' +#' The function relies on the `storeResults()` and `loadResults()` methods of +#' the [Spectra()] object and the used [MsBackend()] to store and restore the +#' MS data. These methods might not be available for all `MsBackend` +#' implementations. Also, it might be required to specify the path containing +#' the MS data files using the `spectraPath` parameter. +#' +#' @param path For `PlainTextParam()`: `character(1)`, defining where the files +#' are going to be stored/ should be loaded from. The default is +#' `path = tempdir()`. +#' +#' @param spectraPath For `loadResults()`: `character(1)` optionally allowing to +#' define the (absolute) path where the spectra files (*data storage files*) +#' can be found. This parameter is passed to the `loadResults()` method of +#' the [MsBackend()]. #' #' @inheritParams storeResults #' -#' @return for `PlainTextParam`: a `PlainTextParam` class. `storeResults` does +#' @return For `PlainTextParam`: a `PlainTextParam` class. `storeResults` does #' not return anything but saves the object to collections of different plain -#' text files to a folder. +#' text files to a folder. The `loadResults()` method returns the restored +#' data as an instance of the class specified with parameter `object`. #' -#' @author Philippine Louail, Johannes Rainer. +#' @author Philippine Louail #' #' @importFrom jsonlite serializeJSON write_json unserializeJSON read_json #' @@ -93,6 +97,7 @@ #' @importFrom stats setNames #' #' @examples +#' #' ## Load test data set of class `MsExperiment` #' library(MsExperiment) #' fls <- dir(system.file("sciex", package = "msdata"), full.names = TRUE) @@ -153,8 +158,8 @@ setMethod("storeResults", function(object, param){ dir.create(path = param@path, recursive = TRUE, - showWarnings = TRUE) - write.table(as.data.frame(sampleData(object)), + showWarnings = FALSE) + write.table(as.data.frame(sampleData(object)), sep = "\t", file = file.path(param@path, "sample_data.txt")) ## call export of individual other objects (not MsExperiment data) @@ -167,7 +172,7 @@ setMethod("storeResults", setMethod("storeResults", signature(object = "XcmsExperiment", param = "PlainTextParam"), - function(object, param){ + function(object, param) { callNextMethod() .store_xcmsexperiment(x = object, path = param@path) } @@ -177,30 +182,28 @@ setMethod("storeResults", setMethod("loadResults", signature(object = "MsExperiment", param = "PlainTextParam"), - function(object, param, spectraPath = character()){ + function(object, param, spectraPath = character()) { fl <- file.path(param@path, "sample_data.txt") - if (file.exists(fl)){ # should i have a error if does not exist ? - sd <- read.table(fl) - rownames(sd) <- NULL #read.table force numbering of rownames - } - s <- loadResults(Spectra(), param) + if (!file.exists(fl)) + stop("No 'sample_data.txt' file found in the provided path.") + sd <- read.table(fl, sep = "\t") + rownames(sd) <- NULL #read.table force numbering of rownames + s <- loadResults(Spectra(), param, spectraPath = spectraPath) res <- MsExperiment(sampleData = sd, spectra = s) validObject(res) res - } -) + }) #' @rdname PlainTextParam setMethod("loadResults", signature(object = "XcmsExperiment", param = "PlainTextParam"), - function(object, param, spectraPath){ - res <- callNextMethod() #check if need to add spectraPath = spectraPath + function(object, param, spectraPath = character()) { + res <- callNextMethod() res <- .load_xcmsexperiment(res, path = param@path) validObject(res) res - } -) + }) #' @rdname PlainTextParam setMethod("storeResults", signature(object = "Spectra", @@ -208,7 +211,7 @@ setMethod("storeResults", signature(object = "Spectra", function(object, param) { dir.create(path = param@path, recursive = TRUE, - showWarnings = TRUE) + showWarnings = FALSE) if (!existsMethod("storeResults", c(class(object@backend)[1L], "PlainTextParam"))) stop("Can not store a 'Spectra' object with backend '", @@ -234,9 +237,9 @@ setMethod("loadResults", signature(object = "Spectra", "PlainTextParam"))) stop("Can not store a 'Spectra' object with backend '", variables["backend"], "'") - b <- loadResults(object= do.call(what = variables[["backend"]], - args = list()), - param = param, spectraPath = spectraPath) ##better way to do this ? + b <- loadResults(object = do.call(what = variables[["backend"]], + args = list()), + param = param, spectraPath = spectraPath) s <- Spectra(b) s@processingQueueVariables <- unlist(strsplit(variables[["processingQueueVariables"]], "|", fixed = TRUE)) @@ -249,20 +252,22 @@ setMethod("loadResults", signature(object = "Spectra", s }) - - -# Notes: This and the Spectra method will be moved to it's respective package #' @rdname PlainTextParam setMethod("storeResults", signature(object = "MsBackendMzR", param = "PlainTextParam"), function(object, param) { dir.create(path = param@path, recursive = TRUE, - showWarnings = TRUE) + showWarnings = FALSE) object <- dropNaSpectraVariables(object) - write.table(object@spectraData, - file = file.path(param@path, "backend_data.txt"), - sep = "\t", quote = FALSE) + fl <- file.path(param@path, "backend_data.txt") + if (file.exists(fl)) + warning("Overwriting already present 'backend_data.txt' file") + writeLines(paste0("# ", class(object)[1L]), con = fl) + suppressWarnings( + write.table(object@spectraData, + file = fl, sep = "\t", quote = FALSE, + append = TRUE, row.names = FALSE)) }) #' @rdname PlainTextParam @@ -270,16 +275,17 @@ setMethod("loadResults", signature(object = "MsBackendMzR", param = "PlainTextParam"), function(object, param, spectraPath = character()) { b <- MsBackendMzR() - data <- read.table(file = file.path(param@path, - "backend_data.txt"), - sep = "\t", header = TRUE) + fl <- file.path(param@path, "backend_data.txt") + if (!file.exists(fl)) + stop("No 'backend_data.txt' file found in the provided path.") + data <- read.table(file = fl, sep = "\t", header = TRUE) rownames(data) <- NULL data <- DataFrame(data) b@spectraData <- data - if (length(spectraPath) > 0){ - old <- MsCoreUtils::common_path(dataStorage(b)) - if (nchar(old) > 0) - old <- paste0(old, "/") + if (length(spectraPath) > 0) { + old <- common_path(dataStorage(b)) + ## if (nchar(old) > 0) + ## old <- paste0(old, "/") dataStorage(b) <- sub(old, spectraPath, dataStorage(b)) } b @@ -296,13 +302,12 @@ setMethod("loadResults", signature(object = "MsBackendMzR", writeLines(paste0("processingQueueVariables = ", paste(pq, collapse = "|")), con = con) p <- x@processing - writeLines(paste0("processing = ", paste(p,collapse = "|")), con = con) + writeLines(paste0("processing = ", paste(p, collapse = "|")), con = con) writeLines(paste0("processingChunkSize = ", processingChunkSize(x)), con = con) writeLines(paste0("backend = ", class(x@backend)[1L]), con = con) } - #' Processing queue #' @param x `Spectra` #' @@ -367,19 +372,20 @@ setMethod("loadResults", signature(object = "MsBackendMzR", #' Chromatographic peaks #' @noRd .export_chrom_peaks <- function(x, path = character()) { - write.table(chromPeaks(x), file = file.path(path, "chrom_peaks.txt")) - write.table(as.data.frame(chromPeakData(x)), + write.table(chromPeaks(x), file = file.path(path, "chrom_peaks.txt"), + sep = "\t") + write.table(as.data.frame(chromPeakData(x)), sep = "\t", file = file.path(path, "chrom_peak_data.txt")) } #' @noRd .import_chrom_peaks <- function(x, path = character()) { f <- file.path(path, "chrom_peaks.txt") - pk <- as.matrix(read.table(f)) + pk <- as.matrix(read.table(f, sep = "\t")) f <- file.path(path, "chrom_peak_data.txt") if (!file.exists(f)) stop("No \"chrom_peak_data.txt\" file found in ", path) - pkd <- read.table(f) + pkd <- read.table(f, sep = "\t") x@chromPeaks <- pk x@chromPeakData <- pkd x @@ -389,12 +395,12 @@ setMethod("loadResults", signature(object = "MsBackendMzR", #' @noRd .export_adjusted_rtime <- function(x, path = character()) { write.table(adjustedRtime(x), file = file.path(path, "rtime_adjusted.txt"), - row.names = FALSE, col.names = FALSE) + row.names = FALSE, col.names = FALSE, sep = "\t") } #' @noRd .import_adjusted_rtime <- function(x, file = character()) { - rts <- read.table(file)[, 1L] + rts <- read.table(file, sep = "\t")[, 1L] x@spectra$rtime_adjusted <- as.numeric(rts) x } @@ -407,18 +413,20 @@ setMethod("loadResults", signature(object = "MsBackendMzR", feature_index = rep(seq_len(nrow(fts)), lengths(fts$peakidx)), peak_index = unlist(fts$peakidx, use.names = FALSE)) fts$peakidx <- NA - write.table(fts, file = file.path(path, "feature_definitions.txt")) - write.table(pkidx, file = file.path(path, "feature_peak_index.txt")) + write.table(fts, file = file.path(path, "feature_definitions.txt"), + sep = "\t") + write.table(pkidx, file = file.path(path, "feature_peak_index.txt"), + sep = "\t") } #' @noRd .import_features <- function(x, path = character()) { f <- file.path(path, "feature_definitions.txt") - fts <- read.table(f) + fts <- read.table(f, sep = "\t") f <- file.path(path, "feature_peak_index.txt") if (!file.exists(f)) stop("No \"feature_peak_index.txt\" file found in ", path) - pkidx <- read.table(f) + pkidx <- read.table(f, sep = "\t") fts$peakidx <- unname(split(pkidx$peak_index, pkidx$feature_index)) x@featureDefinitions <- fts x diff --git a/man/PlainTextParam.Rd b/man/PlainTextParam.Rd index 18b2648d..ddef0371 100644 --- a/man/PlainTextParam.Rd +++ b/man/PlainTextParam.Rd @@ -21,7 +21,7 @@ PlainTextParam(path = tempdir()) \S4method{loadResults}{MsExperiment,PlainTextParam}(object, param, spectraPath = character()) -\S4method{loadResults}{XcmsExperiment,PlainTextParam}(object, param, spectraPath) +\S4method{loadResults}{XcmsExperiment,PlainTextParam}(object, param, spectraPath = character()) \S4method{storeResults}{Spectra,PlainTextParam}(object, param) @@ -32,9 +32,9 @@ PlainTextParam(path = tempdir()) \S4method{loadResults}{MsBackendMzR,PlainTextParam}(object, param, spectraPath = character()) } \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{path}{For `PlainTextParam()`: `character(1)`, defining where the files +are going to be stored/ should be loaded from. The default is +`path = tempdir()`.} \item{object}{\code{MsExperiment} or \code{XcmsExperiment} The data object that needs to be saved.} @@ -43,72 +43,74 @@ to be saved.} saving. It can be one of the following classes: \code{\link{RDataParam}}, \code{\link{PlainTextParam}}, or \code{MzTabMParam}.} -\item{spectraPath}{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{spectraPath}{For `loadResults()`: `character(1)` optionally allowing to +define the (absolute) path where the spectra files (*data storage files*) +can be found. This parameter is passed to the `loadResults()` method of +the [MsBackend()].} } \value{ -for `PlainTextParam`: a `PlainTextParam` class. `storeResults` does +For `PlainTextParam`: a `PlainTextParam` class. `storeResults` does not return anything but saves the object to collections of different plain -text files to a folder. +text files to a folder. The `loadResults()` method returns the restored +data as an instance of the class specified with parameter `object`. } \description{ -The `PlainTextParam` class and method enable users to save/load an -`MsExperiment` or `XcmsExperiment` object as a collections of plain text -files in/from a specified folder. -Note that, while for all xcms results within the `XcmsExperiment` can -and will be exported, the full raw MS data (of the object's `Spectra` object) -will currently not be exported in plain text format. For `Spectra` using the -[MsBackendMzR()] backend, the names of the raw data files will however be -exported (which enables to *load* the full `Spectra` respectively -`MsExperiment` objects). - -For an `MsExperiment` object, the exported files include: +The `storeResults()` and `loadResults()` methods with the `PlainTextParam` +option enable users to save/load an `MsExperiment` or `XcmsExperiment` +object as a collections of plain text files in/from a specified folder. This +folder, defined with the `path` parameter, will be created by the +`storeResults()` function. Any previous exports eventually present in that +folder will be overwritten. -- The [sampleData()] stored as a text file named *sample_data.txt*. - -For a `Spectra` object, the exported files include: +For an `MsExperiment` object, the exported files, stored into the directory +specifyied with the `path` parameter, include: -- The [spectraData()] stored in a tabular format in a text file named -*backend_data.txt*. - -- The `processingQueueVariables`, `processing`, [processingChunkSize()] and -`backend` class information of the object stored in a text file named -*spectra_slots.txt*. - -- Processing queue of the `Spectra` object, ensuring that any spectra data -modifications are retained. It is stored in a `json` file named -*spectra_processing_queue.json*. - -Note : The Spectra object will only be exported if it uses a -[MsBackendMzR()] backend. Other backends are no supported as of now. +- The [sampleData()] stored as a text file named *sample_data.txt*. For an `XcmsExperiment` object, the exported files are the same as those for an `MsExperiment` object, with the addition of the following: - The [processHistory()] information of the object, stored in a `json` file -named *process_history.json*. + named *process_history.json*. - The chromatographic peak information obtained with [chromPeaks()] and -[chromPeaksData()], stored in tabular format in the text files -*chrom_peaks.txt* and *chrom_peak_data.txt* respectively. + [chromPeaksData()], stored in tabular format in the text files + *chrom_peaks.txt* and *chrom_peak_data.txt* respectively. - The retention time information obtained with [adjustedRtime()] stored -in the text file named *rtime_adjusted.txt*. + in a text file named *rtime_adjusted.txt*. - The [featureDefinitions()] stored in a text file named -*feature_definitions.txt*. Additionally, a second file named -*feature_peak_index.txt* is generated to connect the features' definitions -with their names. - -This `param` class and method are part of the possible dispatch of the -generic functions `storeResults()` and `loadResults()`. -The folder defined in the `path` parameter will be created by calling -`storeResults`. If the folder already exists, previous exports in that -folder might get overwritten. + *feature_definitions.txt*. Additionally, a second file named + *feature_peak_index.txt* is generated to connect the features' definitions + with their names. + +For a `Spectra` object, the exported files include: + +- The `processingQueueVariables`, `processing`, [processingChunkSize()] and + `backend` class information of the object stored in a text file named + *spectra_slots.txt*. + +- The processing queue of the `Spectra` object, ensuring that any spectra + data modifications are retained. It is stored in a `json` file named + *spectra_processing_queue.json*. + +Import/export of the MS data depends on the respective implementation of +the respective `MsBackend` object. For `MsBackendMzR`, the exported data +and related text files are: + +- The backend's [spectraData()] stored in a tabular format in a text file + named *backend_data.txt*. +} +\note{ +The function relies on the `storeResults()` and `loadResults()` methods of +the [Spectra()] object and the used [MsBackend()] to store and restore the +MS data. These methods might not be available for all `MsBackend` +implementations. Also, it might be required to specify the path containing +the MS data files using the `spectraPath` parameter. } \examples{ + ## Load test data set of class `MsExperiment` library(MsExperiment) fls <- dir(system.file("sciex", package = "msdata"), full.names = TRUE) @@ -145,6 +147,6 @@ Other xcms result export and import formats.: \code{\link{RDataParam}} } \author{ -Philippine Louail, Johannes Rainer. +Philippine Louail } \concept{xcms result export and import formats.} diff --git a/tests/testthat/test_PlainTextParam.R b/tests/testthat/test_PlainTextParam.R index f5f5e566..17548145 100644 --- a/tests/testthat/test_PlainTextParam.R +++ b/tests/testthat/test_PlainTextParam.R @@ -12,42 +12,80 @@ test_that("storeResults,loadResults,PlainTextParam,MsBackendMzR works", { ## Loading data again b2 <- loadResults(object = MsBackendMzR(), param) expect_true(inherits(b2, "MsBackendMzR")) - b <- dropNaSpectraVariables(b) #the function does this to be robust, is it a problem ? i should mention it in the doc + b <- dropNaSpectraVariables(b) expect_equal(b@spectraData, b2@spectraData) - expect_equal(peaksVariables(b), peaksVariables(b2)) # true even without forcing the slot + expect_equal(peaksVariables(b), peaksVariables(b2)) + expect_equal(mz(b[1:20]), mz(b2[1:20])) + + ## Check the spectraPath parameter. + bp <- dataStorageBasePath(b) + ## manually change dataStorage path of backend + sd <- read.table(file.path(param@path, "backend_data.txt"), header = TRUE) + sd$dataStorage <- sub("faahKO", "other", sd$dataStorage) + write.table(sd, file = file.path(param@path, "backend_data.txt"), + sep = "\t", quote = FALSE, row.names = FALSE) + A <- loadResults(MsBackendMzR(), param) + expect_error(validObject(A), "invalid class") + A <- loadResults(MsBackendMzR(), param, spectraPath = bp) + expect_true(validObject(A)) + + param <- PlainTextParam(tempdir()) + expect_error(loadResults(MsBackendMzR(), param), "No 'backend_data") }) test_that("storeResults,loadResults,PlainTextParam,Spectra works", { pth <- file.path(tempdir(), "test1") param <- PlainTextParam(path = pth) - #add processingQueueVariables to test export + ## add processingQueueVariables to test export + s@processingQueueVariables <- c(s@processingQueueVariables, "rtime") s <- filterMzRange(s, c(200,300)) + s <- filterRt(s, c(3000, 3500)) # to ensure subsetted object would work storeResults(s, param = param) expect_true(dir.exists(pth)) expect_true(file.exists(file.path(param@path, "backend_data.txt"))) expect_true(file.exists(file.path(param@path, "spectra_slots.txt"))) - expect_true(file.exists(file.path(param@path, "spectra_processing_queue.json"))) + expect_true(file.exists(file.path(param@path, + "spectra_processing_queue.json"))) ## Loading data again s2 <- loadResults(object = Spectra(), param) expect_true(inherits(s2, "Spectra")) + expect_true(inherits(s2@backend, "MsBackendMzR")) s <- dropNaSpectraVariables(s) + expect_equal(length(s@processingQueue), length(s2@processingQueue)) expect_equal(s@processingQueue[[1L]]@ARGS, s2@processingQueue[[1L]]@ARGS) expect_equal(s@processingQueueVariables, s2@processingQueueVariables) expect_equal(s@processing, s2@processing) expect_equal(processingChunkSize(s), processingChunkSize(s2)) expect_equal(s@backend@spectraData, s2@backend@spectraData) expect_equal(rtime(s), rtime(s2)) + expect_equal(mz(s[1:10]), mz(s2[1:10])) # that does NOT work without calling functions directly with MsCoreUtils:: in Spectra. expect_no_error(filterRt(s2, c(3000, 3500))) + + ## Check the spectraPath parameter. + ## Changing the path in the MsBackendMzR to simulate moving the exported data + bp <- dataStorageBasePath(s) + sd <- read.table(file.path(param@path, "backend_data.txt"), header = TRUE) + sd$dataStorage <- sub("faahKO", "other", sd$dataStorage) + write.table(sd, file = file.path(param@path, "backend_data.txt"), + sep = "\t", quote = FALSE, row.names = FALSE) + A <- loadResults(Spectra(), param) + expect_error(validObject(A@backend), "invalid class") + A <- loadResults(Spectra(), param, spectraPath = bp) + expect_true(validObject(A@backend)) + + param <- PlainTextParam(file.path(tempdir())) + expect_error(loadResults(Spectra(), param), "No 'spectra_slots") }) test_that("storeResults,loadResults,PlainTextParam,MsExperiment works", { - pth <- file.path(tempdir(), "test") + pth <- file.path(tempdir(), "test3") param <- PlainTextParam(path = pth) param2 <- PlainTextParam() expect_false(is.null(param2)) expect_error(new("PlainTextParam", path = c(tempdir(), tempdir()))) - mse <- filterMzRange(mse, c(200, 500)) - storeResults(mse, param = param) + tmp <- filterMzRange(mse, c(200, 500)) + tmp <- filterRt(tmp, c(3000, 3500)) + storeResults(tmp, param = param) expect_true(dir.exists(pth)) expect_true(file.exists(file.path(param@path, "sample_data.txt"))) expect_true(file.exists(file.path(param@path, "backend_data.txt"))) @@ -56,21 +94,38 @@ test_that("storeResults,loadResults,PlainTextParam,MsExperiment works", { ## Loading data again load_mse <- loadResults(object = MsExperiment(), param) expect_true(inherits(load_mse, "MsExperiment")) - expect_equal(sampleData(mse), sampleData(load_mse)) - a <- spectra(mse) + expect_equal(sampleData(tmp), sampleData(load_mse)) + a <- spectra(tmp) b <- spectra(load_mse) + expect_equal(length(a@processingQueue), length(b@processingQueue)) expect_equal(a@processingQueue[[1L]]@ARGS, b@processingQueue[[1L]]@ARGS) expect_equal(rtime(a), rtime(b)) expect_no_error(filterRt(load_mse, c(3000, 3500))) + + ## Check the spectraPath parameter. + bp <- dataStorageBasePath(tmp@spectra) + ## manually change dataStorage path of backend + sd <- read.table(file.path(param@path, "backend_data.txt"), header = TRUE) + sd$dataStorage <- sub("faahKO", "other", sd$dataStorage) + write.table(sd, file = file.path(param@path, "backend_data.txt"), + sep = "\t", quote = FALSE, row.names = FALSE) + A <- loadResults(MsExperiment(), param) + expect_error(validObject(spectra(A)@backend), "invalid class") + A <- loadResults(MsExperiment(), param, spectraPath = bp) + expect_true(validObject(spectra(A)@backend)) + + param <- PlainTextParam(tempdir()) + expect_error(loadResults(MsExperiment(), param), "No 'sample_data") }) test_that("storeResults,loadResults,PlainTextParam,XcmsExperiment works", { - pth = file.path(tempdir(), "test") + pth = file.path(tempdir(), "test4") param <- PlainTextParam(path = pth) param2 <- PlainTextParam() expect_false(is.null(param2)) - xmse_full <- filterMzRange(xmse_full, c(200, 500)) - storeResults(xmse_full, param = param) + tmp <- filterMzRange(xmse_full, c(200, 500)) + tmp <- filterRt(tmp, c(3000, 4000)) + storeResults(tmp, param = param) expect_true(dir.exists(pth)) expect_true(file.exists(file.path(param@path, "sample_data.txt"))) expect_true(file.exists(file.path(param@path, "backend_data.txt"))) @@ -82,16 +137,39 @@ test_that("storeResults,loadResults,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"))) - ##load data again + + ## load data again load_xmse <- loadResults(object = XcmsExperiment(), param) expect_true(inherits(load_xmse, "XcmsExperiment")) - expect_equal(xmse_full@featureDefinitions, + expect_equal(tmp@featureDefinitions, load_xmse@featureDefinitions) - expect_equal(featureValues(xmse_full), featureValues(load_xmse)) - expect_equal(adjustedRtime(xmse_full), adjustedRtime(load_xmse)) + expect_equal(featureValues(tmp), featureValues(load_xmse)) + expect_equal(adjustedRtime(tmp), adjustedRtime(load_xmse)) expect_no_error(filterRt(load_xmse, c(3000, 3500))) - ## not sure how to check for the processHistory slot - ## still not sure how to create UT for spectraPath -}) + expect_equal(tmp@chromPeaks, load_xmse@chromPeaks) + expect_equal(tmp@chromPeakData, load_xmse@chromPeakData) + expect_equal(tmp@sampleData, load_xmse@sampleData) + expect_equal(length(tmp@processHistory), length(load_xmse@processHistory)) + expect_equal(tmp@processHistory[[1L]], load_xmse@processHistory[[1L]]) + expect_equal(tmp@processHistory[[2L]], load_xmse@processHistory[[2L]]) + expect_equal(tmp@processHistory[[3L]], load_xmse@processHistory[[3L]]) + expect_equal(tmp@processHistory[[4L]], load_xmse@processHistory[[4L]]) + expect_equal(tmp@processHistory[[5L]], load_xmse@processHistory[[5L]]) + ## The 6th param object contains functions for which the comparison fails + ## because of the name/namespace mentioned. See e.g. + ## tmp@processHistory[[6]]@param load_xmse@processHistory[[6]]@param + ## Check the spectraPath parameter. + bp <- dataStorageBasePath(tmp@spectra) + ## manually change dataStorage path of backend + sd <- read.table(file.path(param@path, "backend_data.txt"), header = TRUE) + sd$dataStorage <- sub("faahKO", "other", sd$dataStorage) + write.table(sd, file = file.path(param@path, "backend_data.txt"), + sep = "\t", quote = FALSE, row.names = FALSE) + expect_error(loadResults(XcmsExperiment(), param), "invalid class") + A <- loadResults(XcmsExperiment(), param, spectraPath = bp) + expect_true(validObject(spectra(A)@backend)) + param <- PlainTextParam(tempdir()) + expect_error(loadResults(MsExperiment(), param), "No 'sample_data") +})