Prefix-Tree Decoding for Predicting Mass Spectra from Molecules

Samuel Goldman, John Bradshaw, Jiayi Xin, Connor W. Coley

Introduction

The identification of unknown small molecules, or metabolites, is a fundamental challenge in fields ranging from clinical diagnostics to drug discovery. Tandem mass spectrometry (MS/MS) serves as the primary analytical technique for this task, providing a "chemical fingerprint" by fragmenting molecules and measuring the resulting mass-to-charge ratios ($m/z$). However, the interpretation of these spectra often requires comparing experimental data against reference libraries. When physical libraries are unavailable, researchers rely on "forward" simulators to predict spectra from chemical structures.

Existing simulators generally fall into two categories: rule-based fragmentation methods and binned vector-based deep learning models. Rule-based methods, such as CFM-ID, are chemically interpretable but computationally expensive and often fail to account for complex rearrangements. Conversely, binned methods are fast but often produce physically impossible results, as they lack an inherent understanding of the precursor molecule's elemental composition. This paper introduces SCARF (Subformulae Classification for Autoregressively Reconstructing Fragmentations), a framework that bridges this gap by treating mass spectra as sets of molecular formulae decoded through a prefix-tree structure.

The SCARF Framework

SCARF departs from traditional binned representations by predicting a set of molecular formulae rather than a discretized vector of intensities. The architecture is divided into two specialized components: SCARF-Thread and SCARF-Weave.

SCARF-Thread: Prefix-Tree Formula Prediction

The primary challenge in predicting molecular formulae is the sheer size of the chemical search space. To manage this, SCARF-Thread employs a prefix-tree decoding strategy. In this approach, a chemical formula is treated as an ordered multiset of atoms (e.g., Carbon, then Oxygen, then Nitrogen, etc.).

The model traverses a tree where each level corresponds to a specific element. At each node, the model performs multi-label binary classification to determine which atom counts are valid continuations. This method provides a natural physical constraint: the fragment's formula is bounded by the precursor's formula. A fragment cannot contain more atoms of a specific element than were present in the original molecule, effectively pruning the search space from billions of theoretical combinations to a manageable subset of chemically plausible fragments.

SCARF-Weave: Intensity Prediction via Set Transformers

Once the candidate fragment formulae are identified, SCARF-Weave assigns an intensity to each peak. Unlike models that treat peaks in isolation, SCARF-Weave utilizes a Set Transformer architecture. This allows the model to capture the interdependencies between fragments, recognizing that the relative intensity of one peak is often influenced by the presence and abundance of others within the same spectrum.

Experimental Evaluation

The authors evaluated SCARF using two primary datasets: NIST20, a high-quality commercial library, and NPLIB1, a public dataset containing diverse natural products. To ensure robustness, the evaluation used structure-disjoint splits, testing the model's ability to generalize to molecular architectures not seen during training.

Performance and Accuracy

SCARF demonstrated state-of-the-art performance, achieving a cosine similarity of 0.726 on the NIST20 dataset. This significantly outperforms the leading deep learning baseline, NEIMS-GNN (0.694), and the traditional rule-based simulator CFM-ID (0.412). Furthermore, SCARF proved to be two orders of magnitude faster than CFM-ID, enabling the rapid generation of large-scale in silico libraries.

Physical Groundedness and Retrieval

A key advantage of the SCARF architecture is its 100% validity rate. Because the prefix-tree decoding is constrained by the precursor formula, every predicted peak corresponds to a valid sub-formula. In contrast, binned neural networks frequently predict "impossible" peaks—fragments that could not possibly be derived from the parent molecule—in approximately 5% of cases. In molecular retrieval tasks, SCARF achieved the highest Top-1 and Top-5 accuracy when identifying a target molecule from a pool of 50 similar isomers.

Discussion and Future Directions

While SCARF represents a significant advancement in mass spectrometry simulation, several areas for improvement remain. The model currently depends on peak annotations from pre-processing algorithms like MAGMa, which may introduce latent biases. Additionally, the framework does not yet explicitly model instrument-specific variables, such as collision energy, or rare "adduct switching" events where fragments gain atoms from the ionization environment.

Despite these limitations, SCARF’s use of formulae as a "Goldilocks" representation—balancing the precision of chemical rules with the flexibility of deep learning—sets a new standard for forward simulators in metabolomics.

Summary updated

BibTeX
@inproceedings{goldman2023prefix,
  author = {Samuel Goldman and John Bradshaw and Jiayi Xin and Connor W. Coley},
  title = {{Prefix-Tree Decoding for Predicting Mass Spectra from Molecules}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=NWEbeI2HNQ},
}
Figure