From c0ec9de662f3bf59d2f51e6014b79d3bc46d4b8f Mon Sep 17 00:00:00 2001 From: Guitou1 Date: Wed, 22 Jan 2025 16:45:18 +0100 Subject: [PATCH 1/5] Update plotSpectra environment to allow addFragments returned list --- .Rbuildignore | 2 ++ .gitignore | 1 + R/plotting-functions.R | 42 +++++++++++++++++++++++++++++++++--------- Spectra.Rproj | 17 +++++++++++++++++ 4 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 Spectra.Rproj diff --git a/.Rbuildignore b/.Rbuildignore index 37442c70..fbd6b6b2 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -7,3 +7,5 @@ favicon logo.png _pkgdown.yml ^\.git$ +^.*\.Rproj$ +^\.Rproj\.user$ diff --git a/.gitignore b/.gitignore index 9789cbce..7401cb56 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ docs local_data *.o *.so +.Rproj.user diff --git a/R/plotting-functions.R b/R/plotting-functions.R index c75687f9..db0921fd 100644 --- a/R/plotting-functions.R +++ b/R/plotting-functions.R @@ -217,10 +217,19 @@ plotSpectra <- function(x, xlab = "m/z", ylab = "intensity", type = "h", main <- rep(main[1], nsp) if (nsp > 1) par(mfrow = n2mfrow(nsp, asp = asp)) + if (length(labels)) { + if (is.function(labels)) { + labels <- labels(x) + } + mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) + if (length(mod_check) != length(x)) + stop("Variable modifications are not yet supported.") + } else {labels <- NULL} + for (i in seq_len(nsp)) .plot_single_spectrum(x[i], xlab = xlab, ylab = ylab, type = type, xlim = xlim, ylim = ylim, main = main[i], - col = col[[i]], labels = labels, + col = col[[i]], labels = labels[[i]], labelCex = labelCex, labelSrt = labelSrt, labelAdj = labelAdj, labelPos = labelPos, labelOffset = labelOffset, labelCol = labelCol, @@ -259,9 +268,17 @@ plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", if (frame.plot) box(...) title(main = main, xlab = xlab, ylab = ylab, ...) + if (length(labels)) { + if (is.function(labels)) { + labels <- labels(x) + } + mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) + if (length(mod_check) != length(x)) + stop("Variable modifications are not yet supported.") + } else {labels <- NULL} for (i in seq_len(nsp)) .plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], - labels = labels, labelCex = labelCex, + labels = labels[[i]], labelCex = labelCex, labelSrt = labelSrt, labelAdj = labelAdj, labelPos = labelPos, labelOffset = labelOffset, labelCol = labelCol, ...) @@ -288,9 +305,6 @@ setMethod( matchLty = 1, matchPch = 16, ...) { if (length(x) != 1 || length(y) != 1) stop("'x' and 'y' have to be of length 1") - if (length(labels) & !is.function(labels)) - stop("'plotSpectraMirror' supports only a function with ", - "parameter 'labels'") if (length(col) != 2) col <- rep(col[1], 2) if (!length(xlim)) @@ -309,8 +323,18 @@ setMethod( on.exit(dev.flush()) plot.new() plot.window(xlim = xlim, ylim = ylim) + ## Stop if variable modifications are used + ## Will need to be removed once plotSpectra accepts variable modifications + ## See issue: https://github.com/rformassspectrometry/Spectra/issues/346 if (length(labels)) { - l <- c(labels(x), labels(y)) + if (is.function(labels)) { + labels <- c(labels(x), labels(y)) + } else { + mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) + if (length(mod_check) != length(x)) + stop("This Error occurs either because\n1) Annotations are not of length 2\n2) Variable modifications are not yet supported.") + } + l <- c(labels[[1]], labels[[2]]) wdths <- max(strwidth(l, cex = labelCex)) / 2 usr_lim <- par("usr") ylim[1L] <- ylim[1L] - wdths * @@ -319,7 +343,7 @@ setMethod( xlim[1L] <- xlim[1L] - wdths xlim[2L] <- xlim[2L] + wdths plot.window(xlim = xlim, ylim = ylim, ...) - } + } else {labels <- NULL} if (axes) { axis(side = 1, ...) axis(side = 2, ...) @@ -331,7 +355,7 @@ setMethod( x_data <- peaksData(x)[[1L]] y_data <- peaksData(y)[[1L]] .plot_single_spectrum(x, add = TRUE, type = type, col = col[[1L]], - labels = labels, labelCex = labelCex, + labels = labels[[1L]], labelCex = labelCex, labelSrt = labelSrt, labelAdj = labelAdj, labelPos = labelPos, labelOffset = labelOffset, labelCol = labelCol, ...) @@ -349,7 +373,7 @@ setMethod( labelPos <- 1 labelSrt <- -1 * labelSrt .plot_single_spectrum(y, add = TRUE, type = type, col = col[[1L]], - labels = labels, labelCex = labelCex, + labels = labels[[2L]], labelCex = labelCex, labelSrt = labelSrt, labelAdj = labelAdj, labelPos = labelPos, labelOffset = labelOffset, orientation = -1, labelCol = labelCol, ...) diff --git a/Spectra.Rproj b/Spectra.Rproj new file mode 100644 index 00000000..a4dce495 --- /dev/null +++ b/Spectra.Rproj @@ -0,0 +1,17 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 4 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source From 945ab980bf112f29a0d14bdc3c0abd2465c90b02 Mon Sep 17 00:00:00 2001 From: Guitou1 Date: Thu, 23 Jan 2025 15:04:13 +0100 Subject: [PATCH 2/5] Update based on PR remarks --- .Rhistory | 512 +++++++++++++++++++++++++++++++++++++++++ .gitignore | 1 + R/plotting-functions.R | 13 +- 3 files changed, 518 insertions(+), 8 deletions(-) create mode 100644 .Rhistory diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000..40d10e43 --- /dev/null +++ b/.Rhistory @@ -0,0 +1,512 @@ +#' @param labelSrt `numeric(1)` defining the rotation of the label. See +#' parameter `srt` in [text()]. +#' +#' @param labelCol color for the label(s). +#' +#' @param labelAdj see parameter `adj` in [text()]. +#' +#' @param labelPos see parameter `pos` in [text()]. +#' +#' @param labelOffset see parameter `offset` in [text()]. +#' +#' @param axes `logical(1)` whether (x and y) axes should be drawn. +#' +#' @param frame.plot `logical(1)` whether a box should be drawn around the +#' plotting area. +#' +#' @param ppm for `plotSpectraMirror()`: m/z relative acceptable difference (in +#' ppm) for peaks to be considered matching (see [MsCoreUtils::common()] +#' for more details). +#' +#' @param tolerance for `plotSpectraMirror()`: absolute acceptable difference of +#' m/z values for peaks to be considered matching (see +#' [MsCoreUtils::common()] for more details). +#' +#' @param matchCol for `plotSpectraMirror()`: color for matching peaks. +#' +#' @param matchLwd for `plotSpectraMirror()`: line width (`lwd`) to draw +#' matching peaks. See [par()] for more details. +#' +#' @param matchLty for `plotSpectraMirror()`: line type (`lty`) to draw matching +#' peaks. See [par()] for more details. +#' +#' @param matchPch for `plotSpectraMirror()`: point character (`pch`) to label +#' matching peaks. Defaults to `matchPch = 16`, set to `matchPch = NA` to +#' disable. See [par()] for more details. +#' +#' @param y for `plotSpectraMirror()`: `Spectra` object of length 1 against +#' which `x` should be plotted against. +#' +#' @param asp for `plotSpectra()`: the target ratio (columns / rows) when +#' plotting mutliple spectra (e.g. for 20 spectra use `asp = 4/5` for 4 +#' columns and 5 rows or `asp = 5/4` for 5 columns and 4 rows; see +#' [grDevices::n2mfrow()] for details). +#' +#' @param ... additional parameters to be passed to the [plot.default()] +#' function. +#' +#' @return These functions create a plot. +#' +#' @author Johannes Rainer, Sebastian Gibb, Laurent Gatto +#' +#' @name spectra-plotting +#' +#' @examples +#' +#' ints <- list(c(4.3412, 12, 8, 34, 23.4), +#' c(8, 25, 16, 32)) +#' mzs <- list(c(13.453421, 43.433122, 46.6653553, 129.111212, 322.24432), +#' c(13.452, 43.5122, 129.112, 322.245)) +#' +#' df <- DataFrame(msLevel = c(1L, 1L), rtime = c(123.12, 124)) +#' df$mz <- mzs +#' df$intensity <- ints +#' sp <- Spectra(df) +#' +#' #### --------------------------------------------- #### +#' ## plotSpectra ## +#' +#' ## Plot one spectrum. +#' plotSpectra(sp[1]) +#' +#' ## Plot both spectra. +#' plotSpectra(sp) +#' +#' ## Define a color for each peak in each spectrum. +#' plotSpectra(sp, col = list(c(1, 2, 3, 4, 5), 1:4)) +#' +#' ## Color peaks from each spectrum in different colors. +#' plotSpectra(sp, col = c("green", "blue")) +#' +#' ## Label each peak with its m/z. +#' plotSpectra(sp, labels = function(z) format(unlist(mz(z)), digits = 4)) +#' +#' ## Rotate the labels. +#' plotSpectra(sp, labels = function(z) format(unlist(mz(z)), digits = 4), +#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) +#' +#' ## Add a custom annotation for each peak. +#' sp$label <- list(c("", "A", "B", "C", "D"), +#' c("Frodo", "Bilbo", "Peregrin", "Samwise")) +#' ## Plot each peak in a different color +#' plotSpectra(sp, labels = function(z) unlist(z$label), +#' col = list(1:5, 1:4)) +#' +#' ## Plot a single spectrum specifying the label. +#' plotSpectra(sp[2], labels = c("A", "B", "C", "D")) +#' +#' +#' #### --------------------------------------------- #### +#' ## plotSpectraOverlay ## +#' +#' ## Plot both spectra overlaying. +#' plotSpectraOverlay(sp) +#' +#' ## Use a different color for each spectrum. +#' plotSpectraOverlay(sp, col = c("#ff000080", "#0000ff80")) +#' +#' ## Label also the peaks with their m/z if their intensity is above 15. +#' plotSpectraOverlay(sp, col = c("#ff000080", "#0000ff80"), +#' labels = function(z) { +#' lbls <- format(mz(z)[[1L]], digits = 4) +#' lbls[intensity(z)[[1L]] <= 15] <- "" +#' lbls +#' }) +#' abline(h = 15, lty = 2) +#' +#' ## Use different asp values +#' plotSpectra(sp, asp = 1/2) +#' plotSpectra(sp, asp = 2/1) +#' +#' #### --------------------------------------------- #### +#' ## plotSpectraMirror ## +#' +#' ## Plot two spectra against each other. +#' plotSpectraMirror(sp[1], sp[2]) +#' +#' ## Label the peaks with their m/z +#' plotSpectraMirror(sp[1], sp[2], +#' labels = function(z) format(mz(z)[[1L]], digits = 3), +#' labelSrt = -30, labelPos = 2, labelOffset = 0.2) +#' grid() +#' +#' ## The same plot with a tolerance of 0.1 and using a different color to +#' ## highlight matching peaks +#' plotSpectraMirror(sp[1], sp[2], +#' labels = function(z) format(mz(z)[[1L]], digits = 3), +#' labelSrt = -30, labelPos = 2, labelOffset = 0.2, tolerance = 0.1, +#' matchCol = "#ff000080", matchLwd = 2) +#' grid() +NULL +#' @rdname spectra-plotting +#' +#' @importFrom graphics par +#' @importFrom grDevices n2mfrow +#' +#' @export plotSpectra +plotSpectra <- function(x, xlab = "m/z", ylab = "intensity", type = "h", +xlim = numeric(), ylim = numeric(), +main = character(), col = "#00000080", +labels = character(), labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, labelOffset = 0.5, +labelCol = "#00000080", asp = 1, ...) { +if (!length(main)) +main <- paste0("MS", msLevel(x), " RT: ", round(rtime(x), 1)) +nsp <- length(x) +if (nsp == 1) +col <- list(col) +if (length(col) != nsp) +col <- rep(col[1], nsp) +if (length(main) != nsp) +main <- rep(main[1], nsp) +if (nsp > 1) +par(mfrow = n2mfrow(nsp, asp = asp)) +if (length(labels)) { +if (is.function(labels)) { +labels <- labels(x) +} +mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) +if (length(mod_check) != length(x)) +stop("Variable modifications are not yet supported.") +} else {labels <- NULL} +for (i in seq_len(nsp)) +.plot_single_spectrum(x[i], xlab = xlab, ylab = ylab, type = type, +xlim = xlim, ylim = ylim, main = main[i], +col = col[[i]], labels = labels[[i]], +labelCex = labelCex, labelSrt = labelSrt, +labelAdj = labelAdj, labelPos = labelPos, +labelOffset = labelOffset, labelCol = labelCol, +...) +} +#' @rdname spectra-plotting +#' +#' @export plotSpectraOverlay +plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", +type = "h", xlim = numeric(), +ylim = numeric(), +main = paste(length(x), "spectra"), +col = "#00000080", labels = character(), +labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, +labelOffset = 0.5, labelCol = "#00000080", +axes = TRUE, frame.plot = axes, ...) { +nsp <- length(x) +if (nsp == 1) +col <- list(col) +if (length(col) != nsp) +col <- rep(col[1], nsp) +if (!length(xlim)) +xlim <- range(unlist(mz(x)), na.rm = TRUE) +if (!length(ylim)) +ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) +dev.hold() +on.exit(dev.flush()) +plot.new() +plot.window(xlim = xlim, ylim = ylim) +if (axes) { +axis(side = 1, ...) +axis(side = 2, ...) +} +if (frame.plot) +box(...) +title(main = main, xlab = xlab, ylab = ylab, ...) +if (length(labels)) { +if (is.function(labels)) { +labels <- labels(x) +} +mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) +if (length(mod_check) != length(x)) +stop("Variable modifications are not yet supported.") +} else {labels <- NULL} +for (i in seq_len(nsp)) +.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], +labels = labels[[i]], labelCex = labelCex, +labelSrt = labelSrt, labelAdj = labelAdj, +labelPos = labelPos, labelOffset = labelOffset, +labelCol = labelCol, ...) +} +#' @rdname spectra-plotting +#' +#' @importFrom MsCoreUtils common +#' +#' @importFrom graphics abline +#' +#' @exportMethod plotSpectraMirror +setMethod( +"plotSpectraMirror", "Spectra", +function(x, y, xlab = "m/z", ylab = "intensity", +type = "h", xlim = numeric(), +ylim = numeric(), main = character(), +col = "#00000080", labels = character(), +labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, +labelOffset = 0.5, labelCol = "#00000080", +axes = TRUE, frame.plot = axes, ppm = 20, +tolerance = 0, matchCol = "#80B1D3", matchLwd = 1, +matchLty = 1, matchPch = 16, ...) { +if (length(x) != 1 || length(y) != 1) +stop("'x' and 'y' have to be of length 1") +if (length(col) != 2) +col <- rep(col[1], 2) +if (!length(xlim)) +suppressWarnings( +xlim <- range(unlist(mz(x)), unlist(mz(y)), na.rm = TRUE)) +if (!length(ylim)) +suppressWarnings( +ylim <- c(-1, 1) * max(unlist(intensity(x)), +unlist(intensity(y)), +na.rm = TRUE)) +if (any(is.infinite(xlim))) +xlim <- c(0, 0) +if (any(is.infinite(ylim))) +ylim <- c(0, 0) +dev.hold() +on.exit(dev.flush()) +plot.new() +plot.window(xlim = xlim, ylim = ylim) +## Stop if variable modifications are used +## Will need to be removed once plotSpectra accepts variable modifications +## See issue: https://github.com/rformassspectrometry/Spectra/issues/346 +if (length(labels)) { +if (is.function(labels)) { +labels <- c(labels(x), labels(y)) +} else { +mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) +if (length(mod_check) != length(x)) +stop("This Error occurs either because\n1) Annotations are not of length 2\n2) Variable modifications are not yet supported.") +} +l <- c(labels[[1]], labels[[2]]) +wdths <- max(strwidth(l, cex = labelCex)) / 2 +usr_lim <- par("usr") +ylim[1L] <- ylim[1L] - wdths * +diff(usr_lim[3:4]) / diff(usr_lim[1:2]) +ylim[2L] <- -ylim[1L] +xlim[1L] <- xlim[1L] - wdths +xlim[2L] <- xlim[2L] + wdths +plot.window(xlim = xlim, ylim = ylim, ...) +} else {labels <- NULL} +if (axes) { +axis(side = 1, ...) +axis(side = 2, ...) +} +if (frame.plot) +box(...) +title(main = main, xlab = xlab, ylab = ylab, ...) +## Find common peaks +x_data <- peaksData(x)[[1L]] +y_data <- peaksData(y)[[1L]] +.plot_single_spectrum(x, add = TRUE, type = type, col = col[[1L]], +labels = labels[[1L]], labelCex = labelCex, +labelSrt = labelSrt, labelAdj = labelAdj, +labelPos = labelPos, labelOffset = labelOffset, +labelCol = labelCol, ...) +idx <- which(common(x_data[, "mz"], y_data[, "mz"], +tolerance = tolerance, ppm = ppm)) +if (length(idx)) { +plot.xy(xy.coords(x_data[idx, "mz"], x_data[idx, "intensity"]), +type = "h", col = matchCol, lwd = matchLwd, ...) +plot.xy(xy.coords(x_data[idx, "mz"], x_data[idx, "intensity"]), +type = "p", col = matchCol, pch = matchPch, ...) +} +if (length(labelPos) && labelPos == 1) +labelPos <- 3 +if (length(labelPos) && labelPos == 3) +labelPos <- 1 +labelSrt <- -1 * labelSrt +.plot_single_spectrum(y, add = TRUE, type = type, col = col[[1L]], +labels = labels[[2L]], labelCex = labelCex, +labelSrt = labelSrt, labelAdj = labelAdj, +labelPos = labelPos, labelOffset = labelOffset, +orientation = -1, labelCol = labelCol, ...) +idx <- which(common(y_data[, "mz"], x_data[, "mz"], +tolerance = tolerance, ppm = ppm)) +if (length(idx)) { +plot.xy(xy.coords(y_data[idx, "mz"], -y_data[idx, "intensity"]), +type = "h", col = matchCol, lwd = matchLwd, ...) +plot.xy(xy.coords(y_data[idx, "mz"], -y_data[idx, "intensity"]), +type = "p", col = matchCol, pch = matchPch, ...) +} +abline(h = 0) +}) +#' @description +#' +#' Plot a single spectrum (m/z on x against intensity on y) with the optional +#' possibility to label the individual peaks. +#' +#' @author Johannes Rainer, Sebastian Gibb +#' +#' @importFrom graphics axis box plot.new plot.window plot.xy strwidth +#' +#' @importFrom graphics text title +#' +#' @importFrom grDevices dev.flush dev.hold xy.coords +#' +#' @examples +#' +#' ints <- c(4.3412, 12, 8, 34, 23.4) +#' mzs <- c(13.453421, 43.433122, 46.6653553, 129.111212, 322.24432) +#' +#' df <- DataFrame(msLevel = 1L, rtime = 123.12) +#' df$mz <- list(mzs) +#' df$intensity <- list(ints) +#' sp <- Spectra(df) +#' +#' .plot_single_spectrum(sp, main = "hello") +#' .plot_single_spectrum(sp, bty = "n") +#' .plot_single_spectrum(sp, frame.plot = FALSE) +#' +#' .plot_single_spectrum(sp, col = 1:5) +#' +#' .plot_single_spectrum(sp, labels = 1:5, col = 1:5) +#' +#' .plot_single_spectrum(sp, labels = format(mz(sp)[[1]], digits = 5), +#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) +#' grid() +#' .plot_single_spectrum(sp, col = "red", type = "p", add = TRUE) +#' +#' .plot_single_spectrum(sp, +#' labels = function(z) format(mz(z)[[1]], digits = 5), +#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) +#' grid() +#' +#' @noRd +.plot_single_spectrum <- function(x, xlab = "m/z", ylab = "intensity", +type = "h", xlim = numeric(), +ylim = numeric(), +main = paste("RT", round(rtime(x), 1)), +col = "#00000080", labels = character(), +labelCol = col, labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, +labelOffset = 0.5, add = FALSE, +axes = TRUE, frame.plot = axes, +orientation = 1, ...) { +v <- peaksData(x)[[1L]] +mzs <- v[, "mz"] +ints <- orientation * v[, "intensity"] +if (!length(xlim)) +suppressWarnings(xlim <- range(mzs, na.rm = TRUE)) +if (!length(ylim)) +suppressWarnings( +ylim <- range(orientation * c(0, max(abs(ints), na.rm = TRUE)))) +if (any(is.infinite(xlim))) +xlim <- c(0, 0) +if (any(is.infinite(ylim))) +ylim <- c(0, 0) +if (!add) { +dev.hold() +on.exit(dev.flush()) +plot.new() +plot.window(xlim = xlim, ylim = ylim) +} +if (length(labels)) { +if (is.function(labels)) +labels <- labels(x) +wdths <- max(strwidth(labels, cex = labelCex)) / 2 +usr_lim <- par("usr") +ylim[2L] <- ylim[2L] + wdths * diff(usr_lim[3:4]) / diff(usr_lim[1:2]) +xlim[1L] <- xlim[1L] - wdths +xlim[2L] <- xlim[2L] + wdths +if (!add) +plot.window(xlim = xlim, ylim = ylim, ...) +} +if (!add) { +if (axes) { +axis(side = 1, ...) +axis(side = 2, ...) +} +if (frame.plot) +box(...) +title(main = main, xlab = xlab, ylab = ylab, ...) +} +plot.xy(xy.coords(mzs, ints), type = type, col = col, ...) +if (length(labels)) +text(mzs, ints, labels = labels, adj = labelAdj, pos = labelPos, +col = labelCol, cex = labelCex, srt = labelSrt, +offset = labelOffset) +} +addFragments <- function(x, tolerance = 0, ppm = 20, ...) { +stopifnot(requireNamespace("Spectra")) +stopifnot(inherits(x, "Spectra")) +super_labels <- vector("list", length = length(x)) +for (j in seq_along(x)) { +stopifnot("sequence" %in% Spectra::spectraVariables(x[j])) +y <- Spectra::spectraData(x[j])[["sequence"]] +x_data <- Spectra::peaksData(x[j])[[1L]] +y_data <- calculateFragments(y, verbose = FALSE, ...) +y_data <- split(y_data, y_data$peptide) +labels <- vector("list", length = length(y_data)) +names(labels) <- names(y_data) +for (i in seq_along(y_data)) { +y_data[[i]] <- y_data[[i]][order(y_data[[i]]$mz), +] +idx <- which(MsCoreUtils::common(x_data[, "mz"], +y_data[[i]][, "mz"], +tolerance = tolerance, +ppm = ppm)) +idy <- which(MsCoreUtils::common(y_data[[i]][, "mz"], +x_data[, "mz"], +tolerance = tolerance, +ppm = ppm)) +labels[[i]] <- rep(NA_character_, nrow(x_data)) +labels[[i]][idx] <- y_data[[i]][idy, "ion"] +attr(labels[[i]], "spectrumNumber") <- j +} +super_labels[[j]] <- labels +} +unlist(super_labels, recursive = FALSE) +} +library(PSMatch) +library(identifieR) +data("sc") +plotSpectra(sc[532]) +plotSpectra(sc[532], labels = addFragments) +plotSpectra(sc[532]) +plotSpectra(sc[532], labels = addFragments) +plotSpectra(sc[532], labels = addFragments(sc[532])) +plotSpectra(sc[532], labels = .GlobalEnv::addFragments(sc[532])) +plotSpectra(sc[532:534], labels = addFragments) +plotSpectra(sc[532], labels = addFragments(sc[532])) +addFragments(sc[532]) +addFragments(sc[1]) +sc[1] +sc[1]$sequence +addFragments(sc[1]) +addFragments <- function(x, tolerance = 0, ppm = 20, ...) { +stopifnot(requireNamespace("Spectra")) +stopifnot(inherits(x, "Spectra")) +super_labels <- vector("list", length = length(x)) +for (j in seq_along(x)) { +stopifnot("sequence" %in% Spectra::spectraVariables(x[j])) +y <- Spectra::spectraData(x[j])[["sequence"]] +x_data <- Spectra::peaksData(x[j])[[1L]] +y_data <- calculateFragments(y, verbose = FALSE, ...) +y_data <- split(y_data, y_data$peptide) +labels <- vector("list", length = length(y_data)) +names(labels) <- names(y_data) +for (i in seq_along(y_data)) { +y_data[[i]] <- y_data[[i]][order(y_data[[i]]$mz), +] +idx <- which(MsCoreUtils::common(x_data[, "mz"], +y_data[[i]][, "mz"], +tolerance = tolerance, +ppm = ppm)) +idy <- which(MsCoreUtils::common(y_data[[i]][, "mz"], +x_data[, "mz"], +tolerance = tolerance, +ppm = ppm)) +labels[[i]] <- rep(NA_character_, nrow(x_data)) +labels[[i]][idx] <- y_data[[i]][idy, "ion"] +attr(labels[[i]], "spectrumNumber") <- j +} +super_labels[[j]] <- labels +} +unlist(super_labels, recursive = FALSE) +} +addFragments(sc[1]) +addFragments(sc[1]) +labels +x +sc +x_data +y +n diff --git a/.gitignore b/.gitignore index 7401cb56..7fd846a5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ local_data *.o *.so .Rproj.user +*.Rproj \ No newline at end of file diff --git a/R/plotting-functions.R b/R/plotting-functions.R index db0921fd..5710cdc1 100644 --- a/R/plotting-functions.R +++ b/R/plotting-functions.R @@ -221,9 +221,8 @@ plotSpectra <- function(x, xlab = "m/z", ylab = "intensity", type = "h", if (is.function(labels)) { labels <- labels(x) } - mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) - if (length(mod_check) != length(x)) - stop("Variable modifications are not yet supported.") + if (length(labels) != length(x)) + stop("Please provide a list of annotations of 'length == length(x)'.") } else {labels <- NULL} for (i in seq_len(nsp)) @@ -272,9 +271,8 @@ plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", if (is.function(labels)) { labels <- labels(x) } - mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) - if (length(mod_check) != length(x)) - stop("Variable modifications are not yet supported.") + if (length(labels) != length(x)) + stop("Please provide a list of annotations of 'length == length(x)'.") } else {labels <- NULL} for (i in seq_len(nsp)) .plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], @@ -330,8 +328,7 @@ setMethod( if (is.function(labels)) { labels <- c(labels(x), labels(y)) } else { - mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) - if (length(mod_check) != length(x)) + if (length(labels) != length(x)) stop("This Error occurs either because\n1) Annotations are not of length 2\n2) Variable modifications are not yet supported.") } l <- c(labels[[1]], labels[[2]]) From 7caf05ca9def27dc8af47f34eeb3d03b7f7e013c Mon Sep 17 00:00:00 2001 From: Guitou1 Date: Fri, 24 Jan 2025 15:42:14 +0100 Subject: [PATCH 3/5] Remove unnecessare files Rproj and Rhistory --- .Rhistory | 636 ++++++++++++++++++++++++++--------------------------- .gitignore | 3 +- 2 files changed, 320 insertions(+), 319 deletions(-) diff --git a/.Rhistory b/.Rhistory index 40d10e43..5244865b 100644 --- a/.Rhistory +++ b/.Rhistory @@ -1,321 +1,3 @@ -#' @param labelSrt `numeric(1)` defining the rotation of the label. See -#' parameter `srt` in [text()]. -#' -#' @param labelCol color for the label(s). -#' -#' @param labelAdj see parameter `adj` in [text()]. -#' -#' @param labelPos see parameter `pos` in [text()]. -#' -#' @param labelOffset see parameter `offset` in [text()]. -#' -#' @param axes `logical(1)` whether (x and y) axes should be drawn. -#' -#' @param frame.plot `logical(1)` whether a box should be drawn around the -#' plotting area. -#' -#' @param ppm for `plotSpectraMirror()`: m/z relative acceptable difference (in -#' ppm) for peaks to be considered matching (see [MsCoreUtils::common()] -#' for more details). -#' -#' @param tolerance for `plotSpectraMirror()`: absolute acceptable difference of -#' m/z values for peaks to be considered matching (see -#' [MsCoreUtils::common()] for more details). -#' -#' @param matchCol for `plotSpectraMirror()`: color for matching peaks. -#' -#' @param matchLwd for `plotSpectraMirror()`: line width (`lwd`) to draw -#' matching peaks. See [par()] for more details. -#' -#' @param matchLty for `plotSpectraMirror()`: line type (`lty`) to draw matching -#' peaks. See [par()] for more details. -#' -#' @param matchPch for `plotSpectraMirror()`: point character (`pch`) to label -#' matching peaks. Defaults to `matchPch = 16`, set to `matchPch = NA` to -#' disable. See [par()] for more details. -#' -#' @param y for `plotSpectraMirror()`: `Spectra` object of length 1 against -#' which `x` should be plotted against. -#' -#' @param asp for `plotSpectra()`: the target ratio (columns / rows) when -#' plotting mutliple spectra (e.g. for 20 spectra use `asp = 4/5` for 4 -#' columns and 5 rows or `asp = 5/4` for 5 columns and 4 rows; see -#' [grDevices::n2mfrow()] for details). -#' -#' @param ... additional parameters to be passed to the [plot.default()] -#' function. -#' -#' @return These functions create a plot. -#' -#' @author Johannes Rainer, Sebastian Gibb, Laurent Gatto -#' -#' @name spectra-plotting -#' -#' @examples -#' -#' ints <- list(c(4.3412, 12, 8, 34, 23.4), -#' c(8, 25, 16, 32)) -#' mzs <- list(c(13.453421, 43.433122, 46.6653553, 129.111212, 322.24432), -#' c(13.452, 43.5122, 129.112, 322.245)) -#' -#' df <- DataFrame(msLevel = c(1L, 1L), rtime = c(123.12, 124)) -#' df$mz <- mzs -#' df$intensity <- ints -#' sp <- Spectra(df) -#' -#' #### --------------------------------------------- #### -#' ## plotSpectra ## -#' -#' ## Plot one spectrum. -#' plotSpectra(sp[1]) -#' -#' ## Plot both spectra. -#' plotSpectra(sp) -#' -#' ## Define a color for each peak in each spectrum. -#' plotSpectra(sp, col = list(c(1, 2, 3, 4, 5), 1:4)) -#' -#' ## Color peaks from each spectrum in different colors. -#' plotSpectra(sp, col = c("green", "blue")) -#' -#' ## Label each peak with its m/z. -#' plotSpectra(sp, labels = function(z) format(unlist(mz(z)), digits = 4)) -#' -#' ## Rotate the labels. -#' plotSpectra(sp, labels = function(z) format(unlist(mz(z)), digits = 4), -#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) -#' -#' ## Add a custom annotation for each peak. -#' sp$label <- list(c("", "A", "B", "C", "D"), -#' c("Frodo", "Bilbo", "Peregrin", "Samwise")) -#' ## Plot each peak in a different color -#' plotSpectra(sp, labels = function(z) unlist(z$label), -#' col = list(1:5, 1:4)) -#' -#' ## Plot a single spectrum specifying the label. -#' plotSpectra(sp[2], labels = c("A", "B", "C", "D")) -#' -#' -#' #### --------------------------------------------- #### -#' ## plotSpectraOverlay ## -#' -#' ## Plot both spectra overlaying. -#' plotSpectraOverlay(sp) -#' -#' ## Use a different color for each spectrum. -#' plotSpectraOverlay(sp, col = c("#ff000080", "#0000ff80")) -#' -#' ## Label also the peaks with their m/z if their intensity is above 15. -#' plotSpectraOverlay(sp, col = c("#ff000080", "#0000ff80"), -#' labels = function(z) { -#' lbls <- format(mz(z)[[1L]], digits = 4) -#' lbls[intensity(z)[[1L]] <= 15] <- "" -#' lbls -#' }) -#' abline(h = 15, lty = 2) -#' -#' ## Use different asp values -#' plotSpectra(sp, asp = 1/2) -#' plotSpectra(sp, asp = 2/1) -#' -#' #### --------------------------------------------- #### -#' ## plotSpectraMirror ## -#' -#' ## Plot two spectra against each other. -#' plotSpectraMirror(sp[1], sp[2]) -#' -#' ## Label the peaks with their m/z -#' plotSpectraMirror(sp[1], sp[2], -#' labels = function(z) format(mz(z)[[1L]], digits = 3), -#' labelSrt = -30, labelPos = 2, labelOffset = 0.2) -#' grid() -#' -#' ## The same plot with a tolerance of 0.1 and using a different color to -#' ## highlight matching peaks -#' plotSpectraMirror(sp[1], sp[2], -#' labels = function(z) format(mz(z)[[1L]], digits = 3), -#' labelSrt = -30, labelPos = 2, labelOffset = 0.2, tolerance = 0.1, -#' matchCol = "#ff000080", matchLwd = 2) -#' grid() -NULL -#' @rdname spectra-plotting -#' -#' @importFrom graphics par -#' @importFrom grDevices n2mfrow -#' -#' @export plotSpectra -plotSpectra <- function(x, xlab = "m/z", ylab = "intensity", type = "h", -xlim = numeric(), ylim = numeric(), -main = character(), col = "#00000080", -labels = character(), labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, labelOffset = 0.5, -labelCol = "#00000080", asp = 1, ...) { -if (!length(main)) -main <- paste0("MS", msLevel(x), " RT: ", round(rtime(x), 1)) -nsp <- length(x) -if (nsp == 1) -col <- list(col) -if (length(col) != nsp) -col <- rep(col[1], nsp) -if (length(main) != nsp) -main <- rep(main[1], nsp) -if (nsp > 1) -par(mfrow = n2mfrow(nsp, asp = asp)) -if (length(labels)) { -if (is.function(labels)) { -labels <- labels(x) -} -mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) -if (length(mod_check) != length(x)) -stop("Variable modifications are not yet supported.") -} else {labels <- NULL} -for (i in seq_len(nsp)) -.plot_single_spectrum(x[i], xlab = xlab, ylab = ylab, type = type, -xlim = xlim, ylim = ylim, main = main[i], -col = col[[i]], labels = labels[[i]], -labelCex = labelCex, labelSrt = labelSrt, -labelAdj = labelAdj, labelPos = labelPos, -labelOffset = labelOffset, labelCol = labelCol, -...) -} -#' @rdname spectra-plotting -#' -#' @export plotSpectraOverlay -plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", -type = "h", xlim = numeric(), -ylim = numeric(), -main = paste(length(x), "spectra"), -col = "#00000080", labels = character(), -labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, -labelOffset = 0.5, labelCol = "#00000080", -axes = TRUE, frame.plot = axes, ...) { -nsp <- length(x) -if (nsp == 1) -col <- list(col) -if (length(col) != nsp) -col <- rep(col[1], nsp) -if (!length(xlim)) -xlim <- range(unlist(mz(x)), na.rm = TRUE) -if (!length(ylim)) -ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) -dev.hold() -on.exit(dev.flush()) -plot.new() -plot.window(xlim = xlim, ylim = ylim) -if (axes) { -axis(side = 1, ...) -axis(side = 2, ...) -} -if (frame.plot) -box(...) -title(main = main, xlab = xlab, ylab = ylab, ...) -if (length(labels)) { -if (is.function(labels)) { -labels <- labels(x) -} -mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) -if (length(mod_check) != length(x)) -stop("Variable modifications are not yet supported.") -} else {labels <- NULL} -for (i in seq_len(nsp)) -.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], -labels = labels[[i]], labelCex = labelCex, -labelSrt = labelSrt, labelAdj = labelAdj, -labelPos = labelPos, labelOffset = labelOffset, -labelCol = labelCol, ...) -} -#' @rdname spectra-plotting -#' -#' @importFrom MsCoreUtils common -#' -#' @importFrom graphics abline -#' -#' @exportMethod plotSpectraMirror -setMethod( -"plotSpectraMirror", "Spectra", -function(x, y, xlab = "m/z", ylab = "intensity", -type = "h", xlim = numeric(), -ylim = numeric(), main = character(), -col = "#00000080", labels = character(), -labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, -labelOffset = 0.5, labelCol = "#00000080", -axes = TRUE, frame.plot = axes, ppm = 20, -tolerance = 0, matchCol = "#80B1D3", matchLwd = 1, -matchLty = 1, matchPch = 16, ...) { -if (length(x) != 1 || length(y) != 1) -stop("'x' and 'y' have to be of length 1") -if (length(col) != 2) -col <- rep(col[1], 2) -if (!length(xlim)) -suppressWarnings( -xlim <- range(unlist(mz(x)), unlist(mz(y)), na.rm = TRUE)) -if (!length(ylim)) -suppressWarnings( -ylim <- c(-1, 1) * max(unlist(intensity(x)), -unlist(intensity(y)), -na.rm = TRUE)) -if (any(is.infinite(xlim))) -xlim <- c(0, 0) -if (any(is.infinite(ylim))) -ylim <- c(0, 0) -dev.hold() -on.exit(dev.flush()) -plot.new() -plot.window(xlim = xlim, ylim = ylim) -## Stop if variable modifications are used -## Will need to be removed once plotSpectra accepts variable modifications -## See issue: https://github.com/rformassspectrometry/Spectra/issues/346 -if (length(labels)) { -if (is.function(labels)) { -labels <- c(labels(x), labels(y)) -} else { -mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) -if (length(mod_check) != length(x)) -stop("This Error occurs either because\n1) Annotations are not of length 2\n2) Variable modifications are not yet supported.") -} -l <- c(labels[[1]], labels[[2]]) -wdths <- max(strwidth(l, cex = labelCex)) / 2 -usr_lim <- par("usr") -ylim[1L] <- ylim[1L] - wdths * -diff(usr_lim[3:4]) / diff(usr_lim[1:2]) -ylim[2L] <- -ylim[1L] -xlim[1L] <- xlim[1L] - wdths -xlim[2L] <- xlim[2L] + wdths -plot.window(xlim = xlim, ylim = ylim, ...) -} else {labels <- NULL} -if (axes) { -axis(side = 1, ...) -axis(side = 2, ...) -} -if (frame.plot) -box(...) -title(main = main, xlab = xlab, ylab = ylab, ...) -## Find common peaks -x_data <- peaksData(x)[[1L]] -y_data <- peaksData(y)[[1L]] -.plot_single_spectrum(x, add = TRUE, type = type, col = col[[1L]], -labels = labels[[1L]], labelCex = labelCex, -labelSrt = labelSrt, labelAdj = labelAdj, -labelPos = labelPos, labelOffset = labelOffset, -labelCol = labelCol, ...) -idx <- which(common(x_data[, "mz"], y_data[, "mz"], -tolerance = tolerance, ppm = ppm)) -if (length(idx)) { -plot.xy(xy.coords(x_data[idx, "mz"], x_data[idx, "intensity"]), -type = "h", col = matchCol, lwd = matchLwd, ...) -plot.xy(xy.coords(x_data[idx, "mz"], x_data[idx, "intensity"]), -type = "p", col = matchCol, pch = matchPch, ...) -} -if (length(labelPos) && labelPos == 1) -labelPos <- 3 -if (length(labelPos) && labelPos == 3) -labelPos <- 1 -labelSrt <- -1 * labelSrt -.plot_single_spectrum(y, add = TRUE, type = type, col = col[[1L]], -labels = labels[[2L]], labelCex = labelCex, -labelSrt = labelSrt, labelAdj = labelAdj, labelPos = labelPos, labelOffset = labelOffset, orientation = -1, labelCol = labelCol, ...) idx <- which(common(y_data[, "mz"], x_data[, "mz"], @@ -510,3 +192,321 @@ sc x_data y n +library(identifieR) +data("sc") +addFragments2(sc[532]) +#' @rdname spectra-plotting +#' +#' @importFrom graphics par +#' @importFrom grDevices n2mfrow +#' +#' @export plotSpectra +plotSpectra <- function(x, xlab = "m/z", ylab = "intensity", type = "h", +xlim = numeric(), ylim = numeric(), +main = character(), col = "#00000080", +labels = character(), labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, labelOffset = 0.5, +labelCol = "#00000080", asp = 1, ...) { +if (!length(main)) +main <- paste0("MS", msLevel(x), " RT: ", round(rtime(x), 1)) +nsp <- length(x) +if (nsp == 1) +col <- list(col) +if (length(col) != nsp) +col <- rep(col[1], nsp) +if (length(main) != nsp) +main <- rep(main[1], nsp) +if (nsp > 1) +par(mfrow = n2mfrow(nsp, asp = asp)) +if (length(labels)) { +if (is.function(labels)) { +labels <- labels(x) +} +mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) +if (length(mod_check) != length(x)) +stop("Variable modifications are not yet supported.") +} else {labels <- NULL} +for (i in seq_len(nsp)) +.plot_single_spectrum(x[i], xlab = xlab, ylab = ylab, type = type, +xlim = xlim, ylim = ylim, main = main[i], +col = col[[i]], labels = labels[[i]], +labelCex = labelCex, labelSrt = labelSrt, +labelAdj = labelAdj, labelPos = labelPos, +labelOffset = labelOffset, labelCol = labelCol, +...) +} +addFragments2(sc[532:534]) +plotSpectra(sc[532], labels = addFragments2) +#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) +#' grid() +#' .plot_single_spectrum(sp, col = "red", type = "p", add = TRUE) +#' +#' .plot_single_spectrum(sp, +#' labels = function(z) format(mz(z)[[1]], digits = 5), +#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) +#' grid() +#' +#' @noRd +.plot_single_spectrum <- function(x, xlab = "m/z", ylab = "intensity", +type = "h", xlim = numeric(), +ylim = numeric(), +main = paste("RT", round(rtime(x), 1)), +col = "#00000080", labels = character(), +labelCol = col, labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, +labelOffset = 0.5, add = FALSE, +axes = TRUE, frame.plot = axes, +orientation = 1, ...) { +v <- peaksData(x)[[1L]] +mzs <- v[, "mz"] +ints <- orientation * v[, "intensity"] +if (!length(xlim)) +suppressWarnings(xlim <- range(mzs, na.rm = TRUE)) +if (!length(ylim)) +suppressWarnings( +ylim <- range(orientation * c(0, max(abs(ints), na.rm = TRUE)))) +if (any(is.infinite(xlim))) +xlim <- c(0, 0) +if (any(is.infinite(ylim))) +ylim <- c(0, 0) +if (!add) { +dev.hold() +on.exit(dev.flush()) +plot.new() +plot.window(xlim = xlim, ylim = ylim) +} +if (length(labels)) { +if (is.function(labels)) +labels <- labels(x) +wdths <- max(strwidth(labels, cex = labelCex)) / 2 +usr_lim <- par("usr") +ylim[2L] <- ylim[2L] + wdths * diff(usr_lim[3:4]) / diff(usr_lim[1:2]) +xlim[1L] <- xlim[1L] - wdths +xlim[2L] <- xlim[2L] + wdths +if (!add) +plot.window(xlim = xlim, ylim = ylim, ...) +} +if (!add) { +if (axes) { +axis(side = 1, ...) +axis(side = 2, ...) +} +if (frame.plot) +box(...) +title(main = main, xlab = xlab, ylab = ylab, ...) +} +plot.xy(xy.coords(mzs, ints), type = type, col = col, ...) +if (length(labels)) +text(mzs, ints, labels = labels, adj = labelAdj, pos = labelPos, +col = labelCol, cex = labelCex, srt = labelSrt, +offset = labelOffset) +} +plotSpectra(sc[532], labels = addFragments2) +plotSpectra(sc[532:534], labels = addFragments2) +plotSpectra(sc[532:534], labels = addFragments2sc[532:534][[1]]) +plotSpectra(sc[532:534], labels = addFragments2(sc[532:534][[1]])) +addFragments2(sc[532:534][[1]] +) +addFragments2(sc[532:534])[[1]] +plotSpectra(sc[532:534], labels = addFragments2(sc[532:534])[[1]]) +#' @rdname spectra-plotting +#' +#' @export plotSpectraOverlay +plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", +type = "h", xlim = numeric(), +ylim = numeric(), +main = paste(length(x), "spectra"), +col = "#00000080", labels = character(), +labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, +labelOffset = 0.5, labelCol = "#00000080", +axes = TRUE, frame.plot = axes, ...) { +nsp <- length(x) +if (nsp == 1) +col <- list(col) +if (length(col) != nsp) +col <- rep(col[1], nsp) +if (!length(xlim)) +xlim <- range(unlist(mz(x)), na.rm = TRUE) +if (!length(ylim)) +ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) +dev.hold() +on.exit(dev.flush()) +plot.new() +plot.window(xlim = xlim, ylim = ylim) +if (axes) { +axis(side = 1, ...) +axis(side = 2, ...) +} +if (frame.plot) +box(...) +title(main = main, xlab = xlab, ylab = ylab, ...) +if (length(labels)) { +if (is.function(labels)) { +labels <- labels(x) +} +browser() +mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) +if (length(mod_check) != length(x)) +stop("Variable modifications are not yet supported.") +} else {labels <- NULL} +for (i in seq_len(nsp)) +.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], +labels = labels[[i]], labelCex = labelCex, +labelSrt = labelSrt, labelAdj = labelAdj, +labelPos = labelPos, labelOffset = labelOffset, +labelCol = labelCol, ...) +} +plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:534]) +) +sapply(labels, function(labels) attr(labels, "spectrumNumber")) +addFragments2(sc[532:534], variable_modifications = c(D = 5)) +sapply(addFragments2(sc[532:534], variable_modifications = c(D = 5)), function(labels) attr(labels, "spectrumNumber")) +#' @rdname spectra-plotting +#' +#' @export plotSpectraOverlay +plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", +type = "h", xlim = numeric(), +ylim = numeric(), +main = paste(length(x), "spectra"), +col = "#00000080", labels = character(), +labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, +labelOffset = 0.5, labelCol = "#00000080", +axes = TRUE, frame.plot = axes, ...) { +nsp <- length(x) +if (nsp == 1) +col <- list(col) +if (length(col) != nsp) +col <- rep(col[1], nsp) +if (!length(xlim)) +xlim <- range(unlist(mz(x)), na.rm = TRUE) +if (!length(ylim)) +ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) +dev.hold() +on.exit(dev.flush()) +plot.new() +plot.window(xlim = xlim, ylim = ylim) +if (axes) { +axis(side = 1, ...) +axis(side = 2, ...) +} +if (frame.plot) +box(...) +title(main = main, xlab = xlab, ylab = ylab, ...) +if (length(labels)) { +if (is.function(labels)) { +labels <- labels(x) +} +if (length(labels) != length(x)) +stop("Please provide a list of annotations of `length(x)`.") +} else {labels <- NULL} +for (i in seq_len(nsp)) +.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], +labels = labels[[i]], labelCex = labelCex, +labelSrt = labelSrt, labelAdj = labelAdj, +labelPos = labelPos, labelOffset = labelOffset, +labelCol = labelCol, ...) +} +plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533]) +) +plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533])) +plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533])) +dev.off() +plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533])) +addFragments2(sc[532:533]) +length(addFragments2(sc[532:533])) +length(sc[532:534]) +#' @rdname spectra-plotting +#' +#' @export plotSpectraOverlay +plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", +type = "h", xlim = numeric(), +ylim = numeric(), +main = paste(length(x), "spectra"), +col = "#00000080", labels = character(), +labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, +labelOffset = 0.5, labelCol = "#00000080", +axes = TRUE, frame.plot = axes, ...) { +nsp <- length(x) +if (nsp == 1) +col <- list(col) +if (length(col) != nsp) +col <- rep(col[1], nsp) +if (!length(xlim)) +xlim <- range(unlist(mz(x)), na.rm = TRUE) +if (!length(ylim)) +ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) +dev.hold() +on.exit(dev.flush()) +plot.new() +plot.window(xlim = xlim, ylim = ylim) +if (axes) { +axis(side = 1, ...) +axis(side = 2, ...) +} +if (frame.plot) +box(...) +title(main = main, xlab = xlab, ylab = ylab, ...) +if (length(labels)) { +if (is.function(labels)) { +labels <- labels(x) +} +if (length(labels) != length(x)) +stop("Please provide a list of annotations of `length(x)`.") +} else {labels <- NULL} +for (i in seq_len(nsp)) +.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], +labels = labels[[i]], labelCex = labelCex, +labelSrt = labelSrt, labelAdj = labelAdj, +labelPos = labelPos, labelOffset = labelOffset, +labelCol = labelCol, ...) +} +plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533])) +#' @rdname spectra-plotting +#' +#' @export plotSpectraOverlay +plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", +type = "h", xlim = numeric(), +ylim = numeric(), +main = paste(length(x), "spectra"), +col = "#00000080", labels = character(), +labelCex = 1, labelSrt = 0, +labelAdj = NULL, labelPos = NULL, +labelOffset = 0.5, labelCol = "#00000080", +axes = TRUE, frame.plot = axes, ...) { +nsp <- length(x) +if (nsp == 1) +col <- list(col) +if (length(col) != nsp) +col <- rep(col[1], nsp) +if (!length(xlim)) +xlim <- range(unlist(mz(x)), na.rm = TRUE) +if (!length(ylim)) +ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) +dev.hold() +on.exit(dev.flush()) +plot.new() +plot.window(xlim = xlim, ylim = ylim) +if (axes) { +axis(side = 1, ...) +axis(side = 2, ...) +} +if (frame.plot) +box(...) +title(main = main, xlab = xlab, ylab = ylab, ...) +if (length(labels)) { +if (is.function(labels)) { +labels <- labels(x) +} +if (length(labels) != length(x)) +stop("Please provide a list of annotations of 'length == length(x)'.") +} else {labels <- NULL} +for (i in seq_len(nsp)) +.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], +labels = labels[[i]], labelCex = labelCex, +labelSrt = labelSrt, labelAdj = labelAdj, +labelPos = labelPos, labelOffset = labelOffset, +labelCol = labelCol, ...) +} diff --git a/.gitignore b/.gitignore index 7fd846a5..9cfece03 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ local_data *.o *.so .Rproj.user -*.Rproj \ No newline at end of file +*.Rproj +.Rhistory \ No newline at end of file From 4814b8747b56579541b316461fc05bc962a67f1f Mon Sep 17 00:00:00 2001 From: Guillaume Deflandre <37773040+guideflandre@users.noreply.github.com> Date: Fri, 24 Jan 2025 15:42:51 +0100 Subject: [PATCH 4/5] Delete Spectra.Rproj --- Spectra.Rproj | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 Spectra.Rproj diff --git a/Spectra.Rproj b/Spectra.Rproj deleted file mode 100644 index a4dce495..00000000 --- a/Spectra.Rproj +++ /dev/null @@ -1,17 +0,0 @@ -Version: 1.0 - -RestoreWorkspace: Default -SaveWorkspace: Default -AlwaysSaveHistory: Default - -EnableCodeIndexing: Yes -UseSpacesForTab: Yes -NumSpacesForTab: 4 -Encoding: UTF-8 - -RnwWeave: Sweave -LaTeX: pdfLaTeX - -BuildType: Package -PackageUseDevtools: Yes -PackageInstallArgs: --no-multiarch --with-keep.source From 64872556720cdd54bd1f3c56d24599a873e00ba4 Mon Sep 17 00:00:00 2001 From: Guillaume Deflandre <37773040+guideflandre@users.noreply.github.com> Date: Fri, 24 Jan 2025 15:43:24 +0100 Subject: [PATCH 5/5] Delete .Rhistory --- .Rhistory | 512 ------------------------------------------------------ 1 file changed, 512 deletions(-) delete mode 100644 .Rhistory diff --git a/.Rhistory b/.Rhistory deleted file mode 100644 index 5244865b..00000000 --- a/.Rhistory +++ /dev/null @@ -1,512 +0,0 @@ -labelPos = labelPos, labelOffset = labelOffset, -orientation = -1, labelCol = labelCol, ...) -idx <- which(common(y_data[, "mz"], x_data[, "mz"], -tolerance = tolerance, ppm = ppm)) -if (length(idx)) { -plot.xy(xy.coords(y_data[idx, "mz"], -y_data[idx, "intensity"]), -type = "h", col = matchCol, lwd = matchLwd, ...) -plot.xy(xy.coords(y_data[idx, "mz"], -y_data[idx, "intensity"]), -type = "p", col = matchCol, pch = matchPch, ...) -} -abline(h = 0) -}) -#' @description -#' -#' Plot a single spectrum (m/z on x against intensity on y) with the optional -#' possibility to label the individual peaks. -#' -#' @author Johannes Rainer, Sebastian Gibb -#' -#' @importFrom graphics axis box plot.new plot.window plot.xy strwidth -#' -#' @importFrom graphics text title -#' -#' @importFrom grDevices dev.flush dev.hold xy.coords -#' -#' @examples -#' -#' ints <- c(4.3412, 12, 8, 34, 23.4) -#' mzs <- c(13.453421, 43.433122, 46.6653553, 129.111212, 322.24432) -#' -#' df <- DataFrame(msLevel = 1L, rtime = 123.12) -#' df$mz <- list(mzs) -#' df$intensity <- list(ints) -#' sp <- Spectra(df) -#' -#' .plot_single_spectrum(sp, main = "hello") -#' .plot_single_spectrum(sp, bty = "n") -#' .plot_single_spectrum(sp, frame.plot = FALSE) -#' -#' .plot_single_spectrum(sp, col = 1:5) -#' -#' .plot_single_spectrum(sp, labels = 1:5, col = 1:5) -#' -#' .plot_single_spectrum(sp, labels = format(mz(sp)[[1]], digits = 5), -#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) -#' grid() -#' .plot_single_spectrum(sp, col = "red", type = "p", add = TRUE) -#' -#' .plot_single_spectrum(sp, -#' labels = function(z) format(mz(z)[[1]], digits = 5), -#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) -#' grid() -#' -#' @noRd -.plot_single_spectrum <- function(x, xlab = "m/z", ylab = "intensity", -type = "h", xlim = numeric(), -ylim = numeric(), -main = paste("RT", round(rtime(x), 1)), -col = "#00000080", labels = character(), -labelCol = col, labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, -labelOffset = 0.5, add = FALSE, -axes = TRUE, frame.plot = axes, -orientation = 1, ...) { -v <- peaksData(x)[[1L]] -mzs <- v[, "mz"] -ints <- orientation * v[, "intensity"] -if (!length(xlim)) -suppressWarnings(xlim <- range(mzs, na.rm = TRUE)) -if (!length(ylim)) -suppressWarnings( -ylim <- range(orientation * c(0, max(abs(ints), na.rm = TRUE)))) -if (any(is.infinite(xlim))) -xlim <- c(0, 0) -if (any(is.infinite(ylim))) -ylim <- c(0, 0) -if (!add) { -dev.hold() -on.exit(dev.flush()) -plot.new() -plot.window(xlim = xlim, ylim = ylim) -} -if (length(labels)) { -if (is.function(labels)) -labels <- labels(x) -wdths <- max(strwidth(labels, cex = labelCex)) / 2 -usr_lim <- par("usr") -ylim[2L] <- ylim[2L] + wdths * diff(usr_lim[3:4]) / diff(usr_lim[1:2]) -xlim[1L] <- xlim[1L] - wdths -xlim[2L] <- xlim[2L] + wdths -if (!add) -plot.window(xlim = xlim, ylim = ylim, ...) -} -if (!add) { -if (axes) { -axis(side = 1, ...) -axis(side = 2, ...) -} -if (frame.plot) -box(...) -title(main = main, xlab = xlab, ylab = ylab, ...) -} -plot.xy(xy.coords(mzs, ints), type = type, col = col, ...) -if (length(labels)) -text(mzs, ints, labels = labels, adj = labelAdj, pos = labelPos, -col = labelCol, cex = labelCex, srt = labelSrt, -offset = labelOffset) -} -addFragments <- function(x, tolerance = 0, ppm = 20, ...) { -stopifnot(requireNamespace("Spectra")) -stopifnot(inherits(x, "Spectra")) -super_labels <- vector("list", length = length(x)) -for (j in seq_along(x)) { -stopifnot("sequence" %in% Spectra::spectraVariables(x[j])) -y <- Spectra::spectraData(x[j])[["sequence"]] -x_data <- Spectra::peaksData(x[j])[[1L]] -y_data <- calculateFragments(y, verbose = FALSE, ...) -y_data <- split(y_data, y_data$peptide) -labels <- vector("list", length = length(y_data)) -names(labels) <- names(y_data) -for (i in seq_along(y_data)) { -y_data[[i]] <- y_data[[i]][order(y_data[[i]]$mz), -] -idx <- which(MsCoreUtils::common(x_data[, "mz"], -y_data[[i]][, "mz"], -tolerance = tolerance, -ppm = ppm)) -idy <- which(MsCoreUtils::common(y_data[[i]][, "mz"], -x_data[, "mz"], -tolerance = tolerance, -ppm = ppm)) -labels[[i]] <- rep(NA_character_, nrow(x_data)) -labels[[i]][idx] <- y_data[[i]][idy, "ion"] -attr(labels[[i]], "spectrumNumber") <- j -} -super_labels[[j]] <- labels -} -unlist(super_labels, recursive = FALSE) -} -library(PSMatch) -library(identifieR) -data("sc") -plotSpectra(sc[532]) -plotSpectra(sc[532], labels = addFragments) -plotSpectra(sc[532]) -plotSpectra(sc[532], labels = addFragments) -plotSpectra(sc[532], labels = addFragments(sc[532])) -plotSpectra(sc[532], labels = .GlobalEnv::addFragments(sc[532])) -plotSpectra(sc[532:534], labels = addFragments) -plotSpectra(sc[532], labels = addFragments(sc[532])) -addFragments(sc[532]) -addFragments(sc[1]) -sc[1] -sc[1]$sequence -addFragments(sc[1]) -addFragments <- function(x, tolerance = 0, ppm = 20, ...) { -stopifnot(requireNamespace("Spectra")) -stopifnot(inherits(x, "Spectra")) -super_labels <- vector("list", length = length(x)) -for (j in seq_along(x)) { -stopifnot("sequence" %in% Spectra::spectraVariables(x[j])) -y <- Spectra::spectraData(x[j])[["sequence"]] -x_data <- Spectra::peaksData(x[j])[[1L]] -y_data <- calculateFragments(y, verbose = FALSE, ...) -y_data <- split(y_data, y_data$peptide) -labels <- vector("list", length = length(y_data)) -names(labels) <- names(y_data) -for (i in seq_along(y_data)) { -y_data[[i]] <- y_data[[i]][order(y_data[[i]]$mz), -] -idx <- which(MsCoreUtils::common(x_data[, "mz"], -y_data[[i]][, "mz"], -tolerance = tolerance, -ppm = ppm)) -idy <- which(MsCoreUtils::common(y_data[[i]][, "mz"], -x_data[, "mz"], -tolerance = tolerance, -ppm = ppm)) -labels[[i]] <- rep(NA_character_, nrow(x_data)) -labels[[i]][idx] <- y_data[[i]][idy, "ion"] -attr(labels[[i]], "spectrumNumber") <- j -} -super_labels[[j]] <- labels -} -unlist(super_labels, recursive = FALSE) -} -addFragments(sc[1]) -addFragments(sc[1]) -labels -x -sc -x_data -y -n -library(identifieR) -data("sc") -addFragments2(sc[532]) -#' @rdname spectra-plotting -#' -#' @importFrom graphics par -#' @importFrom grDevices n2mfrow -#' -#' @export plotSpectra -plotSpectra <- function(x, xlab = "m/z", ylab = "intensity", type = "h", -xlim = numeric(), ylim = numeric(), -main = character(), col = "#00000080", -labels = character(), labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, labelOffset = 0.5, -labelCol = "#00000080", asp = 1, ...) { -if (!length(main)) -main <- paste0("MS", msLevel(x), " RT: ", round(rtime(x), 1)) -nsp <- length(x) -if (nsp == 1) -col <- list(col) -if (length(col) != nsp) -col <- rep(col[1], nsp) -if (length(main) != nsp) -main <- rep(main[1], nsp) -if (nsp > 1) -par(mfrow = n2mfrow(nsp, asp = asp)) -if (length(labels)) { -if (is.function(labels)) { -labels <- labels(x) -} -mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) -if (length(mod_check) != length(x)) -stop("Variable modifications are not yet supported.") -} else {labels <- NULL} -for (i in seq_len(nsp)) -.plot_single_spectrum(x[i], xlab = xlab, ylab = ylab, type = type, -xlim = xlim, ylim = ylim, main = main[i], -col = col[[i]], labels = labels[[i]], -labelCex = labelCex, labelSrt = labelSrt, -labelAdj = labelAdj, labelPos = labelPos, -labelOffset = labelOffset, labelCol = labelCol, -...) -} -addFragments2(sc[532:534]) -plotSpectra(sc[532], labels = addFragments2) -#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) -#' grid() -#' .plot_single_spectrum(sp, col = "red", type = "p", add = TRUE) -#' -#' .plot_single_spectrum(sp, -#' labels = function(z) format(mz(z)[[1]], digits = 5), -#' labelPos = 2, labelOffset = 0.1, labelSrt = -30) -#' grid() -#' -#' @noRd -.plot_single_spectrum <- function(x, xlab = "m/z", ylab = "intensity", -type = "h", xlim = numeric(), -ylim = numeric(), -main = paste("RT", round(rtime(x), 1)), -col = "#00000080", labels = character(), -labelCol = col, labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, -labelOffset = 0.5, add = FALSE, -axes = TRUE, frame.plot = axes, -orientation = 1, ...) { -v <- peaksData(x)[[1L]] -mzs <- v[, "mz"] -ints <- orientation * v[, "intensity"] -if (!length(xlim)) -suppressWarnings(xlim <- range(mzs, na.rm = TRUE)) -if (!length(ylim)) -suppressWarnings( -ylim <- range(orientation * c(0, max(abs(ints), na.rm = TRUE)))) -if (any(is.infinite(xlim))) -xlim <- c(0, 0) -if (any(is.infinite(ylim))) -ylim <- c(0, 0) -if (!add) { -dev.hold() -on.exit(dev.flush()) -plot.new() -plot.window(xlim = xlim, ylim = ylim) -} -if (length(labels)) { -if (is.function(labels)) -labels <- labels(x) -wdths <- max(strwidth(labels, cex = labelCex)) / 2 -usr_lim <- par("usr") -ylim[2L] <- ylim[2L] + wdths * diff(usr_lim[3:4]) / diff(usr_lim[1:2]) -xlim[1L] <- xlim[1L] - wdths -xlim[2L] <- xlim[2L] + wdths -if (!add) -plot.window(xlim = xlim, ylim = ylim, ...) -} -if (!add) { -if (axes) { -axis(side = 1, ...) -axis(side = 2, ...) -} -if (frame.plot) -box(...) -title(main = main, xlab = xlab, ylab = ylab, ...) -} -plot.xy(xy.coords(mzs, ints), type = type, col = col, ...) -if (length(labels)) -text(mzs, ints, labels = labels, adj = labelAdj, pos = labelPos, -col = labelCol, cex = labelCex, srt = labelSrt, -offset = labelOffset) -} -plotSpectra(sc[532], labels = addFragments2) -plotSpectra(sc[532:534], labels = addFragments2) -plotSpectra(sc[532:534], labels = addFragments2sc[532:534][[1]]) -plotSpectra(sc[532:534], labels = addFragments2(sc[532:534][[1]])) -addFragments2(sc[532:534][[1]] -) -addFragments2(sc[532:534])[[1]] -plotSpectra(sc[532:534], labels = addFragments2(sc[532:534])[[1]]) -#' @rdname spectra-plotting -#' -#' @export plotSpectraOverlay -plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", -type = "h", xlim = numeric(), -ylim = numeric(), -main = paste(length(x), "spectra"), -col = "#00000080", labels = character(), -labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, -labelOffset = 0.5, labelCol = "#00000080", -axes = TRUE, frame.plot = axes, ...) { -nsp <- length(x) -if (nsp == 1) -col <- list(col) -if (length(col) != nsp) -col <- rep(col[1], nsp) -if (!length(xlim)) -xlim <- range(unlist(mz(x)), na.rm = TRUE) -if (!length(ylim)) -ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) -dev.hold() -on.exit(dev.flush()) -plot.new() -plot.window(xlim = xlim, ylim = ylim) -if (axes) { -axis(side = 1, ...) -axis(side = 2, ...) -} -if (frame.plot) -box(...) -title(main = main, xlab = xlab, ylab = ylab, ...) -if (length(labels)) { -if (is.function(labels)) { -labels <- labels(x) -} -browser() -mod_check <- sapply(labels, function(labels) attr(labels, "spectrumNumber")) -if (length(mod_check) != length(x)) -stop("Variable modifications are not yet supported.") -} else {labels <- NULL} -for (i in seq_len(nsp)) -.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], -labels = labels[[i]], labelCex = labelCex, -labelSrt = labelSrt, labelAdj = labelAdj, -labelPos = labelPos, labelOffset = labelOffset, -labelCol = labelCol, ...) -} -plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:534]) -) -sapply(labels, function(labels) attr(labels, "spectrumNumber")) -addFragments2(sc[532:534], variable_modifications = c(D = 5)) -sapply(addFragments2(sc[532:534], variable_modifications = c(D = 5)), function(labels) attr(labels, "spectrumNumber")) -#' @rdname spectra-plotting -#' -#' @export plotSpectraOverlay -plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", -type = "h", xlim = numeric(), -ylim = numeric(), -main = paste(length(x), "spectra"), -col = "#00000080", labels = character(), -labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, -labelOffset = 0.5, labelCol = "#00000080", -axes = TRUE, frame.plot = axes, ...) { -nsp <- length(x) -if (nsp == 1) -col <- list(col) -if (length(col) != nsp) -col <- rep(col[1], nsp) -if (!length(xlim)) -xlim <- range(unlist(mz(x)), na.rm = TRUE) -if (!length(ylim)) -ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) -dev.hold() -on.exit(dev.flush()) -plot.new() -plot.window(xlim = xlim, ylim = ylim) -if (axes) { -axis(side = 1, ...) -axis(side = 2, ...) -} -if (frame.plot) -box(...) -title(main = main, xlab = xlab, ylab = ylab, ...) -if (length(labels)) { -if (is.function(labels)) { -labels <- labels(x) -} -if (length(labels) != length(x)) -stop("Please provide a list of annotations of `length(x)`.") -} else {labels <- NULL} -for (i in seq_len(nsp)) -.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], -labels = labels[[i]], labelCex = labelCex, -labelSrt = labelSrt, labelAdj = labelAdj, -labelPos = labelPos, labelOffset = labelOffset, -labelCol = labelCol, ...) -} -plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533]) -) -plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533])) -plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533])) -dev.off() -plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533])) -addFragments2(sc[532:533]) -length(addFragments2(sc[532:533])) -length(sc[532:534]) -#' @rdname spectra-plotting -#' -#' @export plotSpectraOverlay -plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", -type = "h", xlim = numeric(), -ylim = numeric(), -main = paste(length(x), "spectra"), -col = "#00000080", labels = character(), -labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, -labelOffset = 0.5, labelCol = "#00000080", -axes = TRUE, frame.plot = axes, ...) { -nsp <- length(x) -if (nsp == 1) -col <- list(col) -if (length(col) != nsp) -col <- rep(col[1], nsp) -if (!length(xlim)) -xlim <- range(unlist(mz(x)), na.rm = TRUE) -if (!length(ylim)) -ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) -dev.hold() -on.exit(dev.flush()) -plot.new() -plot.window(xlim = xlim, ylim = ylim) -if (axes) { -axis(side = 1, ...) -axis(side = 2, ...) -} -if (frame.plot) -box(...) -title(main = main, xlab = xlab, ylab = ylab, ...) -if (length(labels)) { -if (is.function(labels)) { -labels <- labels(x) -} -if (length(labels) != length(x)) -stop("Please provide a list of annotations of `length(x)`.") -} else {labels <- NULL} -for (i in seq_len(nsp)) -.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], -labels = labels[[i]], labelCex = labelCex, -labelSrt = labelSrt, labelAdj = labelAdj, -labelPos = labelPos, labelOffset = labelOffset, -labelCol = labelCol, ...) -} -plotSpectraOverlay(sc[532:534], labels = addFragments2(sc[532:533])) -#' @rdname spectra-plotting -#' -#' @export plotSpectraOverlay -plotSpectraOverlay <- function(x, xlab = "m/z", ylab = "intensity", -type = "h", xlim = numeric(), -ylim = numeric(), -main = paste(length(x), "spectra"), -col = "#00000080", labels = character(), -labelCex = 1, labelSrt = 0, -labelAdj = NULL, labelPos = NULL, -labelOffset = 0.5, labelCol = "#00000080", -axes = TRUE, frame.plot = axes, ...) { -nsp <- length(x) -if (nsp == 1) -col <- list(col) -if (length(col) != nsp) -col <- rep(col[1], nsp) -if (!length(xlim)) -xlim <- range(unlist(mz(x)), na.rm = TRUE) -if (!length(ylim)) -ylim <- c(0, max(unlist(intensity(x)), na.rm = TRUE)) -dev.hold() -on.exit(dev.flush()) -plot.new() -plot.window(xlim = xlim, ylim = ylim) -if (axes) { -axis(side = 1, ...) -axis(side = 2, ...) -} -if (frame.plot) -box(...) -title(main = main, xlab = xlab, ylab = ylab, ...) -if (length(labels)) { -if (is.function(labels)) { -labels <- labels(x) -} -if (length(labels) != length(x)) -stop("Please provide a list of annotations of 'length == length(x)'.") -} else {labels <- NULL} -for (i in seq_len(nsp)) -.plot_single_spectrum(x[i], add = TRUE, type = type, col = col[[i]], -labels = labels[[i]], labelCex = labelCex, -labelSrt = labelSrt, labelAdj = labelAdj, -labelPos = labelPos, labelOffset = labelOffset, -labelCol = labelCol, ...) -}