How coleygroup/desp works
1. What this repo is
DESP is a Python package for goal-constrained, bidirectional synthesis planning. The core runtime takes a target molecule plus a required starting-material set, then searches a reaction graph to find a route that satisfies that constraint.
2. Top-level package layout
The repository is split into three main areas:
- desp/ for the public API, search engine, and ML-based inference wrappers
- processing/ for dataset preparation and feature/template extraction from raw chemistry data
- desp/experiments/ for benchmarking and evaluation scripts
3. Public entry point
desp/DESP.py is the user-facing orchestrator. It exposes the DESP class used in the README, wiring together the search strategy, predictor modules, and route visualization. This is the boundary most users interact with directly.
4. Search engine and graph model
desp/search/ contains the actual planning logic:
- desp_search.py implements the bidirectional search loop
- data_structures/and_or_graph.py and desp_tree.py model the expanding synthesis graph/tree
- nodes.py defines the node types used during search
This layer is responsible for expanding candidate syntheses, checking goal satisfaction, enforcing depth/iteration limits, and storing the explored search graph.
5. Inference layer
desp/inference/ provides model-backed predictors used by search. These are thin wrappers that translate molecules/partial routes into model inputs and return ranked predictions. The search code depends on these predictors rather than on raw model classes directly.
6. Model families used by search
The inference stack includes several specialized predictors:
- forward_predictor.py for forward reaction/template prediction
- retro_predictor.py for one-step retrosynthesis
- retro_value.py for Retro*-style value estimation
- syn_dist_predictor.py for synthetic distance estimation
- building_block_predictor.py for identifying/retaining candidate starting materials
These modules support the different search modes mentioned in the README, such as f2e, f2f, retro, retro_sd, random, and bfs.
7. Neural model implementations
The actual PyTorch model code lives under desp/inference/models/. There are two major model subtrees:
- fwd_model/ for forward template prediction
- syn_value/ for synthetic distance / value modeling
There is also retro_mlp.py for retro/value-style multilayer perceptron components. The trained checkpoints are external artifacts loaded from /desp/data/.
8. Runtime data dependencies
The runtime is tightly coupled to precomputed chemistry assets:
- building-block fingerprints and indices
- template index-to-SMARTS mappings
- pretrained .pt checkpoints for each predictor
- benchmark target lists
These assets are not generated at runtime; they are installed separately and form the model/data boundary for inference.
9. Experiment and evaluation workflow
desp/experiments/ contains the benchmark harness. evaluate.sh is the entry script described in the README, while evaluate.py and parseargs.py handle benchmark selection and method dispatch. Results are written as text plus serialized search graphs, which means experiments preserve both metrics and the full explored route structure.
10. Offline data processing and training pipeline
processing/ is the preprocessing/training subsystem for reproducing models from raw data. The numbered scripts show a staged pipeline:
- process USPTO data
- test/extract templates
- generate forward reaction data
- extract forward-training examples
- extract synthetic-distance labels
- extract values
Supporting utilities in chem_utils.py, network.py, nodes.py, search.py, and utils.py handle chemistry-specific graph construction and data transformations.
11. External chemistry and ML stack
The architecture assumes a GPU-enabled PyTorch environment with CUDA, plus chemistry tooling for template extraction and fingerprinting. The repo also depends on precomputed molecular fingerprints and template vocabularies, so model inference is inseparable from the chemistry feature pipeline.
12. Main data flow
At a high level: raw chemistry data is processed offline into templates, fingerprints, and training sets; models are trained and checkpointed; at runtime, DESP loads those artifacts, ranks retrosynthetic/forward expansions, and drives a constrained bidirectional search over the synthesis graph until it finds a route or hits the search limits.
13. Outputs
The main output is a synthesis route plus the explored search graph. For experiments, those are saved to benchmark-specific result files; for interactive use, visualize_route emits DOT and PNG artifacts that represent the solved synthesis plan.
Main entry points: desp_search.py Search loop · DESP.py DESP · README.md Processing · retro_value.py Retro value
Components 23
Runtime planning
User-facing DESP orchestration, search, inference, and model-backed prediction
- DESP · User-facing entry point that wires search, predictors, and route visualization
desp/DESP.py - Search loop · Core planning loop that expands candidate syntheses and enforces limits
desp/search/desp_search.py - Route graph · Search-state structure representing explored synthesis routes
desp/search/data_structures/and_or_graph.py - Tree state · Tree representation used while growing and evaluating routes
desp/search/data_structures/desp_tree.py - Node types · Typed nodes used to represent molecules, reactions, and search state
desp/search/data_structures/nodes.py - Retro predictor · Ranks precursor sets for target molecules
desp/inference/retro_predictor.py - Forward predictor · Predicts reaction outcomes or templates for forward expansion
desp/inference/forward_predictor.py - Retro value · Synthetic-distance and value estimates used to guide search
desp/inference/retro_value.py - Syn distance · Predicts synthetic distance for route guidance
desp/inference/syn_dist_predictor.py - Building blocks · Identifies and retains candidate starting materials
desp/inference/building_block_predictor.py - Inference utils · Shared inference-side loading and preprocessing utilities
desp/inference/utils.py - Forward models · Neural implementation subtree for forward template prediction
desp/inference/models/fwd_model - Value models · Neural implementation subtree for synthetic-distance and value modeling
desp/inference/models/syn_value - Retro MLP · Shared multilayer perceptron components for retro and value-style models
desp/inference/models/retro_mlp.py - Pretrained assets · Checkpoints, fingerprints, template mappings, and benchmark targets loaded at runtime
Offline pipeline
Chemistry preprocessing and training-data extraction
- Processing · Staged chemistry preprocessing and training-data extraction workflow
processing/README.md - USPTO process · Initial chemistry-data cleaning and standardization stage
processing/01_process_uspto.py - Template extract · Template testing and extraction from processed reactions
processing/02_test_templates_bi.py - Forward data · Generates and extracts forward-reaction training examples
processing/03_get_fwd_rxns.py - SD/value labels · Builds synthetic-distance and value supervision targets
processing/05_extract_sd.py - Chem utils · Shared chemistry graph, fingerprint, and transformation utilities
processing/chem_utils.py
Benchmarks
Evaluation harness and result serialization
- Evaluate · Dispatches benchmark methods and writes result files and serialized graphs
desp/experiments/evaluate.py - Args parser · Parses benchmark and method selection for evaluation runs
desp/experiments/parseargs.py
Connections 40
- DESP → Search loop: runs · The public orchestrator invokes the planning engine
- DESP → Retro predictor: configures · The orchestrator wires in retrosynthesis prediction
- DESP → Forward predictor: configures · The orchestrator wires in forward prediction
- DESP → Retro value: configures · The orchestrator wires in value guidance
- DESP → Syn distance: configures · The orchestrator wires in synthetic-distance guidance
- DESP → Building blocks: configures · The orchestrator wires in starting-material checks
- DESP → Route graph: tracks · Route visualization and explored graph are produced from search state
- Search loop → Route graph: expands · Search builds and updates the and-or search graph
- Search loop → Tree state: maintains · The loop keeps a tree-shaped planning state
- Search loop → Node types: uses · Search manipulates typed synthesis nodes
- Search loop → Retro predictor: queries · Retrosynthesis predictions guide backward expansion
- Search loop → Forward predictor: queries · Forward predictions support forward expansion modes
- Search loop → Retro value: scores · Value estimates prioritize search
- Search loop → Syn distance: scores · Synthetic-distance estimates prioritize search
- Search loop → Building blocks: checks · Starting-material constraints are enforced against building blocks
- Retro predictor → Pretrained assets: loads · Predictors rely on pretrained checkpoints and chemistry assets
- Forward predictor → Forward models: wraps · Forward predictor delegates to the forward model implementation
- Retro value → Value models: wraps · Value predictor delegates to the value-model implementation
- Syn distance → Value models: wraps · Synthetic-distance prediction uses the value-model subtree
- Retro value → Retro MLP: uses · Retro/value predictors share MLP components
- Syn distance → Retro MLP: uses · Synthetic-distance modeling shares MLP components
- Retro predictor → Inference utils: uses · Shared inference helpers handle inputs and loading
- Forward predictor → Inference utils: uses · Shared inference helpers handle inputs and loading
- Retro value → Pretrained assets: loads · Value-guidance depends on pretrained weights and related assets
- Syn distance → Pretrained assets: loads · Distance guidance depends on pretrained weights and related assets
- Building blocks → Pretrained assets: loads · Building-block prediction depends on precomputed indices and fingerprints
- Evaluate → Args parser: parses · Benchmark script dispatch is driven by parsed arguments
- Evaluate → DESP: invokes · Evaluation runs the main DESP planner
- Processing → USPTO process: starts · The offline pipeline begins with raw USPTO processing
- Processing → Template extract: continues · The pipeline continues into template extraction
- Processing → Forward data: continues · The pipeline generates forward-training data
- Processing → SD/value labels: continues · The pipeline extracts synthetic-distance and value labels
- USPTO process → Chem utils: uses · Offline processing relies on chemistry utilities
- Template extract → Chem utils: uses · Template extraction relies on chemistry utilities
- Forward data → Chem utils: uses · Forward-data generation relies on chemistry utilities
- SD/value labels → Chem utils: uses · Label extraction relies on chemistry utilities
- USPTO process → Template extract: feeds · Processed reactions flow into template extraction
- Template extract → Forward data: feeds · Templates and reactions feed forward training data
- Forward data → SD/value labels: feeds · Training examples feed downstream supervision extraction
- Processing → Pretrained assets: produces · Offline pipeline produces checkpoints and chemistry assets