# aertslab/deepscenic architecture

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

- Interactive diagram: https://gitdiagram.com/aertslab/deepscenic
- Repository: https://github.com/aertslab/deepscenic

## Overview

1. **What this repository is**
   - `deepSCENIC` is a Python package for building single-cell gene regulatory network models.
   - Its core architectural boundary is a **multimodal ML pipeline** that combines **scRNA-seq**, **scATAC-seq**, and **DNA sequence** to infer **TF → region → gene** regulation.

2. **Primary public API shape**
   - The package follows a **scanpy-style interface** with three top-level user-facing modules:
     - `ds.pp` for preprocessing
     - `ds.tl` for modeling / inference / analysis
     - `ds.pl` for plotting and diagnostics
   - This is important for diagramming because most user workflows enter through these three layers rather than the low-level internals.

3. **Data model boundary**
   - The repo is built around **AnnData / MuData-style objects** for storing multimodal single-cell data and annotations.
   - Preprocessing functions enrich these containers with TF labels, gene annotations, split masks, and region-to-gene priors.
   - Tests and examples use `.h5ad` files, so file I/O is clearly centered on standard scverse data formats.

4. **Preprocessing subsystem (`src/deepscenic/pp`)**
   - `pp/basic.py` and `pp/search_space.py` handle data preparation.
   - This stage is responsible for:
     - creating multimodal objects
     - marking transcription factors
     - attaching gene annotations / chromosome metadata
     - computing region-to-gene penalty or search-space constraints
     - splitting cells and features for train/test evaluation, including chromosome-based splits
   - Architecturally, this is the bridge between raw datasets and the trainable model.

5. **Reference data and genome integration**
   - `src/deepscenic/_genome.py`, `_datasets.py`, `_data.py`, and `_io.py` form the data/reference layer.
   - They appear to manage:
     - genome registration from FASTA
     - gene and TF reference lookup
     - dataset loading and serialization
     - schema/type definitions for internal data structures
   - The repository’s sequence component depends on a local genome reference, so the genome is a first-class external input to the system.

6. **Modeling subsystem (`src/deepscenic/models`)**
   - The model package contains the neural network building blocks:
     - `_encoder.py`
     - `_decoder.py`
     - `_layers.py`
     - `_motifnet.py`
     - `_vae.py`
   - This suggests a **VAE-centered architecture** with separate encoder/decoder paths and a motif/sequence-aware component.
   - The motif network is likely the point where sequence-derived features are integrated with regulatory inference.

7. **Training and execution subsystem (`src/deepscenic/tl`)**
   - `tl` is the main computational orchestration layer.
   - Key responsibilities are split across:
     - `_dataloaders.py` for batching and sample access
     - `_model.py` and `_loss.py` for training logic and optimization objective
     - `_train.py` for the training loop
     - `_training_state.py` for checkpoints / phase state
     - `_logging.py` for run tracking
     - `_inference.py` for evaluation or prediction paths
   - The README’s staged workflow reflects this design: initial training, then repeated `finetune_r2g` phases.

8. **Regulatory network inference**
   - `tl/_grn.py` extracts gene regulatory networks from the trained model and dataset.
   - This is the system’s main analytical output: a GRN structure that can be queried per TF or visualized as a network/heatmap.
   - The implied data flow is: trained multimodal model → regulatory scores / edges → GRN object or table → downstream analysis.

9. **Perturbation analysis**
   - `tl/_perturbation.py` simulates TF knockdown or overexpression.
   - This is a separate analytical path from GRN extraction, but it depends on the trained model and the same biological annotations.
   - In architecture terms, it is a “what-if” inference layer built on top of the learned regulatory model.

10. **Visualization subsystem (`src/deepscenic/pl`)**
   - Plotting is organized by analysis domain:
     - training diagnostics
     - embedding utilities
     - genomics views
     - GRN plots
     - perturbation plots
     - sequence interpretation plots
   - Notable visualization areas include:
     - GRN heatmaps, network plots, cell-type views
     - perturbation volcano/co-embedding/heatmaps
     - sequence logos and in-silico mutagenesis outputs
     - genome browser / arc-style genomics views
   - This layer consumes outputs from `tl` and presents them for interpretation rather than computation.

11. **Sequence and external model integration**
   - The README says the default sequence model is **Enformer** via `enformer-pytorch`, with an optional extra install.
   - That makes sequence embedding an **external model dependency** rather than an internally implemented architecture.
   - The runtime therefore has a notable boundary between:
     - local single-cell/regulatory modeling
     - external pretrained sequence representation generation

