# qhuang20/gflownet-seq-gen architecture

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

- Interactive diagram: https://gitdiagram.com/qhuang20/gflownet-seq-gen
- Repository: https://github.com/qhuang20/gflownet-seq-gen

## Overview

1. **What this repository is**
   - This is a research codebase for **GFlowNet-based RNA sequence generation**.
   - The main goal is to train models that generate sequences **proportional to a biological reward**, especially for the **LET-7 miRNA family across species**.

2. **Primary runtime and entry points**
   - The repo is **Python-first**, with experiments run through **notebooks** and one main training script: `train_LET7_22bp.py`.
   - The notebooks split the workflow into training, GPU training, and downstream analysis, so the architecture is centered on **experiment orchestration**, not on a long-running service.

3. **Core model subsystem: `gfn/`**
   - `gfn/` contains the reusable GFlowNet implementation.
   - `env.py` defines the **state space and sequence construction dynamics**.
   - `model.py` defines the **parameterized policies / networks** used for TB and DB variants.
   - `losses.py` implements the **GFlowNet objectives**.
   - `training.py` and `training_fast.py` are the two training loops: a standard episode-based loop and a **GPU-batched accelerated path**.

4. **Reward and objective boundary**
   - `gfn/reward.py` is the main boundary between the **biological problem definition** and the GFlowNet core.
   - Reward functions encode sequence-matching criteria such as:
     - Hamming-style target matching
     - entropy-weighted scoring
     - conservation-aware scoring
   - This separation lets the same training engine optimize different biological target formulations.

5. **Data assets and target generation**
   - `data/` holds both **static biological inputs** and **derived target sets**.
   - Inputs include FASTA files for LET-7 family sequences across species and subtree-specific sequences.
   - JSON and `.py` files in the same folder represent **precomputed target collections** and generation utilities, showing a pipeline from raw sequence data to training-ready targets.

6. **Data preparation pipeline**
   - Scripts like `generate_targets.py`, `generate_LET7_22bp_targets.py`, and `targets_10bp.py` suggest a preprocessing stage that:
     - extracts subsequences or motifs,
     - filters/normalizes targets,
     - serializes them into JSON for training.
   - This preprocessing is a distinct upstream layer from model training, and the trained system consumes these prepared target sets rather than raw phylogenetic data directly.

7. **Training flow**
   - Training begins with a target set and reward definition, then the GFlowNet learns a distribution over sequences that matches those rewards.
   - The main data flow is:
     - biological sequence targets → reward evaluation → GFlowNet sampling/update loop → learned sequence generator.
   - The code supports multiple objectives: **TB, DB, and FL-DB**, so the architecture includes objective-specific loss computation under a shared environment/model backbone.

8. **GPU scaling path**
   - `gfn/training_fast.py` and the GPU-oriented notebooks indicate a separate execution path for **batch-based accelerated training**.
   - This is important architecturally because the repo supports both:
     - smaller exploratory runs in notebooks, and
     - larger production-style experiments for LET-7 on GPU.

9. **Analysis and visualization**
   - `gfn/visualization.py` and the analysis notebooks are downstream consumers of training outputs.
   - They appear to inspect:
     - hit trajectories,
     - sequence generation quality,
     - phylogenetic coverage,
     - and search behavior such as greedy, stochastic, or beam-like generation.
   - This is a distinct post-training analysis layer rather than part of the learning loop itself.

10. **Phylogeny subsystem**
   - `phylogeny/` contains a notebook and a shell pipeline for phylogenetic analysis.
   - That subsystem is used to contextualize generated sequences against evolutionary relationships, supporting the “AncestorGFN” framing.
   - In an architecture diagram, this should be shown as an **auxiliary analysis branch** fed by sequence data and model outputs.

11. **Experiment organization**
   - The repo is structured around **reproducible experiment notebooks**:
     - toy 4bp training,
     - 10bp target training,
     - LET-7 22bp long runs,
     - conservation-focused variants.
   - These notebooks are not just documentation; they serve as the primary orchestration layer for switching between target sets, objectives, and training scales.

