Skip to content

smiles_writer

SMILES string generation from molecular graphs.

Generates canonical SMILES strings using DFS traversal with support for: - Ring closures - Aromaticity - Stereochemistry (R/S, E/Z) - Charges and isotopes

smiles_from_molecule(molecule, canonical=True)

Generate SMILES from a Molecule object.

Convenience function that creates a MolecularGraph and generates SMILES.

Parameters:

Name Type Description Default
molecule

A Molecule object from chmpy.

required
canonical bool

If True, generate canonical SMILES (default True).

True

Returns:

Type Description
str

SMILES string.

Source code in chmpy/graph/smiles_writer.py
def smiles_from_molecule(molecule, canonical: bool = True) -> str:
    """
    Generate SMILES from a Molecule object.

    Convenience function that creates a MolecularGraph and generates SMILES.

    Args:
        molecule: A Molecule object from chmpy.
        canonical: If True, generate canonical SMILES (default True).

    Returns:
        SMILES string.
    """
    from .adjacency import MolecularGraph

    graph = MolecularGraph.from_molecule(molecule)
    return to_smiles(graph, canonical=canonical)

to_smiles(graph, canonical=True, explicit_h=False, guess_charges=False)

Generate a SMILES string from a molecular graph.

Parameters:

Name Type Description Default
graph MolecularGraph

A MolecularGraph instance.

required
canonical bool

If True, generate canonical SMILES (default True).

True
explicit_h bool

If True, include explicit hydrogens (default False).

False
guess_charges bool

If True, automatically detect and assign formal charges for common functional groups (nitro, carboxylate, etc.).

False

Returns:

Type Description
str

SMILES string representation of the molecule.

Source code in chmpy/graph/smiles_writer.py
def to_smiles(
    graph: "MolecularGraph",
    canonical: bool = True,
    explicit_h: bool = False,
    guess_charges: bool = False,
) -> str:
    """
    Generate a SMILES string from a molecular graph.

    Args:
        graph: A MolecularGraph instance.
        canonical: If True, generate canonical SMILES (default True).
        explicit_h: If True, include explicit hydrogens (default False).
        guess_charges: If True, automatically detect and assign formal charges
            for common functional groups (nitro, carboxylate, etc.).

    Returns:
        SMILES string representation of the molecule.
    """
    if graph.n_atoms == 0:
        return ""

    # Optionally guess formal charges
    if guess_charges:
        from .formal_charges import assign_formal_charges
        graph = assign_formal_charges(graph)

    # Get canonical ordering if requested
    if canonical:
        ordering = canonical_ordering(graph)
    else:
        ordering = np.arange(graph.n_atoms)

    # Detect aromaticity and rings
    aromatic_atoms, aromatic_bonds = perceive_aromaticity(graph)
    rings = find_sssr(graph)

    # Build ring bond set
    ring_bonds = _identify_ring_bonds(rings)

    # Find stereocenters
    stereocenters = {sc.atom_idx: sc for sc in find_stereocenters(graph)}
    double_bond_stereo = {}
    for db in find_double_bond_stereo(graph):
        double_bond_stereo[(db.atom1, db.atom2)] = db
        double_bond_stereo[(db.atom2, db.atom1)] = db

    # Identify hydrogen atoms to skip (unless explicit_h is True)
    skip_atoms = set()
    if not explicit_h:
        for i in range(graph.n_atoms):
            if graph.atomic_numbers[i] == 1:
                skip_atoms.add(i)

    # DFS traversal
    writer = _SMILESWriter(
        graph,
        ordering,
        aromatic_atoms,
        aromatic_bonds,
        ring_bonds,
        stereocenters,
        double_bond_stereo,
        skip_atoms,
    )

    return writer.generate()