Skip to content

Commit

Permalink
Merge pull request #116 from rformassspectrometry/jomain
Browse files Browse the repository at this point in the history
Add parameter scalePeaks to plotSpectraMirror
  • Loading branch information
jorainer authored May 16, 2024
2 parents 33fc7a0 + f31941e commit b96137d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/check-bioc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ jobs:
fail-fast: false
matrix:
config:
- { os: ubuntu-latest, r: 'devel', bioc: '3.19', cont: "bioconductor/bioconductor_docker:devel", rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest" }
- { os: macOS-latest, r: 'devel', bioc: '3.19'}
- { os: windows-latest, r: 'devel', bioc: '3.19'}
- { os: ubuntu-latest, r: '4.4', bioc: '3.20', cont: "bioconductor/bioconductor_docker:devel", rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest" }
- { os: macOS-latest, r: '4.4', bioc: '3.20'}
- { os: windows-latest, r: '4.4', bioc: '3.20'}
env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MetaboAnnotation
Title: Utilities for Annotation of Metabolomics Data
Version: 1.7.5
Version: 1.9.1
Description:
High level functions to assist in annotation of (metabolomics) data sets.
These include functions to perform simple tentative annotations based on
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ importFrom(S4Vectors,endoapply)
importFrom(Spectra,MsBackendDataFrame)
importFrom(Spectra,Spectra)
importFrom(Spectra,joinPeaks)
importFrom(Spectra,scalePeaks)
importFrom(grDevices,n2mfrow)
importFrom(graphics,par)
importFrom(methods,"slot<-")
importFrom(methods,as)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# MetaboAnnotation 1.9

## Changes in 1.9.1

- Add parameter `scalePeaks` to `plotSpectraMirror` to allow scaling peak
intensities before plotting.

# MetaboAnnotation 1.7

## Changes in 1.7.5
Expand Down
46 changes: 30 additions & 16 deletions R/MatchedSpectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@
#'
#' - `plotSpectraMirror`: creates a mirror plot between the query and each
#' matching target spectrum. Can only be applied to a `MatchedSpectra` with a
#' single query spectrum. Additional plotting parameters can be passed through
#' `...`.
#' single query spectrum. Setting parameter `scalePeaks = TRUE` will scale
#' the peak intensities per spectrum to a total sum of one for a better
#' graphical visualization. Additional plotting parameters can be passed
#' through `...`.
#'
#' - `setBackend`: allows to change the *backend* of both the query and target
#' [Spectra()] object. The function will return a `MatchedSpectra` object with
Expand All @@ -123,6 +125,10 @@
#'
#' @param object `MatchedSpectra` object.
#'
#' @param scalePeaks for `plotSpectraMirror`: `logical(1)` if peak intensities
#' (per spectrum) should be scaled to a total sum of one (per spectrum) prior
#' to plotting.
#'
#' @param spectraVariables for `addProcessing`: `character` with additional
#' spectra variables that should be passed along to the function defined
#' with `FUN`. See [Spectra()] for details.
Expand Down Expand Up @@ -405,21 +411,29 @@ setMethod(
#'
#' @importFrom graphics par
#'
#' @importFrom grDevices n2mfrow
#'
#' @importFrom Spectra scalePeaks
#'
#' @export
setMethod("plotSpectraMirror", "MatchedSpectra", function(x, xlab = "m/z",
ylab = "intensity",
main = "", ...) {
if (length(query(x)) != 1)
stop("Length of 'query(x)' has to be 1.")
y <- x@target[x@matches$target_idx]
if (!length(y))
y <- Spectra(DataFrame(msLevel = 2L))
nr <- sqrt(max(length(y), 1))
par(mfrow = c(floor(nr), ceiling(nr)))
for (i in seq_along(y))
plotSpectraMirror(x = query(x)[1L], y = y[i],
xlab = xlab, ylab = ylab, main = main, ...)
})
setMethod(
"plotSpectraMirror", "MatchedSpectra",
function(x, xlab = "m/z", ylab = "intensity", main = "",
scalePeaks = FALSE, ...) {
if (length(query(x)) != 1)
stop("Length of 'query(x)' has to be 1.")
y <- x@target[x@matches$target_idx]
if (!length(y))
y <- Spectra(DataFrame(msLevel = 2L))
if (scalePeaks) {
x <- scalePeaks(query(x)[1L])
y <- scalePeaks(y)
} else x <- query(x)[1L]
par(mfrow = n2mfrow(length(y)))
for (i in seq_along(y))
plotSpectraMirror(x = x, y = y[i],
xlab = xlab, ylab = ylab, main = main, ...)
})

#' @importMethodsFrom ProtGenerics setBackend
#'
Expand Down
19 changes: 16 additions & 3 deletions man/MatchedSpectra.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/test_MatchedSpectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ test_that("pruneTarget,MatchedSpectra works", {
test_that("plotSpectraMirror throws an error", {
ms <- MatchedSpectra()
expect_error(plotSpectraMirror(ms), "Length")

ms <- MatchedSpectra(sp1, sp2, matches = data.frame(query_idx = integer(),
target_idx = integer(),
score = numeric()))
plotSpectraMirror(ms[1])
plotSpectraMirror(ms[1], scalePeaks = TRUE)
})

test_that("addProcessing works", {
Expand Down

0 comments on commit b96137d

Please sign in to comment.