Skip to content

Commit

Permalink
refactor: import filterSpectra from MsExperiment
Browse files Browse the repository at this point in the history
- Import `filterSpectra` from `MsExperiment`.
  • Loading branch information
jorainer committed Jan 26, 2024
1 parent 601f76a commit 49c1fe2
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 74 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check-bioc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ jobs:
BiocManager::install("MsBackendMgf")
BiocManager::install("MetaboCoreUtils")
BiocManager::install("magick")
BiocManager::install("RforMassSpectrometry/MsExperiment")
## For running the checks
message(paste('****', Sys.time(), 'installing rcmdcheck and BiocCheck ****'))
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Imports:
SummarizedExperiment,
MsCoreUtils (>= 1.15.3),
MsFeatures,
MsExperiment (>= 1.5.3),
MsExperiment (>= 1.5.4),
Spectra (>= 1.13.2),
progress,
multtest,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ importFrom("Spectra", "MsBackendMemory")
## MsExperiment things
importClassesFrom("MsExperiment", "MsExperiment")
importMethodsFrom("MsExperiment", "spectra")
importMethodsFrom("MsExperiment", "filterSpectra")
importMethodsFrom("BiocGenerics", "do.call")
importMethodsFrom("BiocGenerics", "rbind")
importFrom("MsExperiment", "MsExperiment")
Expand Down
31 changes: 0 additions & 31 deletions R/MsExperiment-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,37 +247,6 @@
USE.NAMES = FALSE, BPPARAM = BPPARAM)
}

#' generic method to apply a filtering to the spectra data. The function will
#' apply the filtering and (most importantly) keep/update the link between
#' spectra and samples.
#'
#' @importMethodsFrom Spectra selectSpectraVariables
#'
#' @param x `MsExperiment`.
#'
#' @param FUN filter function.
#'
#' @param ... parameters for `FUN`.
#'
#' @author Johannes Rainer
#'
#' @noRd
.mse_filter_spectra <- function(x, FUN, ...) {
ls <- length(spectra(x))
have_links <- length(x@sampleDataLinks[["spectra"]]) > 0
if (have_links)
x@spectra$._SPECTRA_IDX <- seq_len(ls)
x@spectra <- FUN(x@spectra, ...)
if (have_links) {
if (ls != length(spectra(x)))
x <- .update_sample_data_links_spectra(x)
svs <- unique(c(spectraVariables(spectra(x)), "mz", "intensity"))
x@spectra <- selectSpectraVariables(
x@spectra, svs[svs != "._SPECTRA_IDX"])
}
x
}

#' Ensure that each spectrum is assigned to a sample and that we only have 1:1
#' mappings. That is important for most code involving splitting of samples
#' etc.
Expand Down
8 changes: 3 additions & 5 deletions R/MsExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
setMethod("filterRt", "MsExperiment",
function(object, rt = numeric(), ...) {
message("Filter spectra")
object <- .mse_filter_spectra(object, filterRt, rt = rt, ...)
object
filterSpectra(object, filterRt, rt = rt, ...)
})

#' @rdname XcmsExperiment
Expand All @@ -24,8 +23,7 @@ setMethod("filterMz", "MsExperiment",
setMethod("filterMsLevel", "MsExperiment",
function(object, msLevel. = uniqueMsLevels(object)) {
message("Filter spectra")
.mse_filter_spectra(object, filterMsLevel,
msLevel. = msLevel.)
filterSpectra(object, filterMsLevel, msLevel. = msLevel.)
})

#' @rdname XcmsExperiment
Expand Down Expand Up @@ -93,7 +91,7 @@ setMethod("polarity", "MsExperiment", function(object) {
#' @rdname XcmsExperiment
setMethod(
"filterIsolationWindow", "MsExperiment", function(object, mz = numeric()) {
.mse_filter_spectra(object, filterIsolationWindow, mz = mz)
filterSpectra(object, filterIsolationWindow, mz = mz)
})

#' @rdname XcmsExperiment
Expand Down
2 changes: 1 addition & 1 deletion R/XcmsExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ setMethod(
function(object, mz = numeric()) {
if (length(mz) > 1L)
mz <- mz[1L]
object <- .mse_filter_spectra(object, filterIsolationWindow, mz = mz)
object <- filterSpectra(object, filterIsolationWindow, mz = mz)
if (hasChromPeaks(object) && length(mz) &&
all(c("isolationWindowLowerMz", "isolationWindowUpperMz") %in%
colnames(object@chromPeakData))) {
Expand Down
36 changes: 0 additions & 36 deletions tests/testthat/test_MsExperiment-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,42 +141,6 @@ test_that(".mse_find_chrom_peaks_chunk works", {
expect_true(is.null(res[[2L]]))
})

test_that(".mse_filter_spectra works", {
## Create a custom, small test object.
fls <- normalizePath(faahko_3_files)
df <- data.frame(mzML_file = basename(fls),
dataOrigin = fls,
sample = c("ko15", "ko16", "ko18"))
a <- Spectra::Spectra(fls[1])
b <- Spectra::Spectra(fls[2])
c <- Spectra::Spectra(fls[3])

## Select first 10 spectra from a and c and last 10 from b and combine them
sps <- c(a[1:10], b[(length(b)-9):length(b)], c[1:10])

tst <- MsExperiment()
spectra(tst) <- sps
sampleData(tst) <- DataFrame(df)
## Link samples to spectra.
tst <- linkSampleData(tst, with = "sampleData.dataOrigin = spectra.dataOrigin")

res <- .mse_filter_spectra(tst, filterRt, rt = c(2502, 2505))
expect_true(length(spectra(res)) == 4L)
expect_equal(res@sampleDataLinks[["spectra"]],
cbind(c(1L, 1L, 3L, 3L), 1:4))

## Some artificial sample assignment.
tst@sampleDataLinks[["spectra"]] <- cbind(
c(1, 1, 1, 2, 2, 2, 3, 3, 3),
c(2, 3, 4, 2, 3, 4, 2, 3, 4))
res <- .mse_filter_spectra(tst, filterRt, rt = c(2502, 2505))
## Filtering will filter based on rt
expect_true(length(spectra(res)) == 4L)
## Sample assignment should map the first 2 spectra to all 3 samples
expect_equal(res@sampleDataLinks[["spectra"]],
cbind(c(1L, 1L, 2L, 2L, 3L, 3L), c(1L, 2L, 1L, 2L, 1L, 2L)))
})

test_that(".mse_check_spectra_sample_mapping works", {
expect_true(length(.mse_check_spectra_sample_mapping(mse)) == 0)

Expand Down

0 comments on commit 49c1fe2

Please sign in to comment.