Package 'cascade'

Title: Contextualizing Untargeted Annotation with Semi-Quantitative Charged Aerosol Detection for Pertinent Characterization of Natural Extracts
Description: Provides the infrastructure to perform Automated Composition Assessment of Natural Extracts by combining untargeted mass spectrometry feature tables and taxonomically informed metabolite annotations (TIMA) with semi-quantitative universal detectors such as Charged Aerosol Detection (CAD) and Photodiode Array (PDA) chromatograms. Includes Fourier filtering, derivative-based peak sharpening, peak integration, extracted ion chromatogram correlation, Wikidata/LOTUS chemotaxonomic literature queries, and interactive visualizations.
Authors: Adriano Rutz [aut, cre] (ORCID: <https://orcid.org/0000-0003-0443-9902>)
Maintainer: Adriano Rutz <[email protected]>
License: AGPL (>= 3)
Version: 0.0.0.9003
Built: 2026-11-20 22:56:07 UTC
Source: https://github.com/adafede/cascade

Help Index


CASCADE Example Datasets

Description

Example datasets included with CASCADE for demonstration and testing purposes. All tabular datasets are stored as data.frame objects and converted to data.table::data.table when loaded via the internal ⁠load_*⁠ helpers.

Usage

cascade_annotations

cascade_features

cascade_features_informed

cascade_features_not_informed

cascade_chromatograms_positive

cascade_chromatograms_negative

cascade_ms_data

Format

  • cascade_annotations: data.frame, 3,471 rows x 58 columns (EDAM: data_0962 Small molecule report)

  • cascade_features: data.frame, 3,471 rows x 101 columns (EDAM: data_2536 Mass spectrometry data)

  • cascade_features_informed: data.frame, 4,132 rows x 11 columns (EDAM: data_2536 Mass spectrometry data)

  • cascade_features_not_informed: data.frame, 650 rows x 11 columns (EDAM: data_2536 Mass spectrometry data)

  • cascade_chromatograms_positive: data.frame, 122,541 rows x 3 columns (EDAM: data_2536 Mass spectrometry data)

  • cascade_chromatograms_negative: data.frame, 122,079 rows x 3 columns (EDAM: data_2536 Mass spectrometry data)

  • cascade_ms_data: Named list (rt, mz, intensity) containing 5,668 MS1 scans (EDAM: data_0943 Mass spectrum)

An object of class data.frame with 3471 rows and 58 columns.

An object of class data.frame with 3471 rows and 101 columns.

An object of class data.frame with 4132 rows and 11 columns.

An object of class data.frame with 650 rows and 11 columns.

An object of class data.frame with 122541 rows and 3 columns.

An object of class data.frame with 122079 rows and 3 columns.

An object of class list of length 3.

cascade_annotations

Metabolite annotation table from a Swertia extract analyzed with TIMA. Contains 3,471 unique features with their chemical and taxonomic contextualizations.

cascade_features

Complete mzmine feature table. Contains 3,471 features with m/z, retention time, and intensity.

cascade_features_informed

CAD-detected peaks that matched annotation hypotheses. Contains 4,132 peak-feature matches.

cascade_features_not_informed

CAD-detected peaks that did not match any annotation hypothesis. Contains 650 peak-feature matches.

cascade_chromatograms_positive

Chromatogram traces from positive-mode analysis. Tidy long-form data.frame with 3 columns.

cascade_chromatograms_negative

Chromatogram traces from negative-mode analysis. Tidy long-form data.frame with 3 columns.

cascade_ms_data

Example MS1 spectral dataset used for extracted ion chromatogram (XIC) reconstruction and peak shape comparison.


Check multi-detector chromatogram alignment

Description

Visualizes and aligns BPI, CAD, and PDA chromatograms across positive and/or negative ionization runs after optional Fourier noise filtering, peak sharpening, and baseline correction.

Usage

check_chromatograms_alignment(
  file_negative = NULL,
  file_positive = NULL,
  time_min = 0.5,
  time_max = 32.5,
  cad_shift = 0.05,
  pda_shift = 0.1,
  fourier_components = 0.01,
  frequency = 1,
  resample = 1,
  chromatograms = c("bpi_pos", "cad_pos", "pda_pos"),
  headers = c(bpi = "BasePeak_0", pda = "PDA#1_TotalAbsorbance_0", cad = "UV#1_CAD_1_0"),
  type = "baselined",
  normalize_intensity = TRUE,
  normalize_time = FALSE,
  show_example = FALSE,
  intensity_floor = 0.001,
  k2 = 250,
  k4 = 1250000,
  sigma = 0.05,
  smoothing_width = 8,
  baseline_method = "peakDetection",
  improve_signal = TRUE
)

Arguments

file_negative

Path to negative-mode .mzML file (optional).

file_positive

Path to positive-mode .mzML file (optional).

time_min

Minimum retention time in minutes. Default is 0.5.

time_max

Maximum retention time in minutes. Default is 32.5.

cad_shift

CAD retention time shift in minutes, or "auto" / NULL to estimate the optimal shift via FFT cross-correlation against the MS BPI trace. Default is 0.05.

pda_shift

PDA retention time shift in minutes, or "auto" / NULL to estimate the optimal shift via FFT cross-correlation against the MS BPI trace. Default is 0.1.

fourier_components

Fraction of Fourier components to retain. Default is 0.01.

frequency

Acquisition frequency in Hz. Default is 1.

resample

Resampling factor. Default is 1.

chromatograms

Character vector of chromatograms to plot. Default is c("bpi_pos", "cad_pos", "pda_pos").

headers

Named character vector mapping detector types ("bpi", "pda", "cad") to .mzML chromatogram header IDs.

type

Type of chromatogram to display: "baselined" or "improved". Default is "baselined".

normalize_intensity

Logical; normalize intensity to ⁠[0, 1]⁠. Default is TRUE.

normalize_time

Logical; normalize time to ⁠[0, 1]⁠. Default is FALSE.

show_example

Logical; use bundled example data. Default is FALSE.

intensity_floor

Small positive value for intensity floor. Default is 0.001.

k2

Second-derivative weight for signal sharpening. Default is 250.

k4

Fourth-derivative weight for signal sharpening. Default is 1250000.

sigma

Gaussian sigma for signal sharpening. Default is 0.05.

smoothing_width

Moving-average window width for derivative smoothing. Default is 8.

baseline_method

Baseline correction method passed to baseline::baseline(). Default is "peakDetection".

improve_signal

Logical; apply Fourier filtering and sharpening. Default is TRUE.

Value

A plotly htmlwidget displaying the aligned chromatograms.

Examples

check_chromatograms_alignment(show_example = TRUE)

Check chromatographic peak detection and integration bounds

Description

Detects, fits (via Exponential-Gaussian Hybrid or Gaussian nonlinear least squares), and trapezoidally integrates peaks in a detector chromatogram (CAD, BPI, or PDA), links them to MS features, and returns an interactive plotly visualization showing the signal, detected peak maxima, and start/end integration bounds.

Usage

check_peaks_integration(
  file = NULL,
  features = NULL,
  detector = "cad",
  chromatogram = "baselined",
  headers = c(bpi = "BasePeak_0", pda = "PDA#1_TotalAbsorbance_0", cad = "UV#1_CAD_1_0"),
  min_area = 0.005,
  min_intensity = 10000,
  shift = 0.05,
  show_example = FALSE,
  fourier_components = 0.01,
  time_min = 0.5,
  time_max = 32.5,
  frequency = 1,
  resample = 1,
  intensity_floor = 0.001,
  k2 = 250,
  k4 = 1250000,
  sigma = 0.05,
  smoothing_width = 8,
  baseline_method = "peakDetection",
  sd_max = 50,
  max_iter = 1000,
  noise_threshold = 0.001,
  fit = "egh",
  intensity_threshold = 0.1,
  improve_signal = TRUE
)

Arguments

file

Path to .mzML file (optional when show_example = TRUE).

features

Path to feature table (optional when show_example = TRUE).

detector

Detector type: "cad", "bpi", or "pda". Default is "cad".

chromatogram

Chromatogram processing stage: "original", "improved", or "baselined". Default is "baselined".

headers

Named character vector mapping detector types to .mzML chromatogram header IDs.

min_area

Minimum relative area fraction for peak filtering. Default is 0.005.

min_intensity

Minimum MS intensity for feature filtering. Default is 1e4.

shift

Retention time shift in minutes. Default is 0.05.

show_example

Logical; use bundled example data. Default is FALSE.

fourier_components

Fraction of Fourier components to retain. Default is 0.01.

time_min

Minimum retention time in minutes. Default is 0.5.

time_max

Maximum retention time in minutes. Default is 32.5.

frequency

Acquisition frequency in Hz. Default is 1.

resample

Resampling factor. Default is 1.

intensity_floor

Small positive value for intensity floor. Default is 0.001.

k2

Second-derivative weight for signal sharpening. Default is 250.

k4

Fourth-derivative weight for signal sharpening. Default is 1250000.

sigma

Gaussian sigma for signal sharpening. Default is 0.05.

smoothing_width

Moving-average window width for derivative smoothing. Default is 8.

baseline_method

Baseline correction method passed to baseline::baseline(). Default is "peakDetection".

sd_max

Maximum standard deviation for peak filtering. Default is 50.

max_iter

Maximum iterations for peak fitting. Default is 1000.

noise_threshold

Noise threshold for peak detection. Default is 0.001.

fit

Peak fitting model: "egh", "gaussian", or "raw". Default is "egh".

intensity_threshold

Minimum normalized intensity threshold for peak filtering. Default is 0.1.

improve_signal

Logical; apply Fourier filtering and sharpening. Default is TRUE.

Value

A plotly htmlwidget showing the chromatogram and detected peak boundaries.

Examples

check_peaks_integration(show_example = TRUE)

Generate chemotaxonomic literature hierarchies and plots from Wikidata/LOTUS

Description

Queries Wikidata for natural products reported in the specified taxa within a publication year range (start to end), enriches them with NPClassifier chemical classifications from LOTUS, optionally deduplicates stereoisomers and filters to chromatographiable mass/XLogP ranges, and builds stacked histograms, interactive treemaps, and sunburst plots.

Usage

generate_ids(
  taxa = c("Swertia", "Kopsia", "Ginkgo"),
  comparison = NULL,
  no_stereo = TRUE,
  filter_ms_conditions = TRUE,
  start = "0",
  end = "9999",
  limit = "1000000",
  show_title = FALSE,
  format_tables = FALSE
)

Arguments

taxa

Character vector of scientific taxon names to query. Default is c("Swertia", "Kopsia", "Ginkgo").

comparison

Optional character vector of two taxon names in taxa for side-by-side comparative visualization. Default is NULL.

no_stereo

Logical; collapse stereoisomers to unique 2D structures per taxon/reference. Default is TRUE.

filter_ms_conditions

Logical; filter structures to chromatographiable mass (⁠50..1500⁠ Da) and XLogP (⁠-1..6⁠) ranges. Default is TRUE.

start

Character(1); minimum publication year. Default is "0".

end

Character(1); maximum publication year. Default is "9999".

limit

Character(1); maximum number of SPARQL rows per taxon. Default is "1000000".

show_title

Logical; include taxon name and unique structure count titles on interactive treemaps and sunbursts. Default is FALSE.

format_tables

Logical; format the returned tables element as interactive gt::gt() tables with clickable Wikidata links and 2D structure depictions. Default is FALSE.

Value

A named list with four elements:

  • plots: Named list of ggplot chemical class histograms.

  • treemaps: Named list of interactive plotly treemap widgets.

  • sunbursts: Named list of interactive plotly sunburst widgets.

  • tables: Named list of compound-taxon-reference data.table objects (or gt_tbl objects when format_tables = TRUE).

Examples

old_wd <- setwd(tempdir())
try(generate_ids(taxa = "Swertia"))
setwd(old_wd)

Generate annotated pseudochromatograms and chemical class treemaps

Description

Combines preprocessed detector chromatograms, peak-correlated MS features, and TIMA structural annotations to produce a suite of annotated pseudochromatograms (colored by chemical pathway/superclass and closest taxonomic occurrence) and interactive chemical hierarchy treemaps.

Usage

generate_pseudochromatograms(
  annotations = NULL,
  features_informed = NULL,
  features_not_informed = NULL,
  file = NULL,
  headers = c(bpi = "BasePeak_0", pda = "PDA#1_TotalAbsorbance_0", cad = "UV#1_CAD_1_0"),
  detector = "cad",
  show_example = FALSE,
  min_confidence = 0.4,
  min_similarity_prefilter = 0.6,
  min_similarity_filter = 0.8,
  mode = "pos",
  organism = "Swertia chirayita",
  fourier_components = 0.01,
  frequency = 1,
  resample = 1,
  shift = 0.05,
  time_min = 0.5,
  time_max = 32.5,
  intensity_floor = 0.001,
  k2 = 250,
  k4 = 1250000,
  sigma = 0.05,
  smoothing_width = 8,
  baseline_method = "peakDetection",
  improve_signal = TRUE,
  correct_acn = FALSE,
  acn_time = 30.5,
  dvol = 0.5
)

Arguments

annotations

Path to TIMA annotations .tsv file (optional when show_example = TRUE).

features_informed

Path to peak-informed features .tsv file (optional when show_example = TRUE).

features_not_informed

Path to non-informed features .tsv file (optional when show_example = TRUE).

file

Path to .mzML file (optional when show_example = TRUE).

headers

Named character vector mapping detector types to .mzML chromatogram header IDs.

detector

Detector type: "cad", "bpi", or "pda". Default is "cad".

show_example

Logical; use bundled example data. Default is FALSE.

min_confidence

Minimum final annotation confidence score. Default is 0.4.

min_similarity_prefilter

Minimum correlation score for pre-filtering. Default is 0.6.

min_similarity_filter

Minimum correlation score for final filtering. Default is 0.8.

mode

Ionization mode: "pos" or "neg". Default is "pos".

organism

Investigated organism scientific name for taxonomic prioritization. Default is "Swertia chirayita".

fourier_components

Fraction of Fourier components to retain. Default is 0.01.

frequency

Acquisition frequency in Hz. Default is 1.

resample

Resampling factor. Default is 1.

shift

Retention time shift in minutes. Default is 0.05.

time_min

Minimum retention time in minutes. Default is 0.5.

time_max

Maximum retention time in minutes. Default is 32.5.

intensity_floor

Small positive value for intensity floor. Default is 0.001.

k2

Second-derivative weight for signal sharpening. Default is 250.

k4

Fourth-derivative weight for signal sharpening. Default is 1250000.

sigma

Gaussian sigma for signal sharpening. Default is 0.05.

smoothing_width

Moving-average window width for derivative smoothing. Default is 8.

baseline_method

Baseline correction method passed to baseline::baseline(). Default is "peakDetection".

improve_signal

Logical; apply Fourier filtering and sharpening. Default is TRUE.

correct_acn

Logical; apply acetonitrile gradient response correction to CAD peak areas. Default is FALSE.

acn_time

Numeric; gradient time in minutes to reach 100% acetonitrile when correct_acn = TRUE. Default is 30.5.

dvol

Numeric; dwell time offset in minutes when correct_acn = TRUE. Default is 0.5.

Value

A named list with three elements:

  • plots_1: Named list of 8 ggplot pseudochromatograms.

  • plots_2: Named list of 2 interactive plotly signal-based sunbursts.

  • treemaps: Named list of interactive plotly treemap widgets.

Examples

generate_pseudochromatograms(show_example = TRUE)

Generate interactive HTML and TSV summary tables of annotated peaks

Description

Integrates peak-informed features across positive and/or negative modes with TIMA structural annotations and LOTUS taxonomic/literature metadata, applies confidence-ordered 1D non-maximum suppression across retention-time windows, and produces both an interactive HTML table (with 2D chemical structure depictions via CDK Depict) and a tab-separated summary table.

Usage

generate_tables(
  annotations = NULL,
  file_negative = NULL,
  file_positive = NULL,
  min_confidence = 0.4,
  show_example = FALSE,
  export_csv = TRUE,
  export_html = TRUE,
  export_dir = "data/processed",
  export_name = "cascade_table"
)

Arguments

annotations

Path to TIMA annotations .tsv file (optional when show_example = TRUE).

file_negative

Path to negative-mode informed features .tsv file (optional).

file_positive

Path to positive-mode informed features .tsv file (optional when show_example = TRUE).

min_confidence

Minimum final annotation confidence score. Default is 0.4.

show_example

Logical; use bundled example data. Default is FALSE.

export_csv

Logical; export .tsv summary table. Default is TRUE.

export_html

Logical; export .html interactive gt table. Default is TRUE.

export_dir

Directory where output files are written. Default is "data/processed".

export_name

Base filename (without extension) for exported tables. Default is "cascade_table".

Value

A named list with two elements:

  • pretty_table: A gt_tbl interactive table object.

  • csv_table: A data.table containing the formatted summary rows.

Examples

old_wd <- setwd(tempdir())
generate_tables(show_example = TRUE, export_csv = FALSE, export_html = FALSE)
setwd(old_wd)

Plot TIMA annotation chemical class distributions

Description

Generates a stacked horizontal histogram, an interactive treemap, and an interactive sunburst plot from one or more prepared TIMA annotation tables returned by prepare_tima_annotations().

Usage

plot_tima(tables)

Arguments

tables

A list of prepared annotation data.table objects (as returned by prepare_tima_annotations()).

Value

A named list with three elements:

  • histogram: A ggplot stacked bar chart.

  • treemap: A plotly interactive treemap widget.

  • sunburst: A plotly interactive sunburst widget.

Examples

tables <- prepare_tima_annotations(show_example = TRUE)
plot_tima(tables)

Prepare TIMA structural and predicted annotation tables

Description

Filters and standardizes Taxonomically Informed Metabolite Annotation (TIMA) output tables across initial spectral similarity, matched peak counts, biological, chemical, and final confidence scores, retaining the top candidate per feature for downstream visualization with plot_tima().

Usage

prepare_tima_annotations(
  annotations = NULL,
  predicted_classes = FALSE,
  min_score_initial = 0,
  min_score_biological = 0,
  min_score_chemical = 0,
  min_score_final = 0,
  min_matched_peaks_absolute = 0L,
  min_matched_peaks_percentage = 0,
  min_peaks = 3L,
  libraries = c("gnps", "massbank", "merlin", "ISDB", "ISDB - Wikidata", "TIMA MS1"),
  show_example = FALSE
)

Arguments

annotations

Named character vector of annotation file paths (optional when show_example = TRUE).

predicted_classes

Logical; use Sirius/CANOPUS predicted NPClassifier classes instead of candidate structure classes. Default is FALSE.

min_score_initial

Minimum initial spectral similarity score. Default is 0.0.

min_score_biological

Minimum biological (taxonomic) score. Default is 0.0.

min_score_chemical

Minimum chemical consistency score. Default is 0.0.

min_score_final

Minimum final combined score. Default is 0.0.

min_matched_peaks_absolute

Minimum absolute number of matched MS2 peaks. Default is 0L.

min_matched_peaks_percentage

Minimum fraction of spectrum peaks matched. Default is 0.0.

min_peaks

Minimum total number of peaks in the query MS2 spectrum. Default is 3L.

libraries

Character vector of spectral libraries to retain. Default is ⁠c("gnps", "massbank", "merlin", "ISDB", "ISDB - Wikidata",⁠ ⁠"TIMA MS1")⁠.

show_example

Logical; use bundled example data. Default is FALSE.

Value

A named list of prepared data.table annotation tables.

Examples

prepare_tima_annotations(show_example = TRUE)

Compare MS extracted ion chromatograms against detector peaks

Description

Extracts MS1 ion chromatograms (XICs) for all features falling within detected and trapezoidally integrated chromatographic peaks (e.g., CAD, PDA, or BPI), computes window-preserving interpolated Pearson correlation similarity scores between each feature's XIC profile and the corresponding detector peak profile, and exports both peak-informed and non-informed feature tables as TSV files.

Usage

process_compare_peaks(
  file = NULL,
  features = NULL,
  type = "baselined",
  detector = "cad",
  headers = c(bpi = "BasePeak_0", pda = "PDA#1_TotalAbsorbance_0", cad = "UV#1_CAD_1_0"),
  export_dir = "data/interim/peaks",
  show_example = FALSE,
  fourier_components = 0.01,
  frequency = 1,
  min_area = 0.005,
  min_intensity = 10000,
  resample = 1,
  shift = 0.05,
  time_min = 0.5,
  time_max = 32.5
)

Arguments

file

Path to .mzML file (optional when show_example = TRUE).

features

Path to feature table (optional when show_example = TRUE).

type

Chromatogram processing stage: "original", "baselined", or "improved". Default is "baselined".

detector

Detector type: "cad", "bpi", or "pda". Default is "cad".

headers

Named character vector mapping detector types to .mzML chromatogram header IDs.

export_dir

Directory where the output .tsv files are written. Default is "data/interim/peaks".

show_example

Logical; use bundled example data. Default is FALSE.

fourier_components

Fraction of Fourier components to retain. Default is 0.01.

frequency

Acquisition frequency in Hz. Default is 1.

min_area

Minimum relative area fraction for peak filtering. Default is 0.005.

min_intensity

Minimum MS intensity for feature filtering. Default is 1e4.

resample

Resampling factor. Default is 1.

shift

Retention time shift in minutes. Default is 0.05.

time_min

Minimum retention time in minutes. Default is 0.5.

time_max

Maximum retention time in minutes. Default is 32.5.

Value

Invisibly returns a named list with two data.table elements: features_informed and features_not_informed.

Examples

tmp <- tempdir()
process_compare_peaks(show_example = TRUE, export_dir = tmp)

Query a SPARQL endpoint efficiently

Description

Performs a SPARQL query against Wikidata (or another SPARQL 1.1 endpoint) and returns a data.table. Requests gzip-compressed CSV (falling back to TSV) streamed directly to disk via curl and parsed with data.table::fread(). Automatically retries against the QLever Wikidata endpoint (⁠https://qlever.dev/api/wikidata⁠) when fallback = TRUE.

Usage

query_wikidata(
  sparql_query,
  remove_url = TRUE,
  endpoint = "https://query.wikidata.org/sparql",
  agent = "https://github.com/adafede/cascade",
  timeout = 3600L,
  fallback = TRUE,
  headers = NULL,
  post = FALSE
)

Arguments

sparql_query

Character(1). SPARQL query string.

remove_url

Logical(1). Strip ⁠http(s)://www.wikidata.org/entity/⁠ prefix from character columns (default TRUE).

endpoint

Character(1). SPARQL endpoint URL. Default is "https://query.wikidata.org/sparql".

agent

Character(1). HTTP User-Agent header string.

timeout

Numeric(1). Total request timeout in seconds (default 3600L).

fallback

Logical(1). Retry with QLever Wikidata endpoint on failure (default TRUE).

headers

Character(1) or NULL. Optional Accept header for backward compatibility.

post

Logical(1). Send the SPARQL query as an HTTP POST request body (application/x-www-form-urlencoded) rather than a GET query parameter (default FALSE).

Value

A data.table containing the query results.

Examples

q <- paste0(
  "SELECT ?taxon WHERE { ",
  "?taxon <http://www.wikidata.org/prop/direct/P225> 'Gentiana lutea' . }"
)
try(query_wikidata(q, timeout = 15L))

Convert a scientific taxon name to its Wikidata QID

Description

Queries Wikidata for the item whose taxon name (wdt:P225) matches taxon_name and returns the corresponding Wikidata entity identifier (e.g., "Q158572").

Usage

taxon_name_to_qid(taxon_name)

Arguments

taxon_name

Character(1). Scientific name of the taxon (e.g., "Gentiana lutea" or "Swertia").

Value

Character vector of matching Wikidata QID(s).

Examples

try(taxon_name_to_qid(taxon_name = "Gentiana lutea"))