# sloppysloth37/molecularglues architecture

> Architecture diagram of the GitHub repository sloppysloth37/molecularglues, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sloppysloth37/molecularglues
- Repository: https://github.com/sloppysloth37/molecularglues

## Overview

#### 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`](https://github.com/sloppysloth37/molecularglues/blob/main/losses.py) (Scheduled objectives), [`train.py`](https://github.com/sloppysloth37/molecularglues/blob/main/train.py) (Training entry point), [`data.py`](https://github.com/sloppysloth37/molecularglues/blob/main/data.py) (Dataset pipeline), [`config.py`](https://github.com/sloppysloth37/molecularglues/blob/main/config.py) (Runtime config)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data preparation"]
  node_corpus[("SMILES corpus<br/>dataset<br/>[dataset.smi]")]
  node_data_pipeline["Dataset pipeline<br/>data module<br/>[data.py]"]
  node_rdkit{{"RDKit scaffolds<br/>chemistry dependency"}}
  node_scaffold_splits[("Scaffold splits<br/>data partitions<br/>[data.py]")]
  node_token_batches["Token batches<br/>model input<br/>[data.py]"]
end

subgraph group_model["Model and objectives"]
  node_transformer["Molecular Transformer<br/>sequence encoder<br/>[model.py]"]
  node_pooling["Molecule pooling<br/>representation layer<br/>[model.py]"]
  node_classifier["Binary classifier<br/>MLP head<br/>[model.py]"]
  node_losses["Scheduled objectives<br/>loss module<br/>[losses.py]"]
end

subgraph group_runtime["Training lifecycle"]
  node_config["Runtime config<br/>configuration module<br/>[config.py]"]
  node_trainer["Trainer<br/>training loop<br/>[trainer.py]"]
  node_metrics["Metrics<br/>evaluation module<br/>[metrics.py]"]
  node_train_entry["Training entry point<br/>Python entry point<br/>[train.py]"]
  node_checkpoint["Best checkpoint<br/>model artifact<br/>[best_model.pt]"]
  node_visualization["Visualization hook<br/>plot output"]
end

node_corpus -->|"read"| node_data_pipeline
node_rdkit -->|"derive scaffolds"| node_data_pipeline
node_data_pipeline -->|"split by scaffold"| node_scaffold_splits
node_scaffold_splits -->|"tokenize per partition"| node_token_batches
node_config -.->|"data settings"| node_data_pipeline
node_config -.->|"model settings"| node_transformer
node_config -.->|"loss schedule"| node_losses
node_token_batches -->|"token IDs and masks"| node_transformer
node_transformer -->|"token states"| node_pooling
node_pooling -->|"molecule embedding"| node_classifier
node_classifier -->|"logits"| node_losses
node_pooling -->|"embeddings"| node_losses
node_token_batches -->|"labels"| node_losses
node_train_entry -->|"orchestrates"| node_trainer
node_trainer -->|"optimize"| node_losses
node_trainer -->|"validate predictions"| node_metrics
node_metrics -->|"best validation MCC"| node_checkpoint
node_checkpoint -->|"reload for test"| node_train_entry
node_train_entry -->|"held-out test evaluation"| node_metrics
node_train_entry -->|"invoke"| node_visualization

