$npx -y skills add LeonChaoX/qinyan-academic-skills --skill matchmsSpectral similarity and compound identification for metabolomics. Use for comparing mass spectra, computing similarity scores (cosine, modified cosine), and identifying unknown compounds from spectral libraries. Best for metabolite identification, spectral matching, library searc
| 1 | # Matchms |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | 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. |
| 6 | |
| 7 | ## Core Capabilities |
| 8 | |
| 9 | ### 1. Importing and Exporting Mass Spectrometry Data |
| 10 | |
| 11 | Load spectra from multiple file formats and export processed data: |
| 12 | |
| 13 | ```python |
| 14 | from matchms.importing import load_from_mgf, load_from_mzml, load_from_msp, load_from_json |
| 15 | from matchms.exporting import save_as_mgf, save_as_msp, save_as_json |
| 16 | |
| 17 | # Import spectra |
| 18 | spectra = list(load_from_mgf("spectra.mgf")) |
| 19 | spectra = list(load_from_mzml("data.mzML")) |
| 20 | spectra = list(load_from_msp("library.msp")) |
| 21 | |
| 22 | # Export processed spectra |
| 23 | save_as_mgf(spectra, "output.mgf") |
| 24 | save_as_json(spectra, "output.json") |
| 25 | ``` |
| 26 | |
| 27 | **Supported formats:** |
| 28 | - mzML and mzXML (raw mass spectrometry formats) |
| 29 | - MGF (Mascot Generic Format) |
| 30 | - MSP (spectral library format) |
| 31 | - JSON (GNPS-compatible) |
| 32 | - metabolomics-USI references |
| 33 | - Pickle (Python serialization) |
| 34 | |
| 35 | For detailed importing/exporting documentation, consult `references/importing_exporting.md`. |
| 36 | |
| 37 | ### 2. Spectrum Filtering and Processing |
| 38 | |
| 39 | Apply comprehensive filters to standardize metadata and refine peak data: |
| 40 | |
| 41 | ```python |
| 42 | from matchms.filtering import default_filters, normalize_intensities |
| 43 | from matchms.filtering import select_by_relative_intensity, require_minimum_number_of_peaks |
| 44 | |
| 45 | # Apply default metadata harmonization filters |
| 46 | spectrum = default_filters(spectrum) |
| 47 | |
| 48 | # Normalize peak intensities |
| 49 | spectrum = normalize_intensities(spectrum) |
| 50 | |
| 51 | # Filter peaks by relative intensity |
| 52 | spectrum = select_by_relative_intensity(spectrum, intensity_from=0.01, intensity_to=1.0) |
| 53 | |
| 54 | # Require minimum peaks |
| 55 | spectrum = require_minimum_number_of_peaks(spectrum, n_required=5) |
| 56 | ``` |
| 57 | |
| 58 | **Filter categories:** |
| 59 | - **Metadata processing**: Harmonize compound names, derive chemical structures, standardize adducts, correct charges |
| 60 | - **Peak filtering**: Normalize intensities, select by m/z or intensity, remove precursor peaks |
| 61 | - **Quality control**: Require minimum peaks, validate precursor m/z, ensure metadata completeness |
| 62 | - **Chemical annotation**: Add fingerprints, derive InChI/SMILES, repair structural mismatches |
| 63 | |
| 64 | Matchms provides 40+ filters. For the complete filter reference, consult `references/filtering.md`. |
| 65 | |
| 66 | ### 3. Calculating Spectral Similarities |
| 67 | |
| 68 | Compare spectra using various similarity metrics: |
| 69 | |
| 70 | ```python |
| 71 | from matchms import calculate_scores |
| 72 | from matchms.similarity import CosineGreedy, ModifiedCosine, CosineHungarian |
| 73 | |
| 74 | # Calculate cosine similarity (fast, greedy algorithm) |
| 75 | scores = calculate_scores(references=library_spectra, |
| 76 | queries=query_spectra, |
| 77 | similarity_function=CosineGreedy()) |
| 78 | |
| 79 | # Calculate modified cosine (accounts for precursor m/z differences) |
| 80 | scores = calculate_scores(references=library_spectra, |
| 81 | queries=query_spectra, |
| 82 | similarity_function=ModifiedCosine(tolerance=0.1)) |
| 83 | |
| 84 | # Get best matches |
| 85 | best_matches = scores.scores_by_query(query_spectra[0], sort=True)[:10] |
| 86 | ``` |
| 87 | |
| 88 | **Available similarity functions:** |
| 89 | - **CosineGreedy/CosineHungarian**: Peak-based cosine similarity with different matching algorithms |
| 90 | - **ModifiedCosine**: Cosine similarity accounting for precursor mass differences |
| 91 | - **NeutralLossesCosine**: Similarity based on neutral loss patterns |
| 92 | - **FingerprintSimilarity**: Molecular structure similarity using fingerprints |
| 93 | - **MetadataMatch**: Compare user-defined metadata fields |
| 94 | - **PrecursorMzMatch/ParentMassMatch**: Simple mass-based filtering |
| 95 | |
| 96 | For detailed similarity function documentation, consult `references/similarity.md`. |
| 97 | |
| 98 | ### 4. Building Processing Pipelines |
| 99 | |
| 100 | Create reproducible, multi-step analysis workflows: |
| 101 | |
| 102 | ```python |
| 103 | from matchms import SpectrumProcessor |
| 104 | from matchms.filtering import default_filters, normalize_intensities |
| 105 | from matchms.filtering import select_by_relative_intensity, remove_peaks_around_precursor_mz |
| 106 | |
| 107 | # Define a processing pipeline |
| 108 | processor = SpectrumProcessor([ |
| 109 | default_filters, |
| 110 | normalize_intensities, |
| 111 | lambda s: select_by_relative_intensity(s, intensity_from=0.01), |
| 112 | lambda s: remove_peaks_around_precursor_mz(s, mz_tolerance=17) |
| 113 | ]) |
| 114 | |
| 115 | # Apply to all spectra |
| 116 | processed_spectra = [processor(s) for s in spectra] |
| 117 | ``` |
| 118 | |
| 119 | ### 5. Working with Spectrum Objects |
| 120 | |
| 121 | The core `Spectrum` class contains mass spectral data: |
| 122 | |
| 123 | ```python |
| 124 | from matchms import Spectrum |
| 125 | import numpy as np |
| 126 | |
| 127 | # Create a spectrum |
| 128 | mz = np.array([100.0, 150.0, 200 |