12. **Supporting assets and quality boundary**
   - `docs/` contains tutorials, API docs, architecture notes, and notebooks that mirror the end-to-end workflow.
   - `tests/` includes integration tests and fixture data for genome, RNA/ATAC matrices, and masks, indicating the repo is organized around reproducible pipeline validation.
   - For an architecture diagram, these are supporting layers around the core pipeline, not runtime dependencies.

13. **End-to-end data flow**
   - Typical flow is:
     - load scRNA/scATAC data
     - create MuData
     - attach TF lists, gene annotations, and genome references
     - compute region-to-gene constraints and train/test splits
     - train the multimodal model
     - finetune region-to-gene relationships
     - extract GRNs or simulate perturbations
     - visualize results through the plotting layer
   - The core architectural shape is therefore a **data-enrichment pipeline feeding a multimodal neural network, followed by inference/interpretation modules**.

Main entry points: [`src/deepscenic/tl/__init__.py`](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/__init__.py) (tl), [`src/deepscenic/models/_vae.py`](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_vae.py) (VAE), [`src/deepscenic/pp/basic.py`](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pp/basic.py) (Prep core), [`src/deepscenic/__init__.py`](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/__init__.py) (deepscenic)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public_api["Public API"]
  node_init["deepscenic<br/>[__init__.py]"]
  node_pp["pp<br/>preprocess API<br/>[__init__.py]"]
  node_tl["tl<br/>tools API<br/>[__init__.py]"]
  node_pl["pl<br/>[__init__.py]"]
end

subgraph group_data_ref["Data & Reference"]
  node_data["Data model<br/>schema<br/>[_data.py]"]
  node_types["Types<br/>type defs<br/>[_types.py]"]
  node_datasets["Datasets<br/>loaders<br/>[_datasets.py]"]
  node_io["I/O<br/>serialization<br/>[_io.py]"]
  node_genome["Genome<br/>reference data<br/>[_genome.py]"]
  node_pp_basic["Prep core<br/>preprocessing<br/>[basic.py]"]
  node_pp_search["Search space<br/>r2g constraints<br/>[search_space.py]"]
end

subgraph group_modeling["Modeling"]
  node_enc["Encoder<br/>network block<br/>[_encoder.py]"]
  node_dec["Decoder<br/>network block<br/>[_decoder.py]"]
  node_motif["Motif net<br/>sequence module<br/>[_motifnet.py]"]
  node_vae["VAE<br/>model core<br/>[_vae.py]"]
  node_layers["Layers<br/>nn utils<br/>[_layers.py]"]
end

subgraph group_execution["Training & Inference"]
  node_dataloaders["Loaders<br/>batching<br/>[_dataloaders.py]"]
  node_loss["Loss<br/>objective<br/>[_loss.py]"]
  node_train["Train loop<br/>orchestration<br/>[_train.py]"]
  node_state["State<br/>checkpointing<br/>[_training_state.py]"]
  node_model_api["Model API<br/>training facade<br/>[_model.py]"]
  node_inference["Inference<br/>prediction<br/>[_inference.py]"]
  node_grn["GRN<br/>regulatory inference<br/>[_grn.py]"]
  node_perturb["Perturbation<br/>what-if analysis<br/>[_perturbation.py]"]
  node_log["Logging<br/>run tracking<br/>[_logging.py]"]
end

subgraph group_visualization["Visualization"]
  node_train_plot["Training plots<br/>diagnostics<br/>[_diagnostics.py]"]
  node_embed_plot["Embedding plots<br/>embedding viz<br/>[_embedding.py]"]
  node_grn_plot["GRN plots<br/>network viz<br/>[_network.py]"]
  node_geno_plot["Genome views<br/>genomics viz<br/>[_browser.py]"]
  node_pert_plot["Perturbation plots<br/>analysis viz<br/>[_volcano.py]"]
  node_seq_plot["Sequence plots<br/>sequence viz<br/>[_logo.py]"]
end

