# coleygroup/desp architecture

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

- Interactive diagram: https://gitdiagram.com/coleygroup/desp
- Repository: https://github.com/coleygroup/desp

## Overview

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/desp_search.py`](https://github.com/coleygroup/desp/blob/main/desp/search/desp_search.py) (Search loop), [`desp/DESP.py`](https://github.com/coleygroup/desp/blob/main/desp/DESP.py) (DESP), [`processing/README.md`](https://github.com/coleygroup/desp/blob/main/processing/README.md) (Processing), [`desp/inference/retro_value.py`](https://github.com/coleygroup/desp/blob/main/desp/inference/retro_value.py) (Retro value)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime planning"]
  node_desp_api["DESP<br/>orchestrator<br/>[DESP.py]"]
  node_search_loop["Search loop<br/>bidirectional search<br/>[desp_search.py]"]
  node_graph_model["Route graph<br/>and-or graph<br/>[and_or_graph.py]"]
  node_tree_state["Tree state<br/>search tree<br/>[desp_tree.py]"]
  node_node_types["Node types<br/>search nodes<br/>[nodes.py]"]
  node_retro_pred["Retro predictor<br/>one-step retrosynthesis<br/>[retro_predictor.py]"]
  node_fwd_pred["Forward predictor<br/>template forward model"]
  node_retro_value["Retro value<br/>value scoring<br/>[retro_value.py]"]
  node_sd_pred["Syn distance<br/>distance predictor"]
  node_bb_pred["Building blocks<br/>starting-material filter"]
  node_inference_utils["Inference utils<br/>runtime helpers<br/>[utils.py]"]
  node_fwd_models["Forward models<br/>PyTorch models"]
  node_value_models["Value models<br/>PyTorch models"]
  node_retro_mlp["Retro MLP<br/>MLP component<br/>[retro_mlp.py]"]
  node_artifacts[("Pretrained assets<br/>external data")]
end

subgraph group_processing["Offline pipeline"]
  node_processing_flow["Processing<br/>offline pipeline<br/>[README.md]"]
  node_process_uspto["USPTO process<br/>raw data prep"]
  node_template_extract["Template extract<br/>template pipeline"]
  node_fwd_data["Forward data<br/>training data<br/>[03_get_fwd_rxns.py]"]
  node_sd_value_data["SD/value labels<br/>label extraction<br/>[05_extract_sd.py]"]
  node_chem_utils["Chem utils<br/>chemistry helpers<br/>[chem_utils.py]"]
end

subgraph group_experiments["Benchmarks"]
  node_eval_harness["Evaluate<br/>benchmark runner<br/>[evaluate.py]"]
  node_eval_args["Args parser<br/>cli dispatch<br/>[parseargs.py]"]
end

node_desp_api -->|"runs"| node_search_loop
node_desp_api -->|"configures"| node_retro_pred
node_desp_api -->|"configures"| node_fwd_pred
node_desp_api -->|"configures"| node_retro_value
node_desp_api -->|"configures"| node_sd_pred
node_desp_api -->|"configures"| node_bb_pred
node_desp_api -->|"tracks"| node_graph_model
node_search_loop -->|"expands"| node_graph_model
node_search_loop -->|"maintains"| node_tree_state
node_search_loop -->|"uses"| node_node_types
node_search_loop -->|"queries"| node_retro_pred
node_search_loop -->|"queries"| node_fwd_pred
node_search_loop -->|"scores"| node_retro_value
node_search_loop -->|"scores"| node_sd_pred
node_search_loop -->|"checks"| node_bb_pred
node_retro_pred -->|"loads"| node_artifacts
node_fwd_pred -->|"wraps"| node_fwd_models
node_retro_value -->|"wraps"| node_value_models
node_sd_pred -->|"wraps"| node_value_models
node_retro_value -->|"uses"| node_retro_mlp
node_sd_pred -->|"uses"| node_retro_mlp
node_retro_pred -->|"uses"| node_inference_utils
node_fwd_pred -->|"uses"| node_inference_utils
node_retro_value -->|"loads"| node_artifacts
node_sd_pred -->|"loads"| node_artifacts
node_bb_pred -->|"loads"| node_artifacts
node_eval_harness -->|"parses"| node_eval_args
node_eval_harness -->|"invokes"| node_desp_api
node_processing_flow -->|"starts"| node_process_uspto
node_processing_flow -->|"continues"| node_template_extract
node_processing_flow -->|"continues"| node_fwd_data
node_processing_flow -->|"continues"| node_sd_value_data
node_process_uspto -->|"uses"| node_chem_utils
node_template_extract -->|"uses"| node_chem_utils
node_fwd_data -->|"uses"| node_chem_utils
node_sd_value_data -->|"uses"| node_chem_utils
node_process_uspto -->|"feeds"| node_template_extract
node_template_extract -->|"feeds"| node_fwd_data
node_fwd_data -->|"feeds"| node_sd_value_data
node_processing_flow -.->|"produces"| node_artifacts

click node_desp_api "https://github.com/coleygroup/desp/blob/main/desp/DESP.py"
click node_search_loop "https://github.com/coleygroup/desp/blob/main/desp/search/desp_search.py"
click node_graph_model "https://github.com/coleygroup/desp/blob/main/desp/search/data_structures/and_or_graph.py"
click node_tree_state "https://github.com/coleygroup/desp/blob/main/desp/search/data_structures/desp_tree.py"
click node_node_types "https://github.com/coleygroup/desp/blob/main/desp/search/data_structures/nodes.py"
click node_retro_pred "https://github.com/coleygroup/desp/blob/main/desp/inference/retro_predictor.py"
click node_fwd_pred "https://github.com/coleygroup/desp/blob/main/desp/inference/forward_predictor.py"
click node_retro_value "https://github.com/coleygroup/desp/blob/main/desp/inference/retro_value.py"
click node_sd_pred "https://github.com/coleygroup/desp/blob/main/desp/inference/syn_dist_predictor.py"
click node_bb_pred "https://github.com/coleygroup/desp/blob/main/desp/inference/building_block_predictor.py"
click node_inference_utils "https://github.com/coleygroup/desp/blob/main/desp/inference/utils.py"
click node_fwd_models "https://github.com/coleygroup/desp/tree/main/desp/inference/models/fwd_model"
click node_value_models "https://github.com/coleygroup/desp/tree/main/desp/inference/models/syn_value"
click node_retro_mlp "https://github.com/coleygroup/desp/blob/main/desp/inference/models/retro_mlp.py"
click node_eval_harness "https://github.com/coleygroup/desp/blob/main/desp/experiments/evaluate.py"
click node_eval_args "https://github.com/coleygroup/desp/blob/main/desp/experiments/parseargs.py"
click node_processing_flow "https://github.com/coleygroup/desp/blob/main/processing/README.md"
click node_process_uspto "https://github.com/coleygroup/desp/blob/main/processing/01_process_uspto.py"
click node_template_extract "https://github.com/coleygroup/desp/blob/main/processing/02_test_templates_bi.py"
click node_fwd_data "https://github.com/coleygroup/desp/blob/main/processing/03_get_fwd_rxns.py"
click node_sd_value_data "https://github.com/coleygroup/desp/blob/main/processing/05_extract_sd.py"
click node_chem_utils "https://github.com/coleygroup/desp/blob/main/processing/chem_utils.py"

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_desp_api,node_search_loop,node_graph_model,node_tree_state,node_node_types,node_retro_pred,node_fwd_pred,node_retro_value,node_sd_pred,node_bb_pred,node_inference_utils,node_fwd_models,node_value_models,node_retro_mlp,node_artifacts toneBlue
class node_processing_flow,node_process_uspto,node_template_extract,node_fwd_data,node_sd_value_data,node_chem_utils toneAmber
class node_eval_harness,node_eval_args toneMint
```

## Components

### Runtime planning

User-facing DESP orchestration, search, inference, and model-backed prediction

- [DESP](https://github.com/coleygroup/desp/blob/main/desp/DESP.py): orchestrator · User-facing entry point that wires search, predictors, and route visualization · `desp/DESP.py`
- [Search loop](https://github.com/coleygroup/desp/blob/main/desp/search/desp_search.py): bidirectional search · Core planning loop that expands candidate syntheses and enforces limits · `desp/search/desp_search.py`
- [Route graph](https://github.com/coleygroup/desp/blob/main/desp/search/data_structures/and_or_graph.py): and-or graph · Search-state structure representing explored synthesis routes · `desp/search/data_structures/and_or_graph.py`
- [Tree state](https://github.com/coleygroup/desp/blob/main/desp/search/data_structures/desp_tree.py): search tree · Tree representation used while growing and evaluating routes · `desp/search/data_structures/desp_tree.py`
- [Node types](https://github.com/coleygroup/desp/blob/main/desp/search/data_structures/nodes.py): search nodes · Typed nodes used to represent molecules, reactions, and search state · `desp/search/data_structures/nodes.py`
- [Retro predictor](https://github.com/coleygroup/desp/blob/main/desp/inference/retro_predictor.py): one-step retrosynthesis · Ranks precursor sets for target molecules · `desp/inference/retro_predictor.py`
- [Forward predictor](https://github.com/coleygroup/desp/blob/main/desp/inference/forward_predictor.py): template forward model · Predicts reaction outcomes or templates for forward expansion · `desp/inference/forward_predictor.py`
- [Retro value](https://github.com/coleygroup/desp/blob/main/desp/inference/retro_value.py): value scoring · Synthetic-distance and value estimates used to guide search · `desp/inference/retro_value.py`
- [Syn distance](https://github.com/coleygroup/desp/blob/main/desp/inference/syn_dist_predictor.py): distance predictor · Predicts synthetic distance for route guidance · `desp/inference/syn_dist_predictor.py`
- [Building blocks](https://github.com/coleygroup/desp/blob/main/desp/inference/building_block_predictor.py): starting-material filter · Identifies and retains candidate starting materials · `desp/inference/building_block_predictor.py`
- [Inference utils](https://github.com/coleygroup/desp/blob/main/desp/inference/utils.py): runtime helpers · Shared inference-side loading and preprocessing utilities · `desp/inference/utils.py`
- [Forward models](https://github.com/coleygroup/desp/tree/main/desp/inference/models/fwd_model): PyTorch models · Neural implementation subtree for forward template prediction · `desp/inference/models/fwd_model`
- [Value models](https://github.com/coleygroup/desp/tree/main/desp/inference/models/syn_value): PyTorch models · Neural implementation subtree for synthetic-distance and value modeling · `desp/inference/models/syn_value`
- [Retro MLP](https://github.com/coleygroup/desp/blob/main/desp/inference/models/retro_mlp.py): MLP component · Shared multilayer perceptron components for retro and value-style models · `desp/inference/models/retro_mlp.py`
- **Pretrained assets**: external data · Checkpoints, fingerprints, template mappings, and benchmark targets loaded at runtime

### Offline pipeline

Chemistry preprocessing and training-data extraction

- [Processing](https://github.com/coleygroup/desp/blob/main/processing/README.md): offline pipeline · Staged chemistry preprocessing and training-data extraction workflow · `processing/README.md`
- [USPTO process](https://github.com/coleygroup/desp/blob/main/processing/01_process_uspto.py): raw data prep · Initial chemistry-data cleaning and standardization stage · `processing/01_process_uspto.py`
- [Template extract](https://github.com/coleygroup/desp/blob/main/processing/02_test_templates_bi.py): template pipeline · Template testing and extraction from processed reactions · `processing/02_test_templates_bi.py`
- [Forward data](https://github.com/coleygroup/desp/blob/main/processing/03_get_fwd_rxns.py): training data · Generates and extracts forward-reaction training examples · `processing/03_get_fwd_rxns.py`
- [SD/value labels](https://github.com/coleygroup/desp/blob/main/processing/05_extract_sd.py): label extraction · Builds synthetic-distance and value supervision targets · `processing/05_extract_sd.py`
- [Chem utils](https://github.com/coleygroup/desp/blob/main/processing/chem_utils.py): chemistry helpers · Shared chemistry graph, fingerprint, and transformation utilities · `processing/chem_utils.py`

### Benchmarks

Evaluation harness and result serialization

- [Evaluate](https://github.com/coleygroup/desp/blob/main/desp/experiments/evaluate.py): benchmark runner · Dispatches benchmark methods and writes result files and serialized graphs · `desp/experiments/evaluate.py`
- [Args parser](https://github.com/coleygroup/desp/blob/main/desp/experiments/parseargs.py): cli dispatch · Parses benchmark and method selection for evaluation runs · `desp/experiments/parseargs.py`

## Connections

- 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
