Package 'SpectraToQueries'

Title: Spectra to queries
Description: SpectraToQueries provides the infrastructure to translate spectra to queries.
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-10-19 20:26:08 UTC
Source: https://github.com/spectra-to-knowledge/SpectraToQueries

Help Index


Combine ions into minimal AND/OR expression with sorting

Description

Combine ions into minimal AND/OR expression with sorting

Usage

combine_ions_minimal(ion_lists)

Arguments

ion_lists

List of character vectors representing ion combinations

Value

Single character string representing nested AND/OR expression


Check if spectrum contains all target m/z values

Description

Check if spectrum contains all target m/z values

Usage

contains_all_mz(spec_mz, target_mz, dalton, ppm)

Arguments

spec_mz

Numeric vector of spectrum m/z values

target_mz

Numeric vector of target m/z values to find

dalton

Tolerance in Dalton

ppm

Tolerance in parts per million

Value

Logical indicating if all targets are found

Examples

NULL

Check if spectrum contains all target m/z values (internal variant with pre-computed tolerances)

Description

Check if spectrum contains all target m/z values (internal variant with pre-computed tolerances)

Usage

contains_all_mz_tol(spec_mz, target_mz, tol)

Arguments

spec_mz

Numeric vector of spectrum m/z values

target_mz

Numeric vector of target m/z values to find

tol

Numeric vector of pre-computed tolerances (length must match target_mz)

Value

Logical indicating if all targets are found

Examples

NULL

Create directory

Description

This function creates a directory at the specified path if it does not already exist.

Usage

create_dir(export)

Arguments

export

Path to the directory to be created

Value

Message indicating the status of directory creation

Source

https://github.com/taxonomicallyinformedannotation/tima/blob/main/R/create_dir.R


Create matrix

Description

Create matrix

Usage

create_matrix(spectra, zero_val = 0, name)

Arguments

spectra

Spectra

zero_val

Zero value

name

Name

Value

A matrix with spectra intensity data

Examples

NULL

Filter matrix

Description

Filter matrix

Usage

filter_matrix(matrix, n)

Arguments

matrix

Matrix to filter

n

Minimum number of non-zero values per column

Value

Filtered matrix with columns having at least n non-zero values

Examples

NULL

Fix binned mzs

Description

Fix binned mzs

Usage

fix_binned_mzs(binned_m, original_mzs, dalton, ppm, decimals)

Arguments

binned_m

Binned matrix

original_mzs

Original m/z values from spectra

dalton

Tolerance in Dalton

ppm

Tolerance in parts per million

decimals

Number of decimal places for rounding

Value

Matrix with corrected m/z column names

Examples

NULL

Generate combinations

Description

Generate combinations

Usage

generate_combinations(x, max_ions)

Arguments

x

Vector of elements to combine

max_ions

Maximum number of ions in combinations

Value

List of all combinations

Examples

NULL

Generate combinations with progress

Description

Generate combinations with progress

Usage

generate_combinations_progress(
  indices,
  ions_list,
  max_ions,
  show_progress = TRUE
)

Arguments

indices

Vector of indices to process

ions_list

List of ions for each index

max_ions

Maximum number of ions in combinations

show_progress

Logical: show progress bar (default TRUE)

Value

List of all combinations for each index

Examples

NULL

Harmonize mzs

Description

Harmonize mzs

Usage

harmonize_mzs(spectra, dalton, ppm)

Arguments

spectra

Spectra object

dalton

Tolerance in Dalton

ppm

Tolerance in parts per million

Value

Spectra with harmonized m/z values

Examples

NULL

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

Description

A data.frame containing 321 spectra of monoterpene indole alkaloids (MIAs) from the MIADB (Monoterpene Indole Alkaloid Database). This is stored as a portable data.frame with m/z and intensity peaks as list columns, and is automatically converted to a 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, and other MS/MS spectrum attributes

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

  • intensity (list): Fragment intensities for each spectrum

Total of approximately 48,510 peaks across all spectra.

Details

The mia_spectra_df object represents the raw spectral data with minimal preprocessing. Use mia_spectra_grouped_df for the pre-grouped variant.

This portable data.frame format enables export to CSV, Parquet, JSON, and other formats for cross-platform distribution via r-universe. It is automatically converted to a Spectra object by spectra_to_queries().

Source

Data extracted from MIADB, a public spectral library of monoterpene indole alkaloids. Original source: https://miadb.imsb.qcb.usp.br/

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. https://doi.org/10.1186/s13321-025-01009-0

Examples