click node_corpus "https://github.com/sloppysloth37/molecularglues/blob/main/dataset.smi"
click node_data_pipeline "https://github.com/sloppysloth37/molecularglues/blob/main/data.py"
click node_scaffold_splits "https://github.com/sloppysloth37/molecularglues/blob/main/data.py"
click node_token_batches "https://github.com/sloppysloth37/molecularglues/blob/main/data.py"
click node_config "https://github.com/sloppysloth37/molecularglues/blob/main/config.py"
click node_transformer "https://github.com/sloppysloth37/molecularglues/blob/main/model.py"
click node_pooling "https://github.com/sloppysloth37/molecularglues/blob/main/model.py"
click node_classifier "https://github.com/sloppysloth37/molecularglues/blob/main/model.py"
click node_losses "https://github.com/sloppysloth37/molecularglues/blob/main/losses.py"
click node_trainer "https://github.com/sloppysloth37/molecularglues/blob/main/trainer.py"
click node_metrics "https://github.com/sloppysloth37/molecularglues/blob/main/metrics.py"
click node_train_entry "https://github.com/sloppysloth37/molecularglues/blob/main/train.py"
click node_checkpoint "https://github.com/sloppysloth37/molecularglues/blob/main/best_model.pt"
click node_visualization "https://github.com/sloppysloth37/molecularglues/tree/main/MetricsVisuals"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_corpus,node_data_pipeline,node_rdkit,node_scaffold_splits,node_token_batches toneBlue
class node_transformer,node_pooling,node_classifier,node_losses toneAmber
class node_config,node_trainer,node_metrics,node_train_entry,node_checkpoint,node_visualization toneMint
```

## Components

### Data preparation

Scaffold-aware molecular dataset pipeline.

- [SMILES corpus](https://github.com/sloppysloth37/molecularglues/blob/main/dataset.smi): dataset · Whitespace-delimited SMILES and binary labels. · `dataset.smi`
- [Dataset pipeline](https://github.com/sloppysloth37/molecularglues/blob/main/data.py): data module · Validates SMILES, tokenizes, augments, and builds train-only vocabulary. · `data.py`
- **RDKit scaffolds**: chemistry dependency · Derives Murcko scaffolds for leakage-resistant splits.
- [Scaffold splits](https://github.com/sloppysloth37/molecularglues/blob/main/data.py): data partitions · Train, validation, and held-out test partitions capped per scaffold. · `data.py`
- [Token batches](https://github.com/sloppysloth37/molecularglues/blob/main/data.py): model input · Padded or truncated token IDs, masks, and labels. · `data.py`

### Model and objectives

Transformer representation learning and binary prediction.

- [Molecular Transformer](https://github.com/sloppysloth37/molecularglues/blob/main/model.py): sequence encoder · Pre-norm Transformer blocks with self-attention, RoPE, SwiGLU, residuals, and dropout. · `model.py`
- [Molecule pooling](https://github.com/sloppysloth37/molecularglues/blob/main/model.py): representation layer · Combines attention pooling with weighted mean pooling. · `model.py`
- [Binary classifier](https://github.com/sloppysloth37/molecularglues/blob/main/model.py): MLP head · Projects molecule embeddings to binary logits. · `model.py`
- [Scheduled objectives](https://github.com/sloppysloth37/molecularglues/blob/main/losses.py): loss module · 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](https://github.com/sloppysloth37/molecularglues/blob/main/config.py): configuration module · Centralizes paths, hyperparameters, augmentation, and schedule settings. · `config.py`
- [Trainer](https://github.com/sloppysloth37/molecularglues/blob/main/trainer.py): training loop · Runs epochs, optimizer and scheduler steps, validation, and MCC-based selection. · `trainer.py`
- [Metrics](https://github.com/sloppysloth37/molecularglues/blob/main/metrics.py): evaluation module · Computes MCC, ROC-AUC, PR-AUC, correlations, and confusion outputs. · `metrics.py`
- [Training entry point](https://github.com/sloppysloth37/molecularglues/blob/main/train.py): Python entry point · Orchestrates preparation, training, checkpoint reload, test evaluation, and visualization. · `train.py`
- [Best checkpoint](https://github.com/sloppysloth37/molecularglues/blob/main/best_model.pt): model artifact · Checkpoint selected by strongest validation MCC. · `best_model.pt`
- [Visualization hook](https://github.com/sloppysloth37/molecularglues/tree/main/MetricsVisuals): plot output · Plot generation is currently a stub. · `MetricsVisuals`

## Connections

- 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
