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

