Canonical SMILES
Canonical SMILES gives every molecular graph one deterministic string. It is used to deduplicate reaction-network species, compare molecules parsed from different input orders and stabilise fixtures.
Pipeline
flowchart LR
A[Molecule graph] --> B[Initial invariants]
B --> C[Weisfeiler-Lehman refinement]
C --> D{Equivalent atoms?}
D -- yes --> E[Individualisation]
E --> C
D -- no --> F[Choose lexicographically smallest start]
F --> G[DFS traversal + ring closures]
G --> H[Canonical SMILES]
H --> I[Re-parse and compare]
Invariants include bond order
The initial rank combines atomic number, degree, bond-order sum, hydrogen count, formal charge and aromaticity. The refinement hashes each atom with its neighbours' ranks and bond orders. Without the bond-order term, the two terminal oxygens of ozone would be considered equivalent and the output would depend on the input atom order.
Two atoms that remain equivalent are separated by individualisation: each one is tried, the refinement is repeated and the smallest signature is kept.
Bracket atoms
Atoms parsed from a bracket ([O], [OH], [CH3], [nH]) carry no_implicit_h = true. The writer always emits those atoms in brackets with their explicit hydrogen count. Dropping the brackets would let the reader add implicit hydrogens and silently change the species:
| Input | Canonical | Re-parsed |
|---|---|---|
[O] | [O] | atomic oxygen |
[OH] | [OH] | hydroxyl radical |
[O]O | [O]O | HO₂ |
OO | OO | H₂O₂ |
O=O[O] | O=O[O] | ozone |
Guarantees
- Idempotent: canonicalising a canonical string returns the same string.
- Independent of input atom order.
- Preserves the element composition, bond multiset and radical/charge state.
- All disconnected fragments survive. Salts such as
[Na+].[Cl-]are canonicalised component by component and joined with.in lexicographic order ([Cl-].[Na+]), instead of keeping only the largest fragment. - Every atomic number in
1..=118has a SMILES symbol; unknown values are not silently rendered as?. - Distinct species (for example HO₂ and H₂O₂) never collapse to the same string.
Surfaces
| Surface | Entry point |
|---|---|
| CLI | sci-form canonical-smiles "<SMILES>" |
| Rust | sci_form::canonical_smiles::to_canonical_smiles(&molecule), to_canonical_smiles_all(&molecule) |
| Python | sci_form.to_canonical_smiles(smiles) |
| WASM | to_canonical_smiles(smiles) (JSON-quoted result) |
Validation
tests/rdkit_chem_parity.rs (F83) checks round-trip, idempotence and input-order invariance on 36 molecules. radical_species_survive_template_roundtrip checks the composition and bond-count identity of radicals and closed-shell species. The audit regression also covers the [Na+].[Cl-] ↔ [Cl-].[Na+] salt pair, all 118 element symbols and empty/malformed input.