Skip to content

Commit

Permalink
Merge pull request #21 from rformassspectrometry/jomain
Browse files Browse the repository at this point in the history
feat: add extractByIndex method.
  • Loading branch information
jorainer authored Sep 27, 2024
2 parents 32ac0c5 + f089aca commit f5418ef
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MsBackendSql
Title: SQL-based Mass Spectrometry Data Backend
Version: 1.5.0
Version: 1.5.1
Authors@R:
c(person(given = "Johannes", family = "Rainer",
email = "Johannes.Rainer@eurac.edu",
Expand All @@ -21,7 +21,7 @@ Description: SQL-based mass spectrometry (MS) data backend supporting also
representation for very large or remote MS data sets.
Depends:
R (>= 4.2.0),
Spectra (>= 1.9.12)
Spectra (>= 1.15.10)
Imports:
BiocParallel,
S4Vectors,
Expand Down Expand Up @@ -50,7 +50,7 @@ BugReports: https://github.com/RforMassSpectrometry/MsBackendSql/issues
URL: https://github.com/RforMassSpectrometry/MsBackendSql
biocViews: Infrastructure, MassSpectrometry, Metabolomics, DataImport, Proteomics
Roxygen: list(markdown=TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Collate:
'MsBackendSql-functions.R'
'MsBackendSql.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exportMethods(backendInitialize)
exportMethods(backendMerge)
exportMethods(centroided)
exportMethods(dataStorage)
exportMethods(extractByIndex)
exportMethods(filterDataOrigin)
exportMethods(filterMsLevel)
exportMethods(filterPrecursorMzRange)
Expand Down Expand Up @@ -93,6 +94,7 @@ importMethodsFrom(ProtGenerics,spectraNames)
importMethodsFrom(ProtGenerics,tic)
importMethodsFrom(ProtGenerics,uniqueMsLevels)
importMethodsFrom(Spectra,backendBpparam)
importMethodsFrom(Spectra,extractByIndex)
importMethodsFrom(Spectra,reset)
importMethodsFrom(Spectra,show)
importMethodsFrom(Spectra,spectraData)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# MsBackendSql 1.5

## Changes in 1.5.1

- Implement the new `extractByIndex()` methods.

# MsBackendSql 1.3

## Changes in 1.3.5
Expand Down
2 changes: 1 addition & 1 deletion R/MsBackendSql-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ createMsBackendSqlDatabase <- function(dbcon, x = character(),

.subset_query <- function(object, qry) {
ids <- dbGetQuery(.dbcon(object), qry)[, "spectrum_id_"]
object[object@spectraIds %in% ids]
extractByIndex(object, which(object@spectraIds %in% ids))
}

.precursor_mz_query <- function(mz, ppm = 20, tolerance = 0) {
Expand Down
29 changes: 19 additions & 10 deletions R/MsBackendSql.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@
#'
#' @section Subsetting, merging and filtering data:
#'
#' `MsBackendSql` objects can be subsetted using the `[` function. Internally,
#' this will simply subset the `integer` vector of the primary keys and
#' eventually cached data. The original data in the database **is not**
#' affected by any subsetting operation. Any subsetting operation can be
#' *undone* by resetting the object with the `reset()` function. Subsetting
#' in arbitrary order as well as index replication is supported.
#' `MsBackendSql` objects can be subsetted using the `[` or `extractByIndex()`
#' functions. Internally, this will simply subset the `integer` vector of the
#' primary keys and eventually cached data. The original data in the database
#' **is not** affected by any subsetting operation. Any subsetting operation
#' can be *undone* by resetting the object with the `reset()` function.
#' Subsetting in arbitrary order as well as index replication is supported.
#'
#' Multiple `MsBackendSql` objects can also be merged (combined) with the
#' `backendMerge()` function. Note that this requires that all `MsBackendSql`
Expand Down Expand Up @@ -527,9 +527,17 @@ setMethod("[", "MsBackendSql", 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 MsBackendSql
#'
#' @importMethodsFrom Spectra extractByIndex
#'
#' @export
setMethod("extractByIndex", c("MsBackendSql", "ANY"), function(object, i) {
slot(object, "spectraIds", check = FALSE) <- object@spectraIds[i]
callNextMethod(object, i = i)
})

#' @importMethodsFrom ProtGenerics peaksData
Expand Down Expand Up @@ -707,7 +715,8 @@ setMethod(
object <- .subset_query(object, qry)
## Need to ensure the order is correct.
if (length(dataOrigin) > 1L)
object <- object[order(match(dataOrigin(object), dataOrigin))]
object <- extractByIndex(
object, order(match(dataOrigin(object), dataOrigin)))
object
}
})
Expand Down
15 changes: 9 additions & 6 deletions man/MsBackendSql.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test_MsBackendSql.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ test_that("dataStorage works", {
expect_identical(length(res), length(mm8_be))
})

test_that("extractByIndex,MsBackendSql works", {
idx <- c(4L, 12L, 100L, 14L)
res <- extractByIndex(mm8_be, idx)
expect_identical(res@spectraIds, idx)
})

test_that("[,MsBackendSql works", {
res <- mm8_be[]
expect_equal(res, mm8_be)

idx <- c(4L, 12L, 100L, 14L)
res <- mm8_be[idx]
expect_identical(res@spectraIds, idx)
Expand Down

0 comments on commit f5418ef

Please sign in to comment.