Matchms - Mass Spectrometry Analysis

VerifiedSafe

Provides tools for loading, filtering, and comparing mass spectrometry spectra from mzML, MGF, MSP, and other formats. It includes over 40 filters for metadata harmonization and peak processing, and supports several similarity metrics (cosine, modified cosine, neutral loss) for spectral matching. Useful for building reproducible metabolomics workflows and library searching.

Sby Skills Guide Bot
Data & AIIntermediate
6/2/2026
Claude CodeCursorWindsurfCopilotCodex
#mass-spectrometry#metabolomics#spectral-similarity#data-processing

Recommended for

Automated summary

Matchms is an open-source Python library for mass spectrometry data processing, including import/export, filtering, spectral similarity calculations, and pipeline building.

Strengths

  • Multi-format support (mzML, MGF, MSP, JSON) for data import and export
  • Over 40 built-in filters for metadata harmonization and peak cleaning
  • Multiple similarity algorithms (cosine, modified cosine, neutral losses)

Limitations

  • Requires domain knowledge in mass spectrometry to use effectively
  • Purely Python implementation, less performant than compiled alternatives for very large datasets
When to use it

Use Matchms when you need to process, harmonize, and compare mass spectra in a reproducible pipeline, especially for metabolomics.

When not to use it

Avoid Matchms if you need real-time analysis or processing of extremely large datasets without parallelization options.

Security analysis

Safe
Quality score85/100

The skill is purely instructional, providing code examples for the matchms Python library for mass spectrometry data analysis. It does not involve any destructive, exfiltrating, or obfuscated actions. All operations are local data processing and file I/O with no execution of external commands or network calls.

No concerns found

How this analysis is made

Examples

Load and filter spectra from MGF
Load spectra from an MGF file, apply default filters, and export as MSP.
Calculate spectral similarity
Load a library of mass spectra and a query spectrum, then compute modified cosine similarity scores and return the top 10 matches.
Build a processing pipeline
Create a reproducible pipeline that imports mzML files, normalizes intensities, filters by relative intensity, and saves the results as JSON.

name: matchms description: "Mass spectrometry analysis. Process mzML/MGF/MSP, spectral similarity (cosine, modified cosine), metadata harmonization, compound ID, for metabolomics and MS data processing."

Matchms

Overview

Matchms is an open-source Python library for mass spectrometry data processing and analysis. Import spectra from various formats, standardize metadata, filter peaks, calculate spectral similarities, and build reproducible analytical workflows.

Core Capabilities

1. Importing and Exporting Mass Spectrometry Data

Load spectra from multiple file formats and export processed data:

from matchms.importing import load_from_mgf, load_from_mzml, load_from_msp, load_from_json
from matchms.exporting import save_as_mgf, save_as_msp, save_as_json

# Import spectra
spectra = list(load_from_mgf("spectra.mgf"))
spectra = list(load_from_mzml("data.mzML"))
spectra = list(load_from_msp("library.msp"))

# Export processed spectra
save_as_mgf(spectra, "output.mgf")
save_as_json(spectra, "output.json")

Supported formats:

  • mzML and mzXML (raw mass spectrometry formats)
  • MGF (Mascot Generic Format)
  • MSP (spectral library format)
  • JSON (GNPS-compatible)
  • metabolomics-USI references
  • Pickle (Python serialization)

For detailed importing/exporting documentation, consult references/importing_exporting.md.

2. Spectrum Filtering and Processing

Apply comprehensive filters to standardize metadata and refine peak data:

from matchms.filtering import default_filters, normalize_intensities
from matchms.filtering import select_by_relative_intensity, require_minimum_number_of_peaks

# Apply default metadata harmonization filters
spectrum = default_filters(spectrum)

# Normalize peak intensities
spectrum = normalize_intensities(spectrum)

# Filter peaks by relative intensity
spectrum = select_by_relative_intensity(spectrum, intensity_from=0.01, intensity_to=1.0)

# Require minimum peaks
spectrum = require_minimum_number_of_peaks(spectrum, n_required=5)

Filter categories:

  • Metadata processing: Harmonize compound names, derive chemical structures, standardize adducts, correct charges
  • Peak filtering: Normalize intensities, select by m/z or intensity, remove precursor peaks
  • Quality control: Require minimum peaks, validate precursor m/z, ensure metadata completeness
  • Chemical annotation: Add fingerprints, derive InChI/SMILES, repair structural mismatches

Matchms provides 40+ filters. For the complete filter reference, consult references/filtering.md.

3. Calculating Spectral Similarities

Compare spectra using various similarity metrics:

