# jerryji1993/dnabert architecture

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

- Interactive diagram: https://gitdiagram.com/jerryji1993/dnabert
- Repository: https://github.com/jerryji1993/dnabert

## Overview

#### Purpose and Entry Points
DNABERT is a Python/PyTorch implementation of first-generation DNABERT: BERT-style masked-language pretraining and downstream classification for DNA sequences represented as k-mers. It vendors an older Hugging Face Transformers codebase, adapted with DNA tokenizer/model selection. Primary executable workflows are pretraining, fine-tuning/prediction/attention extraction, motif analysis, and mutation-effect analysis:
- `examples/run_pretrain.py`
- `examples/run_finetune.py`
- `motif/find_motifs.py`
- `SNP/mutate_seqs.py`, `SNP/SNP.py`

#### DNA Representation and Model Configuration
Raw nucleotide sequences must cross a preprocessing boundary into whitespace-separated k-mers; downstream inputs are text for pretraining and TSV classification datasets. DNA-specific tokenization and fixed configurations for k=3–6 define the vocabulary/model contract.
- `src/transformers/tokenization_dna.py`
- `src/transformers/dnabert-config/bert-config-6/config.json`
- `motif/motif_utils.py`

#### Transformer Runtime Layer
The installed `transformers` package is the central model runtime. Auto-selection/configuration resolves `model_type=dna`; BERT modeling provides encoder, masked-LM, and sequence-classification execution, while shared utilities manage loading/saving pretrained checkpoints.
- `src/transformers/__init__.py`
- `src/transformers/modeling_bert.py`
- `src/transformers/modeling_auto.py`

#### Pretraining Pipeline
`run_pretrain.py` consumes line-oriented k-mer corpus data, initializes a DNA tokenizer plus BERT config, applies masked-language-model training, evaluates periodically, and writes resumable model/checkpoint artifacts to an output directory. It is the producer of general-purpose DNABERT weights consumed by fine-tuning.
- `examples/run_pretrain.py`
- `examples/sample_data/pre/6_3k.txt`
- `src/transformers/optimization.py`

#### Fine-Tuning, Prediction, and Attention Extraction
`run_finetune.py` is the main operational entry point. It loads a pretrained/fine-tuned checkpoint and DNA tokenizer, reads task TSVs, trains/evaluates a classifier, or emits predictions. Its visualization mode produces attention-score outputs used as the input boundary for motif analysis.
- `examples/run_finetune.py`
- `examples/sample_data/ft/6/train.tsv`
- `examples/visualize.py`

#### Interpretability and Variant Analysis
Motif analysis consumes prediction/attention artifacts plus original sequences to identify and render enriched motifs. The SNP workflow separately generates mutated sequence datasets, routes them through the same prediction entry point, then compares original versus mutated prediction arrays to quantify effects.
- `motif/find_motifs.py`
- `SNP/mutate_seqs.py`
- `SNP/SNP.py`

#### Runtime, Distribution, and External Dependencies
The target runtime is Python 3.6-era Linux with PyTorch, CUDA 10-compatible NVIDIA GPUs, and optional NVIDIA Apex for FP16. Training supports multi-GPU/distributed-style execution and configurable multiprocessing (`n_process`). Pretrained DNABERT checkpoints are externally hosted on Hugging Face; downloaded model directories are runtime inputs rather than repository assets. Package installation is defined by `setup.py`, with the executable CLI wrapper at `transformers-cli`.
- `setup.py`
- `examples/requirements.txt`
- `transformers-cli`

