SkillAgentSearch skills...

Spec2vec

Word2Vec based similarity measure of mass spectrometry data.

Install / Use

/learn @iomega/Spec2vec
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

fair-software.nl <https://fair-software.nl/>_ recommendations:

|GitHub Badge| |License Badge| |Conda Badge| |Pypi Badge| |Research Software Directory Badge| |Zenodo Badge| |CII Best Practices Badge| |Howfairis Badge|

Code quality checks:

|GitHub Workflow Status| |ReadTheDocs Badge| |Sonarcloud Quality Gate Badge| |Sonarcloud Coverage Badge|

################################################################################ spec2vec ################################################################################ Spec2vec is a novel spectral similarity score inspired by a natural language processing algorithm -- Word2Vec. Where Word2Vec learns relationships between words in sentences, spec2vec does so for mass fragments and neutral losses in MS/MS spectra. The spectral similarity score is based on spectral embeddings learnt from the fragmental relationships within a large set of spectral data.

If you use spec2vec for your research, please cite the following references:

Huber F, Ridder L, Verhoeven S, Spaaks JH, Diblen F, Rogers S, van der Hooft JJJ, (2021) "Spec2Vec: Improved mass spectral similarity scoring through learning of structural relationships". PLoS Comput Biol 17(2): e1008724. doi:10.1371/journal.pcbi.1008724 <https://doi.org/10.1371/journal.pcbi.1008724>_

