Mass spectrum binning and vectorization
Use when when preparing MS/MS spectra for neural network training or inference, particularly when you need to feed variable-length spectra into a Siamese network or embedding model that requires fixed-dimensional input.From its SKILL.md
npx -y skills add HolobiomicsLab/asb-skill-collections --skill mass-spectrum-binning-and-vectorizationAssembled 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
7.6 KB, ~1.1k tokens by cl100k_base, as published. Nobody here has run it
mass-spectrum-binning-and-vectorization
Summary
Convert raw tandem mass spectra into fixed-size numerical vectors by binning peaks into equally-sized m/z intervals and applying intensity transformations. This standardizes spectral representation for machine learning while preserving chemical information relevant to structural similarity prediction.
When to use
When preparing MS/MS spectra for neural network training or inference, particularly when you need to feed variable-length spectra into a Siamese network or embedding model that requires fixed-dimensional input. Essential when converting from heterogeneous spectrum formats (varying number of peaks, different m/z ranges) into a uniform numerical representation suitable for deep learning.
When NOT to use
- When spectra require retention of exact peak positions or high-resolution m/z precision (binning loses sub-bin localization)
- If input spectra are already in fragment ion fingerprint or molecular descriptor format
- When the mass range of interest falls outside 10–1000 m/z (e.g., very high m/z or low m/z specialized analyses)
Inputs
- MS/MS spectra (matchms Spectrum objects or MGF/MSP files)
- Cleaned spectrum metadata including peak lists with m/z and intensity values
- Training dataset specification (to identify bins to retain)
Outputs
- Fixed-size spectral vectors (1D numpy arrays, 10,000 or ~9,948 dimensions)
- Binned spectrum peak intensity arrays with square-root transformation applied
- Bin index mapping (10–1000 m/z → bin indices)
How to apply
Bin spectrum peaks into 10,000 equally-sized bins spanning the 10–1000 m/z mass range, selecting the maximum intensity when multiple peaks fall in the same bin. Apply square root transformation to peak intensities to avoid overweighting high-intensity peaks. Remove bins not represented in the training dataset (typically reducing from 10,000 to ~9,948 retained bins). The resulting spectrum is represented as a vector of bin intensities. Use this vectorized representation directly as input to neural network models without computing fingerprints or metadata features (parent mass, formula) separately, as the model learns to extract structural information directly from the binned intensities.
Related tools
- matchms (Load, clean, and standardize spectrum metadata and peak lists prior to binning; provide Spectrum objects as input to vectorization) — https://github.com/matchms/matchms
- RDKit (Compute molecular fingerprints and Tanimoto similarity scores as reference labels for training; not used in the binning itself but used post-vectorization for label generation)
- Python (NumPy/SciPy) (Perform binning operations, intensity transformations, and vector construction)
Examples
from matchms.importing_utils import load_from_mgf; from matchms.Pipeline import Pipeline, create_workflow; from matchms.filtering.default_pipelines import DEFAULT_FILTERS; pipeline = Pipeline(create_workflow(query_filters=DEFAULT_FILTERS)); spectra = list(load_from_mgf('spectra.mgf')); cleaned = [pipeline.apply_filters(s) for s in spectra]; import numpy as np; vectorized = np.array([spectrum_to_binned_vector(s, bins=10000, mz_min=10, mz_max=1000) for s in cleaned])
Evaluation signals
- Output vectors are exactly 10,000 (or 9,948 if bins not in training data are removed) dimensions with no NaN or infinite values
- Peak intensities fall in [0, 1] range after square-root transformation, with no negative values
- For a given spectrum, the number of non-zero bins matches the count of unique m/z regions containing peaks
- Spectra with identical peak content and intensities produce identical vectorized outputs (deterministic)
- Downstream neural network accepts the vectorized spectra without shape mismatch errors and trains without NaN loss
Limitations
- Binning resolution of 0.09 m/z per bin (1000 m/z / 10,000 bins) may lose high-resolution structural detail in crowded spectral regions; not suitable for resolving isotope fine structure
- Square root transformation compresses high-intensity peaks; may underrepresent dominant diagnostic ions
- Fixed m/z range (10–1000 Da) excludes very low m/z fragments and high-mass neutral losses; unsuitable for specialized applications requiring extended mass range
- Loss of peak metadata such as annotation strings or scan provenance; vectorization is lossy with respect to original spectrum objects
Evidence
- [methods] Spectrum peaks were binned in 10,000 equally-sized bins ranging from 10 to 1000 m/z: "Spectrum peaks were binned in 10,000 equally-sized bins ranging from 10 to 1000 m/z"
- [methods] Peak intensities were square root transformed to avoid a too strong focus on the highest intensity peaks only: "Peak intensities were square root transformed to avoid a too strong focus on the highest intensity peaks only"
- [other] Remove bins not represented in training data (9948 of 10,000 bins retained) and output cleaned spectral vectors and metadata: "Remove bins not represented in training data (9948 of 10,000 bins retained) and output cleaned spectral vectors and metadata"
- [results] the neural network was not trained on any spectrum metadata such as parent mass and elemental formula: "the neural network was not trained on any spectrum metadata such as parent mass and elemental formula"
- [other] selecting the maximum intensity when multiple peaks occupy one bin: "selecting maximum intensity per bin when multiple peaks occupy one bin"
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.