Skip to content

molecule #

Functions:

  • mol_to_smiles

    Convert a molecule to a SMILES string with atom mapping.

  • unmap_smiles

    Remove atom mapping from a SMILES string.

  • map_smiles

    Add atom mapping to a SMILES string.

mol_to_smiles #

mol_to_smiles(mol: Mol, canonical: bool = True) -> str

Convert a molecule to a SMILES string with atom mapping.

Parameters:

  • mol (Mol) –

    The molecule to convert.

  • canonical (bool, default: True ) –

    Whether to canonicalize the atom ordering prior to assigning map indices.

Returns:

  • str

    The SMILES string.

Source code in descent/utils/molecule.py
def mol_to_smiles(mol: "Chem.Mol", canonical: bool = True) -> str:
    """Convert a molecule to a SMILES string with atom mapping.

    Args:
        mol: The molecule to convert.
        canonical: Whether to canonicalize the atom ordering prior to assigning
            map indices.

    Returns:
        The SMILES string.
    """
    from rdkit import Chem

    mol = Chem.AddHs(mol)

    if canonical:
        order = Chem.CanonicalRankAtoms(mol, includeChirality=True)
        mol = Chem.RenumberAtoms(mol, list(order))

    for atom in mol.GetAtoms():
        atom.SetAtomMapNum(atom.GetIdx() + 1)

    return Chem.MolToSmiles(mol)

unmap_smiles #

unmap_smiles(smiles: str) -> str

Remove atom mapping from a SMILES string.

Parameters:

  • smiles (str) –

    The SMILES string to unmap.

Returns:

  • str

    The unmapped SMILES string.

Source code in descent/utils/molecule.py
def unmap_smiles(smiles: str) -> str:
    """Remove atom mapping from a SMILES string.

    Args:
        smiles: The SMILES string to unmap.

    Returns:
        The unmapped SMILES string.
    """
    from rdkit import Chem

    mol = Chem.MolFromSmiles(smiles)

    for atom in mol.GetAtoms():
        atom.SetAtomMapNum(0)

    return Chem.MolToSmiles(mol)

map_smiles #

map_smiles(smiles: str) -> str

Add atom mapping to a SMILES string.

Notes

Fully mapped SMILES strings are returned as-is.

Parameters:

  • smiles (str) –

    The SMILES string to add map indices to.

Returns:

  • str

    The mapped SMILES string.

Source code in descent/utils/molecule.py
def map_smiles(smiles: str) -> str:
    """Add atom mapping to a SMILES string.

    Notes:
        Fully mapped SMILES strings are returned as-is.

    Args:
        smiles: The SMILES string to add map indices to.

    Returns:
        The mapped SMILES string.
    """
    from rdkit import Chem

    params = Chem.SmilesParserParams()
    params.removeHs = False

    mol = Chem.AddHs(Chem.MolFromSmiles(smiles, params))

    map_idxs = sorted(atom.GetAtomMapNum() for atom in mol.GetAtoms())

    if map_idxs == list(range(1, len(map_idxs) + 1)):
        return smiles

    for i, atom in enumerate(mol.GetAtoms()):
        atom.SetAtomMapNum(i + 1)

    return Chem.MolToSmiles(mol)