agentsclimarketplace

Spectral similarity measure implementation

Skill HolobiomicsLab/asb-skill-collections/collections/metabolomics/v2/skills/spectral-similarity-measure-implementation

Use when you have MSMS spectra from two or more compounds and need to identify which are structurally related. Use this skill when you want to rank spectrum pairs by similarity to discover novel analogs or validate structural assignments in untargeted metabolomics or natural products discovery.From its SKILL.md

Install
npx -y skills add HolobiomicsLab/asb-skill-collections --skill spectral-similarity-measure-implementation

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 15 stars15 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its file declares

Copied from the file, not written here

The file declares its own license as CC-BY-4.0. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.

SKILL.md

8.7 KB, ~1.5k tokens by cl100k_base, as published. Nobody here has run it

spectral-similarity-measure-implementation

License: restricted — no clear open-source license detected for the underlying tool; verify licensing before commercial use or redistribution. <!-- asb-license-banner -->

Summary

Implement and apply cosine, modified cosine, and neutral loss similarity measures to compare MSMS spectra for discovery of structurally related molecules. This skill enables quantitative ranking of spectrum pairs by their similarity, supporting library search and molecular networking workflows.

When to use

You have MSMS spectra from two or more compounds and need to identify which are structurally related. Use this skill when you want to rank spectrum pairs by similarity to discover novel analogs or validate structural assignments in untargeted metabolomics or natural products discovery. Apply it specifically when spectrum pairs require alignment-independent comparison or when neutral loss patterns (characteristic of compound class fragmentation) should be weighted equally with fragment peak matching.

When NOT to use

  • Input spectra are not preprocessed (precursor peak not removed, m/z range not normalized) — preprocessing is a prerequisite and this skill assumes valid input.
  • You need spectrum de novo structure elucidation or chemical formula assignment — this skill only ranks similarity; it does not infer chemical structures or provide molecular annotations.
  • Spectra are from different ionization modes or instrument types without cross-calibration — similarity scores are only meaningful for directly comparable spectra.

Inputs

  • MSMS spectrum pair(s) as MsmsSpectrum objects with precursor m/z, precursor charge, and fragment peaks (m/z and intensity)
  • Spectral dataset or library with multiple MSMS spectra (e.g., GNPS-LIBRARY or MSV entries with USI identifiers)
  • Preprocessed spectra with m/z range set and precursor peak removed

Outputs

  • Similarity score (float, range 0–1) for each spectrum pair under each similarity measure
  • Ranked list of candidate molecules ordered by similarity score to query spectrum
  • Mirror plot or comparison visualization showing fragment peak alignment under each similarity measure
  • Performance metrics (e.g., retrieval rank of known structurally related molecules, recall@k)

How to apply

Load two or more preprocessed MSMS spectra as MsmsSpectrum objects (precursor m/z, charge state, and fragment peaks with intensities specified). For each spectrum pair, compute one of three similarity scores: (1) cosine similarity, which treats peak m/z and intensity as vectors and calculates their dot product normalized by magnitude; (2) modified cosine similarity, which allows small m/z offsets (typically 0.1 Da) between matching fragments to account for measurement error; or (3) neutral loss similarity, which matches fragment pairs derived from the same neutral loss mass rather than absolute fragment m/z. Preprocess spectra by setting m/z range (0 to precursor m/z), removing the precursor peak (typically ±0.1 Da tolerance), and normalizing intensities. Apply all three measures to the same spectrum pairs to enable comparative evaluation of their ranking performance; score interpretations range from 0 (no similarity) to 1 (identical spectra). Rank candidate molecules by their similarity score to the query spectrum to assess retrieval effectiveness.

Related tools

  • cosine_neutral_loss (Implements cosine similarity, modified cosine similarity, and neutral loss similarity measures; provides plot and utility functions for spectrum preprocessing and mirror plot generation) — github.com/bittremieux/cosine_neutral_loss
  • spectrum_utils (Provides MsmsSpectrum class for loading, preprocessing (m/z range setting, precursor removal), and manipulating spectrum data; accessed via sus.MsmsSpectrum.from_usi())

Examples

import spectrum_utils.spectrum as sus; import plot; spectrum1 = sus.MsmsSpectrum.from_usi("mzspec:GNPS:GNPS-LIBRARY:accession:CCMSLIB00000424840"); spectrum1 = spectrum1.set_mz_range(0, spectrum1.precursor_mz).remove_precursor_peak(0.1, "Da"); plot.plot_mirror(spectrum1, spectrum2, "neutral_loss", "output.png")

Evaluation signals

  • Similarity scores are bounded in [0, 1] for all three measures and identical spectra score 1.0
  • Known structurally related molecule pairs (e.g., analog/parent compound pairs) rank higher under all three measures than random unrelated spectra
  • Modified cosine similarity recovers matches with m/z offsets (±0.1 Da) that cosine similarity misses, and neutral loss similarity ranks matches with shared fragmentation patterns higher than cosine
  • Mirror plot visually aligns matched fragment peaks between spectra; alignment quality improves moving from cosine to modified cosine to neutral loss for structurally related pairs
  • Retrieval performance (e.g., rank of true match in top-k results, recall@10) is consistent across multiple spectrum pairs and shows expected ranking differences between the three measures

Limitations

  • Similarity measures are insensitive to absolute fragment intensities if normalized; absolute intensity differences (e.g., due to instrument variation or ionization efficiency) do not affect cosine or neutral loss scores but may indicate true chemical differences.
  • Modified cosine similarity requires careful selection of m/z tolerance (typically 0.1 Da); values that are too large will cause spurious matches, while too-small tolerances may miss genuine peaks.
  • Neutral loss similarity is effective only when spectra exhibit characteristic neutral losses (e.g., loss of water or ammonia); compounds with few or non-specific neutral losses may not benefit from this measure.
  • Similarity measures do not account for spectral quality, dynamic range compression, or missing fragments due to instrumental limitations; low signal-to-noise spectra may produce unreliable scores.

Evidence

  • [readme] Similarity measures that are currently implemented are: - Cosine similarity - Modified cosine similarity - Neutral loss similarity: "Similarity measures that are currently implemented are: - Cosine similarity - Modified cosine similarity - Neutral loss similarity"
  • [intro] Comparison of cosine, modified cosine, and neutral loss based spectral alignment for discovery of structurally related molecules: "Comparison of cosine, modified cosine, and neutral loss based spectral alignment for discovery of structurally related molecules"
  • [intro] Code repository implements cosine similarity, modified cosine similarity, and neutral loss similarity measures for spectrum comparison: "Code repository implements cosine similarity, modified cosine similarity, and neutral loss similarity measures for spectrum comparison"
  • [readme] for score, filename in [(None, "spectra.png"), ("cosine", "cosine.png"), ("modified_cosine", "modified_cosine.png"), ("neutral_loss", "neutral_loss.png")]: plot.plot_mirror(spectrum1, spectrum2, score, filename): "for score, filename in [(None, "spectra.png"), ("cosine", "cosine.png"), ("modified_cosine", "modified_cosine.png"), ("neutral_loss", "neutral_loss.png")]: plot.plot_mirror(spectrum1, spectrum2,"
  • [readme] spectrum1 = spectrum1.set_mz_range(0, spectrum1.precursor_mz); spectrum1 = spectrum1.remove_precursor_peak(0.1, "Da"): "spectrum1 = spectrum1.set_mz_range(0, spectrum1.precursor_mz); spectrum1 = spectrum1.remove_precursor_peak(0.1, "Da")"

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.