Skip to content

Local identity

Identity can be computed from the structure you drew — offline, through reference software. The InChI algorithm is never reimplemented: an engine wraps an InChI implementation behind one contract, and the pipe is always Model::Molecule → molfile → engine → InChI/InChIKey. Without an engine, every derivation raises an explicit error with install guidance — there is no silent fallback.

This page is the Ruby (and binding) seam. The authoritative Rust crate needs none of it: chemomer rides in-process as a version-pinned dependency — no FFI, no engine registration (see Rust).

AsciiChem::Inchi.engine = ... # register once, or pass engine: per call
AsciiChem::Inchi.identity_for(molecule)
# => #<AsciiChem::Inchi::Identity inchi: "InChI=1S/...", inchikey: "BSYNRYMUTXBXSQ-UHFFFAOYSA-N">

Two engines ship with the gem; both are opt-in external artifacts — the core gem stays dependency-free.

AsciiChem::Inchi::BinaryEngine shells out to the standalone IUPAC binary (inchi-trust.org downloads):

AsciiChem::Inchi.engine = AsciiChem::Inchi::BinaryEngine.new
AsciiChem::Inchi.engine = AsciiChem::Inchi::BinaryEngine.new(bin: "/usr/local/bin/inchi-1")

From the CLI, --engine-bin points at a binary for one invocation:

Terminal window
asciichem identity -i "CC(=O)OC1=CC=CC=C1C(=O)O" --from smiles
asciichem identity -i "..." --engine-bin /usr/local/bin/inchi-1

The engine stages the molfile to a temp file and reads the output file back — the distributed binary’s file interface is the contract (-STDIO is not part of it). Without any engine, asciichem identity exits 5 with install guidance.

Engine 2: the native Rust engine (chemomer)

Section titled “Engine 2: the native Rust engine (chemomer)”

chemomer is a native Rust InChI engine — canonical numbering, every standard layer (mobile-H groups, tetrahedral and double-bond stereo), and the InChIKey with a dependency-free SHA-256 — gated byte-exactly against the recorded IUPAC 1.07.5 oracle corpus (chemomer/inchi-tests, pinned at v0.3.41). It ships as a Rust library, a C ABI (cdylib), and Python/WASM/Node bindings — no third-party binary at runtime. The C ABI also exports the AuxInfo line (chemomer_aux_from_molfile).

The Ruby gem reaches it through NativeEngine over the C ABI (FFI loads lazily):

AsciiChem::Inchi.engine = AsciiChem::Inchi::NativeEngine.new
AsciiChem::Inchi.engine = AsciiChem::Inchi::NativeEngine.new(lib: "/path/libchemomer.dylib")

Build the cdylib once from an engine checkout and point CHEMOMER_LIB at it (or install the dylib on the loader’s search path):

Terminal window
cargo build --release # in the chemomer checkout
export CHEMOMER_LIB=$PWD/target/release/libchemomer.dylib

The same engine ships ready-made for other stacks — each binding is its own thin package over the same C ABI:

StackPackageCall
Rustcrates.io chemomerEngine::from_molfile(mol) → inchi_string() / inchikey()
Node / WASMnpm chemomerfrom_molfile(text) / aux_from_molfile(text) / inchikey(text)
PythonPyPI chemomerinchi, inchikey = chemomer.from_molfile(text)
RubyRubyGems chemomerinchi, inchikey = Chemomer.from_molfile(text)
  • Connectivity, charges, isotopes cross through the V2000 molfile block.
  • Tetrahedral chirality is written as MDL atom parity; stated hydrogens become the H-count field — so enantiomers derive different InChIs.
  • Directional bonds (/, \) need stereo-aware 2D geometry, which the molfile emitted here does not carry. The loss is documented and asserted, never silent.

Identity derivation needs exactly one molecule; formulas with several (or reactions) are rejected with a count in the message.

Reactions get the same treatment through RInChI — the reaction is written as an MDL RXN file and passed to the opt-in IUPAC rinchi_cmdline binary (built against InChI v1.07.5):

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

Like the InChI engines, it is opt-in: without it, derivation raises EngineMissingError with build instructions.

With an engine configured, the linter’s IdentityCrossCheck derives every @inchi/@inchikey-annotated molecule’s identifier and asserts agreement with what you actually drew — catching transcription errors that format validation cannot. Without an engine it emits one guidance warning. See the Linter reference.

Play