Main entry points: [`examples/run_finetune.py`](https://github.com/jerryji1993/dnabert/blob/master/examples/run_finetune.py) (Fine-tune & Predict CLI), [`examples/run_pretrain.py`](https://github.com/jerryji1993/dnabert/blob/master/examples/run_pretrain.py) (Pretraining CLI), [`src/transformers/modeling_auto.py`](https://github.com/jerryji1993/dnabert/blob/master/src/transformers/modeling_auto.py) (DNA Auto Selection), [`examples/sample_data/ft/6/train.tsv`](https://github.com/jerryji1993/dnabert/blob/master/examples/sample_data/ft/6/train.tsv) (Classification TSVs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["DNABERT Runtime"]
  node_tokenizer["DNA Tokenizer<br/>Transformers tokenizer"]
  node_dna_config["DNA BERT Config<br/>model configuration<br/>[config.json]"]
  node_transformers_api["Transformers Package<br/>Python package<br/>[__init__.py]"]
  node_auto_model["DNA Auto Selection<br/>model resolver<br/>[modeling_auto.py]"]
  node_bert_models["BERT Model Heads<br/>PyTorch models<br/>[modeling_bert.py]"]
  node_optimizer["Optimization Runtime<br/>training utility<br/>[optimization.py]"]
  node_package_install["Package Installation<br/>distribution<br/>[setup.py]"]
  node_cli_wrapper["Transformers CLI<br/>CLI wrapper"]
end

subgraph group_workflows["Training &amp; Analysis"]
  node_pretrain_cli["Pretraining CLI<br/>Python entry point<br/>[run_pretrain.py]"]
  node_finetune_cli["Fine-tune &amp; Predict CLI<br/>Python entry point<br/>[run_finetune.py]"]
  node_motif_cli["Motif Analysis<br/>Python entry point<br/>[find_motifs.py]"]
  node_mutation_generator["Mutation Generator<br/>Python entry point<br/>[mutate_seqs.py]"]
  node_variant_analysis["SNP Effect Analysis<br/>Python entry point<br/>[SNP.py]"]
end

subgraph group_inputs["Data &amp; Artifacts"]
  node_kmer_preprocessing["k-mer Preprocessing<br/>DNA representation<br/>[motif_utils.py]"]
  node_pretrain_corpus["k-mer Corpus<br/>training data<br/>[6_3k.txt]"]
  node_classification_tsv["Classification TSVs<br/>task data<br/>[train.tsv]"]
  node_checkpoints[("DNABERT Checkpoints<br/>model artifacts")]
  node_attention_artifacts["Attention &amp; Predictions<br/>analysis artifacts<br/>[visualize.py]"]
end

node_kmer_preprocessing -->|"produces"| node_pretrain_corpus
node_kmer_preprocessing -->|"formats sequences for"| node_classification_tsv
node_pretrain_corpus -->|"training input"| node_pretrain_cli
node_classification_tsv -->|"task input"| node_finetune_cli
node_pretrain_cli -->|"uses"| node_tokenizer
node_finetune_cli -->|"uses"| node_tokenizer
node_pretrain_cli -->|"initializes from"| node_dna_config
node_pretrain_cli -->|"constructs model through"| node_auto_model
node_finetune_cli -->|"loads model through"| node_auto_model
node_auto_model -->|"selects"| node_bert_models
node_transformers_api -->|"exports"| node_auto_model
node_pretrain_cli -->|"trains with"| node_optimizer
node_pretrain_cli -->|"writes MLM weights"| node_checkpoints
node_checkpoints -->|"pretrained or resumed input"| node_finetune_cli
node_finetune_cli -->|"emits predictions and attention"| node_attention_artifacts
node_attention_artifacts -->|"analysis input"| node_motif_cli
node_classification_tsv -->|"original sequences"| node_motif_cli
node_mutation_generator -->|"routes mutated data to prediction"| node_finetune_cli
node_finetune_cli -->|"original and mutated predictions"| node_variant_analysis
node_mutation_generator -->|"mutation dataset context"| node_variant_analysis
node_package_install -->|"installs"| node_transformers_api
node_cli_wrapper -->|"invokes package tools"| node_transformers_api

click node_pretrain_cli "https://github.com/jerryji1993/dnabert/blob/master/examples/run_pretrain.py"
click node_finetune_cli "https://github.com/jerryji1993/dnabert/blob/master/examples/run_finetune.py"
click node_motif_cli "https://github.com/jerryji1993/dnabert/blob/master/motif/find_motifs.py"
click node_mutation_generator "https://github.com/jerryji1993/dnabert/blob/master/SNP/mutate_seqs.py"
click node_variant_analysis "https://github.com/jerryji1993/dnabert/blob/master/SNP/SNP.py"
click node_kmer_preprocessing "https://github.com/jerryji1993/dnabert/blob/master/motif/motif_utils.py"
click node_pretrain_corpus "https://github.com/jerryji1993/dnabert/blob/master/examples/sample_data/pre/6_3k.txt"
click node_classification_tsv "https://github.com/jerryji1993/dnabert/blob/master/examples/sample_data/ft/6/train.tsv"
click node_tokenizer "https://github.com/jerryji1993/dnabert/blob/master/src/transformers/tokenization_dna.py"
click node_dna_config "https://github.com/jerryji1993/dnabert/blob/master/src/transformers/dnabert-config/bert-config-6/config.json"
click node_transformers_api "https://github.com/jerryji1993/dnabert/blob/master/src/transformers/__init__.py"
click node_auto_model "https://github.com/jerryji1993/dnabert/blob/master/src/transformers/modeling_auto.py"
click node_bert_models "https://github.com/jerryji1993/dnabert/blob/master/src/transformers/modeling_bert.py"
click node_optimizer "https://github.com/jerryji1993/dnabert/blob/master/src/transformers/optimization.py"
click node_attention_artifacts "https://github.com/jerryji1993/dnabert/blob/master/examples/visualize.py"
click node_package_install "https://github.com/jerryji1993/dnabert/blob/master/setup.py"
click node_cli_wrapper "https://github.com/jerryji1993/dnabert/blob/master/transformers-cli"

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_tokenizer,node_dna_config,node_transformers_api,node_auto_model,node_bert_models,node_optimizer,node_package_install,node_cli_wrapper toneBlue
class node_pretrain_cli,node_finetune_cli,node_motif_cli,node_mutation_generator,node_variant_analysis toneAmber
class node_kmer_preprocessing,node_pretrain_corpus,node_classification_tsv,node_checkpoints,node_attention_artifacts toneMint
```

## Components

### DNABERT Runtime

Vendored Transformers runtime adapted for DNA.

- [DNA Tokenizer](https://github.com/jerryji1993/dnabert/blob/master/src/transformers/tokenization_dna.py): Transformers tokenizer · Defines DNA k-mer vocabulary encoding. · `src/transformers/tokenization_dna.py`
- [DNA BERT Config](https://github.com/jerryji1993/dnabert/blob/master/src/transformers/dnabert-config/bert-config-6/config.json): model configuration · Fixed k-mer-specific BERT configuration. · `src/transformers/dnabert-config/bert-config-6/config.json`
- [Transformers Package](https://github.com/jerryji1993/dnabert/blob/master/src/transformers/__init__.py): Python package · Public runtime and model registration surface. · `src/transformers/__init__.py`
- [DNA Auto Selection](https://github.com/jerryji1993/dnabert/blob/master/src/transformers/modeling_auto.py): model resolver · Resolves model_type=dna to the model runtime. · `src/transformers/modeling_auto.py`
- [BERT Model Heads](https://github.com/jerryji1993/dnabert/blob/master/src/transformers/modeling_bert.py): PyTorch models · Encoder, masked-LM, and sequence-classification execution. · `src/transformers/modeling_bert.py`
- [Optimization Runtime](https://github.com/jerryji1993/dnabert/blob/master/src/transformers/optimization.py): training utility · Optimizer and learning-rate scheduling. · `src/transformers/optimization.py`
- [Package Installation](https://github.com/jerryji1993/dnabert/blob/master/setup.py): distribution · Defines installable vendored Transformers package. · `setup.py`
- [Transformers CLI](https://github.com/jerryji1993/dnabert/blob/master/transformers-cli): CLI wrapper · `transformers-cli`

### Training & Analysis

Primary executable pipelines.

- [Pretraining CLI](https://github.com/jerryji1993/dnabert/blob/master/examples/run_pretrain.py): Python entry point · Trains masked-language models on k-mer corpora. · `examples/run_pretrain.py`
- [Fine-tune & Predict CLI](https://github.com/jerryji1993/dnabert/blob/master/examples/run_finetune.py): Python entry point · Trains classifiers, predicts, and exports attention. · `examples/run_finetune.py`
- [Motif Analysis](https://github.com/jerryji1993/dnabert/blob/master/motif/find_motifs.py): Python entry point · Finds and renders enriched sequence motifs. · `motif/find_motifs.py`
- [Mutation Generator](https://github.com/jerryji1993/dnabert/blob/master/SNP/mutate_seqs.py): Python entry point · Creates mutated sequence datasets. · `SNP/mutate_seqs.py`
- [SNP Effect Analysis](https://github.com/jerryji1993/dnabert/blob/master/SNP/SNP.py): Python entry point · Compares original and mutated predictions. · `SNP/SNP.py`

### Data & Artifacts

Sequence data, task data, and generated artifacts.

- [k-mer Preprocessing](https://github.com/jerryji1993/dnabert/blob/master/motif/motif_utils.py): DNA representation · Converts nucleotide sequences to whitespace-separated k-mers. · `motif/motif_utils.py`
- [k-mer Corpus](https://github.com/jerryji1993/dnabert/blob/master/examples/sample_data/pre/6_3k.txt): training data · `examples/sample_data/pre/6_3k.txt`
- [Classification TSVs](https://github.com/jerryji1993/dnabert/blob/master/examples/sample_data/ft/6/train.tsv): task data · `examples/sample_data/ft/6/train.tsv`
- **DNABERT Checkpoints**: model artifacts · External pretrained weights and resumable training outputs.
- [Attention & Predictions](https://github.com/jerryji1993/dnabert/blob/master/examples/visualize.py): analysis artifacts · Prediction arrays and visualization attention outputs. · `examples/visualize.py`

## Connections

- k-mer Preprocessing → k-mer Corpus: produces
- k-mer Preprocessing → Classification TSVs: formats sequences for
- k-mer Corpus → Pretraining CLI: training input
- Classification TSVs → Fine-tune & Predict CLI: task input
- Pretraining CLI → DNA Tokenizer: uses
- Fine-tune & Predict CLI → DNA Tokenizer: uses
- Pretraining CLI → DNA BERT Config: initializes from
- Pretraining CLI → DNA Auto Selection: constructs model through
- Fine-tune & Predict CLI → DNA Auto Selection: loads model through
- DNA Auto Selection → BERT Model Heads: selects
- Transformers Package → DNA Auto Selection: exports
- Pretraining CLI → Optimization Runtime: trains with
- Pretraining CLI → DNABERT Checkpoints: writes MLM weights
- DNABERT Checkpoints → Fine-tune & Predict CLI: pretrained or resumed input
- Fine-tune & Predict CLI → Attention & Predictions: emits predictions and attention
- Attention & Predictions → Motif Analysis: analysis input
- Classification TSVs → Motif Analysis: original sequences
- Mutation Generator → Fine-tune & Predict CLI: routes mutated data to prediction
- Fine-tune & Predict CLI → SNP Effect Analysis: original and mutated predictions
- Mutation Generator → SNP Effect Analysis: mutation dataset context
- Package Installation → Transformers Package: installs
- Transformers CLI → Transformers Package: invokes package tools
