Molecule Embeddings
Relevant source files
The following files were used as context for generating this wiki page:
- examples/molecule_embeddings/README.md
- examples/molecule_embeddings/main.py
- python/dgllife/utils/featurizers.py
- python/dgllife/model/model_zoo/gin_predictor.py (Referenced via model architecture documentation)
- examples/README.md
- README.md
Molecule embeddings in DGL-LifeSci represent a method of mapping molecular structures into a continuous, low-dimensional vector space using pre-trained Graph Neural Networks (GNNs). These embeddings function similarly to traditional molecular fingerprints but are derived from models trained on massive datasets (e.g., two million molecules) using various supervised and semi-supervised learning strategies. This approach allows for capturing complex structural and chemical information that traditional rule-based fingerprints might miss.
Sources: examples/molecule_embeddings/README.md:1-12, README.md:15-20
The primary architecture for these embeddings is based on Graph Isomorphism Networks (GIN), which have been pre-trained using distinct strategies such as context prediction, information maximization, edge prediction, and attribute masking. These models take a molecular graph as input and produce a global graph representation that can be used for downstream tasks like property prediction or similarity searching.
Sources: examples/molecule_embeddings/README.md:10-16, examples/README.md:65-69
Core Architecture and Data Flow
The generation of molecule embeddings follows a structured pipeline: parsing SMILES strings, constructing molecular graphs, featurizing atoms and bonds, and finally passing the graph through a pre-trained GIN model with a readout layer.
Embedding Generation Process
The workflow begins with raw chemical data (SMILES) and ends with a saved NumPy array of vector representations.
The diagram above illustrates the transformation of textual chemical representations into numerical embedding vectors.
Sources: examples/molecule_embeddings/main.py:33-100, examples/molecule_embeddings/README.md:46-52
Key Components
- Graph Construction: Uses
mol_to_bigraphto convert RDKit molecule objects into DGLGraphs. Self-loops are typically added to ensure nodes can aggregate their own features during message passing.
Sources: examples/molecule_embeddings/main.py:53-58 - Featurization: Pre-training specific featurizers are required. These differ from "canonical" featurizers by focusing on raw properties like atomic numbers and chirality types for nodes, and bond types/directions for edges.
Sources: python/dgllife/utils/featurizers.py:656-708, python/dgllife/utils/featurizers.py:1145-1200 - Model Loading: The
load_pretrainedutility retrieves specific GIN weights based on the chosen pre-training strategy.
Sources: examples/molecule_embeddings/main.py:73 - Readout Layer: An
AvgPoolinglayer is applied to aggregate node-level representations into a single graph-level embedding.
Sources: examples/molecule_embeddings/main.py:75-88
Pre-training Strategies
DGL-LifeSci implements models based on "Strategies for Pre-training Graph Neural Networks." These strategies determine how the underlying GIN model was trained to understand molecular structures.
| Strategy | Description |
|---|---|
| Contextpred | Uses subgraphs to predict the surrounding graph structures. |
| Infomax | Maximizes mutual information between local node representations and global graph representations. |
| Edgepred | Encourages nearby nodes to have similar representations while distancing disparate nodes. |
| Masking | Randomly masks node/edge attributes for the model to predict. |
Sources: examples/molecule_embeddings/README.md:10-18, examples/molecule_embeddings/main.py:113-116
Data Preparation and Output
Users can provide molecules in either .txt (one SMILES per line) or .csv (requiring a SMILES column header) formats. The implementation handles batching to optimize computation on CPU or CUDA devices.
Input Configuration
The system uses argparse to configure the embedding task:
-fi / --file: Path to the input file.-m / --model: The pre-trained model variant (e.g.,gin_supervised_masking).-b / --batch-size: Number of molecules processed per iteration (default 256).
Sources: examples/molecule_embeddings/main.py:104-123, examples/molecule_embeddings/README.md:25-44
Result Files
After execution, two files are generated in the output directory:
mol_parsed.npy: A boolean array indicating which input SMILES were successfully parsed by RDKit.mol_emb.npy: A 2D NumPy array of shape(N, D), whereNis the number of valid molecules andDis the embedding dimension.
Sources: examples/molecule_embeddings/README.md:46-53, examples/molecule_embeddings/main.py:91-100
Implementation Details
The featurization process is critical for compatibility with pre-trained models. The PretrainAtomFeaturizer and PretrainBondFeaturizer specifically encode attributes into integer indices that match the embedding lookup tables in the pre-trained GIN.
# Node featurization for pre-trained models
# Sources: [python/dgllife/utils/featurizers.py:696-705]
def __call__(self, mol):
atom_features = []
for i in range(mol.GetNumAtoms()):
atom = mol.GetAtomWithIdx(i)
atom_features.append([
self._atomic_number_types.index(atom.GetAtomicNum()),
self._chiral_types.index(atom.GetChiralTag())
])
atom_features = np.stack(atom_features)
# ... returns dict with atomic_number and chirality_type
The execution logic in main.py iterates through a DataLoader, extracting these features and feeding them into the model to produce the final catenated tensor.
Sources: examples/molecule_embeddings/main.py:72-89, python/dgllife/utils/featurizers.py:656-708
Conclusion
Molecule Embeddings in DGL-LifeSci provide a high-level API for generating neural fingerprints. By leveraging GIN architectures pre-trained on millions of molecules, the system enables users to transform chemical strings into expressive vectors suitable for machine learning workflows. The modular design supports multiple pre-training strategies and handles the heavy lifting of graph construction and featurization internally.
Sources: examples/molecule_embeddings/README.md:1-23, examples/README.md:65-70