From 1d3e4824bda0633af3516f2d9cd8f9d976884767 Mon Sep 17 00:00:00 2001 From: Philippine Louail <127301965+philouail@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:04:56 +0100 Subject: [PATCH] update code chromPeakSpectra() --- R/XcmsExperiment-functions.R | 21 ++++++++++++++------- R/XcmsExperiment.R | 19 +++++++++++++------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/R/XcmsExperiment-functions.R b/R/XcmsExperiment-functions.R index c8366696..be67d750 100644 --- a/R/XcmsExperiment-functions.R +++ b/R/XcmsExperiment-functions.R @@ -793,7 +793,9 @@ "largest_bpi"), msLevel = 2L, expandRt = 0, expandMz = 0, ppm = 0, skipFilled = FALSE, - peaks = integer(), peaksInfo = c("rt", "mz"), + peaks = integer(), + addColumnsChromPeaks = c("rt", "mz"), + addColumnsChrompeaksPrefix = "chrom_peak_", BPPARAM = bpparam()) { method <- match.arg(method) pks <- .chromPeaks(x)[, c("mz", "mzmin", "mzmax", "rt", @@ -819,7 +821,8 @@ res <- bpmapply( split.data.frame(pks, f), split(spectra(x), factor(fromFile(x), levels = levels(f))), - FUN = function(pk, sp, msLevel, method) { + FUN = function(pk, sp, msLevel, method, addColumnsChromPeaks, + addColumnsChrompeaksPrefix) { sp <- filterMsLevel(sp, msLevel) idx <- switch( method, @@ -830,13 +833,17 @@ largest_bpi = .spectra_index_list_largest_bpi(sp, pk, msLevel)) ids <- rep(rownames(pk), lengths(idx)) res <- sp[unlist(idx)] - res$peak_id <- ids - info <- pk[res$peak_id, peaksInfo] - colnames(info) <- paste("peak_", peaksInfo, sep = "") - res@backend@spectraData <- cbind(res@backend@spectraData, info) + pk_data <- DataFrame(pk[ids, addColumnsChromPeaks, drop = FALSE]) + pk_data$id <- ids + colnames(pk_data) <- paste0(addColumnsChrompeaksPrefix, + colnames(pk_data)) + pk_data$spectrumId <- res$spectrumId + res <- Spectra::joinSpectraData(res, pk_data) res }, - MoreArgs = list(msLevel = msLevel, method = method), + MoreArgs = list(msLevel = msLevel, method = method, + addColumnsChromPeaks = addColumnsChromPeaks, + addColumnsChrompeaksPrefix = addColumnsChrompeaksPrefix), BPPARAM = BPPARAM) Spectra:::.concatenate_spectra(res) } diff --git a/R/XcmsExperiment.R b/R/XcmsExperiment.R index c695da70..1cbb8545 100644 --- a/R/XcmsExperiment.R +++ b/R/XcmsExperiment.R @@ -1232,7 +1232,9 @@ setMethod( function(object, method = c("all", "closest_rt", "closest_mz", "largest_tic", "largest_bpi"), msLevel = 2L, expandRt = 0, expandMz = 0, ppm = 0, - skipFilled = FALSE, peaks = character(), peaksInfo = c("rt", "mz"), + skipFilled = FALSE, peaks = character(), + addColumnsChromPeaks = c("rt", "mz"), + addColumnsChromPeaksPrefix = "chrom_peak_", return.type = c("Spectra", "List"), BPPARAM = bpparam()) { if (hasAdjustedRtime(object)) object <- applyAdjustedRtime(object) @@ -1248,14 +1250,19 @@ setMethod( else pkidx <- integer() res <- .mse_spectra_for_peaks(object, method, msLevel, expandRt, expandMz, ppm, skipFilled, pkidx, - peaksInfo, BPPARAM) + addColumnsChromPeaks, + addColumnsChromPeaksPrefix, + BPPARAM) if (!length(pkidx)) peaks <- rownames(.chromPeaks(object)) else peaks <- rownames(.chromPeaks(object))[pkidx] - if (return.type == "Spectra") - res <- res[as.matrix(findMatches(peaks, res$peak_id))[, 2L]] - else - as(split(res, factor(res$peak_id, levels = peaks)), "List") + if (return.type == "Spectra") { + col <- paste0(addColumnsChromPeaksPrefix, "id") + res <- res[as.matrix(findMatches(peaks, res[[col]]))[, 2L]] + } else { + col <- paste0(addColumnsChromPeaksPrefix, "id") + as(split(res, factor(res[[col]], levels = peaks)), "List") + } }) #' @rdname reconstructChromPeakSpectra