Skip to content

Commit

Permalink
Merge pull request #330 from rformassspectrometry/jomain
Browse files Browse the repository at this point in the history
Improve and fix the Spectra constructor method
  • Loading branch information
jorainer authored Sep 13, 2024
2 parents 69da6d6 + 2f11a85 commit f37fb1d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 22 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
^\.github$
.editorconfig
.travis.yml
man/figures*
local_data
favicon
logo.png
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion R/MsBackendMzR.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
38 changes: 25 additions & 13 deletions R/Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
61 changes: 54 additions & 7 deletions tests/testthat/test_Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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))
Expand Down

0 comments on commit f37fb1d

Please sign in to comment.