From da1e687e82767f94948f0ce8c2f6036f33bc05b8 Mon Sep 17 00:00:00 2001 From: Johannes Rainer Date: Fri, 20 Sep 2024 13:08:46 +0200 Subject: [PATCH] tests: complete test coverate --- DESCRIPTION | 8 ++- NEWS.md | 8 ++- R/MsBackendMzR.R | 7 ++- R/MsExperiment.R | 46 ++++++++------- R/Spectra.R | 6 +- R/XcmsExperiment.R | 23 ++++---- R/utils.R | 23 ++++++++ tests/testthat/test_MsBackendMzR.R | 33 +++++++++++ tests/testthat/test_MsExperiment.R | 61 +++++++++++++++++++- tests/testthat/test_Spectra.R | 23 ++++++++ tests/testthat/test_XcmsExperiment.R | 84 ++++++++++++++++++++++++++-- tests/testthat/test_mzTabParam.R | 16 ------ tests/testthat/test_utils.R | 23 ++++++++ tests/testthat/test_zzz.R | 24 ++++++++ 14 files changed, 323 insertions(+), 62 deletions(-) create mode 100644 R/utils.R create mode 100644 tests/testthat/test_utils.R create mode 100644 tests/testthat/test_zzz.R diff --git a/DESCRIPTION b/DESCRIPTION index 8c2398d..3bc3d08 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: MsIO Title: Serializing and restoring/importing mass spectrometry data objects -Version: 0.0.5 +Version: 0.0.6 Authors@R: c(person(given = "Johannes", family = "Rainer", email = "Johannes.Rainer@eurac.edu", @@ -44,8 +44,9 @@ Suggests: testthat, xcms, alabaster.se, - alabaster.matrix, - MsBackendMetaboLights + alabaster.matrix, + MsBackendMetaboLights, + QFeatures License: Artistic-2.0 Encoding: UTF-8 VignetteBuilder: knitr @@ -66,4 +67,5 @@ Collate: 'Spectra.R' 'mzTabParam.R' 'XcmsExperiment.R' + 'utils.R' 'zzz.R' diff --git a/NEWS.md b/NEWS.md index 552cc93..b0bda56 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,12 @@ -# Version 0.0.5 +# Version 0.0 + +## Changes in 0.0.6 + +- Expand unit tests. ## Changes in 0.0.5 -- Add *MetaboLights* `readMsObject()` method for `MsExpriment()` objects. +- Add *MetaboLights* `readMsObject()` method for `MsExpriment()` objects. ## Changes in 0.0.4 diff --git a/R/MsBackendMzR.R b/R/MsBackendMzR.R index 74c678d..18702b8 100644 --- a/R/MsBackendMzR.R +++ b/R/MsBackendMzR.R @@ -26,8 +26,9 @@ setMethod("saveMsObject", signature(object = "MsBackendMzR", object <- Spectra::dropNaSpectraVariables(object) fl <- file.path(param@path, "ms_backend_data.txt") if (file.exists(fl)) - warning("Overwriting already present ", - "'ms_backend_data.txt' file") + stop("Overwriting or saving to an existing directory is not", + " supported. Please remove the directory defined with", + " parameter `path` first.") writeLines(paste0("# ", class(object)[1L]), con = fl) if (nrow(object@spectraData)) suppressWarnings( @@ -126,7 +127,7 @@ validateAlabasterMsBackendMzR <- function(path = character(), #' @noRd readAlabasterMsBackendMzR <- function(path = character(), metadata = list(), spectraPath = character()) { - if (!requireNamespace("Spectra", quietly = TRUE)) + if (!.is_spectra_installed()) stop("Required package 'Spectra' missing. Please install ", "and try again.", call. = FALSE) validateAlabasterMsBackendMzR(path, metadata) diff --git a/R/MsExperiment.R b/R/MsExperiment.R index 2ffff28..daf4744 100644 --- a/R/MsExperiment.R +++ b/R/MsExperiment.R @@ -102,12 +102,12 @@ setMethod("saveObject", "MsExperiment", function(x, path, ...) { stop("Saving of an 'MsExperiment' with an object of type 'QFeatures'", " in the qdata slot is currently not supported.", call. = FALSE) if (inherits(x@qdata, "SummarizedExperiment") && - !requireNamespace("alabaster.se", quietly = TRUE)) + !.is_alabaster_se_installed()) stop("Required package 'alabaster.se' for export of ", "'SummarizedExperiment' objects missing. Please install and ", "try again.", call. = FALSE) if (length(x@sampleDataLinks) > 0 && - !requireNamespace("alabaster.matrix", quietly = TRUE)) + !.is_alabaster_matrix_installed()) stop("Required package 'alabaster.matrix' missing. Please install and ", "try again.", call. = FALSE) dir.create(path = path, recursive = TRUE, showWarnings = FALSE) @@ -149,7 +149,7 @@ validateAlabasterMsExperiment <- function(path = character(), #' @importFrom alabaster.base readObjectFile readAlabasterMsExperiment <- function(path = character(), metadata = list(), ...) { - if (!requireNamespace("MsExperiment", quietly = TRUE)) + if (!.is_ms_experiment_installed()) stop("Required package 'MsExperiment' missing. Please install ", "and try again.", call. = FALSE) validateAlabasterMsExperiment(path, metadata) @@ -169,7 +169,7 @@ readAlabasterMsExperiment <- function(path = character(), metadata = list(), if (file.exists(file.path(path, "qdata"))) { qdata_obj <- readObjectFile(file.path(path, "qdata")) if (qdata_obj$type[1L] == "summarized_experiment") { - if (!requireNamespace("alabaster.se", quietly = TRUE)) + if (!.is_alabaster_se_installed()) stop("Required package 'alabaster.se' not available. Please ", "install and try again.", call. = FALSE) i <- altReadObject(file.path(path, "qdata")) @@ -216,21 +216,27 @@ setMethod("readMsObject", param = "MetaboLightsParam"), function(object, param, keepOntology = TRUE, keepProtocol = TRUE, simplify = TRUE, ...) { - if (!requireNamespace("MsBackendMetaboLights", quietly = TRUE)) { + if (!.is_ms_backend_metabo_lights_installed()) stop("Required package 'MsBackendMetaboLights' is missing. ", "Please install it and try again.", call. = FALSE) - } + if (!.is_spectra_installed()) + stop("Required package 'Spectra' is missing. ", + "Please install and try again.", call. = FALSE) pth <- MsBackendMetaboLights::mtbls_ftp_path(param@mtblsId) all_fls <- MsBackendMetaboLights::mtbls_list_files(param@mtblsId) ## Extract and read assay files assays <- all_fls[grepl("^a_", all_fls)] - if (length(param@assayName) > 0) + if (length(param@assayName) > 0) { selected_assay <- param@assayName + if (!selected_assay %in% assays) + stop("Specified assay \"", selected_assay, "\" does ", + "not exist.", call. = FALSE) + } else { if (length(assays) == 1) { selected_assay <- assays - message("Only one assay file found:", selected_assay, "\n") + message("Only one assay file found:", selected_assay) } else { message("Multiple assay files found:\n") selection <- menu(assays, @@ -250,7 +256,7 @@ setMethod("readMsObject", header = TRUE, sep = "\t", check.names = FALSE) - # merging + ## merging ord <- match(assay_data$`Sample Name`, sample_info$`Sample Name`) merged_data <- cbind(assay_data, sample_info[ord, ]) names(merged_data) <- gsub(" ", "_", names(merged_data)) @@ -259,24 +265,25 @@ setMethod("readMsObject", keepProtocol = keepProtocol, keepOntology = keepOntology, simplify = simplify) - ## Assemble object - object@spectra <- Spectra::Spectra(mtblsId = param@mtblsId, - source = MsBackendMetaboLights::MsBackendMetaboLights(), - assayName = selected_assay, - filePattern = param@filePattern) + b <- MsBackendMetaboLights::MsBackendMetaboLights() + object@spectra <- Spectra::Spectra( + mtblsId = param@mtblsId, + source = b, + assayName = selected_assay, + filePattern = param@filePattern) ## sample to spectra link - fl <- object@spectra@backend@spectraData[1, "derived_spectral_data_file"] + fl <- object@spectra@backend@spectraData[ + 1, "derived_spectral_data_file"] nme <- colnames(merged_data)[which(merged_data[1, ] == fl)] merged_data <- merged_data[grepl(param@filePattern, merged_data[, nme]), ] nme <- gsub(" ", "_", nme) #use concatenate instead ? object@sampleData <- DataFrame(merged_data, check.names = FALSE) - object <- MsExperiment::linkSampleData(object, - with = paste0("sampleData.", - nme, - "= spectra.derived_spectral_data_file")) + l <- paste0("sampleData.", nme, + " = spectra.derived_spectral_data_file") + object <- MsExperiment::linkSampleData(object, with = l) validObject(object) object }) @@ -313,4 +320,3 @@ setMethod("readMsObject", } return(x) } - diff --git a/R/Spectra.R b/R/Spectra.R index e2cdb1d..30242f7 100644 --- a/R/Spectra.R +++ b/R/Spectra.R @@ -45,7 +45,7 @@ setMethod("readMsObject", signature(object = "Spectra", if (!existsMethod("readMsObject", c(variables[["backend"]], "PlainTextParam"))) stop("Can not read a 'Spectra' object with backend '", - variables["backend"], "'") + variables["backend"], "'", call. = FALSE) tryCatch({ object@backend <- readMsObject( do.call(variables[["backend"]], list()), param, ...) @@ -148,7 +148,7 @@ validateAlabasterSpectra <- function(path = character(), readAlabasterSpectra <- function(path = character(), metadata = list(), ...) { - if (!requireNamespace("Spectra", quietly = TRUE)) + if (!.is_spectra_installed()) stop("Required package 'Spectra' missing. Please install ", "and try again.", call. = FALSE) @@ -174,7 +174,7 @@ setMethod("saveMsObject", signature(object = "Spectra", if (file.exists(param@path)) stop("Overwriting or saving to an existing directory is not", " supported. Please remove the directory defined with", - " parameter `path` first.") + " parameter `path` first.", call. = FALSE) saveObject(object, param@path) }) diff --git a/R/XcmsExperiment.R b/R/XcmsExperiment.R index ecb577d..b01374e 100644 --- a/R/XcmsExperiment.R +++ b/R/XcmsExperiment.R @@ -137,7 +137,7 @@ setMethod("readMsObject", ################################################################################ #' @rdname AlabasterParam setMethod("saveObject", "XcmsExperiment", function(x, path, ...) { - if (!requireNamespace("alabaster.matrix", quietly = TRUE)) + if (!.is_alabaster_matrix_installed()) stop("Required package 'alabaster.matrix' missing. Please install and ", "try again.", call. = FALSE) ## Save the MsExperiment part @@ -163,11 +163,11 @@ validateAlabasterXcmsExperiment <- function(path = character(), } readAlabasterXcmsExperiment <- function(path = character(), metadata = list(), - ...) { - if (!requireNamespace("xcms", quietly = TRUE)) + ...) { + if (!.is_xcms_installed()) stop("Required package 'xcms' missing. Please install ", "and try again.", call. = FALSE) - if (!requireNamespace("MsExperiment", quietly = TRUE)) + if (!.is_ms_experiment_installed()) stop("Required package 'MsExperiment' missing. Please install ", "and try again.", call. = FALSE) validateAlabasterXcmsExperiment(path, metadata) @@ -216,18 +216,21 @@ setMethod("saveMsObject", param = "mzTabParam"), function(object, param){ if (!param@sampleDataColumn %in% colnames(object@sampleData)) - stop("'sampleDataColumn' has to correspond to column names", - "of the sampleData() table") + stop("'sampleDataColumn' has to correspond to column names ", + "of the sampleData() table", call. = FALSE) if (length(param@optionalFeatureColumns) != 0) - if (!param@optionalFeatureColumns %in% colnames(object@featureDefinitions)) - stop("'optionalFeatureColumns' have to correspond to", - "column names of the featureDefinitions() table") + if (!param@optionalFeatureColumns %in% + colnames(object@featureDefinitions)) + stop("'optionalFeatureColumns' have to correspond to ", + "column names of the featureDefinitions() table", + call. = FALSE) var_list <- unique(.mztab_study_variables(object@sampleData, param@sampleDataColumn)) fl <- file.path(param@path, paste0(param@studyId, ".mztab")) if (file.exists(fl)) - stop("File ", basename(fl), " already exists. ") + stop("File \"", basename(fl), "\" already exists.", + call. = FALSE) con <- file(fl, open = "at") on.exit(close(con)) diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..9334abf --- /dev/null +++ b/R/utils.R @@ -0,0 +1,23 @@ +.is_alabaster_matrix_installed <- function() { + requireNamespace("alabaster.matrix", quietly = TRUE) +} + +.is_spectra_installed <- function() { + requireNamespace("Spectra", quietly = TRUE) +} + +.is_alabaster_se_installed <- function() { + requireNamespace("alabaster.se", quietly = TRUE) +} + +.is_ms_experiment_installed <- function() { + requireNamespace("MsExperiment", quietly = TRUE) +} + +.is_ms_backend_metabo_lights_installed <- function() { + requireNamespace("MsBackendMetaboLights", quietly = TRUE) +} + +.is_xcms_installed <- function() { + requireNamespace("xcms", quietly = TRUE) +} diff --git a/tests/testthat/test_MsBackendMzR.R b/tests/testthat/test_MsBackendMzR.R index 7d9bd02..11597a7 100644 --- a/tests/testthat/test_MsBackendMzR.R +++ b/tests/testthat/test_MsBackendMzR.R @@ -11,6 +11,10 @@ test_that("saveMsObject,readMsObject,PlainTextParam,MsBackendMzR works", { saveMsObject(b, param = param) expect_true(dir.exists(pth)) expect_true(file.exists(file.path(param@path, "ms_backend_data.txt"))) + + ## Overwriting is not supported + expect_error(saveMsObject(b, param = param), "Overwriting or saving") + ## Loading data again b_load <- readMsObject(object = MsBackendMzR(), param) expect_true(inherits(b_load, "MsBackendMzR")) @@ -47,6 +51,14 @@ test_that("saveMsObject,readMsObject,PlainTextParam,MsBackendMzR works", { res <- readMsObject(MsBackendMzR(), param) expect_s4_class(res, "MsBackendMzR") expect_true(length(res) == 0) + + ## Reading wrong backend. + pth <- tempdir() + writeLines("# MsBackendDataFrame\nSome other line\n", + file.path(pth, "ms_backend_data.txt")) + p <- PlainTextParam(path = pth) + expect_error(readMsObject(MsBackendMzR(), p), "Invalid class") + file.remove(file.path(pth, "ms_backend_data.txt")) }) test_that("saveObject,readObject,MsBackendMzR works", { @@ -69,6 +81,12 @@ test_that("saveObject,readObject,MsBackendMzR works", { expect_s4_class(res, "MsBackendMzR") expect_equal(length(b), length(res)) + ## package Spectra not available: + with_mock( + "MsIO:::.is_spectra_installed" = function() FALSE, + expect_error(MsIO:::readAlabasterMsBackendMzR(pth), "package 'Spectra'") + ) + ## readObject res <- readObject(pth) expect_s4_class(res, "MsBackendMzR") @@ -101,6 +119,8 @@ test_that("saveObject,readObject,MsBackendMzR works", { }) test_that(".ms_backend_mzr_update_storage_path works", { + res <- .ms_backend_mzr_update_storage_path(numeric()) + expect_equal(res, numeric()) x <- sciex_mzr res <- .ms_backend_mzr_update_storage_path(x, "/new/path") expect_true(all(grepl("/new/path", res$dataStorage))) @@ -122,4 +142,17 @@ test_that("saveMsObject,readMsObject,MsBackendMzR,AlabasterParam works", { expect_equal(res@spectraData, x@spectraData) expect_equal(spectraData(res), spectraData(x)) expect_equal(res@peaksVariables, x@peaksVariables) + + ## Unexpected errors. + x <- MsBackendMzR() + pth <- file.path(tempdir(), "remove") + slot(x, "spectraData", check = FALSE) <- AlabasterParam() + expect_error(saveObject(x, pth), "failed to save 'spectraData'") + unlink(pth, recursive = TRUE) + + x <- MsBackendMzR() + pth <- file.path(tempdir(), "remove") + slot(x, "peaksVariables", check = FALSE) <- AlabasterParam() + expect_error(saveObject(x, pth), "failed to save 'peaksVariables'") + unlink(pth, recursive = TRUE) }) diff --git a/tests/testthat/test_MsExperiment.R b/tests/testthat/test_MsExperiment.R index 4bb642e..8d2b3f3 100644 --- a/tests/testthat/test_MsExperiment.R +++ b/tests/testthat/test_MsExperiment.R @@ -68,6 +68,18 @@ test_that("saveObject,MsExperiment,readAlabasterMsExperiment etc works", { ## Alabaster MsExperiment save/read functions. pth <- file.path(tempdir(), "ms_experiment_alabaster") + ## errors + m <- MsExperiment() + library(QFeatures) + m@qdata <- QFeatures() + expect_error(saveObject(m, pth), "QFeatures' in the qdata slot") + + m@qdata <- SummarizedExperiment() + with_mock( + "MsIO:::.is_alabaster_se_installed" = function() FALSE, + expect_error(saveObject(m, pth), "'SummarizedExperiment' objects") + ) + ## Empty object. m <- MsExperiment() saveObject(m, pth) @@ -83,6 +95,20 @@ test_that("saveObject,MsExperiment,readAlabasterMsExperiment etc works", { m <- mse_filt expect_error(saveObject(m, pth), "existing") unlink(pth, recursive = TRUE) + with_mock( + "MsIO:::.is_alabaster_matrix_installed" = function() FALSE, + expect_error(saveObject(m, pth), "alabaster.matrix' missing") + ) + m_2 <- m + slot(m_2, "spectra", check = FALSE) <- AlabasterParam() + expect_error(saveObject(m_2, pth), "failed to save '@spectra'") + unlink(pth, recursive = TRUE) + + m_2 <- m + m_2@otherData[[1L]] <- AlabasterParam() + expect_error(saveObject(m_2, pth), "failed to save '@otherData'") + unlink(pth, recursive = TRUE) + saveObject(m, pth) expect_true(all(c("sample_data", "sample_data_links", "spectra", "sample_data_links_mcols", "metadata", @@ -112,13 +138,26 @@ test_that("saveObject,MsExperiment,readAlabasterMsExperiment etc works", { expect_true(all(c("sample_data", "sample_data_links", "spectra", "sample_data_links_mcols", "metadata", "qdata", "experiment_files", "other_data") %in% dir(pth))) - res <- MsIO:::readAlabasterMsExperiment(pth) + res <- readAlabasterMsExperiment(pth) expect_s4_class(res, "MsExperiment") expect_equal(assayNames(res@qdata), assayNames(se)) expect_equal(rowData(res@qdata), rowData(se)) expect_equal(colData(res@qdata), colData(se)) expect_equal(assay(res@qdata)[[1L]], assay(se)[[1L]]) + with_mock( + "MsIO:::.is_ms_experiment_installed" = function() FALSE, + expect_error(readAlabasterMsExperiment(pth), "'MsExperiment' missing") + ) + with_mock( + "MsIO:::.is_alabaster_se_installed" = function() FALSE, + expect_error(readAlabasterMsExperiment(pth), "alabaster.se' not") + ) + l <- readLines(file.path(pth, "qdata", "OBJECT")) + l <- sub("summarized_experiment", "q_features", l) + writeLines(l, file.path(pth, "qdata", "OBJECT")) + expect_error(readAlabasterMsExperiment(pth), "not be imported") + ## Non-empty object with MsExperimentFiles m@experimentFiles <- MsExperimentFiles( list(data = c(a = "a.txt", b = "b.txt"), annotation = "ann.txt")) @@ -163,3 +202,23 @@ test_that("saveMsObject,MsExperiment,AlabasterParam works", { expect_s4_class(m_in, "MsExperiment") expect_equal(mz(spectra(m_in)[1:10]), mz(spectra(mse[1L])[1:10])) }) + +test_that("readMsObject,MsExperiment,MetaboLightsParam works", { + p <- MetaboLightsParam(mtblsId = "MTBLS39", filePattern = "63A.cdf") + res <- readMsObject(MsExperiment::MsExperiment(), p) + expect_s4_class(res, "MsExperiment") + expect_true(length(res) > 0) + p <- MetaboLightsParam(mtblsId = "MTBLS39", filePattern = "63A.cdf", + assayName = "aa") + expect_error(readMsObject(MsExperiment::MsExperiment(), p), "not exist.") + with_mock( + "MsIO:::.is_ms_backend_metabo_lights_installed" = function() FALSE, + expect_error(readMsObject(MsExperiment::MsExperiment(), p), + "'MsBackendMetaboLights'") + ) + with_mock( + "MsIO:::.is_spectra_installed" = function() FALSE, + expect_error(readMsObject(MsExperiment::MsExperiment(), p), + "'Spectra' is missing") + ) +}) diff --git a/tests/testthat/test_Spectra.R b/tests/testthat/test_Spectra.R index b516d5b..6b61103 100644 --- a/tests/testthat/test_Spectra.R +++ b/tests/testthat/test_Spectra.R @@ -41,6 +41,10 @@ test_that("saveMsObject,readMsObject,PlainTextParam,Spectra works", { expect_equal(mz(s[1:10]), mz(s_load[1:10])) expect_no_error(filterRt(s_load, c(3000, 3500))) + l <- readLines(file.path(pth, "spectra_slots.txt")) + writeLines(sub("MsBackendMzR", "M", l), file.path(pth, "spectra_slots.txt")) + expect_error(readMsObject(Spectra(), param), "with backend 'M'") + ## Errors param <- PlainTextParam(file.path(tempdir())) expect_error(readMsObject(Spectra(), param), "No 'spectra_slots") @@ -69,7 +73,14 @@ test_that("saveObject,readObject,saveMsObject,readMsObject,Spectra works", { expect_error(saveObject(Spectra(), path = pth), "available yet") + s <- Spectra() + b <- MsBackendMzR() + slot(b, "spectraData", check = FALSE) <- AlabasterParam() + slot(s, "backend", check = FALSE) <- b + expect_error(saveObject(s, path = pth), "failed to save 'backend'") + ## save/load real object + unlink(pth, recursive = TRUE) s <- sps_dda saveObject(s, pth) expect_silent(validateAlabasterSpectra(pth)) @@ -90,6 +101,11 @@ test_that("saveObject,readObject,saveMsObject,readMsObject,Spectra works", { res_2 <- readObject(pth) expect_equal(res, res_2) + with_mock( + "MsIO:::.is_spectra_installed" = function() FALSE, + expect_error(readAlabasterSpectra(pth), "'Spectra' missing") + ) + ## save/load empty object unlink(pth, recursive = TRUE) s <- sps_dda[integer()] @@ -139,4 +155,11 @@ test_that("saveObject,readObject,saveMsObject,readMsObject,Spectra works", { expect_equal(res@processingQueueVariables, ref@processingQueueVariables) expect_equal(rtime(res), rtime(ref)) expect_equal(mz(res[1:3]), mz(ref[1:3])) + + unlink(pth, recursive = TRUE) + saveMsObject(sps_dda, AlabasterParam(path = pth)) + res <- readMsObject(Spectra(), AlabasterParam(path = pth)) + expect_s4_class(res, "Spectra") + expect_equal(length(res), length(sps_dda)) + unlink(pth, recursive = TRUE) }) diff --git a/tests/testthat/test_XcmsExperiment.R b/tests/testthat/test_XcmsExperiment.R index 1cb3676..f6d8533 100644 --- a/tests/testthat/test_XcmsExperiment.R +++ b/tests/testthat/test_XcmsExperiment.R @@ -89,6 +89,19 @@ test_that("saveMsObject,readMsObject,PlainTextParam,XcmsExperiment works", { test_that("saveObject,readObject,XcmsExperiment works", { pth <- file.path(tempdir(), "xcms_experiment_alabaster") + with_mock( + "MsIO:::.is_alabaster_matrix_installed" = function() FALSE, + expect_error(saveObject(XcmsExperiment(), pth), "alabaster.matrix'") + ) + with_mock( + "MsIO:::.is_xcms_installed" = function() FALSE, + expect_error(readAlabasterXcmsExperiment(pth), "xcms'") + ) + with_mock( + "MsIO:::.is_ms_experiment_installed" = function() FALSE, + expect_error(readAlabasterXcmsExperiment(pth), "MsExperiment'") + ) + ## Empty object. m <- XcmsExperiment() saveObject(m, pth) @@ -97,8 +110,8 @@ test_that("saveObject,readObject,XcmsExperiment works", { "experiment_files", "other_data", "chrom_peaks", "chrom_peak_data", "feature_definitions") %in% dir(pth))) expect_true(!any(dir(pth) %in% c("spectra", "qdata"))) - expect_silent(MsIO:::validateAlabasterXcmsExperiment(pth)) - res <- MsIO:::readAlabasterXcmsExperiment(pth) + expect_silent(validateAlabasterXcmsExperiment(pth)) + res <- readAlabasterXcmsExperiment(pth) expect_equal(res, m) ## Real object @@ -107,8 +120,8 @@ test_that("saveObject,readObject,XcmsExperiment works", { unlink(pth, recursive = TRUE) saveObject(m, pth) - expect_silent(MsIO:::validateAlabasterXcmsExperiment(pth)) - res <- MsIO:::readAlabasterXcmsExperiment(pth) + expect_silent(validateAlabasterXcmsExperiment(pth)) + res <- readAlabasterXcmsExperiment(pth) expect_s4_class(res, "XcmsExperiment") expect_equal(res@chromPeaks, m@chromPeaks) expect_equal(res@chromPeakData, m@chromPeakData) @@ -160,3 +173,66 @@ test_that("saveMsObject,XcmsExperiment,AlabasterParam works", { ## Check that access to MS data works expect_true(length(mz(spectra(m_in)[1L])) > 0) }) + +test_that(".import_chrom_peaks works", { + pth <- tempdir() + expect_error(.import_chrom_peaks(xcmse, pth), "chrom_peaks.txt") + write.table(chromPeaks(xmse), + file = file.path(pth, "xcms_experiment_chrom_peaks.txt"), + sep = "\t") + expect_error(.import_chrom_peaks(xmse, pth), + "chrom_peak_data.txt") + file.remove(file.path(pth, "xcms_experiment_chrom_peaks.txt")) +}) + +test_that(".import_features works", { + pth <- tempdir() + write.table( + featureDefinitions(xmse)[, 1:8], + file = file.path(pth, "xcms_experiment_feature_definitions.txt"), + sep = "\t") + expect_error(.import_features(xmse, pth), "feature_peak_index.txt") +}) + +test_that(".import_process_history works", { + pth <- tempdir() + expect_error(.import_process_history(xmse, pth), "process_history.json") +}) + +test_that("saveMsObject,mzTabParam works", { + faahko <- loadXcmsData("faahko_sub2") + faahko <- groupChromPeaks( + faahko, PeakDensityParam(sampleGroups = rep(1, length(faahko)))) + + d <- file.path(tempdir(), "mzt_test") + dir.create(d, recursive = TRUE) + + ## errors + expect_error( + saveMsObject(faahko, mzTabParam(studyId = "test_study", path = d, + sampleDataColumn = "sample_name")), + "has to correspond to column names of the sampleData()") + expect_error( + saveMsObject(faahko, mzTabParam(studyId = "test_study", path = d, + sampleDataColumn = "sample_index", + optionalFeatureColumns = "other")), + "'optionalFeatureColumns' have to correspond") + + p <- mzTabParam(studyId = "test_study", path = d, + sampleDataColumn = "sample_index", + optionalFeatureColumns = "peakidx") + saveMsObject(faahko, p) + expect_true(file.exists(file.path(d, "test_study.mztab"))) + res <- readLines(file.path(d, "test_study.mztab")) + expect_true(length(res) > 0L) + expect_true(length(grep("^MTD", res)) > 0) + expect_true(length(grep("^SML", res)) > 0) + expect_true(length(grep("^SMF", res)) > 0) + ## Check for empty lines + expect_true(length(grep(c("^MTD|SML|SMF"), res, invert = TRUE)) == 2) + + expect_error( + saveMsObject(faahko, p), "File \"test_study.mztab\" already exists") + + unlink(d, recursive = TRUE) +}) diff --git a/tests/testthat/test_mzTabParam.R b/tests/testthat/test_mzTabParam.R index 8cbd262..a1e0b49 100644 --- a/tests/testthat/test_mzTabParam.R +++ b/tests/testthat/test_mzTabParam.R @@ -77,19 +77,3 @@ test_that(".mztab_study_variable_entries works", { res2 <- .mztab_study_variable_entries(x, variable = c("a", "b")) expect_true(length(res2) > length(res)) }) - -test_that("saveMsObject,mzTabParam works", { - d <- tempdir() - p <- mzTabParam(studyId = "test_study", path = d, - sampleDataColumn = "sample_index", - optionalFeatureColumns = "peakidx") - saveMsObject(faahko, p) - expect_true(file.exists(file.path(d, "test_study.mztab"))) - res <- readLines(file.path(d, "test_study.mztab")) - expect_true(length(res) > 0L) - expect_true(length(grep("^MTD", res)) > 0) - expect_true(length(grep("^SML", res)) > 0) - expect_true(length(grep("^SMF", res)) > 0) - ## Check for empty lines - expect_true(length(grep(c("^MTD|SML|SMF"), res, invert = TRUE)) == 2) -}) diff --git a/tests/testthat/test_utils.R b/tests/testthat/test_utils.R new file mode 100644 index 0000000..8280388 --- /dev/null +++ b/tests/testthat/test_utils.R @@ -0,0 +1,23 @@ +test_that(".is_alabaster_matrix_installed works", { + expect_true(.is_alabaster_matrix_installed()) +}) + +test_that(".is_spectra_installed works", { + expect_true(.is_spectra_installed()) +}) + +test_that(".is_alabaster_se_installed works", { + expect_true(.is_alabaster_se_installed()) +}) + +test_that(".is_ms_experiment_installed works", { + expect_true(.is_ms_experiment_installed()) +}) + +test_that(".is_ms_backend_metabo_lights_installed works", { + expect_true(.is_ms_backend_metabo_lights_installed()) +}) + +test_that(".is_xcms_installed works", { + expect_true(.is_xcms_installed()) +}) diff --git a/tests/testthat/test_zzz.R b/tests/testthat/test_zzz.R new file mode 100644 index 0000000..c0e4dad --- /dev/null +++ b/tests/testthat/test_zzz.R @@ -0,0 +1,24 @@ +test_that(".onLoad works", { + expect_true( + alabaster.base:::deregister_validate_function("ms_backend_mz_r")) + expect_false( + alabaster.base:::deregister_validate_function("ms_backend_mz_r")) + reg <- alabaster.base:::read.registry$registry + reg$ms_backend_mz_r <- NULL + reg$spectra <- NULL + reg$ms_experiment_files <- NULL + reg$ms_experiment <- NULL + reg$xcms_experiment <- NULL + assign("registry", reg, envir = alabaster.base:::read.registry) + expect_false(any(names(alabaster.base:::read.registry$register) %in% + c("ms_backend_mz_r", "ms_experiment_files", + "ms_experiment", "xcms_experiment"))) + .onLoad() + expect_true(all(c("ms_backend_mz_r", "ms_experiment_files", + "ms_experiment", "xcms_experiment") %in% + names(alabaster.base:::read.registry$registry))) + + expect_true( + alabaster.base:::deregister_validate_function("ms_backend_mz_r")) + .onLoad() +})