12. **Key architectural boundary**
   - The most important boundary is between:
     - **biological data + reward specification** on one side, and
     - **GFlowNet training/inference** on the other.
   - The outputs are not classifiers or predictors, but **generated RNA sequences and trajectories** sampled from a learned reward-proportional distribution.

Main entry points: [`gfn/training.py`](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/training.py) (Training loop), [`data/generate_targets.py`](https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/generate_targets.py) (Target builders), [`gfn/reward.py`](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/reward.py) (Reward rules), [`gfn/training_fast.py`](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/training_fast.py) (GPU trainer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_data["Data & targets"]
  node_node_data_raw["Raw sequences<br/>FASTA inputs"]
  node_node_data_unique["Unique species<br/>FASTA inputs"]
  node_node_data_targets10["10bp targets<br/>target set<br/>[targets_10bp.json]"]
  node_node_data_targets22["22bp targets<br/>target set"]
  node_node_data_p2b3["P2b3 set<br/>subtree targets<br/>[P2b3_targets.json]"]
  node_node_data_gen["Target builders<br/>prep scripts"]
end

subgraph group_group_gfn["GFlowNet core"]
  node_node_env["Sequence env<br/>state dynamics<br/>[env.py]"]
  node_node_reward["Reward rules<br/>biological reward<br/>[reward.py]"]
  node_node_model["Policies<br/>TB/DB nets<br/>[model.py]"]
  node_node_losses["Objectives<br/>GFN losses<br/>[losses.py]"]
  node_node_train["Training loop<br/>episode trainer<br/>[training.py]"]
  node_node_train_fast["GPU trainer<br/>batched trainer<br/>[training_fast.py]"]
  node_node_viz["Visual tools<br/>analysis helpers<br/>[visualization.py]"]
end

subgraph group_group_orchestration["Experiment orchestration"]
  node_node_main["LET7 train<br/>entry script<br/>[train_LET7_22bp.py]"]
  node_node_nb_train["Train notebooks<br/>orchestration<br/>[run_training.ipynb]"]
  node_node_nb_gpu["GPU notebooks<br/>orchestration"]
  node_node_nb_analysis["Analysis notebooks<br/>orchestration<br/>[run_analysis.ipynb]"]
end

subgraph group_group_phylogeny["Phylogeny analysis"]
  node_node_phylo["Phylogeny pipeline<br/>analysis pipeline"]
end

node_node_data_gen -->|"builds"| node_node_data_targets10
node_node_data_gen -->|"builds"| node_node_data_targets22
node_node_data_gen -->|"builds"| node_node_data_p2b3
node_node_data_raw -->|"inputs"| node_node_data_gen
node_node_data_unique -->|"inputs"| node_node_data_gen
node_node_data_targets10 -->|"drives"| node_node_reward
node_node_data_targets22 -->|"drives"| node_node_reward
node_node_data_p2b3 -->|"drives"| node_node_reward
node_node_reward -->|"optimizes"| node_node_losses
node_node_env -->|"conditions"| node_node_model
node_node_model -->|"trained by"| node_node_losses
node_node_env -->|"simulates"| node_node_train
node_node_reward -->|"scores"| node_node_train
node_node_losses -->|"updates"| node_node_train
node_node_train_fast -.->|"accelerates"| node_node_train
node_node_main -->|"runs"| node_node_train_fast
node_node_nb_train -->|"orchestrates"| node_node_train
node_node_nb_gpu -->|"orchestrates"| node_node_train_fast
node_node_nb_analysis -->|"consumes"| node_node_viz
node_node_train -->|"outputs"| node_node_viz
node_node_train_fast -->|"outputs"| node_node_viz
node_node_viz -.->|"contextualizes"| node_node_phylo
node_node_data_raw -.->|"context"| node_node_phylo
node_node_main -.->|"compares"| node_node_nb_analysis

click node_node_data_raw "https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/LET7_family_mature_ALLspecies.fa"
click node_node_data_unique "https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/LET7_family_mature_ALLspecies_UNIQUEspecies.fa"
click node_node_data_targets10 "https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/targets_10bp.json"
click node_node_data_targets22 "https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/LET7_22bp_targets.json"
click node_node_data_p2b3 "https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/P2b3_targets.json"
click node_node_data_gen "https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/generate_targets.py"
click node_node_env "https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/env.py"
click node_node_reward "https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/reward.py"
click node_node_model "https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/model.py"
click node_node_losses "https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/losses.py"
click node_node_train "https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/training.py"
click node_node_train_fast "https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/training_fast.py"
click node_node_viz "https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/visualization.py"
click node_node_main "https://github.com/qhuang20/gflownet-seq-gen/blob/main/train_LET7_22bp.py"
click node_node_nb_train "https://github.com/qhuang20/gflownet-seq-gen/blob/main/run_training.ipynb"
click node_node_nb_gpu "https://github.com/qhuang20/gflownet-seq-gen/blob/main/run_training_gpu.ipynb"
click node_node_nb_analysis "https://github.com/qhuang20/gflownet-seq-gen/blob/main/run_analysis.ipynb"
click node_node_phylo "https://github.com/qhuang20/gflownet-seq-gen/blob/main/phylogeny/phylogeny_pipeline.sh"

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_node_data_raw,node_node_data_unique,node_node_data_targets10,node_node_data_targets22,node_node_data_p2b3,node_node_data_gen toneBlue
class node_node_env,node_node_reward,node_node_model,node_node_losses,node_node_train,node_node_train_fast,node_node_viz toneAmber
class node_node_main,node_node_nb_train,node_node_nb_gpu,node_node_nb_analysis toneMint
class node_node_phylo toneRose
```

## Components

### Data & targets

Biological sequence inputs, derived target sets, and target-generation scripts.

- [Raw sequences](https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/LET7_family_mature_ALLspecies.fa): FASTA inputs · LET-7 family and subtree sequence corpora used as biological source material. · `data/LET7_family_mature_ALLspecies.fa`
- [Unique species](https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/LET7_family_mature_ALLspecies_UNIQUEspecies.fa): FASTA inputs · Deduplicated LET-7 mature sequences across species. · `data/LET7_family_mature_ALLspecies_UNIQUEspecies.fa`
- [10bp targets](https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/targets_10bp.json): target set · Prepared 10bp target collections used for smaller experiments. · `data/targets_10bp.json`
- [22bp targets](https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/LET7_22bp_targets.json): target set · Prepared LET-7 22bp target collections for the main long-run experiments. · `data/LET7_22bp_targets.json`
- [P2b3 set](https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/P2b3_targets.json): subtree targets · Subtree-derived sequences and targets for a phylogenetic branch experiment. · `data/P2b3_targets.json`
- [Target builders](https://github.com/qhuang20/gflownet-seq-gen/blob/main/data/generate_targets.py): prep scripts · Scripts that extract, filter, normalize, and serialize training targets from raw sequence data. · `data/generate_targets.py`

### GFlowNet core

Reusable environment, reward, model, objective, and training code.

- [Sequence env](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/env.py): state dynamics · Defines the sequence construction state space and transition rules. · `gfn/env.py`
- [Reward rules](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/reward.py): biological reward · Encodes target matching, entropy-aware, and conservation-aware reward functions. · `gfn/reward.py`
- [Policies](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/model.py): TB/DB nets · Parameterized policies and networks used by trajectory balance and detailed balance variants. · `gfn/model.py`
- [Objectives](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/losses.py): GFN losses · Implements training objectives for TB, DB, and FL-DB style optimization. · `gfn/losses.py`
- [Training loop](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/training.py): episode trainer · Standard training loop for iterative sequence sampling and parameter updates. · `gfn/training.py`
- [GPU trainer](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/training_fast.py): batched trainer · Accelerated GPU-oriented training path for larger experiments and higher throughput. · `gfn/training_fast.py`
- [Visual tools](https://github.com/qhuang20/gflownet-seq-gen/blob/main/gfn/visualization.py): analysis helpers · Visualization utilities for trajectories, generation quality, and search behavior. · `gfn/visualization.py`

### Experiment orchestration

Notebooks and scripts that configure training and analysis runs.

- [LET7 train](https://github.com/qhuang20/gflownet-seq-gen/blob/main/train_LET7_22bp.py): entry script · Main Python entry point for LET-7 22bp training runs. · `train_LET7_22bp.py`
- [Train notebooks](https://github.com/qhuang20/gflownet-seq-gen/blob/main/run_training.ipynb): orchestration · Notebook-based training workflows for toy, 10bp, and LET-7 variants. · `run_training.ipynb`
- [GPU notebooks](https://github.com/qhuang20/gflownet-seq-gen/blob/main/run_training_gpu.ipynb): orchestration · Notebook workflows for accelerated GPU training and long-running experiments. · `run_training_gpu.ipynb`
- [Analysis notebooks](https://github.com/qhuang20/gflownet-seq-gen/blob/main/run_analysis.ipynb): orchestration · Downstream notebooks for inspecting outputs, hits, and sequence design behavior. · `run_analysis.ipynb`

### Phylogeny analysis

Auxiliary evolutionary analysis pipeline for interpreting generated sequences.

- [Phylogeny pipeline](https://github.com/qhuang20/gflownet-seq-gen/blob/main/phylogeny/phylogeny_pipeline.sh): analysis pipeline · Shell and notebook pipeline for evolutionary context and AncestorGFN-style analysis. · `phylogeny/phylogeny_pipeline.sh`

## Connections

- Target builders → 10bp targets: builds · Generates curated 10bp target sets from raw inputs.
- Target builders → 22bp targets: builds · Generates LET-7 22bp targets used for training.
- Target builders → P2b3 set: builds · Produces subtree-specific targets and sequence collections.
- Raw sequences → Target builders: inputs · Raw FASTA sequence corpora feed preprocessing.
- Unique species → Target builders: inputs · Deduplicated species sequences are another preprocessing source.
- 10bp targets → Reward rules: drives · Target collections define reward matching objectives.
- 22bp targets → Reward rules: drives · Main LET-7 targets are scored by reward functions.
- P2b3 set → Reward rules: drives · Subtree targets inform conservation or branch-aware scoring.
- Reward rules → Objectives: optimizes · Reward evaluations are used inside GFlowNet objectives.
- Sequence env → Policies: conditions · Model policies act over the sequence construction environment.
- Policies → Objectives: trained by · Policy parameters are updated through GFlowNet losses.
- Sequence env → Training loop: simulates · Training loop repeatedly rolls out sequence trajectories.
- Reward rules → Training loop: scores · Training consumes biological rewards for sampled sequences.
- Objectives → Training loop: updates · Objectives produce gradients for the standard loop.
- GPU trainer → Training loop: accelerates · GPU path is a higher-throughput implementation of the same training flow.
- LET7 train → GPU trainer: runs · Main script launches the accelerated LET-7 training path.
- Train notebooks → Training loop: orchestrates · Notebook runs configure and launch standard experiments.
- GPU notebooks → GPU trainer: orchestrates · GPU notebooks drive accelerated long-run training.
- Analysis notebooks → Visual tools: consumes · Analysis notebooks render trajectories and sequence-quality summaries.
- Training loop → Visual tools: outputs · Training produces artifacts consumed by visualization utilities.
- GPU trainer → Visual tools: outputs · GPU training also feeds downstream inspection and plotting.
- Visual tools → Phylogeny pipeline: contextualizes · Visualization and outputs are interpreted with phylogenetic analysis.
- Raw sequences → Phylogeny pipeline: context · Raw sequence corpora support evolutionary interpretation.
- LET7 train → Analysis notebooks: compares · Main experiments are typically followed by notebook-based analysis.
