Skip to content

Clarification on Directed vs Undirected Graph Construction in _read_data of GraphPropertyReader #6

Closed

Description

@aditya0by0

While reviewing the GraphPropertyReader class, I noticed that the edge_index in the _read_data method is built like this:

edge_index = torch.tensor(
    [
        [bond.GetBeginAtomIdx() for bond in mol.GetBonds()],
        [bond.GetEndAtomIdx() for bond in mol.GetBonds()],
    ]
)

This seems to construct the molecular graph as directed graph, where each bond is added in one direction only [begin_atom_idx] -> [end_atom_idx].

❓ Clarification

  • Is this directed representation intentional?
  • Or is the graph converted to undirected later in the pipeline (perhaps via symmetrization)?

Since molecular graphs typically model bonds as undirected, I wanted to confirm the intended behavior.

💡 Suggestion (if not intentional)

If the goal is an undirected graph, this can be handled cleanly using PyTorch Geometric's to_undirected utility:

from torch_geometric.utils import to_undirected

edge_index = torch.tensor(
    [
        [bond.GetBeginAtomIdx() for bond in mol.GetBonds()],
        [bond.GetEndAtomIdx() for bond in mol.GetBonds()],
    ]
)
edge_index = to_undirected(edge_index)

OR

by explicitly creating undirected graphs:

src = [bond.GetBeginAtomIdx() for bond in mol.GetBonds()]
tgt = [bond.GetEndAtomIdx() for bond in mol.GetBonds()]
edge_index = torch.tensor([src + tgt, tgt + src])

Let me know if this was an intentional design or if we should update it. Thanks!

Activity

  1. linked a pull request that will close this issueFix to make Directed graph to Undirected #7on May 25, 2025
  2. aditya0by0 commented on Jun 6, 2025

    @aditya0by0
    MemberAuthor

    @sfluegel05
    I've noticed that some SMILES strings or molecules (e.g., CHEBI:32129) contain dative bonds, which are fundamentally directional in nature. These bonds represent a donation of a lone pair from a donor atom to an acceptor atom (e.g., metal-ligand interactions or coordinate covalent bonds).

    RDKit explicitly models these bonds as directed, using types like BondType.DATIVE, BondType.DATIVEL, and BondType.DATIVER. This is also supported by RDKit documentation reference where it emphasizes the directional behavior of such bonds.

    Currently, in our fix for graph construction logic adds all bonds as bidirectional edges (i.e., both src → tgt and tgt → src), treating the molecular graph as undirected. However, this may not be chemically accurate for dative bonds, where directionality carries important meaning. Treating these as one-way edges (i.e., donor → acceptor only) could lead to more faithful graph representations.

    Shall we treat dative bonds as directed edges only during edge_index construction to preserve chemical semantics?
    This might improve the performance (or may be not) in comparison to directed graphs.

    This would involve checking bond.GetBondType() and conditionally skipping the reverse edge for dative bond types.

  3. aditya0by0 commented on Jun 6, 2025

    @aditya0by0
    MemberAuthor

    https://www.rdkit.org/docs/source/rdkit.Chem.rdchem.html#rdkit.Chem.rdchem.BondType

    This all are available bond types from RDKIT api.

    I think only the below are directed bonds.

    [
        Chem.rdchem.BondType.DATIVE,
        Chem.rdchem.BondType.DATIVEL,
        Chem.rdchem.BondType.DATIVER,
        Chem.rdchem.BondType.DATIVEONE
    ]
  4. aditya0by0 commented on Jun 6, 2025

    @aditya0by0
    MemberAuthor
    import pandas as pd
    from rdkit import Chem
    
    df_or = pd.read_pickle(
        "G:/github-aditya0by0/python-chebai/data/chebi_v241/ChEBI50/processed/__data.pkl"
    )
    
    total_bonds = 0
    dative_bonds = 0
    num_molecules = 0
    molecules_with_atleast_one_dative_bond = 0
    for row in df_or.itertuples():
        smiles = row.SMILES
        mol = Chem.MolFromSmiles(smiles)
        if mol is None:
            continue  # Skip invalid molecules
        num_molecules += 1
    
        has_dative_bond = False
        for bond in mol.GetBonds():
            btype = bond.GetBondType()
            total_bonds += 1
            if btype in [
                Chem.rdchem.BondType.DATIVE,
                Chem.rdchem.BondType.DATIVEL,
                Chem.rdchem.BondType.DATIVER,
                Chem.rdchem.BondType.DATIVEONE,
            ]:
                dative_bonds += 1
                has_dative_bond = True
        if has_dative_bond:
            molecules_with_atleast_one_dative_bond += 1
    
    print(f"Total bonds: {total_bonds}")
    print(f"Dative bonds: {dative_bonds}")
    print(f"Percentage of dative bonds: {dative_bonds / total_bonds * 100:.2f}%")
    print(f"Number of molecules: {num_molecules}")
    print(
        f"Molecules with at least one dative bond: {molecules_with_atleast_one_dative_bond}"
    )
    print(
        f"Percentage of molecules with at least one dative bond: {molecules_with_atleast_one_dative_bond / num_molecules * 100:.2f}%"
    )

    Total bonds: 6962481
    Dative bonds: 471
    Percentage of dative bonds: 0.01%

    Number of molecules: 188923
    Molecules with at least one dative bond: 128
    Percentage of molecules with at least one dative bond: 0.07%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions