Skip to content

Commit

Permalink
jo comments
Browse files Browse the repository at this point in the history
  • Loading branch information
philouail committed Nov 22, 2024
1 parent 8f6fa9a commit 5f8a4f2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 102 deletions.
48 changes: 17 additions & 31 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,13 @@ setGeneric("chromPeakData<-", function(object, value)
#' spectra for one chromatographic peak (or a `Spectra` of length 0 if no
#' spectrum was found for the respective chromatographic peak).
#'
#' Parameters `addColumnsChromPeaks` allow the user to add specific metadata
#' Parameters `chromPeakColumns` allow the user to add specific metadata
#' columns from the chromatographic peaks (`chromPeaks`) to the returned
#' spectra object. This can be useful to retain information such as retention
#' time (`rt`), m/z (`mz`). The columns will be named as they is written in the
#' `chromPeaks` object with a prefix that is defined by the parameter
#' `addColumnsChromPeaksPrefix`. The *peak ID* (i.e., the row name of the
#' peak in the `chromPeaks` matrix) is always added to the spectra object as
#' metadata column `paste0(addColumnsChromPeaksPrefix,id)`, by default it will
#' be `"chrom_peak_id"`.
#' `chromPeaks` object with the prefix `"chrom_peak_"`. The *peak ID*
#' (i.e., the row name of the peak in the `chromPeaks` matrix) is always added
#' to the spectra object as a metadata column named `"chrom_peak_id"`.
#'
#' See also the *LC-MS/MS data analysis* vignette for more details and examples.
#'
Expand Down Expand Up @@ -460,15 +458,10 @@ setGeneric("chromPeakData<-", function(object, value)
#' @param return.type `character(1)` defining the type of result object that
#' should be returned.
#'
#' @param addColumnsChromPeaks `character` vector with the names of the columns
#' @param chromPeakColumns `character` vector with the names of the columns
#' from `chromPeaks` that should be added to the returned spectra object.
#' The columns will be named as they are written in the `chromPeaks` object
#' with a prefix that is defined by the parameter
#' `addColumnsChromPeaksPrefix`. Defaults to `c("mz", "rt")`.
#'
#' @param addColumnsChromPeaksPrefix `character(1)` defining the prefix that
#' should be used for the columns from `chromPeaks` that are added to the
#' returned spectra object. Defaults to `"chrom_peak_"`.
#' with a prefix `"chrom_peak_"`. Defaults to `c("mz", "rt")`.
#'
#' @param BPPARAM parallel processing setup. Defaults to [bpparam()].
#'
Expand Down Expand Up @@ -517,7 +510,7 @@ setGeneric("chromPeakData<-", function(object, value)
#' ms2_sps <- chromPeakSpectra(dda)
#' ms2_sps
#'
#' ## spectra variable *peak_id* contain the row names of the peaks in the
#' ## spectra variable *chrom_peak_id* contain the row names of the peaks in the
#' ## chromPeak matrix and allow thus to map chromatographic peaks to the
#' ## returned MS2 spectra
#' ms2_sps$chrom_peak_id
Expand Down Expand Up @@ -817,23 +810,20 @@ setGeneric("featureDefinitions<-", function(object, value)
#' spectra per feature).
#'
#' The information from `featureDefinitions` for each feature can be included
#' in the returned [Spectra()] object using the `addColumnsFeatures` parameter.
#' in the returned [Spectra()] object using the `featureColumns` parameter.
#' This is useful for retaining details such as the median retention time (`rtmed`)
#' or median m/z (`mzmed`). The columns will retain their names as specified
#' in the `featureDefinitions` object, prefixed by the value of the
#' `addColumnsFeaturesPrefix` parameter. Additionally, the *feature ID*
#' (i.e., the row name of the feature in the `featureDefinitions` data.frame)
#' is always added as a metadata column with the name
#' `paste0(addColumnsFeaturesPrefix, "id")`, which defaults to `"feature_id"`.
#' in the `featureDefinitions` object, prefixed by `"feature_"`
#' (e.g., `"feature_mzmed"`). Additionally, the *feature ID* (i.e., the row
#' name of the feature in the `featureDefinitions` data.frame) is always added
#' as a metadata column named `"feature_id"`.
#'
#' See also [chromPeakSpectra()], as it supports a similar parameter for
#' including columns from the chromatographic peaks in the returned spectra object.
#' These parameters can be used in combination to include information from both
#' the chromatographic peaks and the features in the returned [Spectra()].
#' The *peak ID* (i.e., the row name of the peak in the `chromPeaks` matrix)
#' is added as a metadata column with the name
#' `paste0(addColumnsChromPeaksPrefix, "id")`, which defaults to
#' `"chrom_peak_id"`.
#' is added as a metadata column named `"chrom_peak_id"`.
#'
#' @param object [XcmsExperiment] or [XCMSnExp] object with feature defitions.
#'
Expand All @@ -846,15 +836,11 @@ setGeneric("featureDefinitions<-", function(object, value)
#' `featureDefinitions(x)`). This parameter overrides `skipFilled` and is
#' only supported for `return.type` being either `"Spectra"` or `"List"`.
#'
#' @param addColumnsFeatures `character` vector with the names of the columns
#' @param featureColumns `character` vector with the names of the columns
#' from `featureDefinitions` that should be added to the returned spectra
#' object. The columns will be named as they are written in the
#' `featureDefinitions` object with a prefix that is defined by the parameter
#' `addColumnsFeaturesPrefix`. Defaults to `c("mzmed", "rtmed")`.
#'
#' @param addColumnsFeaturesPrefix `character(1)` defining the prefix that
#' should be used for the columns from `featureDefinitions` that are added
#' to the returned spectra object. Defaults to `"feature_"`.
#' `featureDefinitions` object with the prefix `"feature_`.
#' Defaults to `c("mzmed", "rtmed")`.
#'
#' @param ... additional arguments to be passed along to [chromPeakSpectra()],
#' such as `method`.
Expand All @@ -866,7 +852,7 @@ setGeneric("featureDefinitions<-", function(object, value)
#' the order and the length matches parameter `features` (or if no `features`
#' is defined the order of the features in `featureDefinitions(object)`).
#'
#' Spectra variables `"peak_id"` and `"feature_id"` define to which
#' Spectra variables `"chrom_peak_id"` and `"feature_id"` define to which
#' chromatographic peak or feature each individual spectrum is associated
#' with.
#'
Expand Down
15 changes: 5 additions & 10 deletions R/XcmsExperiment-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,7 @@
msLevel = 2L, expandRt = 0, expandMz = 0,
ppm = 0, skipFilled = FALSE,
peaks = integer(),
addColumnsChromPeaks = c("rt", "mz"),
addColumnsChromPeaksPrefix = "chrom_peak_",
chromPeakColumns = c("rt", "mz"),
BPPARAM = bpparam()) {
method <- match.arg(method)
pks <- .chromPeaks(x)[, c("mz", "mzmin", "mzmax", "rt",
Expand All @@ -821,8 +820,7 @@
res <- bpmapply(
split.data.frame(pks, f),
split(spectra(x), factor(fromFile(x), levels = levels(f))),
FUN = function(pk, sp, msLevel, method, addColumnsChromPeaks,
addColumnsChromPeaksPrefix) {
FUN = function(pk, sp, msLevel, method, chromPeakColumns) {
sp <- filterMsLevel(sp, msLevel)
idx <- switch(
method,
Expand All @@ -833,17 +831,14 @@
largest_bpi = .spectra_index_list_largest_bpi(sp, pk, msLevel))
ids <- rep(rownames(pk), lengths(idx))
res <- sp[unlist(idx)]
pk_data <- as.data.frame(pk[ids, addColumnsChromPeaks,
drop = FALSE])
pk_data <- as.data.frame(pk[ids, chromPeakColumns, drop = FALSE])
pk_data$id <- ids
colnames(pk_data) <- paste0(addColumnsChromPeaksPrefix,
colnames(pk_data))
colnames(pk_data) <- paste0("chrom_peak_", colnames(pk_data))
res <- .add_spectra_data(res, pk_data)
res
},
MoreArgs = list(msLevel = msLevel, method = method,
addColumnsChromPeaks = addColumnsChromPeaks,
addColumnsChromPeaksPrefix = addColumnsChromPeaksPrefix),
chromPeakColumns = chromPeakColumns),
BPPARAM = BPPARAM)
Spectra:::.concatenate_spectra(res)
}
Expand Down
32 changes: 12 additions & 20 deletions R/XcmsExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -1229,8 +1229,7 @@ setMethod(
"largest_tic", "largest_bpi"),
msLevel = 2L, expandRt = 0, expandMz = 0, ppm = 0,
skipFilled = FALSE, peaks = character(),
addColumnsChromPeaks = c("rt", "mz"),
addColumnsChromPeaksPrefix = "chrom_peak_",
chromPeakColumns = c("rt", "mz"),
return.type = c("Spectra", "List"), BPPARAM = bpparam()) {
if (hasAdjustedRtime(object))
object <- applyAdjustedRtime(object)
Expand All @@ -1246,19 +1245,15 @@ setMethod(
else pkidx <- integer()
res <- .mse_spectra_for_peaks(object, method, msLevel, expandRt,
expandMz, ppm, skipFilled, pkidx,
addColumnsChromPeaks,
addColumnsChromPeaksPrefix,
chromPeakColumns,
BPPARAM)
if (!length(pkidx))
peaks <- rownames(.chromPeaks(object))
else peaks <- rownames(.chromPeaks(object))[pkidx]
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")
}
if (return.type == "Spectra")
res <- res[as.matrix(findMatches(peaks, res$chrom_peak_id))[, 2L]]
else
as(split(res, factor(res$chrom_peak_id, levels = peaks)), "List")
})

#' @rdname reconstructChromPeakSpectra
Expand Down Expand Up @@ -1781,9 +1776,7 @@ setMethod(
function(object, msLevel = 2L, expandRt = 0, expandMz = 0, ppm = 0,
skipFilled = FALSE, return.type = c("Spectra", "List"),
features = character(),
addColumnsFeatures = c("rtmed", "mzmed"),
addColumnsFeaturesPrefix = "feature_",
addColumnsChromPeaksPrefix = "chrom_peak_",
featureColumns = c("rtmed", "mzmed"),
...) {
return.type <- match.arg(return.type)
if (!hasFeatures(object))
Expand All @@ -1803,17 +1796,16 @@ setMethod(
sps <- .mse_spectra_for_peaks(
object, msLevel = msLevel, expandRt = expandRt,
expandMz = expandMz, ppm = ppm, skipFilled = skipFilled,
peaks = unique(pindex),
addColumnsChromPeaksPrefix = addColumnsChromPeaksPrefix, ...)
col <- paste0(addColumnsChromPeaksPrefix, "id")
peaks = unique(pindex), ...)
mtch <- as.matrix(
findMatches(sps[[col]], rownames(.chromPeaks(object))[pindex]))
findMatches(sps$chrom_peak_id,
rownames(.chromPeaks(object))[pindex]))
sps <- sps[mtch[, 1L]]
fid <- rep(
ufeatures, lengths(featureDefinitions(object)$peakidx[findex]))
f_data <- featureDefinitions(object)[fid[mtch[, 2L]], addColumnsFeatures]
f_data <- featureDefinitions(object)[fid[mtch[, 2L]], featureColumns]
f_data$id <- fid[mtch[, 2L]]
colnames(f_data) <- paste0(addColumnsFeaturesPrefix, colnames(f_data))
colnames(f_data) <- paste0("feature_", colnames(f_data))
sps <- .add_spectra_data(sps, f_data)
if (return.type == "List") {
sps <- List(split(sps, f = factor(sps$feature_id,
Expand Down
24 changes: 8 additions & 16 deletions man/chromPeakSpectra.Rd

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

35 changes: 11 additions & 24 deletions man/featureSpectra.Rd

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

2 changes: 1 addition & 1 deletion vignettes/xcms-lcms-ms.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ dda_spectra$chrom_peak_id
```

Some information about the chromatographic peak can also be added to the
returned `Spectra` object using the `addChromPeaksColumns` parameter in
returned `Spectra` object using the `chrompeakColumns` parameter in
`chromPeakSpectra()`. By default, the m/z and retention time of the
chromatographic peak are added to the spectra metadata.

Expand Down

0 comments on commit 5f8a4f2

Please sign in to comment.