Skip to content

Commit 971db33

Browse files
committed
addition of addCollmns.. parameters and documentation
1 parent 1d3e482 commit 971db33

8 files changed

+192
-71
lines changed

Diff for: R/AllGenerics.R

+54-13
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,21 @@ setGeneric("chromPeakData<-", function(object, value)
411411
#'
412412
#' Parameter `return.type` allows to specify the *type* of the result object.
413413
#' With `return.type = "Spectra"` (the default) a [Spectra] object with all
414-
#' matching spectra is returned. The spectra variable `"peak_id"` of the
415-
#' returned `Spectra` contains the ID of the chromatographic peak (i.e., the
416-
#' rowname of the peak in the `chromPeaks` matrix) for each spectrum.
417-
#' With `return.type = "Spectra"` a `List` of `Spectra` is returned. The
418-
#' length of the list is equal to the number of rows of `chromPeaks`. Each
419-
#' element of the list contains thus a `Spectra` with all spectra for one
420-
#' chromatographic peak (or a `Spectra` of length 0 if no spectrum was found
421-
#' for the respective chromatographic peak).
414+
#' matching spectra is returned. With `return.type = "Spectra"` a `List` of
415+
#' `Spectra` is returned. The length of the list is equal to the number of rows
416+
#' of `chromPeaks`. Each element of the list contains thus a `Spectra` with all
417+
#' spectra for one chromatographic peak (or a `Spectra` of length 0 if no
418+
#' spectrum was found for the respective chromatographic peak).
419+
#'
420+
#' Parameters `addColumnsChromPeaks` allow the user to add specific metadata
421+
#' columns from the chromatographic peaks (`chromPeaks`) to the returned
422+
#' spectra object. This can be useful to retain information such as retention
423+
#' time (`rt`), m/z (`mz`). The columns will be named as they is written in the
424+
#' `chromPeaks` object with a prefix that is defined by the parameter
425+
#' `addColumnsChromPeaksPrefix`. The *peak ID* (i.e., the row name of the
426+
#' peak in the `chromPeaks` matrix) is always added to the spectra object as
427+
#' metadata column `paste0(addColumnsChromPeaksPrefix,id)`, by default it will
428+
#' be `"chrom_peak_id"`.
422429
#'
423430
#' See also the *LC-MS/MS data analysis* vignette for more details and examples.
424431
#'
@@ -453,6 +460,16 @@ setGeneric("chromPeakData<-", function(object, value)
453460
#' @param return.type `character(1)` defining the type of result object that
454461
#' should be returned.
455462
#'
463+
#' @param addColumnsChromPeaks `character` vector with the names of the columns
464+
#' from `chromPeaks` that should be added to the returned spectra object.
465+
#' The columns will be named as they are written in the `chromPeaks` object
466+
#' with a prefix that is defined by the parameter
467+
#' `addColumnsChromPeaksPrefix`. Defaults to `c("mz", "rt")`.
468+
#'
469+
#' @param addColumnsChromPeaksPrefix `character(1)` defining the prefix that
470+
#' should be used for the columns from `chromPeaks` that are added to the
471+
#' returned spectra object. Defaults to `"chrom_peak_"`.
472+
#'
456473
#' @param BPPARAM parallel processing setup. Defaults to [bpparam()].
457474
#'
458475
#' @param ... ignored.
@@ -503,7 +520,7 @@ setGeneric("chromPeakData<-", function(object, value)
503520
#' ## spectra variable *peak_id* contain the row names of the peaks in the
504521
#' ## chromPeak matrix and allow thus to map chromatographic peaks to the
505522
#' ## returned MS2 spectra
506-
#' ms2_sps$peak_id
523+
#' ms2_sps$chrom_peak_id
507524
#' chromPeaks(dda)
508525
#'
509526
#' ## Alternatively, return the result as a List of Spectra objects. This list
@@ -799,10 +816,24 @@ setGeneric("featureDefinitions<-", function(object, value)
799816
#' spectrum **per chromatographic peak** will be returned (hence multiple
800817
#' spectra per feature).
801818
#'
802-
#' The ID of each chromatographic peak (i.e. its row name in `chromPeaks`)
803-
#' and each feature (i.e., its row name in `featureDefinitions`) are
804-
#' available in the returned [Spectra()] with spectra variables `"peak_id"`
805-
#' and `"feature_id"`, respectively.
819+
#' The information from `featureDefinitions` for each feature can be included
820+
#' in the returned [Spectra()] object using the `addColumnsFeatures` parameter.
821+
#' This is useful for retaining details such as the median retention time (`rtmed`)
822+
#' or median m/z (`mzmed`). The columns will retain their names as specified
823+
#' in the `featureDefinitions` object, prefixed by the value of the
824+
#' `addColumnsFeaturesPrefix` parameter. Additionally, the *feature ID*
825+
#' (i.e., the row name of the feature in the `featureDefinitions` data.frame)
826+
#' is always added as a metadata column with the name
827+
#' `paste0(addColumnsFeaturesPrefix, "id")`, which defaults to `"feature_id"`.
828+
#'
829+
#' See also [chromPeakSpectra()], as it supports a similar parameter for
830+
#' including columns from the chromatographic peaks in the returned spectra object.
831+
#' These parameters can be used in combination to include information from both
832+
#' the chromatographic peaks and the features in the returned [Spectra()].
833+
#' The *peak ID* (i.e., the row name of the peak in the `chromPeaks` matrix)
834+
#' is added as a metadata column with the name
835+
#' `paste0(addColumnsChromPeaksPrefix, "id")`, which defaults to
836+
#' `"chrom_peak_id"`.
806837
#'
807838
#' @param object [XcmsExperiment] or [XCMSnExp] object with feature defitions.
808839
#'
@@ -815,6 +846,16 @@ setGeneric("featureDefinitions<-", function(object, value)
815846
#' `featureDefinitions(x)`). This parameter overrides `skipFilled` and is
816847
#' only supported for `return.type` being either `"Spectra"` or `"List"`.
817848
#'
849+
#' @param addColumnsFeatures `character` vector with the names of the columns
850+
#' from `featureDefinitions` that should be added to the returned spectra
851+
#' object. The columns will be named as they are written in the
852+
#' `featureDefinitions` object with a prefix that is defined by the parameter
853+
#' `addColumnsFeaturesPrefix`. Defaults to `c("mzmed", "rtmed")`.
854+
#'
855+
#' @param addColumnsFeaturesPrefix `character(1)` defining the prefix that
856+
#' should be used for the columns from `featureDefinitions` that are added
857+
#' to the returned spectra object. Defaults to `"feature_"`.
858+
#'
818859
#' @param ... additional arguments to be passed along to [chromPeakSpectra()],
819860
#' such as `method`.
820861
#'

Diff for: R/XcmsExperiment-functions.R

+25-9
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@
795795
ppm = 0, skipFilled = FALSE,
796796
peaks = integer(),
797797
addColumnsChromPeaks = c("rt", "mz"),
798-
addColumnsChrompeaksPrefix = "chrom_peak_",
798+
addColumnsChromPeaksPrefix = "chrom_peak_",
799799
BPPARAM = bpparam()) {
800800
method <- match.arg(method)
801801
pks <- .chromPeaks(x)[, c("mz", "mzmin", "mzmax", "rt",
@@ -822,8 +822,8 @@
822822
split.data.frame(pks, f),
823823
split(spectra(x), factor(fromFile(x), levels = levels(f))),
824824
FUN = function(pk, sp, msLevel, method, addColumnsChromPeaks,
825-
addColumnsChrompeaksPrefix) {
826-
sp <- filterMsLevel(sp, msLevel)
825+
addColumnsChromPeaksPrefix) {
826+
sp <- Spectra::filterMsLevel(sp, msLevel)
827827
idx <- switch(
828828
method,
829829
all = .spectra_index_list(sp, pk, msLevel),
@@ -833,21 +833,37 @@
833833
largest_bpi = .spectra_index_list_largest_bpi(sp, pk, msLevel))
834834
ids <- rep(rownames(pk), lengths(idx))
835835
res <- sp[unlist(idx)]
836-
pk_data <- DataFrame(pk[ids, addColumnsChromPeaks, drop = FALSE])
837-
pk_data$id <- ids
838-
colnames(pk_data) <- paste0(addColumnsChrompeaksPrefix,
836+
pk_data <- pk[ids, addColumnsChromPeaks, drop = FALSE]
837+
pk_data <- cbind(pk_data, id = ids)
838+
colnames(pk_data) <- paste0(addColumnsChromPeaksPrefix,
839839
colnames(pk_data))
840-
pk_data$spectrumId <- res$spectrumId
841-
res <- Spectra::joinSpectraData(res, pk_data)
840+
res <- .add_spectra_data(res, pk_data)
842841
res
843842
},
844843
MoreArgs = list(msLevel = msLevel, method = method,
845844
addColumnsChromPeaks = addColumnsChromPeaks,
846-
addColumnsChrompeaksPrefix = addColumnsChrompeaksPrefix),
845+
addColumnsChromPeaksPrefix = addColumnsChromPeaksPrefix),
847846
BPPARAM = BPPARAM)
848847
Spectra:::.concatenate_spectra(res)
849848
}
850849

850+
#' @param x `Spectra` object.
851+
#'
852+
#' @param data `data.frame` or `matrix` with the data to be added to the
853+
#' spectra object.
854+
#'
855+
#' @noRd
856+
.add_spectra_data <- function(x, data) {
857+
if (is(data, "matrix"))
858+
data <- as.data.frame(data)
859+
if (nrow(data) != length(x))
860+
stop("Length of 'data' does not match the number of spectra in 'x'")
861+
for (i in colnames(data)) {
862+
x[[i]] <- data[, i]
863+
}
864+
x
865+
}
866+
851867
#' @param peaks `matrix` with chrom peaks.
852868
#'
853869
#' @param peakIdx `list` of `integer` indices defining which chromatographic

Diff for: R/XcmsExperiment.R

+13-8
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,6 @@
515515
#' indicating the identified chromatographic peaks. Only a single color
516516
#' is supported. Defaults to `peakCol = "#ff000060".
517517
#'
518-
#' @param peaksInfo For `chromPeakSpectra`: `character` vector of additional
519-
#' information from `chromPeaks()` to be added to the spectra object. The
520-
#' columns names will be appended with "peaks_".
521-
#'
522518
#' @param ppm For `chromPeaks` and `featureDefinitions`: optional `numeric(1)`
523519
#' specifying the ppm by which the m/z range (defined by `mz` should be
524520
#' extended. For a value of `ppm = 10`, all peaks within `mz[1] - ppm / 1e6`
@@ -1784,7 +1780,11 @@ setMethod(
17841780
"featureSpectra", "XcmsExperiment",
17851781
function(object, msLevel = 2L, expandRt = 0, expandMz = 0, ppm = 0,
17861782
skipFilled = FALSE, return.type = c("Spectra", "List"),
1787-
features = character(), ...) {
1783+
features = character(),
1784+
addColumnsFeatures = c("rtmed", "mzmed"),
1785+
addColumnsFeaturesPrefix = "feature_",
1786+
addColumnsChromPeaksPrefix = "chrom_peak_",
1787+
...) {
17881788
return.type <- match.arg(return.type)
17891789
if (!hasFeatures(object))
17901790
stop("No feature definitions present. Please run ",
@@ -1803,13 +1803,18 @@ setMethod(
18031803
sps <- .mse_spectra_for_peaks(
18041804
object, msLevel = msLevel, expandRt = expandRt,
18051805
expandMz = expandMz, ppm = ppm, skipFilled = skipFilled,
1806-
peaks = unique(pindex), ...)
1806+
peaks = unique(pindex),
1807+
addColumnsChromPeaksPrefix = addColumnsChromPeaksPrefix)
1808+
col <- paste0(addColumnsChromPeaksPrefix, "id")
18071809
mtch <- as.matrix(
1808-
findMatches(sps$peak_id, rownames(.chromPeaks(object))[pindex]))
1810+
findMatches(sps[[col]], rownames(.chromPeaks(object))[pindex]))
18091811
sps <- sps[mtch[, 1L]]
18101812
fid <- rep(
18111813
ufeatures, lengths(featureDefinitions(object)$peakidx[findex]))
1812-
sps$feature_id <- fid[mtch[, 2L]]
1814+
f_data <- featureDefinitions(object)[fid[mtch[, 2L]], addColumnsFeatures]
1815+
f_data$id <- fid[mtch[, 2L]]
1816+
colnames(f_data) <- paste0(addColumnsFeaturesPrefix, colnames(f_data))
1817+
sps <- .add_spectra_data(sps, f_data)
18131818
if (return.type == "List") {
18141819
sps <- List(split(sps, f = factor(sps$feature_id,
18151820
levels = ufeatures)))

Diff for: man/XcmsExperiment.Rd

-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: man/chromPeakSpectra.Rd

+28-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: man/featureSpectra.Rd

+35-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)