node_init -->|"exports"| node_pp
node_init -->|"exports"| node_tl
node_init -->|"exports"| node_pl
node_pp -->|"prepare"| node_pp_basic
node_pp -->|"constraints"| node_pp_search
node_pp_basic -->|"uses"| node_data
node_pp_basic -->|"annotates"| node_genome
node_pp_search -->|"uses"| node_genome
node_datasets -->|"loads"| node_io
node_io -->|"persists"| node_data
node_genome -->|"defines"| node_types
node_vae -->|"contains"| node_enc
node_vae -->|"contains"| node_dec
node_vae -->|"contains"| node_motif
node_vae -->|"builds on"| node_layers
node_tl -->|"feeds"| node_dataloaders
node_tl -->|"orchestrates"| node_model_api
node_model_api -->|"instantiates"| node_vae
node_tl -->|"optimizes"| node_loss
node_tl -->|"runs"| node_train
node_train -->|"updates"| node_state
node_tl -->|"predicts"| node_inference
node_tl -->|"extracts"| node_grn
node_tl -->|"simulates"| node_perturb
node_tl -->|"records"| node_log
node_grn -->|"visualized by"| node_grn_plot
node_perturb -->|"visualized by"| node_pert_plot
node_train -->|"visualized by"| node_train_plot
node_inference -->|"visualized by"| node_embed_plot
node_grn -->|"contextualized by"| node_geno_plot
node_motif -->|"interpreted by"| node_seq_plot
node_pp_basic -->|"prepares"| node_tl

