Skip to content

Commit

Permalink
update code chromPeakSpectra()
Browse files Browse the repository at this point in the history
  • Loading branch information
philouail committed Nov 21, 2024
1 parent 367aada commit 1d3e482
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
21 changes: 14 additions & 7 deletions R/XcmsExperiment-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand All @@ -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)
}
Expand Down
19 changes: 13 additions & 6 deletions R/XcmsExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 1d3e482

Please sign in to comment.