Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add storeResults/loadResults for Spectra etc #749

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions R/PlainTextParam.R
Original file line number Diff line number Diff line change
@@ -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)
}
)

@@ -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)),
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()
@@ -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")
@@ -59,5 +70,3 @@ test_that("loadResults, PlainTextParam works", {
expect_equal(xmse_full, load_xmse)
# not sure how to check for `spectraFilePath`
})