## Not run: 
  # Load the raw spectra
  utils::data(mia_spectra_df)
  nrow(mia_spectra_df)  # 321 spectra

  # Convert to Spectra object (for advanced users)
  # spectra_obj <- SpectraToQueries:::df_to_spectra(mia_spectra_df)

## End(Not run)

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

Description

A data.frame containing 321 grouped mass spectra from the MIADB (Monoterpene Indole Alkaloid Database), combined by spectrum title. This is the primary example dataset used in the spectra_to_queries() function. Stored as a portable data.frame with m/z 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, and other MS/MS spectrum attributes

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

  • intensity (list): Fragment intensities for each spectrum

Total of approximately 48,510 peaks across all spectra (median ~132 peaks per spectrum).

Details

This dataset is grouped by spectrum title (via Spectra::combineSpectra()) to simplify downstream analysis. It is the recommended example for demonstrating the spectra_to_queries() workflow.

This portable data.frame format enables export to CSV, Parquet, JSON, and other formats for cross-platform distribution via r-universe. It is automatically converted to a Spectra object by spectra_to_queries().

Source

Data extracted and pre-grouped from MIADB, a public spectral library of monoterpene indole alkaloids. Original source: https://miadb.imsb.qcb.usp.br/

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. https://doi.org/10.1186/s13321-025-01009-0

Examples

## Not run: 
  # Load the grouped spectra
  utils::data(mia_spectra_grouped_df)
  nrow(mia_spectra_grouped_df)  # 321 spectra
  table(mia_spectra_grouped_df$SKELETON)  # Skeleton distribution

  # Convert to Spectra object (for advanced users)
  # spectra_obj <- SpectraToQueries:::df_to_spectra(mia_spectra_grouped_df)

## End(Not run)

Normalize peaks

Description

Normalize peaks

Usage

normalize_peaks()

Examples

NULL

Perform list of queries

Description

Perform list of queries

Usage

perform_list_of_queries(index, ions_list, query_data, dalton, ppm)

Arguments

index

Index of the ion list to process

ions_list

List of ions for queries

query_data

Data frame with columns mz, precursor_mz, and skeleton

dalton

Tolerance in Dalton

ppm

Tolerance in parts per million

Value

Data frame with target and value columns

Examples

NULL

Perform list of queries

Description

Perform list of queries

Usage

perform_list_of_queries_progress(
  ions_list,
  spectra,
  dalton,
  ppm,
  show_progress = TRUE
)

Arguments

ions_list

List of ion combinations for queries

spectra

Spectra object to search

dalton

Tolerance in Dalton

ppm

Tolerance in parts per million

show_progress

Logical: show progress bar (default TRUE)

Value

List of query results

Examples

NULL

Perform query

Description

Perform query

Usage

perform_query(all_mz, precursor_mz, frags, nls, dalton, ppm)

Arguments

all_mz

List of numeric vectors of m/z values for each spectrum

precursor_mz

Numeric vector of precursor m/z values for each spectrum

frags

Fragment masses to search for

nls

Neutral losses to search for

dalton

Tolerance in Dalton

ppm

Tolerance in parts per million

Value

Filtered spectra object

Examples

NULL

Prepare spectra data for querying

Description

Extracts mz, precursor m/z, and skeleton labels once, so perform_query() never has to touch the Spectra backend again.

Usage

prepare_query_data(spectra)

Arguments

spectra

Spectra object

Examples

NULL

Spectra to queries

Description

This function converts spectra to diagnostic ion 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

Spectra object or path to MGF file. If NULL, loads the example spectra dataset. If the string "grouped", loads the grouped example spectra. Otherwise, a path to an MGF file or a Spectra object.

export

Export path

dalton

Tolerance in Dalton. Default to 0.01

decimals

Number of decimals for rounding. Default to 4L

intensity_min

Minimal intensity. Default to 0.0

ions_max

Maximal number of ions in the query. Default to 10L

n_skel_min

Minimal number of individuals per skeleton. Default to 5L

n_spec_min

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

ppm

Tolerance in parts per million Default to 30.0

mcc_min

Minimal single-ion Matthews Correlation Coefficient. Default to 0.0

precision_min

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

recall_min

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

zero_val

Zero value for intensity. Default to 0.0

Value

A file with diagnostic query ions

Examples

## Not run: 
  # Run with default spectra
  spectra_to_queries()

  # Or explicitly load and pass spectra_grouped
  data(spectra_grouped)
  spectra_to_queries(spectra = spectra_grouped)

## End(Not run)