diff --git a/.Rbuildignore b/.Rbuildignore index 22a5d1be..37442c70 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,6 +1,7 @@ ^\.github$ .editorconfig .travis.yml +man/figures* local_data favicon logo.png diff --git a/DESCRIPTION b/DESCRIPTION index c233fc3c..c547b903 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: Spectra Title: Spectra Infrastructure for Mass Spectrometry Data -Version: 1.15.7 +Version: 1.15.8 Description: The Spectra package defines an efficient infrastructure for storing and handling mass spectrometry spectra and functionality to subset, process, visualize and compare spectra data. It provides different diff --git a/NEWS.md b/NEWS.md index 49e8e289..2d6c6193 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # Spectra 1.15 +## Changes in 1.15.8 + +- Refactor the `Spectra()` constructor method: better support for + initialization of backends that define their own specific parameters. + ## Changes in 1.15.7 - Change `estimatePrecursorIntensity()` to a method to avoid overrides/clashes diff --git a/R/MsBackendMzR.R b/R/MsBackendMzR.R index 7cadc0d5..69a04987 100644 --- a/R/MsBackendMzR.R +++ b/R/MsBackendMzR.R @@ -43,12 +43,14 @@ setValidity("MsBackendMzR", function(object) { #' @importFrom BiocParallel bpparam setMethod("backendInitialize", "MsBackendMzR", function(object, files, ..., BPPARAM = bpparam()) { - if (missing(files) || !length(files)) + if (missing(files)) stop("Parameter 'files' is mandatory for 'MsBackendMzR'") if (!is.character(files)) stop("Parameter 'files' is expected to be a character vector", " with the files names from where data should be", " imported") + if (!length(files)) + return(object) files <- normalizePath(files, mustWork = FALSE) msg <- .valid_ms_backend_files_exist(files) if (length(msg)) diff --git a/R/Spectra.R b/R/Spectra.R index 76a467c5..9bcc784c 100644 --- a/R/Spectra.R +++ b/R/Spectra.R @@ -1634,8 +1634,10 @@ setMethod("Spectra", "missing", function(object, processingQueue = list(), metadata = list(), ..., backend = MsBackendMemory(), BPPARAM = bpparam()) { - new("Spectra", metadata = metadata, processingQueue = processingQueue, - backend = backend) + if (length(backend)) + new("Spectra", metadata = metadata, processingQueue = processingQueue, + backend = backend) + else callNextMethod() }) #' @rdname Spectra @@ -1654,13 +1656,12 @@ setMethod("Spectra", "character", function(object, processingQueue = list(), source = MsBackendMzR(), backend = source, ..., BPPARAM = bpparam()) { - if (!length(object)) - Spectra(backend, metadata = metadata, - processingQueue = processingQueue) - else - callNextMethod(object = object, processingQueue = processingQueue, - metadata = metadata, source = source, backend = backend, - ..., BPPARAM = BPPARAM) + sp <- .create_spectra(object, processingQueue = processingQueue, + metadata = metadata, backend = source, + ..., BPPARAM = BPPARAM) + if (class(source)[1L] != class(backend)[1L]) + setBackend(sp, backend, ..., BPPARAM = backendBpparam(backend, BPPARAM)) + else sp }) #' @rdname Spectra @@ -1669,15 +1670,26 @@ setMethod("Spectra", "ANY", function(object, processingQueue = list(), source = MsBackendMemory(), backend = source, ..., BPPARAM = bpparam()) { - sp <- new("Spectra", metadata = metadata, processingQueue = processingQueue, - backend = backendInitialize( - source, object, ..., - BPPARAM = backendBpparam(source, BPPARAM))) + sp <- .create_spectra(object, processingQueue = processingQueue, + metadata = metadata, backend = source, + ..., BPPARAM = BPPARAM) if (class(source)[1L] != class(backend)[1L]) setBackend(sp, backend, ..., BPPARAM = backendBpparam(backend, BPPARAM)) else sp }) +.create_spectra <- function(object, processingQueue = list(), metadata = list(), + backend = MsBackendMemory(), ..., + BPPARAM = bpparam()) { + if (missing(object)) + backend <- backendInitialize( + backend, ..., BPPARAM = backendBpparam(backend, BPPARAM)) + else backend <- backendInitialize( + backend, object, ..., BPPARAM = backendBpparam(backend, BPPARAM)) + new("Spectra", metadata = metadata, processingQueue = processingQueue, + backend = backend) +} + #' @rdname Spectra #' #' @importMethodsFrom ProtGenerics setBackend diff --git a/tests/testthat/test_Spectra.R b/tests/testthat/test_Spectra.R index e81efdcb..43638a4d 100644 --- a/tests/testthat/test_Spectra.R +++ b/tests/testthat/test_Spectra.R @@ -13,32 +13,48 @@ test_that("Spectra,ANY works", { df$polarity <- "NEG" expect_error(Spectra(df), "wrong data type: polarity") + + res <- Spectra(files = sciex_file, source = MsBackendMzR()) + expect_s4_class(res@backend, "MsBackendMzR") + expect_true(length(res) > 1) }) test_that("Spectra,missing works", { res <- Spectra() expect_true(length(res) == 0) + expect_s4_class(res@backend, "MsBackendMemory") + + res <- Spectra(backend = MsBackendDataFrame()) + expect_true(length(res) == 0) + expect_s4_class(res@backend, "MsBackendDataFrame") + + res <- Spectra(source = MsBackendDataFrame()) + expect_true(length(res) == 0) + expect_s4_class(res@backend, "MsBackendDataFrame") be <- backendInitialize(MsBackendDataFrame(), DataFrame(msLevel = c(1L, 2L), fromFile = 1L)) res <- Spectra(backend = be) + expect_s4_class(res@backend, "MsBackendDataFrame") expect_true(length(res) == 2) expect_identical(msLevel(res), c(1L, 2L)) }) test_that("Spectra,MsBackend works", { - res <- Spectra() - expect_true(length(res) == 0) - - be <- backendInitialize(MsBackendDataFrame(), DataFrame(msLevel = c(1L, 2L), - fromFile = 1L)) + be <- backendInitialize(MsBackendDataFrame(), + DataFrame(msLevel = c(1L, 2L), + fromFile = 1L)) res <- Spectra(be) expect_true(length(res) == 2) expect_identical(msLevel(res), c(1L, 2L)) }) test_that("Spectra,character works", { - res <- Spectra(sciex_file, backend = MsBackendMzR()) + res <- Spectra(sciex_file) + expect_true(is(res@backend, "MsBackendMzR")) + expect_true(length(res) > 0) + + res <- Spectra(sciex_file, source = MsBackendMzR()) expect_true(is(res@backend, "MsBackendMzR")) expect_equal(unique(res@backend$dataStorage), sciex_file) expect_identical(rtime(res), rtime(sciex_mzr)) @@ -51,7 +67,7 @@ test_that("Spectra,character works", { show(res) ## Empty character - res <- Spectra(character(), backend = MsBackendMzR()) + res <- Spectra(character()) expect_s4_class(res, "Spectra") expect_s4_class(res@backend, "MsBackendMzR") expect_true(length(res) == 0) @@ -62,6 +78,37 @@ test_that("Spectra,character works", { expect_true(length(res) == 0) }) +test_that(".create_spectra works, ", { + ## missing object + res <- .create_spectra() + expect_true(length(res) == 0) + expect_s4_class(res@backend, "MsBackendMemory") + expect_error(res <- .create_spectra(backend = MsBackendMzR()), "mandatory") + + ## object being a character, backend a MsBackendMemory -> error + res <- expect_error(.create_spectra(sciex_file), "DataFrame") + ## object being a character, backend a MsBackendMzR + res <- .create_spectra(sciex_file, backend = MsBackendMzR()) + expect_s4_class(res@backend, "MsBackendMzR") + dta <- spectraData(res@backend) + + ## object being a DataFrame, backend a MsBackendDataFrame + res <- .create_spectra(dta, backend = MsBackendDataFrame()) + expect_s4_class(res@backend, "MsBackendDataFrame") + expect_equal(res$msLevel, dta$msLevel) + + ## object missing but providing files + res <- .create_spectra(files = sciex_file, backend = MsBackendMzR()) + expect_s4_class(res@backend, "MsBackendMzR") + expect_equal(res$msLevel, dta$msLevel) + + ## object missing but providing data + res <- .create_spectra(data = dta, backend = MsBackendMemory()) + expect_s4_class(res@backend, "MsBackendMemory") + expect_equal(res$msLevel, dta$msLevel) + +}) + test_that("setBackend,Spectra works", { df <- DataFrame(rtime = as.numeric(1:9), fact = c(2L, 1L, 2L, 1L, 3L, 2L, 3L, 3L, 1L))