Package 'SpectraToQueries'

Title: Spectra to queries
Description: Translates tandem mass spectrometry (MS/MS) spectral libraries into diagnostic fragment and neutral-loss ion queries for chemical structure and skeleton classification, ranked by Matthews Correlation Coefficient (MCC).
Authors: Adriano Rutz [aut, cre] (ORCID: <https://orcid.org/0000-0003-0443-9902>), Sarah Szwarc [ctb] (ORCID: <https://orcid.org/0000-0001-6369-5522>)
Maintainer: Adriano Rutz <[email protected]>
License: AGPL (>= 3)
Version: 0.0.0.9002
Built: 2026-11-20 20:10:35 UTC
Source: https://github.com/spectra-to-knowledge/SpectraToQueries

Help Index


Example mass spectrometry spectra from MIADB (as portable data.frame)

Description

A data.frame containing 321 MS/MS spectra of monoterpene indole alkaloids (MIAs) from the MIADB (Monoterpene Indole Alkaloid Database) across 81 fine-grained chemical skeletons (SKELETON). This is stored as a portable data.frame with mz and intensity peaks as list columns, and is automatically converted to a Spectra::Spectra object when used in spectra_to_queries().

Usage

mia_spectra_df

Format

A data.frame with 321 rows and 37 columns:

  • Metadata columns: msLevel, rtime, acquisitionNum, TITLE, PRECURSOR_MZ, SKELETON (81 unique skeletons), and other MS/MS spectrum attributes

  • mz (list): Fragment m/z values for each spectrum

  • intensity (list): Fragment intensities for each spectrum

Total of 24,255 peaks across all 321 spectra (median 66 peaks per spectrum).

Details

The mia_spectra_df object represents the spectral data with fine-grained skeleton annotations. Use mia_spectra_grouped_df for the variant with broader grouped skeleton annotations.

This portable data.frame format enables cross-platform distribution and is automatically converted to a Spectra::Spectra object by spectra_to_queries().

Source

Data extracted from MIADB, a public spectral library of monoterpene indole alkaloids.

Accompanying publication: Szwarc, S., Rutz, A., Lee, K. et al. (2025). "Translating community-wide spectral library into actionable chemical knowledge: a proof of concept with monoterpene indole alkaloids." Journal of Cheminformatics, 17:62. doi:10.1186/s13321-025-01009-0

Examples

utils::data(mia_spectra_df, package = "SpectraToQueries")
nrow(mia_spectra_df) # 321 spectra

Grouped mass spectrometry spectra from MIADB (as portable data.frame)

Description

A data.frame containing the 321 MS/MS spectra from the MIADB (Monoterpene Indole Alkaloid Database) with SKELETON annotations grouped into 53 broader chemical skeleton classes. Stored as a portable data.frame with mz and intensity peaks as list columns.

Usage

mia_spectra_grouped_df

Format

A data.frame with 321 rows and 36 columns:

  • Metadata columns: msLevel, rtime, acquisitionNum, TITLE, PRECURSOR_MZ, SKELETON (53 grouped skeleton classes), and other MS/MS spectrum attributes

  • mz (list): Fragment m/z values for each spectrum

  • intensity (list): Fragment intensities for each spectrum

Total of 24,255 peaks across all 321 spectra (median 66 peaks per spectrum).

Details

In this dataset, closely related sub-skeletons are merged into broader skeleton classes (e.g., corynanthean_1+corynanthean_2+...) to evaluate diagnostic ion queries at a higher structural hierarchy level.

This portable data.frame format enables cross-platform distribution and is automatically converted to a Spectra::Spectra object by spectra_to_queries().

Source

Data extracted from MIADB, a public spectral library of monoterpene indole alkaloids.

Accompanying publication: Szwarc, S., Rutz, A., Lee, K. et al. (2025). "Translating community-wide spectral library into actionable chemical knowledge: a proof of concept with monoterpene indole alkaloids." Journal of Cheminformatics, 17:62. doi:10.1186/s13321-025-01009-0

Examples

utils::data(mia_spectra_grouped_df, package = "SpectraToQueries")
nrow(mia_spectra_grouped_df) # 321 spectra
length(unique(mia_spectra_grouped_df$SKELETON)) # 53 grouped classes

Spectra to queries

Description

Converts tandem mass spectra into diagnostic fragment (⁠_frag⁠) and neutral-loss (⁠_nl⁠) ion queries ranked by Matthews Correlation Coefficient (MCC), together with ready-to-run MassQL queries.

Usage

spectra_to_queries(
  spectra = NULL,
  export = "data/interim/queries.tsv",
  dalton = 0.01,
  decimals = 4L,
  intensity_min = 0,
  ions_max = 10L,
  n_skel_min = 5L,
  n_spec_min = 3L,
  ppm = 30,
  mcc_min = 0,
  precision_min = 0,
  recall_min = 0,
  zero_val = 0
)

Arguments

spectra

A Spectra::Spectra object, a portable data.frame (such as mia_spectra_df or mia_spectra_grouped_df), or a file path to an MGF file. If NULL, loads the example mia_spectra_df dataset. If the string "grouped", loads the grouped example mia_spectra_grouped_df dataset.

export

Export file path for the resulting TSV table (or NULL to skip writing to disk). Default is "data/interim/queries.tsv".

dalton

Tolerance in Dalton. Default is 0.01.

decimals

Number of decimals for rounding. Default is 4L.

intensity_min

Minimal relative intensity threshold (0 to 100). Default is 0.0.

ions_max

Maximal number of ions in a query combination. Default is 10L.

n_skel_min

Minimal number of spectra per skeleton class. Default is 5L.

n_spec_min

Minimal number of spectra where a signal has to be found. Default is 3L.

ppm

Tolerance in parts per million. Default is 30.0.

mcc_min

Minimal single-ion Matthews Correlation Coefficient. Default is 0.0.

precision_min

Minimal single-ion precision (pre-filter before MCC). Default is 0.0.

recall_min

Minimal single-ion recall (pre-filter before MCC). Default is 0.0.

zero_val

Zero value threshold for intensity. Default is 0.0.

Details

For each skeleton class with at least n_skel_min spectra, the pipeline:

  1. Normalizes MS2 intensities, removes sub-threshold and non-positive neutral-loss peaks, and aligns m/z values across spectra using intensity-weighted consensus centroids within dalton + mz * ppm / 1e6.

  2. Evaluates candidate fragment and neutral-loss centroids against the raw unbinned spectra (requiring precursor_mz - nl > 0).

  3. Selects up to ions_max candidate ions per skeleton using greedy hitting-set pruning on 100%-recall ions followed by forward conjunctive MCC selection with negative-class correlation penalty (mRMR).

  4. Prunes the power set of ion combinations via Branch-and-Bound when the optimistic zero-FP upper bound cannot reach the current best MCC.

  5. Returns the single most parsimonious query achieving the maximum MCC per skeleton and translates it to MassQL via to_massql().

Value

Invisibly returns a data.frame with columns skeleton, mcc, combined_ions, and massql, and writes the TSV table to export when export is non-NULL.

Examples

df <- data.frame(
  msLevel = c(2L, 2L, 2L, 2L),
  SKELETON = c("SkelA", "SkelA", "SkelB", "SkelB"),
  precursorMz = c(300.0, 300.0, 300.0, 300.0)
)
df$mz <- list(
  c(100.0, 150.0),
  c(100.0, 160.0),
  c(200.0, 250.0),
  c(200.0, 260.0)
)
df$intensity <- list(c(80, 100), c(70, 100), c(90, 100), c(85, 100))
spectra_to_queries(
  spectra = df,
  export = NULL,
  n_skel_min = 2L,
  n_spec_min = 2L
)

Convert ion queries to MassQL syntax

Description

Translates one or more ion query expressions (such as "108.0804_frag & 241.095_nl", "100_frag & (200_frag | 300_frag)", or a list of ion vectors/expressions) into MassQL queries (⁠QUERY scaninfo(MS2DATA) WHERE ...⁠).

Usage

to_massql(query, dalton = 0.01, ppm = 30, intensity_min = 0)

Arguments

query

Character vector of query expressions, or a list of character vectors of ions or expression strings.

dalton

Tolerance in Dalton (TOLERANCEMZ). Default is 0.01.

ppm

Tolerance in parts per million (TOLERANCEPPM). Default is 30.0.

intensity_min

Minimal relative intensity threshold in percent (INTENSITYPERCENT). Omitted when ⁠<= 0⁠. Default is 0.0.

Details

MassQL ignores TOLERANCEMZ when TOLERANCEPPM is present on the same condition. When dalton > 0, to_massql() emits a single effective TOLERANCEMZ modifier (round(dalton + max(mz) * ppm / 1e6, 6) for MS2PROD, and static dalton for MS2NL). When dalton == 0 and ppm > 0, it emits TOLERANCEPPM. Disjunctive normal form clauses that cannot share a single OR group are joined with " ; ".

Value

Character vector of MassQL query strings.

Examples

to_massql("108.0804_frag & 241.095_nl", dalton = 0.01, ppm = 30)