from matchms import calculate_scores
from matchms.similarity import CosineGreedy, ModifiedCosine, CosineHungarian

# Calculate cosine similarity (fast, greedy algorithm)
scores = calculate_scores(references=library_spectra,
                         queries=query_spectra,
                         similarity_function=CosineGreedy())

# Calculate modified cosine (accounts for precursor m/z differences)
scores = calculate_scores(references=library_spectra,
                         queries=query_spectra,
                         similarity_function=ModifiedCosine(tolerance=0.1))

# Get best matches
best_matches = scores.scores_by_query(query_spectra[0], sort=True)[:10]

Available similarity functions:

  • CosineGreedy/CosineHungarian: Peak-based cosine similarity with different matching algorithms
  • ModifiedCosine: Cosine similarity accounting for precursor mass differences
  • NeutralLossesCosine: Similarity based on neutral loss patterns
  • FingerprintSimilarity: Molecular structure similarity using fingerprints
  • MetadataMatch: Compare user-defined metadata fields
  • PrecursorMzMatch/ParentMassMatch: Simple mass-based filtering

For detailed similarity function documentation, consult references/similarity.md.

4. Building Processing Pipelines

Create reproducible, multi-step analysis workflows:

from matchms import SpectrumProcessor
from matchms.filtering import default_filters, normalize_intensities
from matchms.filtering import select_by_relative_intensity, remove_peaks_around_precursor_mz

# Define a processing pipeline
processor = SpectrumProcessor([
    default_filters,
    normalize_intensities,
    lambda s: select_by_relative_intensity(s, intensity_from=0.01),
    lambda s: remove_peaks_around_precursor_mz(s, mz_tolerance=17)
])

# Apply to all spectra
processed_spectra = [processor(s) for s in spectra]

5. Working with Spectrum Objects

The core Spectrum class contains mass spectral data:

from matchms import Spectrum
import numpy as np

# Create a spectrum
mz = np.array([100.0, 150.0, 200.0, 250.0])
intensities = np.array([0.1, 0.5, 0.9, 0.3])
metadata = {"precursor_mz": 250.5, "ionmode": "positive"}

spectrum = Spectrum(mz=mz, intensities=intensities, metadata=metadata)

# Access spectrum properties
print(spectrum.peaks.mz)           # m/z values
print(spectrum.peaks.intensities)  # Intensity values
print(spectrum.get("precursor_mz")) # Metadata field

# Visualize spectra
spectrum.plot()
spectrum.plot_against(reference_spectrum)

6. Metadata Management

Standardize and harmonize spectrum metadata:

# Metadata is automatically harmonized
spectrum.set("Precursor_mz", 250.5)  # Gets harmonized to lowercase key
print(spectrum.get("precursor_mz"))   # Returns 250.5

# Derive chemical information
from matchms.filtering import derive_inchi_from_smiles, derive_inchikey_from_inchi
from matchms.filtering import add_fingerprint

spectrum = derive_inchi_from_smiles(spectrum)
spectrum = derive_inchikey_from_inchi(spectrum)
spectrum = add_fingerprint(spectrum, fingerprint_type="morgan", nbits=2048)

Common Workflows

For typical mass spectrometry analysis workflows, including:

  • Loading and preprocessing spectral libraries
  • Matching unknown spectra against reference libraries
  • Quality filtering and data cleaning
  • Large-scale similarity comparisons
  • Network-based spectral clustering

Consult references/workflows.md for detailed examples.

Installation

uv pip install matchms

For molecular structure processing (SMILES, InChI):

uv pip install matchms[chemistry]

Reference Documentation

Detailed reference documentation is available in the references/ directory:

  • filtering.md - Complete filter function reference with descriptions
  • similarity.md - All similarity metrics and when to use them
  • importing_exporting.md - File format details and I/O operations
  • workflows.md - Common analysis patterns and examples

Load these references as needed for detailed information about specific matchms capabilities.

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/matchms-mass-spectrometry-analysis-mm5t89vx/download' --output '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md.part' && mv '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md.part' '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

Original excerpt: Installation
uv pip install matchms

For molecular structure processing (SMILES, InChI):

uv pip install matchms[chemistry]

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md' && test -s '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/matchms' && cp -R '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx' '.claude/skills/matchms'

Cursor
test -f '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md' && test -s '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/matchms' && cp -R '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx' '.cursor/skills/matchms'

Codex
test -f '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md' && test -s '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/matchms' && cp -R '.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx' '.agents/skills/matchms'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx'

openskills read 'matchms-mass-spectrometry-analysis-mm5t89vx'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/matchms-mass-spectrometry-analysis-mm5t89vx/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills