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

Introducing filterRanges Function and Discussion on a possible filterSimilarities #312

Merged
merged 17 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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.13.4
Version: 1.13.5
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ exportMethods(filterPrecursorMz)
exportMethods(filterPrecursorMzRange)
exportMethods(filterPrecursorMzValues)
exportMethods(filterPrecursorScan)
exportMethods(filterRanges)
exportMethods(filterRt)
exportMethods(intensity)
exportMethods(ionCount)
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.13

## Changes in 1.13.5

- Add `filterRanges` function to allow to filter the spectra object based on
philouail marked this conversation as resolved.
Show resolved Hide resolved
ranges of any variables of the `spectraData`.

## Changes in 1.13.4

- Add `entropy` and `nentropy` functions to allow to calculate the (normalized)
Expand Down
3 changes: 3 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ setGeneric("filterPrecursorMzValues", function(object, ...)
setGeneric("filterPrecursorMzRange", function(object, ...)
standardGeneric("filterPrecursorMzRange"))
#' @rdname hidden_aliases
setGeneric("filterRanges", function(object, ...)
standardGeneric("filterRanges"))
#' @rdname hidden_aliases
setGeneric("isReadOnly", function(object, ...)
standardGeneric("isReadOnly"))
#' @rdname neutralLoss
Expand Down
61 changes: 58 additions & 3 deletions R/Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ NULL
#' `rt[2]`. Returns the filtered `Spectra` (with spectra in their
#' original order).
#'
#' - `filterRanges`: allows filtering of the `Spectra` object
#' based on specified ranges for *any* values of `spectraVariables(object)`.
jorainer marked this conversation as resolved.
Show resolved Hide resolved
#'
#' - `reduceSpectra`: for groups of peaks within highly similar m/z values
#' within each spectrum (given `ppm` and `tolerance`), this function keeps
#' only the peak with the highest intensity removing all other peaks hence
Expand Down Expand Up @@ -956,6 +959,9 @@ NULL
#' @param processingQueue For `Spectra`: optional `list` of
#' [ProcessingStep-class] objects.
#'
#' @param ranges for `filterRanges`: A `numeric` vector of paired values that
philouail marked this conversation as resolved.
Show resolved Hide resolved
#' define the ranges to filter the spectra data.
#'
#' @param rt for `filterRt`: `numeric(2)` defining the retention time range to
#' be used to subset/filter `object`.
#'
Expand All @@ -971,11 +977,15 @@ NULL
#' to import spectrum data from the provided files. See section *Creation
#' of objects, conversion and changing the backend* for more details.
#'
#' @param spectraVariables For `selectSpectraVariables`: `character` with the
#' @param spectraVariables
#' - For `selectSpectraVariables`: `character` with the
#' names of the spectra variables to which the backend should be subsetted.
#' For `addProcessing`: `character` with additional spectra variables that
#' - For `addProcessing`: `character` with additional spectra variables that
#' should be passed along to the function defined with `FUN`. See function
#' description for details.
#' - For `filterRanges`: A `character` or `integer` vector specifying the
philouail marked this conversation as resolved.
Show resolved Hide resolved
#' column from `spectraVariables(object)` that correspond to the ranges
#' provided. The order must match the order of the parameter `ranges`.
#'
#' @param substDefinition For `deisotopeSpectra` and `filterPrecursorIsotopes`:
#' `matrix` or `data.frame` with definitions of isotopic substitutions.
Expand Down Expand Up @@ -1256,6 +1266,13 @@ NULL
#' length(mz(fft_spectrum_filtered)[[1]])
#' plotSpectra(fft_spectrum_filtered, xlim = c(264.5, 265.5), ylim = c(0, 5e6))
#'
#' ## using filterRanges to filter spectra object based on variables available
#' ## in `spectraData`.
#' spectraVariables <- c("rtime", "precursorMz", "peaksCount")
philouail marked this conversation as resolved.
Show resolved Hide resolved
philouail marked this conversation as resolved.
Show resolved Hide resolved
#' ranges <- c(30, 350, 200,500, 350, 600)
philouail marked this conversation as resolved.
Show resolved Hide resolved
#' filt_spectra <- filterRanges(sciex, spectraVariables = spectraVariables,
#' ranges = ranges)
#'
#' ## ---- DATA MANIPULATIONS AND OTHER OPERATIONS ----
#'
#' ## Set the data to be centroided
Expand Down Expand Up @@ -2396,6 +2413,44 @@ setMethod("reset", "Spectra", function(object, ...) {
object
})


#' @rdname Spectra
#' @importFrom MsCoreUtils between
#' @export
setMethod("filterRanges", "Spectra",
function(object, spectraVariables, ranges, ...){
if (is.logical(spectraVariables))
philouail marked this conversation as resolved.
Show resolved Hide resolved
spectraVariables <- which(spectraVariables)
if (length(spectraVariables) != length(ranges) / 2)
stop("Length of 'spectraVariables' must be half the length ",
"of 'ranges'")
if (is.character(spectraVariables)){
if(!all(spectraVariables %in% spectraVariables(object)))
stop("'spectraVariables' need to correspond to colnames of",
philouail marked this conversation as resolved.
Show resolved Hide resolved
"the 'spectraData' of the object")
}
query <- spectraData(object)[, spectraVariables]
philouail marked this conversation as resolved.
Show resolved Hide resolved
nc <- ncol(query)

within_ranges <- vapply(seq_len(nc), function(i) {
pairs <- c(ranges[2*i - 1], ranges[2*i])
between(query[[i]], pairs)
}, logical(nrow(query)))

idc <- which(rowSums(within_ranges, na.rm = FALSE) == nc)
jorainer marked this conversation as resolved.
Show resolved Hide resolved
object@processing <- .logging(object@processing,
"Filter: select spectra with a ",
spectraVariables, " within: [",
ranges[seq(ranges)%% 2 == 0], ", ",
ranges[seq(ranges)%% 2 != 0], "]"
)
message("Started with ", length(object),
philouail marked this conversation as resolved.
Show resolved Hide resolved
" spectra, after filtering ", length(idc),
" spectra are left")
object <- object[idc]
})


#### ---------------------------------------------------------------------------
##
## DATA MANIPULATION METHODS
Expand Down Expand Up @@ -2666,4 +2721,4 @@ setMethod("entropy", "Spectra", function(object, normalized = TRUE) {
#' @rdname Spectra
setMethod("entropy", "ANY", function(object, ...) {
MsCoreUtils::entropy(object)
})
})
17 changes: 10 additions & 7 deletions man/MsBackend.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/MsBackendCached.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 33 additions & 12 deletions man/Spectra.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading