Skip to content

Commit

Permalink
Merge branch 'main' into filterPrecursorPeaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed Aug 3, 2023
2 parents 536dd60 + 59dd2c1 commit f35bd5f
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(filterPrecursorMaxIntensity)
export(filterPrecursorPeaks)
export(joinPeaks)
export(joinPeaksGnps)
export(joinPeaksNone)
export(joinSpectraData)
export(plotMzDelta)
export(plotSpectra)
Expand Down
12 changes: 11 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# Spectra 1.11

## Changes in 1.11.7
## Changes in 1.11.8

- Add `filterPrecursorPeaks` to allow filtering peaks within each spectrum
with m/z values relative to the precursor m/z of the spectrum.

## Changes in 1.11.7

- Fix in `compareSpectra` to also pass parameters `ppm` and `tolerance` to
the peak similarity calculation functions `FUN`: this allows to use custom
similarity function with integrated mapping of peaks.
- Add `joinPeaksNone` to skip the peak matching in `compareSpectra` if the
similarity scoring function performs its own peak matching.
- Only use parallel processing in `setBackend,Spectra` if both backends support
it.

## Changes in 1.11.6

- Add `filterPrecursorMaxIntensity` function.
Expand Down
2 changes: 1 addition & 1 deletion R/Spectra-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ applyProcessing <- function(object, f = dataStorage(object),
.check = FALSE,...)
m[cb[i, 1L], cur] <- m[cur, cb[i, 1L]] <-
FUN(map[[1L]], map[[2L]], xPrecursorMz = pmzx,
yPrecursorMz = pmzy, ...)
yPrecursorMz = pmzy, ppm = ppm, tolerance = tolerance, ...)
}
m
}
Expand Down
6 changes: 4 additions & 2 deletions R/Spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,8 @@ NULL
#' the spectra's precursor m/z values are passed to the function as parameters
#' `xPrecursorMz` (precursor m/z of the `x` peak matrix) and `yPrecursorMz`
#' (precursor m/z of the `y` peak matrix). Additional parameters to functions
#' `FUN` and `MAPFUN` can be passed with `...`.
#' `FUN` and `MAPFUN` can be passed with `...`. Parameters `ppm` and
#' `tolerance` are passed to both `MAPFUN` and `FUN`.
#' The function returns a `matrix` with the results of `FUN` for each
#' comparison, number of rows equal to `length(x)` and number of columns
#' equal `length(y)` (i.e. element in row 2 and column 3 is the result from
Expand Down Expand Up @@ -1529,6 +1530,7 @@ setMethod(
BPPARAM = bpparam()) {
backend_class <- class(object@backend)
BPPARAM <- backendBpparam(object@backend, BPPARAM)
BPPARAM <- backendBpparam(backend, BPPARAM)
if (!supportsSetBackend(backend))
stop(class(backend), " does not support 'setBackend'")
if (!length(object)) {
Expand All @@ -1538,7 +1540,7 @@ setMethod(
f <- force(factor(f, levels = unique(f)))
if (length(f) != length(object))
stop("length of 'f' has to match the length of 'object'")
if (length(levels(f)) == 1L) {
if (length(levels(f)) == 1L || inherits(BPPARAM, "SerialParam")) {
bknds <- backendInitialize(
backend, data = spectraData(object@backend), ...)
} else {
Expand Down
3 changes: 2 additions & 1 deletion R/peak-list-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ combinePeaksData <- function(x, intensityFun = base::mean,
.check = FALSE, ...)
mat[i, j] <- FUN(peak_map[[1L]], peak_map[[2L]],
xPrecursorMz = xPrecursorMz[i],
yPrecursorMz = yPrecursorMz[j], ...)
yPrecursorMz = yPrecursorMz[j],
ppm = ppm, tolerance = tolerance, ...)
}
}
mat
Expand Down
15 changes: 14 additions & 1 deletion R/peaks-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ NULL
#' between `x` and `y` are returned by `type = "inner"`, i.e. only
#' peaks present in both spectra are reported.
#'
#' - `joinPeaksGnps` matches/maps peaks between spectra with the same approach
#' - `joinPeaksGnps`: matches/maps peaks between spectra with the same approach
#' used in GNPS: peaks are considered matching if a) the
#' difference in their m/z values is smaller than defined by `tolerance`
#' and `ppm` (this is the same as `joinPeaks`) **and** b) the difference of
Expand All @@ -250,6 +250,12 @@ NULL
#' `compareSpectra` should be called with `MAPFUN = joinPeaksGnps` and
#' `FUN = MsCoreUtils::gnps`).
#'
#' - `joinPeaksNone`: does not perform any peak matching but simply returns
#' the peak matrices in a `list`. This function should be used with the
#' `MAPFUN` parameter of [compareSpectra()] if the spectra similarity
#' function used (parameter `FUN` of `compareSpectra`) performs its own
#' peak matching and does hence not expect matched peak matrices as an input.
#'
#' @section Implementation notes:
#'
#' A mapping function must take two numeric matrices `x` and `y` as input and
Expand Down Expand Up @@ -363,6 +369,13 @@ joinPeaksGnps <- function(x, y, xPrecursorMz = NA_real_,
list(x = x[map[[1L]], , drop = FALSE], y = y[map[[2L]], , drop = FALSE])
}

#' @export
#'
#' @rdname joinPeaks
joinPeaksNone <- function(x, y, ...) {
list(x = x, y = y)
}

#' @importFrom MsCoreUtils localMaxima noise refineCentroids
#'
#' @description
Expand Down
3 changes: 2 additions & 1 deletion man/Spectra.Rd

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

10 changes: 9 additions & 1 deletion man/joinPeaks.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test_peaks-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ test_that(".peaks_bin works", {
expect_equal(res[, 2], c(1, 5, 1, 0, 1, 13, 8, 1, 3, 0, 6, 15, 1, 0, 0))
})

test_that("joinPeaksNone works", {
x <- cbind(c(31.34, 50.14, 60.3, 120.9, 230, 514.13, 874.1),
1:7)
y <- cbind(c(12, 31.35, 70.3, 120.9 + ppm(120.9, 5),
230 + ppm(230, 10), 315, 514.14, 901, 1202),
1:9)
res <- joinPeaksNone(x, y, ppm = 20)
expect_equal(res, list(x = x, y = y))
})

test_that("joinPeaks works", {
x <- cbind(c(31.34, 50.14, 60.3, 120.9, 230, 514.13, 874.1),
1:7)
Expand Down

0 comments on commit f35bd5f

Please sign in to comment.