matchms.similarity.ParentMassMatch module

class matchms.similarity.ParentMassMatch.ParentMassMatch(tolerance: float = 0.1)[source]

Bases: matchms.similarity.BaseSimilarity.BaseSimilarity

Return True if spectrums match in parent mass (within tolerance), and False otherwise.

Example to calculate scores between 2 spectrums and iterate over the scores

import numpy as np
from matchms import calculate_scores
from matchms import Spectrum
from matchms.similarity import ParentMassMatch

spectrum_1 = Spectrum(mz=np.array([]),
                      intensities=np.array([]),
                      metadata={"id": "1", "parent_mass": 100})
spectrum_2 = Spectrum(mz=np.array([]),
                      intensities=np.array([]),
                      metadata={"id": "2", "parent_mass": 110})
spectrum_3 = Spectrum(mz=np.array([]),
                      intensities=np.array([]),
                      metadata={"id": "3", "parent_mass": 103})
spectrum_4 = Spectrum(mz=np.array([]),
                      intensities=np.array([]),
                      metadata={"id": "4", "parent_mass": 111})
references = [spectrum_1, spectrum_2]
queries = [spectrum_3, spectrum_4]

similarity_score = ParentMassMatch(tolerance=5.0)
scores = calculate_scores(references, queries, similarity_score)

for (reference, query, score) in scores:
    print(f"Parentmass match between {reference.get('id')} and {query.get('id')}" +
          f" is {score:.2f}")

Should output

Parentmass match between 1 and 3 is 1.00
Parentmass match between 1 and 4 is 0.00
Parentmass match between 2 and 3 is 0.00
Parentmass match between 2 and 4 is 1.00
__init__(tolerance: float = 0.1)[source]
Parameters

tolerance – Specify tolerance below which two masses are counted as match.

matrix(references: List[Spectrum], queries: List[Spectrum], is_symmetric: bool = False) numpy.ndarray[source]

Compare parent masses between all references and queries.

Parameters
  • references – List/array of reference spectrums.

  • queries – List/array of Single query spectrums.

  • is_symmetric – Set to True when references and queries are identical (as for instance for an all-vs-all comparison). By using the fact that score[i,j] = score[j,i] the calculation will be about 2x faster.

pair(reference: Spectrum, query: Spectrum) float[source]

Compare parent masses between reference and query spectrum.

Parameters
  • reference – Single reference spectrum.

  • query – Single query spectrum.

score_datatype

alias of bool

sort(scores: numpy.ndarray)

Return array of indexes for sorted list of scores. This method can be adapted for different styles of scores.

Parameters

scores – 1D Array of scores.

Returns

Indexes of sorted scores.

Return type

idx_sorted