Skip to content

Commit ea35751

Browse files
committed
feat: allow exclusion of samples/groups from feature definition
- Support exclusion of samples/groups from the definition of new features with the *PeakDensity* method by using a value of `NA` for them with the `sampleGroups` parameter (issue #742).
1 parent faba48d commit ea35751

11 files changed

+111
-40
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: xcms
2-
Version: 4.1.13
2+
Version: 4.1.14
33
Title: LC-MS and GC-MS Data Analysis
44
Description: Framework for processing and visualization of chromatographically
55
separated and single-spectra mass spectral data. Imports from AIA/ANDI NetCDF,

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Changes in version 4.1.14
44

5+
- Support excluding samples or sample groups from defining features with
6+
*PeakDensity* correspondence analysis (issue #742).
57
- Add `plotPrecursorIons()` function.
68

79
## Changes in version 4.1.13

R/AllGenerics.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,15 @@ setGeneric("group", function(object, ...) standardGeneric("group"))
13191319
#' representing the m/z dependent measurement error of some MS instruments).
13201320
#' All peaks (from the same or from different samples) with their apex
13211321
#' position being close on the retention time axis are grouped into a LC-MS
1322-
#' feature. See in addition [do_groupChromPeaks_density()] for the core API
1322+
#' feature. Only samples with non-missing sample group assignment (i.e. for
1323+
#' which the value provided with parameter `sampleGroups` is different than
1324+
#' `NA`) are considered and counted for the feature definition. This allows
1325+
#' to exclude certain samples or groups (e.g. blanks) from the feature
1326+
#' definition avoiding thus features with only detected peaks in these. Note
1327+
#' that this affects only the **definition** of **new** features.
1328+
#' Chromatographic peaks in these samples will still be assigned to features
1329+
#' which were defined based on the other samples.
1330+
#' See in addition [do_groupChromPeaks_density()] for the core API
13231331
#' function.
13241332
#'
13251333
#' - `NearestPeaksParam`: performs peak grouping based on the proximity of
@@ -1399,11 +1407,13 @@ setGeneric("group", function(object, ...) standardGeneric("group"))
13991407
#'
14001408
#' @param sampleGroups For `PeakDensityParam`: A vector of the same length than
14011409
#' samples defining the sample group assignments (i.e. which samples
1402-
#' belong to which sample
1403-
#' group). This parameter is mandatory for the `PeakDensityParam`
1404-
#' and has to be provided also if there is no sample grouping in the
1405-
#' experiment (in which case all samples should be assigned to the
1406-
#' same group).
1410+
#' belong to which sample group). This parameter is mandatory for
1411+
#' `PeakDensityParam` and has to be defined also if there is no sample
1412+
#' grouping in the experiment (in which case all samples should be
1413+
#' assigned to the same group). Samples for which a `NA` is provided will
1414+
#' not be considered in the feature definitions step. Providing `NA` for
1415+
#' all blanks in an experiment will for example avoid features to be
1416+
#' defined for signals (chrom peaks) present only in blank samples.
14071417
#'
14081418
#' @param value Replacement value for `<-` methods.
14091419
#'

R/XcmsExperiment-plotting.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,11 @@ setMethod(
450450
#' pest_dda <- readMsExperiment(fl)
451451
#'
452452
#' plotPrecursorIons(pest_dda)
453+
#' grid()
454+
#'
455+
#' ## Subset the data object to plot the data specifically for one or
456+
#' ## selected file/sample:
457+
#' plotPrecursorIons(pest_dda[1L])
453458
plotPrecursorIons <- function(x, pch = 21, col = "#00000080",
454459
bg = "#00000020", xlab = "retention time",
455460
ylab = "m/z", main = character(), ...) {
@@ -468,6 +473,5 @@ plotPrecursorIons <- function(x, pch = 21, col = "#00000080",
468473
main <- basename(dataOrigin(spectra(x_sub)[1L]))
469474
plot(prt, pmz, xlim = rtr, ylim = mzr, pch = pch, col = col, bg = bg,
470475
xlab = xlab, ylab = ylab, main = main[1L], ...)
471-
grid()
472476
}
473477
}

R/do_groupChromPeaks-functions.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,11 @@ do_groupChromPeaks_density <- function(peaks, sampleGroups,
113113
paste0("'", .reqCols[!.reqCols %in% colnames(peaks)],"'",
114114
collapse = ", "), " not found in 'peaks' parameter")
115115

116-
sampleGroups <- as.character(sampleGroups)
117-
sampleGroupNames <- unique(sampleGroups)
116+
## With a `factor` we also support excluding samples/groups, i.e. samples
117+
## with an NA are not considered in the feature definition.
118+
if (!is.factor(sampleGroups))
119+
sampleGroups <- factor(sampleGroups)
120+
sampleGroupNames <- levels(sampleGroups)
118121
sampleGroupTable <- table(sampleGroups)
119122
nSampleGroups <- length(sampleGroupTable)
120123

@@ -160,15 +163,12 @@ do_groupChromPeaks_density <- function(peaks, sampleGroups,
160163
pb$tick()
161164
if (endIdx - startIdx < 0)
162165
next
163-
resL[[i]] <- .group_peaks_density(peaks[startIdx:endIdx, , drop = FALSE],
164-
bw = bw, densFrom = densFrom,
165-
densTo = densTo, densN = densN,
166-
sampleGroups = sampleGroups,
167-
sampleGroupTable = sampleGroupTable,
168-
minFraction = minFraction,
169-
minSamples = minSamples,
170-
maxFeatures = maxFeatures,
171-
sleep = sleep)
166+
resL[[i]] <- .group_peaks_density(
167+
peaks[startIdx:endIdx, , drop = FALSE], bw = bw,
168+
densFrom = densFrom, densTo = densTo, densN = densN,
169+
sampleGroups = sampleGroups, sampleGroupTable = sampleGroupTable,
170+
minFraction = minFraction, minSamples = minSamples,
171+
maxFeatures = maxFeatures, sleep = sleep)
172172
}
173173
res <- do.call(rbind, resL)
174174
if (nrow(res)) {

man/do_groupChromPeaks_density.Rd

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/do_groupChromPeaks_nearest.Rd

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/do_groupPeaks_mzClust.Rd

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/groupChromPeaks.Rd

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plotPrecursorIons.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_do_groupChromPeaks-functions.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,37 @@ test_that(".group_peaks_density works", {
8585
expect_true(nrow(res) == 0)
8686
expect_true(is(res, "data.frame"))
8787
})
88+
89+
test_that("do_groupChromPeaks_density works with skipping samples", {
90+
x <- loadXcmsData("xmse")
91+
pks <- chromPeaks(x)
92+
## Errors
93+
expect_error(do_groupChromPeaks_density(pks), "sampleGroups")
94+
expect_error(do_groupChromPeaks_density(3, sampleGroups = 3), "matrix")
95+
expect_error(do_groupChromPeaks_density(pks[, 1:3], sampleGroups = 1),
96+
"not found")
97+
expect_error(do_groupChromPeaks_density(pks, sampleGroups = 1:3),
98+
"Sample indices")
99+
100+
## groups for all samples.
101+
grps <- sampleData(x)$sample_group
102+
res <- do_groupChromPeaks_density(pks, sampleGroups = grps,
103+
minFraction = 1, bw = 30)
104+
expect_true(all(res$WT == 4 | res$KO == 4))
105+
expect_true(all(res$WT <= 4))
106+
expect_true(all(res$KO <= 4))
107+
108+
res_2 <- do_groupChromPeaks_density(
109+
pks, sampleGroups = rep(1, length(grps)), minFraction = 1)
110+
expect_true(nrow(res_2) < nrow(res))
111+
expect_true(all(res_2$`1` == 8))
112+
113+
## using only one sample group
114+
grps[grps == "KO"] <- NA
115+
res_3 <- do_groupChromPeaks_density(pks, sampleGroups = grps,
116+
minFraction = 1)
117+
expect_true(nrow(res_3) < nrow(res))
118+
expect_true(all(res_3$WT == 4))
119+
expect_equal(nrow(res_3), sum(res$WT == 4))
120+
tmp <- res[res$WT == 4, ]
121+
})

0 commit comments

Comments
 (0)