---
name: chemical-formula-representation
description: Use when you need to feed chemical formulas into a neural network-based formula scorer (such as MIST-CF) that must learn data-dependent representations of formula structure and composition.
license: CC-BY-4.0
metadata:
  edam_operation: http://edamontology.org/operation_3799
  edam_topics:
  - http://edamontology.org/topic_0218
  - http://edamontology.org/topic_3520
  tools:
  - MIST
  - SCARF
  - MIST-CF
  - SIRIUS
derived_from:
- doi: 10.1021/acs.jcim.3c01082
  title: mistcf
evidence_spans:
- an extension of MIST for annotating MS1 precursor masses from MS/MS data
- Utilizing sinusoidal formula embeddings as developed in our previous work SCARF
- MIST-CF ranks chemical formula and adduct assignments for an unknown mass spectrum using an end-to-end energy based modeling approach
claims: []
provenance:
  collection: https://w3id.org/holobiomicslab/asb-skill/collection/metabolomics/v1
  assembled_by: scripts/collect_metabolomics_collection.py
  sources:
  - build: coll_mistcf
    doi: 10.1021/acs.jcim.3c01082
    title: mistcf
  dedup_kept_from: coll_mistcf
schema_version: 0.2.0
---

# Chemical Formula Representation

## Summary

Encode chemical formulas into learnable vector embeddings using sinusoidal positional encoding (SCARF embeddings) to enable neural network processing of formula structure. This technique is essential for formula transformer architectures that rank candidate chemical formulas against unknown mass spectra without referencing spectral databases.

## When to use

Apply this skill when you need to feed chemical formulas into a neural network-based formula scorer (such as MIST-CF) that must learn data-dependent representations of formula structure and composition. Use it specifically when your task involves ranking or scoring multiple candidate chemical formulas (generated by SIRIUS decomp or similar enumeration) against tandem MS/MS spectra in a de novo annotation setting.

## When NOT to use

- Input is already a pretrained molecular fingerprint or fixed descriptor (e.g., ECFP, Morgan fingerprint); use those directly rather than re-encoding via formula embeddings.
- Task does not involve learning formula rankings or scoring; sinusoidal embeddings add computational overhead and are not needed for simple formula matching or exact identity lookup.
- Formulas are unknown or malformed; SIRIUS decomp or another deterministic formula enumerator must first generate valid candidate formulas before embedding.

## Inputs

- Chemical formula strings (e.g., 'C6H12O6', 'C3H7NO2', 'C8H10N4O2')
- Embedding dimensionality parameter (configured as model hyperparameter)
- Formula candidate set (typically 10–1000 candidates per spectrum from SIRIUS decomposition)

## Outputs

- Embedding vectors of shape [batch_size, embedding_dimension]
- Contextualized formula representations from transformer encoder
- Validation metrics (L2 distance to reference, embedding range checks, reproducibility report)

## How to apply

Load or instantiate chemical formulas as strings (e.g., 'C6H12O6', 'C8H10N4O2'). Pass each formula through the sinusoidal formula embedding layer (adopted from SCARF) configured with a fixed embedding dimensionality parameter. The embedding layer computes sinusoidal position encodings for each element and atom count in the formula, producing a dense vector representation. Forward the embedding vectors through the formula transformer encoder to learn contextualized formula representations. Verify embeddings fall within expected numerical range (typically [-1, 1] for sinusoidal embeddings) and match reference outputs from MIST-CF with numerical tolerance (L2 distance < 1e-6) to confirm reproducibility before using in downstream scoring.

## Related tools

- **MIST-CF** (Neural network architecture that ingests sinusoidal formula embeddings to rank chemical formula and adduct assignments from MS/MS spectra) — https://github.com/samgoldman97/mist-cf
- **SCARF** (Prior work defining sinusoidal formula embeddings as an advance over previous MIST formula encoding) — https://arxiv.org/abs/2303.06470
- **SIRIUS** (Generates candidate chemical formulas via dynamic programming decomposition; formulas are then embedded using this skill before neural scoring) — https://bio.informatik.uni-jena.de/software/sirius/

## Examples

```
From samgoldman97/mist-cf: instantiate sinusoidal embedding layer and encode formulas: `from mist_cf.mist_cf_score.model import FormulaEmbedding; emb = FormulaEmbedding(embedding_dim=256); formulas = ['C6H12O6', 'C8H10N4O2']; embeddings = [emb.forward(f) for f in formulas]; print(embeddings[0].shape)  # torch.Size([256])`
```

## Evaluation signals

- Embedding vector dimensionality matches the configured embedding_dimension parameter (e.g., 256, 512).
- All embedding values fall within expected sinusoidal range of approximately [-1, 1].
- L2 distance between produced embeddings and reference outputs from MIST-CF repository is < 1e-6, confirming numerical reproducibility.
- Formula transformer encoder produces distinct, non-collapsed representations for chemically diverse formulas (e.g., glucose vs. caffeine vs. alanine).
- Downstream MIST-CF scoring module can consume embedding vectors without shape or type errors and produces well-calibrated ranking scores.

## Limitations

- Sinusoidal embeddings assume formulas can be decomposed into element-atom count pairs; formulas with non-standard or unrecognized elements may fail or produce degraded embeddings.
- MIST-CF model is optimized for positive-mode tandem MS/MS only; negative-mode or other ionization methods are not currently supported by the architecture.
- Models trained on NPLIB1 (public natural products dataset) are less performant than models trained on proprietary NIST20 library, particularly for Orbitrap or higher-resolution mass spectrometry data.
- Embedding reproducibility depends on exact numerical precision and the same codebase version; embeddings computed with different MIST-CF or PyTorch versions may exceed the 1e-6 tolerance.

## Evidence

- [other] MIST-CF incorporates sinusoidal formula embeddings, a technique developed in prior work (SCARF), as an advance to the chemical formula transformer architecture: "Utilizing sinusoidal *formula* embeddings as developed in our previous work SCARF"
- [other] Formulas are embedded and scored to rank candidate chemical formulas and adduct assignments in de novo MS/MS annotation: "MIST-CF ranks chemical formula and adduct assignments for an unknown mass spectrum using an end-to-end energy based modeling approach, without referencing any spectrum databases"
- [other] Sinusoidal embeddings enable formula transformer architecture that learns data-dependent representations: "Instead of computing fragmentation trees, MIST-CF adopts a formula transformer neural network architecture and learns in a data dependent fashion"
- [other] Embedding validation workflow includes dimensionality check, numerical range verification, and reproducibility comparison against reference outputs: "Verify embedding vector dimensionality matches the configured embedding dimension parameter. 5. Verify all embedding values fall within the expected numerical range (typically [-1, 1] for sinusoidal"
