Skip to content

Commit

Permalink
refactor: add storeResults/loadResults for Spectra etc
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed May 30, 2024
1 parent 61f86d6 commit 7157198
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
36 changes: 36 additions & 0 deletions R/PlainTextParam.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ setMethod("storeResults",
recursive = TRUE,
showWarnings = TRUE)
.store_msexperiment(x = object, path = param@path)
## call export of individual other objects (not MsExperiment data)
storeResults(spectra(object), param)
}
)

Expand Down Expand Up @@ -189,6 +191,40 @@ setMethod("loadResults",
}
)

setMethod("storeResults", signature(object = "Spectra",
param = "PlainTextParam"),
function(object, param) {
## Check if there is a method to store the backend. Throw an
## error if not.
if (!existsMethod("storeResults", c(class(object@backend)[1L],
"PlainTextParam")))
stop("Can not store a 'Spectra' object with backend '",
class(object@backend)[1L], "'")
## - Call storeResults on @backend.
## - save @processingQueue -> json (use previously implemented
## function).
## Save the rest of the slots to a txt file, spectra_slots.txt
## - save @processingQueueVariables, separated by "|"
## - save @processingChunkSize.
## - save the class of the backend (to allow calling import on
## the specific class.
})

setMethod("storeResults", signature(object = "MsBackendMzR",
param = "PlainTextParam"),
function(object, param) {
## save the @spectraData -> text file (tab delimited table).
})

setMethod("loadResults", signature(object = "MsBackendMzR",
param = "PlainTextParam"),
function(object, param, spectraPath = character()) {
## load spectraData data.frame
## replace the absolute paths in "dataStorage" with
## spectraPath if that is defined.
})


#' @noRd
.store_msexperiment <- function(x, path = tempdir()) {
.export_sample_data(as.data.frame(sampleData(x)),
Expand Down
33 changes: 21 additions & 12 deletions tests/testthat/test_PlainTextParam.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
xmse_full <- loadXcmsData("xmse")

test_that("storeResults,PlainTextParam,MsExperiment works", {
test_that("storeResults,loadResults,PlainTextParam,MsExperiment works", {
pth <- file.path(tempdir(), "test")
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))
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, "spectra_files.txt")))
expect_true(file.exists(file.path(param@path, "spectra_processing_queue.json")))
## Loading data again
load_mse <- loadResults(object = MsExperiment(), param)
expect_true(inherits(load_mse, "MsExperiment"))
expect_equal(sampleData(tmp), sampleData(load_mse))
a <- spectra(tmp)
b <- spectra(load_mse)
## processingQueue can not be identical because of FUN, which is a function
## expect_equal(a@processingQueue, b@processingQueue)
expect_equal(a@processingQueue[[1L]]@ARGS, b@processingQueue[[1L]]@ARGS)
expect_equal(rtime(a), rtime(b))
expect_equal(intensity(a), intensity(b))
expect_equal(mz(a), mz(b))
## NOTE: if we in addition filter or subset the Spectra we can't store
## properly to a txt file! Would need to store information on the data
## subset too.
tmp <- filterRt(tmp, c(3000, 3500))

})

test_that("storeResults,PlainTextParam,XcmsExperiment works", {
test_that("storeResults,loadResults,PlainTextParam,XcmsExperiment works", {
pth = file.path(tempdir(), "test")
param <- PlainTextParam(path = pth)
param2 <- PlainTextParam()
Expand All @@ -38,12 +55,6 @@ test_that("storeResults,PlainTextParam,XcmsExperiment works", {

test_that("loadResults, PlainTextParam works", {
## test for MsExperiment object only
pth = file.path(tempdir(), "test3")
param <- PlainTextParam(path = pth)
storeResults(mse, param = param)
load_mse <- loadResults(object = MsExperiment(), param)
expect_true(inherits(load_mse, "MsExperiment"))
expect_equal(mse, load_mse)

## test for XcmsExperiment object
pth = file.path(tempdir(), "test4")
Expand All @@ -59,5 +70,3 @@ test_that("loadResults, PlainTextParam works", {
expect_equal(xmse_full, load_xmse)
# not sure how to check for `spectraFilePath`
})


0 comments on commit 7157198

Please sign in to comment.