(and if you use matchms as well: F. Huber, S. Verhoeven, C. Meijer, H. Spreeuw, E. M. Villanueva Castilla, C. Geng, J.J.J. van der Hooft, S. Rogers, A. Belloum, F. Diblen, J.H. Spaaks, (2020). "matchms - processing and similarity evaluation of mass spectrometry data". Journal of Open Source Software, 5(52), 2411, https://doi.org/10.21105/joss.02411 )

Thanks!

.. |GitHub Badge| image:: https://img.shields.io/badge/github-repo-000.svg?logo=github&labelColor=gray&color=blue :target: https://github.com/iomega/spec2vec :alt: GitHub Badge

.. |License Badge| image:: https://img.shields.io/github/license/iomega/spec2vec :target: https://github.com/iomega/spec2vec :alt: License Badge

.. |Conda Badge| image:: https://img.shields.io/conda/v/bioconda/spec2vec?color=blue :target: https://bioconda.github.io/recipes/spec2vec/README.html :alt: Conda Badge (Bioconda)

.. |Pypi Badge| image:: https://img.shields.io/pypi/v/spec2vec?color=blue :target: https://pypi.org/project/spec2vec/ :alt: spec2vec on PyPI

.. |Research Software Directory Badge| image:: https://img.shields.io/badge/rsd-spec2vec-00a3e3.svg :target: https://www.research-software.nl/software/spec2vec :alt: Research Software Directory Badge

.. |Zenodo Badge| image:: https://zenodo.org/badge/DOI/10.5281/zenodo.3873169.svg :target: https://doi.org/10.5281/zenodo.3873169 :alt: Zenodo Badge

.. |CII Best Practices Badge| image:: https://bestpractices.coreinfrastructure.org/projects/3967/badge :target: https://bestpractices.coreinfrastructure.org/projects/3967 :alt: CII Best Practices Badge

.. |Howfairis Badge| image:: https://img.shields.io/badge/fair--software.eu-%E2%97%8F%20%20%E2%97%8F%20%20%E2%97%8F%20%20%E2%97%8F%20%20%E2%97%8F-green :target: https://fair-software.eu :alt: Howfairis Badge

.. |ReadTheDocs Badge| image:: https://readthedocs.org/projects/spec2vec/badge/?version=latest :alt: Documentation Status :target: https://spec2vec.readthedocs.io/en/latest/?badge=latest

.. |Sonarcloud Quality Gate Badge| image:: https://sonarcloud.io/api/project_badges/measure?project=iomega_spec2vec&metric=alert_status :target: https://sonarcloud.io/dashboard?id=iomega_spec2vec :alt: Sonarcloud Quality Gate

.. |Sonarcloud Coverage Badge| image:: https://sonarcloud.io/api/project_badges/measure?project=iomega_spec2vec&metric=coverage :target: https://sonarcloud.io/component_measures?id=iomega_spec2vec&metric=Coverage&view=list :alt: Sonarcloud Coverage

.. |GitHub Workflow Status| image:: https://img.shields.io/github/actions/workflow/status/matchms/spec2vec/CI_build.yml?branch=master :target: https://img.shields.io/github/workflow/status/iomega/spec2vec/CI%20Build :alt: GitHub Workflow Status


Documentation for users


For more extensive documentation see our readthedocs <https://spec2vec.readthedocs.io/en/latest/>_ or get started with our spec2vec introduction tutorial <https://blog.esciencecenter.nl/build-a-mass-spectrometry-analysis-pipeline-in-python-using-matchms-part-ii-spec2vec-8aa639571018>_.

Versions

Since version 0.9.0 Spec2Vec uses gensim >= 4.4.0 which should make it faster and more future proof. Model trained with older versions should still be importable without any issues. If you had scripts that used additional gensim code, however, those might occationally need some adaptation, see also the gensim documentation on how to migrate your code <https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4>_.

Installation

Prerequisites:

  • Python 3.10, 3.11, 3.12 or 3.13
  • Recommended: Anaconda

We recommend installing spec2vec from Anaconda Cloud with

.. code-block:: console

conda create --name spec2vec python=3.13 conda activate spec2vec conda install --channel bioconda --channel conda-forge spec2vec

Alternatively, spec2vec can also be installed using pip. When using spec2vec together with matchms it is important to note that only the Anaconda install will make sure that also rdkit is installed properly, which is requried for a few matchms filter functions (it is not required for any spec2vec related functionalities though).

.. code-block:: console

pip install spec2vec

Examples

Below a code example of how to process a large data set of reference spectra to train a word2vec model from scratch. Spectra are converted to documents using SpectrumDocument which converts spectrum peaks into "words" according to their m/z ratio (for instance "peak@100.39"). A new word2vec model can then trained using train_new_word2vec_model which will set the training parameters to spec2vec defaults unless specified otherwise. Word2Vec models learn from co-occurences of peaks ("words") across many different spectra. To get a model that can give a meaningful representation of a set of given spectra it is desirable to train the model on a large and representative dataset.

.. code-block:: python

from matchms import SpectrumProcessor
from matchms.filtering.default_pipelines import DEFAULT_FILTERS
from matchms.importing import load_from_mgf
from spec2vec import SpectrumDocument
from spec2vec.model_building import train_new_word2vec_model

# Load spectra from MGF
spectra = list(load_from_mgf("reference_spectrums.mgf"))

# Add some default filters. You can add more filters functions like require min. number of peaks
processor = SpectrumProcessor(DEFAULT_FILTERS)

# Apply filter pipeline
spectra_cleaned, _ = processor.process_spectra(spectra)
spectra_cleaned = [s for s in spectra_cleaned if s is not None]

# Create spectrum documents
reference_documents = [SpectrumDocument(s, n_decimals=2) for s in spectra_cleaned]

# Train your reference model
model_file = "references.model"
model = train_new_word2vec_model(reference_documents, iterations=[10, 20, 30], filename=model_file,
                                 workers=2, progress_logger=True)

Once a word2vec model has been trained, spec2vec allows to calculate the similarities between mass spectra based on this model. In cases where the word2vec model was trained on data different than the data it is applied for, a number of peaks ("words") might be unknown to the model (if they weren't part of the training dataset). To account for those cases it is important to specify the allowed_missing_percentage, as in the example below.

.. code-block:: python

import gensim
from matchms import calculate_scores
from spec2vec import Spec2Vec

# query_spectra loaded from files using https://matchms.readthedocs.io/en/latest/api/matchms.importing.load_from_mgf.html
query_spectra = list(load_from_mgf("query_spectrums.mgf"))
query_spectra_cleaned, _ = processor.process_spectra(query_spectra)

# Omit spectra that didn't qualify for analysis
query_spectra_cleaned = [s for s in query_spectra_cleaned if s is not None]

# Import pre-trained word2vec model (see code example above)
model_file = "references.model"
model = gensim.models.Word2Vec.load(model_file)

# Define similarity_function
spec2vec_similarity = Spec2Vec(model=model, intensity_weighting_power=0.5,
                               allowed_missing_percentage=5.0)

# Calculate scores on all combinations of reference spectra and queries
scores = calculate_scores(reference_documents, query_spectra_cleaned, spec2vec_similarity)

# Find the highest scores for a query spectrum of interest
best_matches = scores.scores_by_query(query_spectra_cleaned[0], sort=True)[:10]

# Return highest scores
print([x[1] for x in best_matches])

Glossary of terms

.. list-table:: :header-rows: 1

    • Term
    • Description
    • adduct / addition product
    • During ionization in a mass spectrometer, the molecules of the injected compound break apart into fragments. When fragments combine into a new compound, this is known as an addition product, or adduct. Wikipedia <https://en.wikipedia.org/wiki/Adduct>__
    • GNPS
    • Knowledge base for sharing of mass spectrometry data (link <https://gnps.ucsd.edu/ProteoSAFe/static/gnps-splash.jsp>__).
    • InChI / :code:INCHI
    • InChI is short for International Chemical Identifier. InChIs are useful in retrieving information associated with a certain molecule from a database.
    • InChIKey / InChI key / :code:INCHIKEY
    • An indentifier for molecules. For example, the InChI key for carbon dioxide is :code:`InChIKey=CURLTUGMZLYL

Related Skills

View on GitHub
GitHub Stars80
CategoryProduct
Updated5d ago
Forks20

Languages

Python

Security Score

100/100

Audited on Mar 21, 2026

No findings