| 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 |
Combine ions into minimal AND/OR expression with sorting
combine_ions_minimal(ion_lists)combine_ions_minimal(ion_lists)
ion_lists |
List of character vectors representing ion combinations |
Single character string representing nested AND/OR expression
Check if spectrum contains all target m/z values
contains_all_mz(spec_mz, target_mz, dalton, ppm)contains_all_mz(spec_mz, target_mz, dalton, ppm)
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 |
Logical indicating if all targets are found
NULLNULL
Check if spectrum contains all target m/z values (internal variant with pre-computed tolerances)
contains_all_mz_tol(spec_mz, target_mz, tol)contains_all_mz_tol(spec_mz, target_mz, tol)
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) |
Logical indicating if all targets are found
NULLNULL
This function creates a directory at the specified path if it does not already exist.
create_dir(export)create_dir(export)
export |
Path to the directory to be created |
Message indicating the status of directory creation
https://github.com/taxonomicallyinformedannotation/tima/blob/main/R/create_dir.R
Create matrix
create_matrix(spectra, zero_val = 0, name)create_matrix(spectra, zero_val = 0, name)
spectra |
Spectra |
zero_val |
Zero value |
name |
Name |
A matrix with spectra intensity data
NULLNULL
Filter matrix
filter_matrix(matrix, n)filter_matrix(matrix, n)
matrix |
Matrix to filter |
n |
Minimum number of non-zero values per column |
Filtered matrix with columns having at least n non-zero values
NULLNULL
Fix binned mzs
fix_binned_mzs(binned_m, original_mzs, dalton, ppm, decimals)fix_binned_mzs(binned_m, original_mzs, dalton, ppm, decimals)
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 |
Matrix with corrected m/z column names
NULLNULL
Generate combinations
generate_combinations(x, max_ions)generate_combinations(x, max_ions)
x |
Vector of elements to combine |
max_ions |
Maximum number of ions in combinations |
List of all combinations
NULLNULL
Generate combinations with progress
generate_combinations_progress( indices, ions_list, max_ions, show_progress = TRUE )generate_combinations_progress( indices, ions_list, max_ions, show_progress = TRUE )
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) |
List of all combinations for each index
NULLNULL
Harmonize mzs
harmonize_mzs(spectra, dalton, ppm)harmonize_mzs(spectra, dalton, ppm)
spectra |
Spectra object |
dalton |
Tolerance in Dalton |
ppm |
Tolerance in parts per million |
Spectra with harmonized m/z values
NULLNULL
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().
mia_spectra_dfmia_spectra_df
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.
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().
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
## 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)## 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)
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.
mia_spectra_grouped_dfmia_spectra_grouped_df
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).
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().
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
## 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)## 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
normalize_peaks()normalize_peaks()
NULLNULL
Perform list of queries
perform_list_of_queries(index, ions_list, query_data, dalton, ppm)perform_list_of_queries(index, ions_list, query_data, dalton, ppm)
index |
Index of the ion list to process |
ions_list |
List of ions for queries |
query_data |
Data frame with columns |
dalton |
Tolerance in Dalton |
ppm |
Tolerance in parts per million |
Data frame with target and value columns
NULLNULL
Perform list of queries
perform_list_of_queries_progress( ions_list, spectra, dalton, ppm, show_progress = TRUE )perform_list_of_queries_progress( ions_list, spectra, dalton, ppm, show_progress = TRUE )
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) |
List of query results
NULLNULL
Perform query
perform_query(all_mz, precursor_mz, frags, nls, dalton, ppm)perform_query(all_mz, precursor_mz, frags, nls, dalton, ppm)
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 |
Filtered spectra object
NULLNULL
Extracts mz, precursor m/z, and skeleton labels once, so perform_query() never has to touch the Spectra backend again.
prepare_query_data(spectra)prepare_query_data(spectra)
spectra |
Spectra object |
NULLNULL
This function converts spectra to diagnostic ion queries.
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 )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 )
spectra |
Spectra object or path to MGF file. If |
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 |
A file with diagnostic query ions
## 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)## 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)