Matchms count spectra

From XuShuwenn/RedAct · View interactive version

Matchms count spectra is an AI agent skill from the GitHub repository XuShuwenn/RedAct. Parse MSP mass spectrometry files using matchms library to count spectra and find spectrum with most peaks.

How to use this skill

Skill file

Mass Spectrometry Peak Counting

When to Use

Input

Output

To /root/output.txt:

Total spectra: N
Spectrum with most peaks: NAME (K peaks)
Total peaks across all spectra: M

Write /root/output.txt in the exact three-line format shown above. Preserve labels, punctuation, spacing, and line order exactly. Do not add extra commentary, warnings, or debug output. If the selected spectrum has no usable name, write N/A as the spectrum name.

Using matchms

try:
    from matchms.importing import load_msp_file as _load_msp
except ImportError:
    from matchms.importing import load_from_msp as _load_msp

try:
    spectra = list(_load_msp("/root/input.msp"))
except Exception:
    spectra = []

Calculations

  1. If parsing fails or no spectra are returned, write the documented zero-result output.
  2. Otherwise, compute all summary statistics in one pass over the spectra.
  3. Count peaks for each spectrum with len(spectrum.peaks.mz) when available; if peak data is missing or malformed for an individual record, treat that spectrum as having 0 peaks and continue.
  4. For the spectrum label, use compound_name first and fall back to name, then N/A.
  1. Track total spectra, total peaks, and the spectrum with the most peaks in that same loop.
  2. Write /root/output.txt in the exact required three-line format.
  1. Check whether /root/input.msp exists before parsing; if it is missing, write the documented zero-result output immediately.
  2. Keep the aggregation compact: for each parsed spectrum, increment the spectrum count, derive that spectrum's peak count once, add it to the running total, and update the current maximum only when the new peak count is strictly larger.
  3. Initialize the current maximum as N/A with 0 peaks so the first spectrum with the highest peak count remains the winner if later spectra tie it.
  4. Read metadata defensively, e.g. via spectrum.get("compound_name") / spectrum.get("name") or equivalent normalized access, and normalize the chosen label to a usable string; if the selected value is missing, empty, malformed, or not string-like, write N/A.
  5. For the zero-result fallback, still write the exact three-line output file instead of exiting early without creating /root/output.txt.

Edge Cases

If file is empty/unparseable:

Total spectra: 0
Spectrum with most peaks: N/A (0 peaks)
Total peaks across all spectra: 0

Use the same fallback output for parsing exceptions, missing files, and successfully parsed files with zero spectra. If matchms emits warnings but still returns spectra, continue and compute the requested metrics. Only use the zero-valued fallback output when the file is unreadable, parsing raises an exception, or no spectra are parsed.

Tips

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.