Package 'MsBackendMongoDb'

Title: NoSQL-based Mass Spectrometry Data Backend
Description: Mass spectrometry (MS) data backend for data storage in NoSQL MongoDB dagabases. Objects from this package expand the data storage and handling infrastructure of the Spectra Bioconductor package. This package adds support to store and retrieve MS data from MongoDB databases.
Authors: Ahlam Mentag [aut] (ORCID: <https://orcid.org/0009-0008-5438-7067>), Johannes Rainer [aut, cre] (ORCID: <https://orcid.org/0000-0002-6977-7147>)
Maintainer: Johannes Rainer <[email protected]>
License: GPL-3
Version: 0.97.4
Built: 2026-07-23 11:08:30 UTC
Source: https://github.com/rformassspectrometry/msbackendmongodb

Help Index


Create a connection to MongoDb

Description

Creates connections to required MongoDb collections used by the MsBackendMongoDb.

Usage

connectMsBackendMongoDb(
  db = "spectra_db",
  url = "mongodb://localhost",
  clean = FALSE
)

Arguments

db

character(1) scalar, name of the MongoDb database.

url

character(1) a MongoDb server URL.

clean

logical(1) whether the collection should be cleaned, i.e., if all content should be deleted. Be careful with this parameter as setting to TRUE will delete evantually persent content from the database.

Value

A named list() of mongo connection objects.

Author(s)

Ahlam Mentag


Spectra MS backend storing data in a MongoDb database

Description

MsBackendMongoDb is an implementation of the Spectra::MsBackend() class for Spectra::Spectra() objects, storing and retrieving MS data from a MongoDb database.

This backend allows read-only access to spectra and peaks stored in MongoDb, but supports temporary modifications of spectra variables via caching.

Usage

## S4 method for signature 'MsBackendMongoDb'
show(object)

## S4 method for signature 'MsBackendMongoDb'
backendInitialize(object, dbcon, data, ...)

## S4 method for signature 'MsBackendMongoDb'
dataStorage(object)

## S4 method for signature 'MsBackendMongoDb'
peaksData(object, columns = c("mz", "intensity"))

## S4 method for signature 'MsBackendMongoDb'
peaksVariables(object)

## S4 method for signature 'MsBackendMongoDb'
mz(object)

## S4 replacement method for signature 'MsBackendMongoDb'
mz(object) <- value

## S4 method for signature 'MsBackendMongoDb'
intensity(object)

## S4 replacement method for signature 'MsBackendMongoDb'
intensity(object) <- value

## S4 method for signature 'MsBackendMongoDb'
spectraNames(object)

## S4 replacement method for signature 'MsBackendMongoDb'
spectraNames(object) <- value

## S4 method for signature 'MsBackendMongoDb'
spectraData(object, columns = spectraVariables(object))

## S4 method for signature 'MsBackendMongoDb'
x[i, j, ..., drop = FALSE]

## S4 method for signature 'MsBackendMongoDb,ANY'
extractByIndex(object, i)

## S4 method for signature 'MsBackendMongoDb'
reset(object)

## S4 method for signature 'MsBackendMongoDb'
supportsSetBackend(object, ...)

Arguments

object

A MsBackendMongoDb instance

dbcon

list() of MongoDb collections, e.g., list(compounds_info_coll = mongo_conn)

data

Optional DataFrame with data to insert into MongoDb

...

Additional arguments passed internally

columns

Character() vector of columns to fetch for peaksData or spectraData

value

Replacement values for setter functions

x

for ⁠[i]⁠: a MsBackendMongoDb instance

i

integer() Indices to subset the backend

j

integer() Ignored.

drop

logical(1), ignored for subsetting

Value

Depends on the method. See individual function documentation.

Creation of backend objects

New backend objects can be created using:

con <- connectMsBackendMongoDb(db = <database name>, url = <db url>)
backend <- backendInitialize(MsBackendMongoDb(), dbcon = con)
  • dbcon: A list of MongoDb collections (e.g. from mongolite::mongo()) as created by the connectMsBackendMongoDb().

  • backendInitialize(): populates the object with spectrum IDs and caches metadata. If data is provided, it can insert data into the collection.

Accessing data

  • peaksData(object, columns = c("mz", "intensity")): returns a list of matrices containing the peak data for each spectrum.

  • peaksVariables(object): returns available columns for peaksData.

  • intensity(object), mz(object): return SimpleList objects with numeric intensity or m/z values for each spectrum.

  • spectraData(object, columns): returns a DataFrame with requested spectrum metadata columns.

  • spectraNames(object): returns spectrum_id values as character.

Read-only data

  • ⁠intensity<-⁠ and ⁠mz<-⁠ are not supported.

  • ⁠spectraNames<-⁠ is not supported.

Subsetting and extraction

  • ⁠[i]⁠ and extractByIndex(object, i) subset the backend by spectrum indices. Original data in the database is never changed.

  • reset(object) restores the backend to its original state, removing subsetting and cached variables.

Miscellaneous

  • dataStorage(object) returns a string describing the MongoDb collections used by the backend.

Implementation notes

Internally, the backend stores:

  • spectraIds: integer vector of spectrum IDs

  • dbcon: MongoDb connection(s)

  • .collections: list of collection names

  • peak_fun: function used to fetch peak data

  • localData: cached spectrum variables

  • nspectra: total number of spectra

Data access functions use peak_fun to fetch peaks on demand.

Note

MsBackendMongoDb keeps a connection to a MongoDb collection(s) and fetches spectra and peaks data on demand. By storing only the primary keys (spectrum_id) in memory, it ensures minimal memory usage, while peaks and metadata are fetched dynamically.

The backend inherits from Spectra::MsBackendCached() and supports temporary modification of spectra variables via the ⁠$<-⁠ operator. Original data in the database cannot be changed (intensities and m/z values are read-only).

Author(s)

Ahlam Mentag

Examples

## Start a MongoDB for the example; this requires the *mongod* binary of
## a MongoDB server installation to be present in the System's path.
## We use a specific temporary folder for the database that we can remove
## later safely
d <- file.path(tempdir(), "test_example")
start_mongodb()

## Loading test data files from MsDataHub
library(Spectra)
library(MsDataHub)
fls <- c(X20171016_POOL_POS_1_105.134.mzML(),
         X20171016_POOL_POS_3_105.134.mzML())

## Load the data as a `Spectra` object

s <- Spectra(fls)

## Connect to the MongoDB database
mcon <- connectMsBackendMongoDb(
   db = "ms_backend_mongodb",
   url = "mongodb://localhost:27017",
   clean = TRUE)

## Change the backend of the `Spectra` object to `MsBackendMongoDb`: store
## all MS data into the MongoDB
mng <- setBackend(s, backend = MsBackendMongoDb(), dbcon = mcon)
mng

## Get the *m/z* values from the data
mz(mng)

## Stopping the MongoDB and removing the directory
stop_mongodb()
unlink(d, recursive = TRUE)

Start and stop MongoDB instances

Description

The start_mongodb() and stop_mongodb() functions start and stop a MongoDB database using the mongod system call. These functions require an installation of the MongoDB server and the mongod binary to be present in the System path.

Usage

start_mongodb(
  cmd = "mongod",
  dbpath = file.path(tempdir(), "mongo"),
  logpath = file.path(tempdir(), "mongo.log")
)

stop_mongodb(cmd = "mongod", dbpath = file.path(tempdir(), "mongo"))

Arguments

cmd

character(1) with the binary to start or stop the MongoDB.

dbpath

character(1) with the location of the MongoDB database. Defaults to file.path(tempdir(), "mongo") hence using a temporary folder for the data.

logpath

character(1) with the location of the log file for the database. Defaults to file.path(tempdir(), "mongo.log").

Value

start_mongodb() and stop_mongodb() don't return any value, but throw an error if the system2 call did not return 0.

Examples

## Start a MongoDB service with the database location in *test_mongo* in
## the System's temporary folder.
start_mongodb(dbpath = file.path(tempdir(), "test_mongo"))

## Stop the MongoDB
stop_mongodb(dbpath = file.path(tempdir(), "test_mongo"))

## Remove the database
unlink(file.path(tempdir(), "test_mongo"), recursive = TRUE)