Skip to content

Structures and resolution

AsciiChem’s model has two more front doors besides the ASCII syntax: structure interchange (SMILES, molfile) and resolution (identifiers → substance records). Both land in the same semantic model, so every formatter and the wire form work unchanged.

A molecular formula is not a structure — C_2H_6O is ethanol or dimethyl ether. The chemistry world carries connectivity as SMILES or connection tables (molfile/SDF), and so does AsciiChem:

AsciiChem.parse_smiles("CC(=O)OC1=CC=CC=C1C(=O)O") # aspirin
AsciiChem.parse_molfile(molfile_text) # V2000, coordinates preserved

Both produce ordinary Model::Molecules: bonds, ring closures, charges, isotopes, aromaticity. Render them (to_structural_svg), re-emit them (to_smiles — a deterministic writer; aspirin round-trips exactly), or serialise to the wire form.

From the CLI:

Terminal window
asciichem convert -i "CCO" --from smiles -t structural-svg
asciichem convert -f ethanol.mol --from molfile -t model-json

The supported subset is honest about its edges: chirality (@) and E/Z bond directions (/ \) raise actionable errors rather than silently dropping stereo.

The playground takes all three grammars side by side — type AsciiChem, paste a database SMILES, or paste a molfile and watch its coordinates drive the diagram. Same model, same renderers, in your browser.

Resolution turns identifiers into substance records with provenance (TODO.v2 07). All shipped sources — PubChem (keyless, permissive), OPSIN, and CAS Common Chemistry — are registered and on by default; every record carries its source’s required attribution, and callers limit the set explicitly (sources:) when license obligations require it. A failing source never poisons the others: the resolver skips sources that answer with errors (CAS key-gated the Common Chemistry API — export COMMON_CHEMISTRY_API_KEY for live lookups there; see Sources and licensing).

substance = AsciiChem::Resolver.resolve(value: "50-78-2", convention: "cas")
substance.identifier_value("inchikey") # => "BSYNRYMUTXBXSQ-UHFFFAOYSA-N"
substance.structure.to_structural_svg # parsed from the source SMILES
substance.to_model_json # canonical substance record
# every answering source, one record each:
AsciiChem::Resolver.resolve_all(value: "50-78-2", convention: "cas")
# cross-check mode: raises Resolver::Conflict on InChIKey disagreement
AsciiChem::Resolver.resolve!(value: "50-78-2", convention: "cas")

Every result carries provenance — source, retrieved-at, attribution — because the citation track emits one bibitem per (substance, source). Results are cached in your user cache directory (7-day TTL); --refresh bypasses it.

Terminal window
asciichem resolve --name aspirin --source pubchem
asciichem validate -i 'H_2O @cas("7732-18-5")' # offline

Identity can also be computed from the structure you drew — offline, through reference software (never a reimplementation). The engine is opt-in and external:

AsciiChem::Inchi.engine = AsciiChem::Inchi::BinaryEngine.new # IUPAC inchi-1 binary
# or the native Rust engine (chemomer), driven through CHEMOMER_LIB:
AsciiChem::Inchi.engine = AsciiChem::Inchi::NativeEngine.new
AsciiChem::Inchi.identity_for(molecule).inchikey

With an engine configured, asciichem lint structurally cross-checks @inchi and @inchikey annotations against the drawn structure — catching transcription errors that format validation cannot. Python wraps the same IUPAC library via RDKit (pip install 'asciichem[inchi]').

Stereochemistry crosses into the engine: tetrahedral chirality is written as MDL atom parity and stated hydrogens as the H-count field, so enantiomers derive different InChIs. Directional bonds (/, \) need stereo-aware 2D geometry, which V2000 does not carry here — the loss is documented and asserted, not silent.

Reactions get the same treatment through RInChI: the reaction is written as an MDL RXN file and passed to the opt-in rinchi-1 binary, yielding the RInChI and its short/long InChIKeys:

AsciiChem::Rinchi.engine = AsciiChem::Rinchi::BinaryEngine.new
AsciiChem::Rinchi.identity_for(reaction).long_rinchikey

Engines, env vars, the native engine’s bindings, and what crosses into the molfile pipe are on Local identity.

Wire form

The canonical JSON every implementation emits — /models.

Live renderer

AsciiChem, SMILES, and molfile in, diagrams and wire JSON out — /playground.

Cite what you resolve

Provenance becomes dataset-type citations — Citing substances.

Play