click node_init "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/__init__.py"
click node_pp "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pp/__init__.py"
click node_tl "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/__init__.py"
click node_pl "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/__init__.py"
click node_data "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_data.py"
click node_types "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_types.py"
click node_datasets "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_datasets.py"
click node_io "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_io.py"
click node_genome "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_genome.py"
click node_pp_basic "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pp/basic.py"
click node_pp_search "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pp/search_space.py"
click node_enc "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_encoder.py"
click node_dec "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_decoder.py"
click node_motif "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_motifnet.py"
click node_vae "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_vae.py"
click node_layers "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_layers.py"
click node_dataloaders "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_dataloaders.py"
click node_loss "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_loss.py"
click node_train "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_train.py"
click node_state "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_training_state.py"
click node_model_api "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_model.py"
click node_inference "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_inference.py"
click node_grn "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_grn.py"
click node_perturb "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_perturbation.py"
click node_log "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_logging.py"
click node_train_plot "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/training/_diagnostics.py"
click node_embed_plot "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/_embedding.py"
click node_grn_plot "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/grn/_network.py"
click node_geno_plot "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/genomics/_browser.py"
click node_pert_plot "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/perturbation/_volcano.py"
click node_seq_plot "https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/sequence/_logo.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_init,node_pp,node_tl,node_pl toneBlue
class node_data,node_types,node_datasets,node_io,node_genome,node_pp_basic,node_pp_search toneAmber
class node_enc,node_dec,node_motif,node_vae,node_layers toneMint
class node_dataloaders,node_loss,node_train,node_state,node_model_api,node_inference,node_grn,node_perturb,node_log toneRose
class node_train_plot,node_embed_plot,node_grn_plot,node_geno_plot,node_pert_plot,node_seq_plot toneIndigo
```

## Components

### Public API

User-facing scanpy-style entry points

- [deepscenic](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/__init__.py): package · Top-level package namespace and public exports · `src/deepscenic/__init__.py`
- [pp](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pp/__init__.py): preprocess API · Public preprocessing entry points · `src/deepscenic/pp/__init__.py`
- [tl](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/__init__.py): tools API · Public modeling and analysis entry points · `src/deepscenic/tl/__init__.py`
- [pl](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/__init__.py): plot API · Public plotting entry points · `src/deepscenic/pl/__init__.py`

### Data & Reference

Containers, I/O, datasets, and genome registration

- [Data model](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_data.py): schema · Internal data structures and multimodal containers · `src/deepscenic/_data.py`
- [Types](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_types.py): type defs · Shared internal type definitions · `src/deepscenic/_types.py`
- [Datasets](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_datasets.py): loaders · Dataset access and packaging helpers · `src/deepscenic/_datasets.py`
- [I/O](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_io.py): serialization · Persistence and file handling · `src/deepscenic/_io.py`
- [Genome](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/_genome.py): reference data · Genome registration and sequence reference handling · `src/deepscenic/_genome.py`
- [Prep core](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pp/basic.py): preprocessing · Multimodal data preparation and annotation enrichment · `src/deepscenic/pp/basic.py`
- [Search space](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pp/search_space.py): r2g constraints · Region-to-gene priors, penalties, and split logic · `src/deepscenic/pp/search_space.py`

### Modeling

Neural network building blocks

- [Encoder](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_encoder.py): network block · Latent encoding path for multimodal inputs · `src/deepscenic/models/_encoder.py`
- [Decoder](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_decoder.py): network block · Reconstruction and output path · `src/deepscenic/models/_decoder.py`
- [Motif net](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_motifnet.py): sequence module · Sequence-aware regulatory feature integration · `src/deepscenic/models/_motifnet.py`
- [VAE](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_vae.py): model core · Central variational model combining encoder, decoder, and motif path · `src/deepscenic/models/_vae.py`
- [Layers](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/models/_layers.py): nn utils · Shared neural network layers and primitives · `src/deepscenic/models/_layers.py`

### Training & Inference

Orchestration, learning, and downstream analysis

- [Loaders](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_dataloaders.py): batching · Training batch construction and sample access · `src/deepscenic/tl/_dataloaders.py`
- [Loss](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_loss.py): objective · Training objective and optimization terms · `src/deepscenic/tl/_loss.py`
- [Train loop](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_train.py): orchestration · Fit/finetune execution loop · `src/deepscenic/tl/_train.py`
- [State](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_training_state.py): checkpointing · Training phase and checkpoint state · `src/deepscenic/tl/_training_state.py`
- [Model API](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_model.py): training facade · High-level model construction and training interface · `src/deepscenic/tl/_model.py`
- [Inference](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_inference.py): prediction · Evaluation and prediction paths · `src/deepscenic/tl/_inference.py`
- [GRN](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_grn.py): regulatory inference · Extract regulatory networks from trained models · `src/deepscenic/tl/_grn.py`
- [Perturbation](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_perturbation.py): what-if analysis · TF knockdown and overexpression simulation · `src/deepscenic/tl/_perturbation.py`
- [Logging](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/tl/_logging.py): run tracking · Training and analysis logging · `src/deepscenic/tl/_logging.py`

### Visualization

Plotting and interpretation

- [Training plots](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/training/_diagnostics.py): diagnostics · Training diagnostics and convergence views · `src/deepscenic/pl/training/_diagnostics.py`
- [Embedding plots](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/_embedding.py): embedding viz · Embedding and latent-space utilities · `src/deepscenic/pl/_embedding.py`
- [GRN plots](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/grn/_network.py): network viz · Heatmaps, cell-type views, and network plots · `src/deepscenic/pl/grn/_network.py`
- [Genome views](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/genomics/_browser.py): genomics viz · Arc and browser-style genomic visualizations · `src/deepscenic/pl/genomics/_browser.py`
- [Perturbation plots](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/perturbation/_volcano.py): analysis viz · Volcano, co-embedding, and heatmap plots · `src/deepscenic/pl/perturbation/_volcano.py`
- [Sequence plots](https://github.com/aertslab/deepscenic/blob/main/src/deepscenic/pl/sequence/_logo.py): sequence viz · Sequence logos and in-silico mutagenesis plots · `src/deepscenic/pl/sequence/_logo.py`

## Connections

- deepscenic → pp: exports · Public preprocessing entry point
- deepscenic → tl: exports · Public modeling and analysis entry point
- deepscenic → pl: exports · Public plotting entry point
- pp → Prep core: prepare · Build multimodal containers and enrich annotations
- pp → Search space: constraints · Derive region-to-gene priors and split masks
- Prep core → Data model: uses · Operates on multimodal data containers
- Prep core → Genome: annotates · Attaches genome and sequence reference context
- Search space → Genome: uses · Chromosome and reference-based constraints
- Datasets → I/O: loads · Dataset access relies on serialization and file handling
- I/O → Data model: persists · Reads and writes internal data objects
- Genome → Types: defines · Reference structures use shared schema types
- VAE → Encoder: contains · Latent encoder path
- VAE → Decoder: contains · Output decoder path
- VAE → Motif net: contains · Sequence-aware motif component
- VAE → Layers: builds on · Shared neural primitives
- tl → Loaders: feeds · Batching and sample access for training
- tl → Model API: orchestrates · High-level training/model interface
- Model API → VAE: instantiates · Constructs the central multimodal model
- tl → Loss: optimizes · Training objective computation
- tl → Train loop: runs · Main training and finetuning loop
- Train loop → State: updates · Checkpoint and phase state management
- tl → Inference: predicts · Evaluation and prediction paths
- tl → GRN: extracts · Derives regulatory networks from trained models
- tl → Perturbation: simulates · Runs perturbation analyses on the learned model
- tl → Logging: records · Captures run metrics and training events
- GRN → GRN plots: visualized by · GRN outputs feed network and heatmap views
- Perturbation → Perturbation plots: visualized by · Perturbation outputs feed analysis plots
- Train loop → Training plots: visualized by · Training diagnostics consume loop outputs
- Inference → Embedding plots: visualized by · Latent embeddings and diagnostics
- GRN → Genome views: contextualized by · Genomic views for regulatory interpretation
- Motif net → Sequence plots: interpreted by · Sequence motif outputs support logos and ISM plots
- Prep core → tl: prepares · Preprocessed multimodal objects flow into training
