Skip to content

SMILES round-trip validation

By default, MolGR writes the reconstructed graph to SMILES and reads it back with the installed RDKit. The check uses the writer’s atom mapping to verify explicit hydrogens, bond orders, and dative bond directions. 简体中文

New in 0.1.11 · mol_from_smiles and validate_smiles_roundtrip.

Write and read a graph

validate_smiles_roundtrip(mol) returns the validated SMILES string. mol_from_smiles(text) reads it into an RDKit molecule. In this example, the two hydrogen atoms in water are separate nodes. Reading it back gives three atoms.

from molgr import mol_from_smiles, validate_smiles_roundtrip

water = mol_from_smiles("[H]O[H]")
text = validate_smiles_roundtrip(water)
restored = mol_from_smiles(text)
print(restored.GetNumAtoms(), restored.GetNumBonds())  # 3 2

MolGR’s reconstructed graphs already contain all hydrogen atoms. Other RDKit inputs must also have separate hydrogen nodes. Expand implicit hydrogens such as those in O and atom-level hydrogen counts such as the H in [NH] before validation.

Choose a bond and stereo representation

Option Default Use
kekulize True Use the same setting for reconstruction, validation, and reading. True keeps local integer bond orders and skips aromaticity reassignment on readback. False uses aromatic bonds and normal RDKit aromaticity perception. Both preserve explicit hydrogen nodes.
check_stereochemistry False On validate_smiles_roundtrip, set this to True to also check mapped tetrahedral, double-bond, and supported coordination stereo labels.
config.interface.require_smiles_roundtrip True Controls automatic validation before reconstruction returns. Calling the validator directly always runs the check.

The reconstruction argument make_stereochemistry=True assigns stereo labels from the original coordinates. To check their recovery through SMILES, call validate_smiles_roundtrip(mol, check_stereochemistry=True).

What survives the round trip?

Information What is checked
Atoms Element, isotope, formal charge, explicit hydrogen nodes and counts, aromaticity, and atom-map number. Writer order can change.
Bonds Endpoints, type, aromaticity, and donor→acceptor direction of dative bonds.
Coordinates and electronic labels Keep the original RDKit molecule for coordinates and MolGR electronic attributes that ordinary SMILES cannot encode. RDKit-inferred radical counts are outside the default checks.

A changed bond order affects a round-trip check even if the resulting drawing is resonance-equivalent. For graphs from independent sources, use graph equivalence.

Why can a metal contact appear as an ordinary bond?

For certain metal acceptors, MolGR uses the installed RDKit valence tables to choose a representation that passes valence checks. For example, converting a donor→metal dative single bond to an ordinary single bond increases the donor’s formal charge by one and decreases the metal’s by one. Total charge is conserved. The check validates this final representation. Coordination and readback tests

Diagnose a failure

Direct reader and validator calls raise molgr.SmilesRoundtripError. Use str(error) for the reason. For a changed atom field, error.details can include the one-based source atom ID, field name, and before and after values.

With the default reconstruction policy, ReconstructionError.diagnostics reports code SMILES_ROUNDTRIP_FAILED at stage rdkit.smiles_roundtrip. Batch processing records diagnostics in the failed item’s status and continues. Inspect any suspicious_fallback graph separately. Reconstruction API · Reader and validator