Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extractByIndex method #118

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: CompoundDb
Type: Package
Title: Creating and Using (Chemical) Compound Annotation Databases
Version: 1.9.4
Version: 1.9.5
Authors@R: c(person(given = "Jan", family = "Stanstrup",
email = "[email protected]",
role = c("aut"),
Expand Down Expand Up @@ -52,7 +52,7 @@ Imports:
ProtGenerics (>= 1.35.3),
xml2,
IRanges,
Spectra (>= 1.9.12),
Spectra (>= 1.15.10),
MsCoreUtils,
MetaboCoreUtils,
BiocParallel
Expand All @@ -69,6 +69,7 @@ VignetteBuilder: knitr
License: Artistic-2.0
RoxygenNote: 7.3.2
Roxygen: list(markdown=TRUE)
Encoding: UTF-8
Collate:
'AllGenerics.R'
'AnnotationFilters.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ exportMethods(dbconn)
exportMethods(deleteCompound)
exportMethods(deleteIon)
exportMethods(deleteSpectra)
exportMethods(extractByIndex)
exportMethods(insertCompound)
exportMethods(insertIon)
exportMethods(insertSpectra)
Expand Down Expand Up @@ -161,4 +162,5 @@ importMethodsFrom(ProtGenerics,tic)
importMethodsFrom(S4Vectors,metadata)
importMethodsFrom(Spectra,Spectra)
importMethodsFrom(Spectra,backendBpparam)
importMethodsFrom(Spectra,extractByIndex)
importMethodsFrom(Spectra,precScanNum)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CompoundDb version 1.9

## Changes in version 1.9.5

- Add new `extractByIndex()` method.

## Changes in version 1.9.4

- `compound_tbl_lipidblast` supports now parallel processing and extracts more
Expand Down
14 changes: 11 additions & 3 deletions R/MsBackendCompDb.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,17 @@ setMethod("[", "MsBackendCompDb", function(x, i, j, ..., drop = FALSE) {
if (missing(i))
return(x)
i <- i2index(i, length(x), x@spectraIds)
slot(x, "spectraIds", check = FALSE) <- x@spectraIds[i]
x <- callNextMethod(x, i = i)
x
extractByIndex(x, i)
})

#' @rdname MsBackendCompDb
#'
#' @importMethodsFrom Spectra extractByIndex
#'
#' @export
setMethod("extractByIndex", c("MsBackendCompDb", "ANY"), function(object, i) {
slot(object, "spectraIds", check = FALSE) <- object@spectraIds[i]
callNextMethod(object, i = i)
})

#' @rdname MsBackendCompDb
Expand Down
3 changes: 3 additions & 0 deletions man/MsBackendCompDb.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test_MsBackendCompDb.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,21 @@ test_that("$<-,MsBackendCompDb works", {
expect_error(be$mz <- be$mz, "not supported")
})

test_that("extractByIndex,MsBackendCompDb works", {
be <- backendInitialize(MsBackendCompDb(), cmp_spctra_db)
res <- extractByIndex(be, c(2, 4))
expect_true(length(res) == 2)
expect_equal(res$polarity, be$polarity[c(2, 4)])
expect_equal(res@spectraIds, be@spectraIds[c(2, 4)])
expect_equal(res$mz, be$mz[c(2, 4)])

})

test_that("[,MsBackendCompDb works", {
be <- backendInitialize(MsBackendCompDb(), cmp_spctra_db)
res <- be[]
expect_equal(res, be)

res <- be[c(2, 4)]
expect_true(length(res) == 2)
expect_equal(res$polarity, be$polarity[c(2, 4)])
Expand Down
Loading