Improved method for increased speed cosine calculations

A GPU-accelerated method for cosine similarity calculations in tandem mass spectrometry addresses inefficiencies in existing technologies by processing large datasets up to 1,000 times faster, maintaining accuracy and supporting broader tolerance ranges.

GB2642734APending Publication Date: 2026-01-21PANGEA BOTANICA LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
GB2024010551
Authority / Receiving Office
GB · GB
Patent Type
Applications
Current Assignee / Owner
Filing Date
2024-07-19
Publication Date
2026-01-21

AI Technical Summary

Technical Problem

Existing methods for calculating cosine similarity in tandem mass spectrometry, such as MatchMS, are inefficient and require excessive processing time for large datasets, making them impractical for routine untargeted metabolomics workflows.

Method used

A GPU-accelerated method utilizing rewritten greedy and modified cosine algorithms, implemented as a single optimized GPU kernel, processes cosine similarity calculations up to 1,000 times faster by splitting data into batches, allocating blocks to GPUs, and performing sparse matrix operations.

Benefits of technology

The method achieves exact cosine scores up to 1,000 times faster than MatchMS, supporting a wider tolerance range without compromising accuracy, significantly reducing processing time from weeks to hours for large datasets.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure 00000000_0000_ABST
    Figure 00000000_0000_ABST
Patent Text Reader

Abstract

The present invention relates to a method of increasing the speed of identifying molecular species represented in mass spectrometry data, particularly for small molecules such as metabolites. The met
Need to check novelty before this filing date? Find Prior Art

Description

FIELD OF THE INVENTION

[0001] The present invention relates to an improved method of increasing the speed of the cosine calculations.

[0002] Preferably the improved methodology comprises a GPU-accelerated cosine similarity implementation for Tandem Mass Spectrometry (MS / MS) with approximately 1000-fold speedup compared to the MatchMS reference at a rate of 0.005% incorrect matches and a rate of 0.002% incorrect scores. BACKGROUND TO THE INVENTION

[0003] In the field of untargeted metabolomics, Tandem Mass Spectrometry (MS / MS) is a well-established technique for identifying compounds within complex biological samples. The process works by comparing an unknown compound’s mass spectrum fragmentation pattern (“query”) against a database of known spectra (“reference”) in an effort to identify the unknown compound’s chemical composition.

[0004] Cosine similarity and its variants are popular methods facilitating MS / MS spectra comparison. Cosine similarity works by calculating a cosine of the angle between vectors of fragmentation intensities (“peaks”). Since an exact match between two fragmentation spectra isn’t practically possible due to measurement errors, finding the cosine score involves solving an assignment problem between the two sets of spectral peaks, with the goal of finding a valid matching of peaks (within m / z tolerance) that maximize the value of the cosine. Calculating the exact cosine similarity (Hungarian cosine) is usually impractical for even moderate numbers of spectra. “Greedy” cosine similarity is an efficient approximation of the cosine similarity, which solves the assignment problem using a greedy heuristic. “Modified” cosine similarity is an extension of greedy cosine that uses the precursor mass as an additional input and has been shown to outperform greedy cosine in some cases.

[0005] Huber etal. also introduced MatchMS, an open-source MS / MS processing library in python. This library allows implementing easy-to-reproduce workflows to process raw mass spectral data into more useful forms (i.e. molecular networks). MatchMS conveniently implements all three types of cosine similarities. Unfortunately, while the MatchMS implementation is convenient, it is too slow for processing spectra on the scale of ten pairwise comparisons or more. At such scales, which are routine in untargeted metabolomics workflows, MatchMS requires tens of CPU-days, necessitating a search for more efficient and robust approaches.

[0006] To address this issue, Harwood etal. introduced BLINK. This process approximates the greedy cosine by first blurring the query spectral peaks, transforming it into a sparse matrix, and then performing sparse matrix multiplication between the query matrix and the reference matrix to directly compute the score matrix. This effectively side steps solving the peak assignment problem, allowing BLINK significant speed improvements over MatchMS. Unfortunately, BLINK rapidly loses accuracy when tolerance is larger than ten.

[0007] The present application describes an improved method of increasing the speed of cosine calculations. The improved cosine calculation includes rewritten greedy and modified cosine algorithms using CUDA such that a single, highly optimized GPU kernel is able to process spectra up to 1,000 times faster than MatchMS. The application presents data to demonstrate that the methodology is able to exactly replicate MatchMS results with a much wider tolerance range without compromising accuracy. BRIEF SUMMARY OF THE DISCLOSURE

