Skip to content

Commit

Permalink
Merge pull request #38 from rformassspectrometry/jomain
Browse files Browse the repository at this point in the history
Fix subset with negative indices
  • Loading branch information
jorainer authored Oct 27, 2023
2 parents 0fbcd03 + 8ad3f32 commit eee276a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MsExperiment
Title: Infrastructure for Mass Spectrometry Experiments
Version: 1.1.4
Version: 1.5.1
Description: Infrastructure to store and manage all aspects related to
a complete proteomics or metabolomics mass spectrometry (MS)
experiment. The MsExperiment package provides light-weight and
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# MsExperiment 1.5

## MsExperiment 1.5.1

- Fix subset with negative indices ([issue #37](https://github.com/rformassspectrometry/MsExperiment/issues/37).)

# MsExperiment 1.1

## MsExperiment 1.1.4
Expand Down
5 changes: 5 additions & 0 deletions R/MsExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -501,5 +501,10 @@ setMethod("[", "MsExperiment", function(x, i, j, ..., drop = FALSE) {
"samples in 'x'.")
i <- which(i)
}
if (any(i < 0)) {
if (all(i < 0))
i <- seq_along(x)[i]
else stop("Mixing positive and negative indices is not supported.")
}
.extractSamples(x, i, newx = x)
})
29 changes: 29 additions & 0 deletions tests/testthat/test_MsExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,35 @@ test_that("[,LinkedMsExperiment works", {
qdata(mse2) <- se[, 1:2]
res <- mse2[1:2]
expect_equal(res, mse2)

## Subsetting with negative indices
msel <- mse
spectra(msel)$mzML_file <- basename(spectra(msel)$dataOrigin)
msel <- linkSampleData(
msel, with = "sampleData.mzML_file = spectra.mzML_file")
res <- msel[-1]
expect_equal(length(res), (length(msel) - 1))
expect_equal(sampleData(res)$sample, sampleData(msel)$sample[-1])
expect_equal(unique(basename(spectra(res)$dataOrigin)),
unique(basename(spectra(msel)$dataOrigin))[2])

expect_error(msel[c(1, -2)], "not supported")

## Multiple files/samples.
sd <- data.frame(sample = c("A", "B", "C"))
f <- c(fls, system.file("microtofq", "MM14.mzML", package = "msdata"))
msel <- readMsExperiment(spectraFiles = f, sampleData = sd)
res <- msel[-2]
expect_true(length(res) == 2L)
expect_equal(sampleData(res)$sample, c("A", "C"))
expect_equal(unique(basename(spectra(res)$dataOrigin)),
unique(basename(spectra(msel)$dataOrigin))[c(1, 3)])

ref <- msel[2]
res <- msel[c(-1, -3)]
expect_equal(sampleData(ref), sampleData(res))
expect_equal(spectra(ref), spectra(res))
expect_equal(ref@sampleDataLinks, res@sampleDataLinks)
})

test_that("MsExperiment works", {
Expand Down

0 comments on commit eee276a

Please sign in to comment.