COSMolKit
cosmolkit is the public Rust API for COSMolKit, a Rust-native cheminformatics and structural biology toolkit. It provides molecular graphs, SMILES/SMARTS processing, molecular file IO, fingerprints, descriptors, 2D depiction, native 3D conformer generation, UFF/MMFF optimization, InChI, substructure search, batch workflows, and protein structure APIs. The crate is a lightweight facade over cosmolkit-core and related COSMolKit components, providing the primary Rust import surface without hiding the underlying modules.
Documentation
- Rust API documentation: https://docs.rs/cosmolkit/latest/cosmolkit/
- Core source layout:
../cosmolkit-core/src/README.md - Python package notes:
../../README.md
Installation
Quick Start
use ;
Molecule Operations
Normal Molecule operations return new values and leave the receiver
unchanged:
let mol = from_smiles?;
let with_h = mol.with_hydrogens?;
assert_ne!;
In-place operations are explicit and always end with _:
let mut mol = from_smiles?;
mol.add_hydrogens_?;
mol.sanitize_?;
The trailing underscore is reserved for in-place mutation on public Molecule
methods; it has no other meaning. In-place operations prioritize avoiding the
operation-system working-copy clone when molecule blocks are uniquely owned. If
an in-place operation returns an error, the receiver is not guaranteed to equal
its pre-call value and may retain partial changes, while its internal storage
remains complete. Use the non-mutating operation when failure-preserving value
semantics are required.
Stable molecule operations include assigning atom chiral tags from a selected
3D conformer through with_chiral_tags_from_structure() and its explicit
in-place counterpart assign_chiral_tags_from_structure_().
Protein Structures
use Protein;
Batch Workflows
use ;
InChI
The Rust facade exposes the four audited scalar InChI APIs directly:
use ;
let molecule = from_smiles?;
let generated = mol_to_inchi?;
assert_eq!;
let key = inchi_to_inchi_key?;
assert_eq!;
let parsed = mol_from_inchi?;
assert!;
Pinned official InChI v1.07.5 and RDKit 2026.03.1 establish exact parity for
source-defined behavior in this boundary. Official-C undefined behavior on the
audited NormalizeAndCompare initial-allocation path is mapped to a
deterministic structured allocation error. MolBlock, SDF/V3000, IXA, AuxInfo,
INCHIGEN, version-query, and extended-polymer InChI APIs are not exposed.
Molecular Descriptors
The facade re-exports the source-backed descriptor functions from
cosmolkit-core:
use ;
let molecule = from_smiles?;
assert_eq!;
assert!;
assert_eq!;
The documented descriptor surface is stable. Supported rows and parameter combinations are checked field-by-field against pinned RDKit golden data; unmodeled source states return an explicit descriptor error.
Fingerprints
The Rust facade exposes source-backed Morgan, MACCS, RDKit topological, and Avalon fingerprints. Topological fingerprints can also return typed atom and path provenance:
use ;
let molecule = from_smiles?;
let topological = molecule.topological_fingerprint?;
let provenance = molecule.topological_fingerprint_with_output?;
let avalon = molecule.avalon_fingerprint?;
assert_eq!;
assert!;
assert_eq!;
The documented topological and Avalon profiles are checked against pinned RDKit across all 2,897,804 mutually parseable ChEMBL 37 molecules. The full-corpus audit completed 113,014,356 exact comparisons over 14 topological vectors, 23 Avalon vectors, and two complete topological provenance outputs with zero mismatches. The committed 5,000-row matrices remain the continuous regression gates for these profiles.
Conformer Generation And Force Field Applications
Native conformer generation uses RDKit-aligned distance-geometry parameters. The default value-style molecule operation uses ETKDGv3 and returns a new molecule value. Multi-conformer generation supports deterministic seeded runs, RMS pruning, and sequential seed expansion:
use ;
Force-field APIs operate on molecules with existing 3D conformers and return new molecule values, so the input coordinates are left unchanged.
use ;
Examples
Development
Core validation should use operation-contract checks:
Use debug-profile test filters for small local iterations. Use release mode with
op-contracts-strict for large local runs, parity suites, and CI; release-mode
testing keeps operation contracts and runtime invariants enabled through the
strict feature set.
Python binding development:
The facade crate should stay thin. Public Rust APIs should be exposed through
cosmolkit or clearly scoped public modules, while molecule mutation continues
to go through registered operations in the core.