Skip to content

Commit

Permalink
refactor: support storing database name instead of active connection
Browse files Browse the repository at this point in the history
- Add support to store the database file name in the object instead of the
  active connection to the database. This allows to serialize/deserialize
  `CompDb` and `IonDb` classes (and would even support parallel processing). For
  SQLite databases this is now the default. Issue #112.
- Add brackets `()` for all functions in the documentation.
  • Loading branch information
jorainer committed May 29, 2024
1 parent 7c2ef96 commit 322cacd
Show file tree
Hide file tree
Showing 34 changed files with 695 additions and 514 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/check-bioc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ jobs:
fail-fast: false
matrix:
config:
- { os: ubuntu-latest, r: 'devel', bioc: '3.19', cont: "bioconductor/bioconductor_docker:devel", rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest" }
- { os: macOS-latest, r: 'devel', bioc: '3.19'}
- { os: windows-latest, r: 'devel', bioc: '3.19'}
- { os: ubuntu-latest, r: '4.4', bioc: '3.20', cont: "bioconductor/bioconductor_docker:devel", rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest" }
- { os: macOS-latest, r: '4.4', bioc: '3.19'}
- { os: windows-latest, r: '4.4', bioc: '3.20'}
env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
Expand Down
2 changes: 1 addition & 1 deletion 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.0
Version: 1.9.1
Authors@R: c(person(given = "Jan", family = "Stanstrup",
email = "[email protected]",
role = c("aut"),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ importFrom(DBI,dbDisconnect)
importFrom(DBI,dbDriver)
importFrom(DBI,dbExecute)
importFrom(DBI,dbGetQuery)
importFrom(DBI,dbIsValid)
importFrom(DBI,dbListTables)
importFrom(DBI,dbWriteTable)
importFrom(IRanges,CharacterList)
Expand All @@ -109,6 +110,7 @@ importFrom(MsCoreUtils,rbindFill)
importFrom(MsCoreUtils,vapply1d)
importFrom(RSQLite,SQLITE_RO)
importFrom(RSQLite,SQLITE_RW)
importFrom(RSQLite,SQLITE_RWC)
importFrom(RSQLite,dbConnect)
importFrom(S4Vectors,DataFrame)
importFrom(S4Vectors,extractCOLS)
Expand All @@ -120,6 +122,7 @@ importFrom(dplyr,bind_cols)
importFrom(dplyr,bind_rows)
importFrom(jsonlite,read_json)
importFrom(methods,"slot<-")
importFrom(methods,.hasSlot)
importFrom(methods,as)
importFrom(methods,callNextMethod)
importFrom(methods,getMethod)
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# CompoundDb version 1.9

## Changes in version 1.9.1

- Allow `CompDb` to store that database name as alternative to an active
database connection. This allows to serialize and load an object to/from disk
(serializing an active database connection would not be possible) . Each call
to extract data from the database will however open (and close) its own
connection.

# CompoundDb version 1.7

## Changes in version 1.7.2
Expand Down
27 changes: 18 additions & 9 deletions R/CompDb-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ setMethod("show", "CompDb", function(object) {
cat("class:", class(object), "\n")
con <- .dbconn(object)
if (!is.null(con)) {
if (length(.dbname(object)))
on.exit(dbDisconnect(con))
cat(" data source:", .metadata_value(con, "source"), "\n")
cat(" version:", .metadata_value(con, "source_version"), "\n")
cat(" organism:", .metadata_value(con, "organism"), "\n")
Expand Down Expand Up @@ -124,15 +126,18 @@ setMethod("compounds", "CompDb", function(object,
#' @rdname CompDb
setMethod("insertSpectra", signature(object = "CompDb", spectra = "Spectra"),
function(object, spectra, columns = spectraVariables(spectra), ...) {
if (is.null(.dbconn(object)))
con <- .dbconn(object)
if (is.null(con))
stop("Database not initialized")
if (length(.dbname(object)))
on.exit(dbDisconnect(con))
new_sD <- as.data.frame(spectraData(spectra, columns))
if (!any(colnames(new_sD) == "compound_id"))
stop("Column 'compound_id' needs to be provided (as a ",
"spectra variable to insert to the database).")
if (!all(new_sD$compound_id %in%
dbGetQuery(.dbconn(object),
"select compound_id from ms_compound")[, 1]))
dbGetQuery(
con, "select compound_id from ms_compound")[, 1]))
stop("All values of spectra variable 'compound_id' must be",
" in 'compound_id' column of the database 'ms_compound'",
" table")
Expand All @@ -150,20 +155,18 @@ setMethod("insertSpectra", signature(object = "CompDb", spectra = "Spectra"),
new_sD$mz <- as.list(spectra$mz)
new_sD$intensity <- as.list(spectra$intensity)
if (hasMsMsSpectra(object))
.append_msms_spectra(.dbconn(object), new_sD)
.append_msms_spectra(con, new_sD)
else {
if("spectrum_id" %in% colnames(new_sD))
warning("'spectrum_id' variable in 'spectra' will be
replaced with internal indexes")
new_sD$spectrum_id <- seq_len(nrow(new_sD))
.insert_msms_spectra(.dbconn(object), new_sD)
.insert_msms_spectra(con, new_sD)
}
object@.properties$tables$msms_spectrum <- colnames(
dbGetQuery(.dbconn(object),
"select * from msms_spectrum limit 1"))
dbGetQuery(con, "select * from msms_spectrum limit 1"))
object@.properties$tables$msms_spectrum_peak <- colnames(
dbGetQuery(.dbconn(object),
"select * from msms_spectrum_peak limit 1"))
dbGetQuery(con, "select * from msms_spectrum_peak limit 1"))
object
})

Expand All @@ -178,6 +181,8 @@ setMethod("deleteSpectra", signature(object = "CompDb"),
dbcon <- .dbconn(object)
if (is.null(dbcon))
stop("Database not initialized")
if (length(.dbname(object)))
on.exit(dbDisconnect(dbcon))
if (hasMsMsSpectra(object)) {
if(any(!ids %in%
dbGetQuery(dbcon, paste0("select spectrum_id ",
Expand Down Expand Up @@ -225,6 +230,8 @@ setMethod("insertCompound", "CompDb", function(object, compounds = data.frame(),
stop("'compounds' is expected to be a data.frame")
dbcon <- .dbconn(object)
if (is.null(dbcon)) stop("Database not initialized")
if (length(.dbname(object)))
on.exit(dbDisconnect(dbcon))
if (!nrow(compounds)) return(object)
ref <- data.frame(name = character(), inchi = character(),
inchikey = character(), formula = character(),
Expand Down Expand Up @@ -269,6 +276,8 @@ setMethod("deleteCompound", "CompDb", function(object, ids = character(),
recursive = FALSE, ...) {
dbcon <- .dbconn(object)
if (is.null(dbcon)) stop("Database not initialized")
if (length(.dbname(object)))
on.exit(dbDisconnect(dbcon))
if (!length(ids)) return(object)
id_string <- paste0("'", ids, "'", collapse = ",")
if (hasMsMsSpectra(object)) {
Expand Down
Loading

0 comments on commit 322cacd

Please sign in to comment.