[0008] In accordance with a first aspect of the present invention there is provided a method of increasing the speed of identifying one or more molecular species represented in mass spectrometry data, the method comprising: a) Providing a set of reference (R) and a set of query (Q) mass spectrometry data and grouping the data into batches (B); b) Concatenate each batch into a single tensor and split the RxQ grid into BxB blocks; c) Allocate each block an output tensor and transfer to at least one GPU; d) Collect pairs of peaks and sort by the value of the product of the peak intensities; e) Produce the unnormalized cosine score by deduplicating and accumulating the ordered pairs; and f) Produce the final cosine score by multiplying the two norms from metadata and dividing by the unnormalized cosine score.

[0009] Preferably the one or more molecular species is a small molecule. More preferably the one or more molecular species is a metabolite.

[0010] Preferably the mass spectrometry data is tandem mass spectrometry or MS / MS data.

[0011] Preferably the GPU is any one of an AMD, Nvidia, or Intel graphics processing unit. More preferably the GPU has a speed of at least 56 TFLOPS. More preferably the GPU has at least 16GB RAM.

[0012] Preferably the final cosine score achieved is an exact score not an approximation.

[0013] In a further embodiment of the invention there is provided a system arranged to carry out a method as described above.

[0014] In a further embodiment of the invention there is provided a computer program which when executed by a processor causes the processor to carry out the method described above. Further there is provided a computer-readable medium storing the computer program. BRIEF SUMMARY OF THE DRAWINGS

[0015] The present invention is described with reference to the figures listed below:

[0016] Figure 1 denotes an overview of the processing on a GPU. In a first step (A), all spectra and metadata were packed in 3D and 2D tensors, respectively. In a second step (B), the spectra were aligned with the GPU grid. Steps (C) and (D) happened within a single GPU thread. In (C), potential matching peaks upto the given match limit were accumulated. In the last step (D), matched peaks were sorted, deduplicated and reduced, returning three values.

[0017] Figure 2 details cosine scoring runtimes for GNPS subsets with default parameters, with different GPUs and methods. The red line shows the SimMS performance on A100 for modified cosine scoring, and the brown line is the same calculation with Match MS. Marker dots (A) represent real measurements, and dashed lines (B) are their linear interpolations. (C) is a marker of 1.5x1011 comparisons and represents target processing goals.

[0018] Figure 3: Direct comparison of scores of 4.1 M random GNPS spectra pairs, illustrating SimMS error patterns. Red dots denote incorrect (absolute error >0.001) SimMS outputs. (A) and (B) show greedy cosine results between SimMS and MatchMS, performed at float32 precision. In (C) and (D) modified cosine results atfloat64 precision are compared. Occasional large errors in (A) and (C) were caused by spectra with very few (one or two) large peaks.

[0019] Figure 4: Key metrics as a function of changing tolerance (A) - (C) and match limit (D) -(F). In a and d speed means the number of comparisons per second. “Overflow” is the proportion of scores that are returned with the overflow flag. Accuracy is the proportion of scores that are within ±0.001 of MatchMS. DEFINITIONS

[0020] Various definitions are made throughout this document. Most words have the meaning that would be attributed to those words by one skilled in the art. Words specifically defined either below or elsewhere in this document have the meaning provided in the context of the present invention as a whole and as typically understood by those skilled in the art.

[0021] “Cosine similarity” or “Cosine score” is defined as a measure of similarity between two non-zero vectors defined in an inner product space. Cosine similarity is the cosine of the angle between the vectors; it is the dot product of the vectors divided by the product of their lengths. The technique is used to measure cohesion within clusters in the field of data mining.

[0022] A “greedy algorithm” or “greedy search” is a simple, intuitive algorithm that is used in optimization problems. The algorithm makes the optimal choice at each step as it attempts to find the overall optimal way to solve the entire problem.

[0023] A “modified cosine” score aims to quantify the similarity between two mass spectra. The score is calculated by finding best possible matches between peaks of two spectra.

[0024] “Tandem mass spectrometry”, also known as “MS / MS”, is a two-step technique used to analyse a sample either by using two or more mass spectrometers connected to each other or a single mass spectrometer by several analysers arranged one after another.

[0025] “Metabolomics” is the large-scale study of metabolites, the small molecule substrates, intermediates, and products of cell metabolism. Specifically, metabolomics is the "systematic study of the unique chemical fingerprints that specific cellular processes leave behind", the study of their small-molecule metabolite profiles.

