How sloppysloth37/molecularglues works
Purpose and Entry Point
This repository trains and evaluates a PyTorch binary classifier that predicts whether a molecule, represented as a SMILES string, is a molecular glue. The operational entry point is train.py, which orchestrates dataset preparation, model training, checkpoint selection, and final test evaluation. Hyperparameters, paths, and runtime settings are centralized in config.py.
Data Ingestion, Chemistry Boundaries, and Splitting
data.py reads the whitespace-delimited SMILES/label corpus at dataset.smi. RDKit is the chemistry-specific dependency used to derive Murcko scaffolds. Molecules are capped per scaffold and split by scaffold into train/validation/test partitions, preventing scaffold leakage across evaluation boundaries. Invalid or unsuitable SMILES handling belongs at this input boundary.
Key paths: data.py, dataset.smi, config.py.
Tokenization and Training Vocabulary
The data layer tokenizes SMILES into sequence tokens, applies augmentation as configured, pads/truncates to MAX_LEN, and constructs a vocabulary using training data only. This establishes the interface into the neural network: batched token IDs and padding/mask information, paired with binary labels.
Key paths: data.py, config.py.
Molecular Transformer Encoder
model.py defines the learned sequence encoder. Token embeddings feed repeated Transformer blocks with pre-layer normalization, multi-head self-attention, Rotary Positional Embeddings applied to attention Q/K projections, residual connections, dropout, and SwiGLU feed-forward layers. RoPE removes the need for learned absolute positional embeddings and makes positional encoding an internal attention concern.
Key paths: model.py, config.py.
Molecule Representation and Classification
The encoder output is converted from token-level states to one molecule embedding through attention pooling combined with a weighted mean-pooling contribution. An MLP classifier projects this representation to one binary logit. The model must expose both logits and representation embeddings because training combines classification and contrastive objectives.
Key paths: model.py, losses.py.
Loss Scheduling and Optimization
losses.py combines focal loss, for label imbalance, with supervised contrastive loss, for class-separable embedding geometry. Training starts with focal loss only; after warmup, contrastive weight ramps toward a configured maximum. trainer.py owns epoch execution, validation loss, optimizer/LR-scheduler progression, and best-model selection using validation MCC.
Key paths: losses.py, trainer.py, config.py.
Evaluation and Artifact Lifecycle
metrics.py obtains predictions and computes MCC, ROC-AUC, PR-AUC, Pearson/Spearman correlations, classification outputs, and confusion information. train.py saves the checkpoint with the strongest validation MCC to best_model.pt, reloads it for held-out scaffold test evaluation, and invokes the visualization hook. Plot generation is currently a stub.
Key paths: train.py, metrics.py, best_model.pt.
Runtime and External Dependencies
The main runtime is Python with PyTorch for model execution/training. RDKit provides scaffold chemistry; scikit-learn and SciPy support split/evaluation metrics; NumPy supports numerical processing. The repository is a local batch-training workflow: it has no indicated service API, database, distributed runtime, or web frontend.
Key paths: ReadME.md, train.py, MetricsVisuals.
Main entry points: losses.py Scheduled objectives · train.py Training entry point · data.py Dataset pipeline · config.py Runtime config
Components 15
Data preparation
Scaffold-aware molecular dataset pipeline.
- SMILES corpus · Whitespace-delimited SMILES and binary labels.
dataset.smi - Dataset pipeline · Validates SMILES, tokenizes, augments, and builds train-only vocabulary.
data.py - RDKit scaffolds · Derives Murcko scaffolds for leakage-resistant splits.
- Scaffold splits · Train, validation, and held-out test partitions capped per scaffold.
data.py - Token batches · Padded or truncated token IDs, masks, and labels.
data.py
Model and objectives
Transformer representation learning and binary prediction.
- Molecular Transformer · Pre-norm Transformer blocks with self-attention, RoPE, SwiGLU, residuals, and dropout.
model.py - Molecule pooling · Combines attention pooling with weighted mean pooling.
model.py - Binary classifier · Projects molecule embeddings to binary logits.
model.py - Scheduled objectives · Focal loss is joined by ramped supervised contrastive loss after warmup.
losses.py
Training lifecycle
Local batch training, selection, and held-out evaluation.
- Runtime config · Centralizes paths, hyperparameters, augmentation, and schedule settings.
config.py - Trainer · Runs epochs, optimizer and scheduler steps, validation, and MCC-based selection.
trainer.py - Metrics · Computes MCC, ROC-AUC, PR-AUC, correlations, and confusion outputs.
metrics.py - Training entry point · Orchestrates preparation, training, checkpoint reload, test evaluation, and visualization.
train.py - Best checkpoint · Checkpoint selected by strongest validation MCC.
best_model.pt - Visualization hook · Plot generation is currently a stub.
MetricsVisuals
Connections 20
- SMILES corpus → Dataset pipeline: read
- RDKit scaffolds → Dataset pipeline: derive scaffolds
- Dataset pipeline → Scaffold splits: split by scaffold
- Scaffold splits → Token batches: tokenize per partition
- Runtime config → Dataset pipeline: data settings
- Runtime config → Molecular Transformer: model settings
- Runtime config → Scheduled objectives: loss schedule
- Token batches → Molecular Transformer: token IDs and masks
- Molecular Transformer → Molecule pooling: token states
- Molecule pooling → Binary classifier: molecule embedding
- Binary classifier → Scheduled objectives: logits
- Molecule pooling → Scheduled objectives: embeddings
- Token batches → Scheduled objectives: labels
- Training entry point → Trainer: orchestrates
- Trainer → Scheduled objectives: optimize
- Trainer → Metrics: validate predictions
- Metrics → Best checkpoint: best validation MCC
- Best checkpoint → Training entry point: reload for test
- Training entry point → Metrics: held-out test evaluation
- Training entry point → Visualization hook: invoke