From 790c371efcbb3766d7b9788d056dea4412fc97ad Mon Sep 17 00:00:00 2001 From: jorainer Date: Fri, 6 Oct 2023 06:51:46 +0200 Subject: [PATCH] refactor: chromatogram,XcmsExperiment with only mz or rt defined - Either `mz` or `rt` need to be provided for `chromatogram,XcmsExperiment`, but not both. --- DESCRIPTION | 2 +- R/XcmsExperiment.R | 4 ++++ inst/NEWS | 6 ++++++ tests/testthat/test_XcmsExperiment.R | 29 ++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 855beed05..c131239f6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: xcms -Version: 3.99.4 +Version: 3.99.5 Title: LC-MS and GC-MS Data Analysis Description: Framework for processing and visualization of chromatographically separated and single-spectra mass spectral data. Imports from AIA/ANDI NetCDF, diff --git a/R/XcmsExperiment.R b/R/XcmsExperiment.R index 23bb80d08..9fdc90f7c 100644 --- a/R/XcmsExperiment.R +++ b/R/XcmsExperiment.R @@ -1917,6 +1917,10 @@ setMethod( "'chromPeaks' instead") chromPeaks <- include } + if (nrow(mz) && !nrow(rt)) + rt <- cbind(rep(-Inf, nrow(mz)), rep(Inf, nrow(mz))) + if (nrow(rt) && !nrow(mz)) + mz <- cbind(rep(-Inf, nrow(rt)), rep(Inf, nrow(rt))) return.type <- match.arg(return.type) chromPeaks <- match.arg(chromPeaks) if (hasAdjustedRtime(object)) diff --git a/inst/NEWS b/inst/NEWS index 3ad4d96cb..ba0536418 100644 --- a/inst/NEWS +++ b/inst/NEWS @@ -1,3 +1,9 @@ +Changes in version 3.99.5 +---------------------- + +- Only `mz` or `rt` need to be provided for `chromatograms`. + + Changes in version 3.99.4 ---------------------- diff --git a/tests/testthat/test_XcmsExperiment.R b/tests/testthat/test_XcmsExperiment.R index 6c0bb20f7..0b6a31854 100644 --- a/tests/testthat/test_XcmsExperiment.R +++ b/tests/testthat/test_XcmsExperiment.R @@ -1090,6 +1090,35 @@ test_that("chromatogram,XcmsExperiment and .xmse_extract_chromatograms_old", { rt = c(100, 600), isolationWindowTargetMz = 270.85, aggregationFun = "sum") expect_true(all(intensity(res[[1L]]) > 0)) + + ## Defining only mz or rt. + rtr <- c(2600, 2700) + mzr <- c(340, 400) + res <- chromatogram(xmse, mz = mzr) + expect_s4_class(res, "XChromatograms") + expect_true(nrow(res) == 1L) + expect_true(nrow(chromPeaks(res)) > 0) + expect_true(all(chromPeaks(res)[, "mz"] >= 340 & + chromPeaks(res)[, "mz"] <= 400)) + expect_true(all(chromPeaks(res[1, 1])[, "sample"] == 1L)) + expect_true(all(chromPeaks(res[1, 2])[, "sample"] == 2L)) + expect_true(all(chromPeaks(res[1, 3])[, "sample"] == 3L)) + rrt <- range(lapply(res, rtime)) + expect_true(rrt[1] < 2600) + expect_true(rrt[2] > 4400) + + res <- chromatogram(xmse, rt = rtr) + expect_s4_class(res, "XChromatograms") + expect_true(nrow(res) == 1L) + expect_true(nrow(chromPeaks(res)) > 0) + expect_true(any(chromPeaks(res)[, "mz"] < 340 | + chromPeaks(res)[, "mz"] > 400)) + expect_true(all(chromPeaks(res[1, 1])[, "sample"] == 1L)) + expect_true(all(chromPeaks(res[1, 2])[, "sample"] == 2L)) + expect_true(all(chromPeaks(res[1, 3])[, "sample"] == 3L)) + rrt <- range(lapply(res, rtime)) + expect_true(rrt[1] >= 2600) + expect_true(rrt[2] <= 2700) }) test_that("featureChromatograms,XcmsExperiment works", {