[0026] “MatchMS” is an open-source Python package to import, process, clean, and compare mass spectrometry data (MS / MS). It implements and runs a workflow from raw mass spectra to pre- and post-processed spectral data. Spectral data can be imported from common formats such mzML, mzXML, msp, metabolomics-USI, MGF, orjson (e.g. GNPS-style json files). MatchMS then provides filters for metadata cleaning and checking, as well as for basic peak filtering. MatchMS can apply different similarity measures to compare large amounts of spectra however this results in long runtimes. This includes common Cosine scores.

[0027] “Global Natural Products Social Molecular Networking” or “GNPS” is a web-based mass spectrometry ecosystem that aims to be an open-access knowledge base for communitywide organization and sharing of raw, processed or identified tandem mass (MS / MS) spectrometry data. DETAILED DESCRIPTION OF THE INVENTION EXAMPLE 1: ACCELERATED COSINE SIMILARITY INDENTIFICATION METHODOLOGY FOR TANDEM MASS SPECTROMETRY

[0028] The use of packages such as MatchMS to compare and identify small molecules from tandem mass spectrometry data and produce cosine score is inefficient for large data sets given the long runtimes associated with the package.

[0029] As such there is a need in the art for a methodology to accelerate the rate in which cosine similarity can achieved to enable efficient and fast processing of vast datasets.

[0030] The present example provides a method of increasing the speed of identifying one or more molecular species represented in mass spectrometry data. Methods

[0031] A list of references and queries were used as an input, denoting their respective lengths as R and Q. For both lists, consecutive spectra were grouped into batches of size B, the last batch contains leftover spectra and padding, as needed.

[0032] Inside each batch, all spectra were concatenated into a single R2 B M tensor, where M is the number of peaks in the longest spectrum in that batch, B is the batch size (Figure 1A). A batch contained stacked peak m / z and intensity values in the first dimension. Spectra that were shorter than M were padded with zeros. Spectra that were larger than M were truncated with an argument N max peaks-

[0033] The “metadata” tensor was created alongside the spectra batch. The metadata tensor contained the length, cosine norm, and, in case of modified cosine, the precursor m / z values. Metadata tensor is a RKB tensor, where K is either 4 or 6, and B is the batch size. For greedy cosine, dimension K was 4 and consisted of lengths of reference and query spectra and norms of reference and query spectra. In the case of modified cosine metadata additionally contained precursor m / z values for reference and query spectra. For batches corresponding to leftover spectra, the resulting empty space was padded with zeros.

[0034] The full similarity matrix of size RxQ was infeasible to store in GPU memory. Therefore, processing was done block-by-block. The full R*Q grid was split into B*B sized, nonoverlapping blocks. In total this resulted in ceil(R / B) x ceil(Q / B) number of blocks. For each block an output R3B B tensor was allocated on the GPU. A pair of batches of spectra, and their respective metadata tensors were transferred to the GPU. The tolerance, m / z power, intensity power, N match limit and N max peaks were supplied to the kernel as compilation-time constants. At this point the kernel was launched.

[0035] The kernel was written using Numba. Inside the kernel, a single CUDA thread was assigned to calculate the cosine score between one reference spectrum and one query spectrum from the supplied batches. The computing power of SimMS stemmed from the fact that modern GPUs can process tens of thousands of threads simultaneously, allowing each block to be processed in a fraction of a second.

[0036] The kernel itself consisted of three main stages (Figures 1Bto 1D). First, pairs of peaks within tolerance were collected. A maximum of N match limit pairs were collected. If the number of pairs exceeded this limit, an overflow flag was raised, and the collection was stopped early. Next, the pairs were sorted by the value of the product of the intensities of paired peaks. Finally, the ordered pairs were deduplicated and accumulated into an unnormalized cosine score. An auxiliary boolean array was used to mark each accumulated peak, to avoid duplicate contributions to the score. As a final step, the two norms from metadata were multiplied to obtain the normalizing constant, which divided the unnormalized score to produce the final cosine score.

[0037] After the execution, the block output tensor contained three results for every reference / query pair in the batch. These were: score; number of matches; and overflow (binary flag). Each block was then concatenated together in order to form the full R*Q similarity matrix. When processing a very large set of references and queries (larger than 100,000), the required memory to store the full similarity matrix as a dense array would be impractically large. Additionally, we found that most of the scores were lower than 0.5. For such cases we used “sparse” implementation, where the similarity matrix was filtered to discard all results with scores below a user-defined “sparse threshold” and then stored the remaining entries as a sparse matrix in DOK format.

