liveBook · Manning

Molecular graph

As explained in Graph Neural Networks in Action

A molecular graph is a graph representation in which atoms are nodes and chemical bonds are edges.

A molecular graph represents a molecule as atoms (nodes) connected by chemical bonds (edges). This structure makes small-molecule data usable for graph neural networks, especially when predicting properties or generating new molecules. section 5.4.1 section 5.4.4

A practical molecular-graph workflow follows this pipeline:

  • Start with SMILES strings and molecular properties such as QED, logP, and SAS. section 5.4.1
  • Parse each SMILES string with RDKit and reject invalid molecules. section 5.4.1
  • Store atom features in a node-feature matrix and bonds with their types in edge indices and edge attributes. section 5.4.1
  • Pass the resulting graph objects through a graph autoencoder, predicting adjacency, features, and molecular properties. section 5.4.4

Drug-discovery dataset

A ZINC dataset of roughly 250,000 screened molecules supplies SMILES strings and drug-related properties. Converting these strings into PyTorch Geometric objects enables property prediction and generation of novel molecular graphs optimized for QED. section 5.4.1 section 5.4.4

Listing 5.17 Create the molecular graph dataset
   from torch_geometric.data import Data
   import torch
   from rdkit import Chem


   def smiles_to_graph(smiles, qed):
     mol = Chem.MolFromSmiles(smiles)
        if not mol:
             return None

        edges = []
        edge_features = []
        for bond in mol.GetBonds():
             edges.append([bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()])
             bond_type = bond.GetBondTypeAsDouble()
             bond_feature = [1 if i == bond_type\
             else 0 for i in range(4)]
             edge_features.append(bond_feature)

        edge_index = torch.tensor(edges, dtype=torch.long).t().contiguous()
        edge_attr = torch.tensor(edge_features, dtype=torch.float)
        x = torch.tensor([atom.GetAtomicNum()\
 for atom in mol.GetAtoms()], \
 dtype=torch.float).view(-1, 1)

        num_heavy_atoms = mol.GetNumHeavyAtoms()

        return Data(x=x, edge_index=edge_index,\
 edge_attr=edge_attr, \
qed=torch.tensor([qed], \
dtype=torch.float), \
num_heavy_atoms=num_heavy_atoms)

Inspect the implementation that converts SMILES into PyTorch Geometric graph data, including bond encodings, atomic-number features, and heavy-atom counts. section 5.4.1

How Graph Neural Networks in Action covers it

How deep it goes
Builds graphs from SMILES through node and edge features, then reconstructs adjacency and feature matrices during model training.
Examples
Uses ZINC molecules, QED values, atomic-number features, bond-type encodings, and generated small-molecule graphs.
What you can do after
Build it from scratch by converting SMILES into graph objects for PyTorch Geometric workflows.
How it connects
Connects SMILES, RDKit, NetworkX, PyTorch Geometric, graph neural networks, graph autoencoders, adjacency matrices, and latent spaces.

Also explained in

Compare how 2 books explain molecular graph →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Neural Networks in Action. The section links point to the parts of the book it draws on.