Skip to content

from_smiles

from_smiles(
    smiles: str, with_hydrogen: bool = False, kekulize: bool = False
) -> GraphData

Converts a SMILES string to a GraphData, with the featurization of PyG's from_smiles (see from_rdmol()).

An invalid SMILES string gives an empty molecule (no atoms), as in PyG. Requires rdkit.

Parameters:

Name Type Description Default
smiles str

The SMILES string.

required
with_hydrogen bool

Whether to add the hydrogens to the graph. Default: False

False
kekulize bool

Whether to convert the aromatic bonds to single and double bonds. Default: False

False

Returns:

Type Description
GraphData

The graph, with int32 node_features of shape `[num_atoms,

GraphData

9]andedge_featuresof shape[num_edges, 3]`, and the

GraphData

SMILES string in its smiles attribute.

Example:

from mlx_graphs.utils import from_smiles, to_smiles

graph = from_smiles("CCO")
>>> GraphData(
        edge_index(shape=(2, 4), int32)
        node_features(shape=(3, 9), int32)
        edge_features(shape=(4, 3), int32)
        smiles=CCO)

to_smiles(graph)
>>> 'CCO'
Source code in mlx_graphs/utils/smiles.py
def from_smiles(
    smiles: str, with_hydrogen: bool = False, kekulize: bool = False
) -> "GraphData":
    r"""Converts a SMILES string to a :class:`~mlx_graphs.data.GraphData`,
    with the featurization of PyG's ``from_smiles`` (see :func:`from_rdmol`).

    An invalid SMILES string gives an empty molecule (no atoms), as in PyG.
    Requires ``rdkit``.

    Args:
        smiles: The SMILES string.
        with_hydrogen: Whether to add the hydrogens to the graph.
            Default: ``False``
        kekulize: Whether to convert the aromatic bonds to single and double
            bonds. Default: ``False``

    Returns:
        The graph, with ``int32`` ``node_features`` of shape ``[num_atoms,
        9]`` and ``edge_features`` of shape ``[num_edges, 3]``, and the
        SMILES string in its ``smiles`` attribute.

    Example:

    .. code-block:: python

        from mlx_graphs.utils import from_smiles, to_smiles

        graph = from_smiles("CCO")
        >>> GraphData(
                edge_index(shape=(2, 4), int32)
                node_features(shape=(3, 9), int32)
                edge_features(shape=(4, 3), int32)
                smiles=CCO)

        to_smiles(graph)
        >>> 'CCO'
    """
    data = from_rdmol(_smiles_to_rdmol(smiles, with_hydrogen, kekulize))
    data.smiles = smiles
    return data

from mlx_graphs.utils import from_smiles · mlx_graphs/utils/smiles.py