[0038] Finally, we concatenated all the results and returned them, either as a dense array or as a sparse DOK matrix. Hardware

[0039] All kernel and MatchMS experiments were performed on Vastai (Vast.ai) instances. Preferably, with at least 16GB RAM, 8 or more CPUs, and at least a single RTX4090 GPU. Results

[0040] Figure 2 details cosine scoring runtimes for GNPS subsets with default parameters, with different GPUs and methods.

[0041] We found that using the Cosine kernel implementation presented in this example with a single NVIDIA A100 GPU for calculations was approximately 1,000x faster than using MatchMS for both greedy and modified cosine similarities, as shown in Figure 2. Calculating greedy cosine similarity at the scale of 100,000 queries paired with 1.5 million reference spectra would take an estimated 13 weeks with MatchMS, in contrast to only 3 hours using the kernel.

[0042] Modified cosine similarity was slower than greedy cosine (also shown in Figure 2), independent of the hardware used, since the steps required to calculate the modified cosine score included all steps required for calculating the greedy cosine score.

[0043] A direct comparison of results for predicted scores and the number of matches was undertaken and shown in Figure 3. It was found that for default parameters, the kernel and MatchMS results are within ±0.001 of each other 99.99% of the time. Since the kernel was algorithmically equivalent to its MatchMS counterpart, the few errors stem from peaks that were almost exactly tolerance distance apart. In other words, when paired with MatchMS, these peaks appeared to be inside tolerance distance, but processing them with a GPU changes their binary floating point representation, which was enough to make them appear outside of the tolerance distance. When a spectrum has a single very intense peak, such binary representation changes can result in large score errors. The score comparison plots in panels Figure 3A and Figure 3C show these kinds of errors in the bottom right.

[0044] The rate of incorrect scores was usually lower than the rate of incorrect number of matches. This asymmetry originated from the filtering step. Filtering usually removed most of the low-intensity matches from the score calculation, thus making the missing or extra match unlikely to affect the score value.

[0045] In Figure 4 the changing tolerance and match limit influence SimMS performance is shown. In Figure 4A and 4D it is shown that lowering these values significantly speeded up the spectra processing time, however this increased the overflow rate, as seen in Figure 4B and 4E. The accuracy rate in Figure 4G and 4F is approximately equal to the 1 - overflow rate. Conclusion

[0046] The example described above provides a method of increasing the speed of calculating cosine similarity. The cosine calculation included rewritten greedy and modified cosine algorithms using CUDA into a single, highly optimized GPU kernel that, depending on underlying GPU hardware, can process spectra up to 1,000 times faster than MatchMS.

[0047] The approach exactly replicated MatchMS results and importantly supports a much wider tolerance range without compromising accuracy.

[0048] Figure 2 demonstrates that using a target processing goal of 1.5x1011 comparisons the use of the MatchMS package with a greedy cosine search would take approximately 13 weeks to process. Conversely, using the methodology presented in the example, with a GPU and a greedy cosine search, to make the same number of comparisons would take 3 hours.

[0049] Clearly the methodology presented represents a vast improvement on the standard methodology.

Claims

1. A method of increasing the speed of identifying one or more molecular species represented in mass spectrometry data, the method comprising:a) Providing a set of reference (R) and a set of query (Q) mass spectrometry data and grouping the data into batches (B);b) Concatenate each batch into a single tensor and split the RxQ grid into BxB blocks;c) Allocate each block an output tensor and transfer to at least one GPU;d) Collect pairs of peaks and sort by the value of the product of the peak intensities;e) Produce the unnormalized cosine score by deduplicating and accumulating the ordered pairs; andf) Produce the final cosine score by multiplying the two norms from metadata and dividing by the unnormalized cosine score.

2. The method of claim 1, wherein the one or more molecular species is a small molecule.

3. The method of claim 1 or 2, wherein the one or more molecular species is a metabolite.

4. The method of claim 1, wherein the mass spectrometry data is tandem mass spectrometry or MS / MS data.

5. The method of claim 1, wherein the GPU is any one of an AMD, Nvidia, Intel graphics processing unit.

6. The method of claim 5, wherein the GPU has a speed of at least 56 TFLOPS.

7. The method of claim 5 or claim 6, wherein the GPU has at least 16GB RAM.

8. The method of any of the preceding claims, wherein the final cosine score achieved is an exact score not an approximation.

9. A system arranged to carry out a method according to any one of the preceding claims.

10. A computer program which when executed by a processor causes the processor to carry out the method according to any one of claims 1 to 8.

11. A computer-readable medium storing a computer program according to claim 10.