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

from matchms.importing import load_msp_file

try:
    spectra = list(load_msp_file("/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.
  5. Track total spectra, total peaks, and the spectrum with the most peaks in that same loop.
  6. Write /root/output.txt in the exact required three-line format.

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.