# lab-of-biochemai/deepsems architecture

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

- Interactive diagram: https://gitdiagram.com/lab-of-biochemai/deepsems
- Repository: https://github.com/lab-of-biochemai/deepsems

## Overview

1. **Repository purpose**
   DeepSeMS is an offline inference-and-training repository for a Transformer model that predicts **SMILES chemical structures from biosynthetic gene cluster (BGC) annotations**. The core architectural split is between **data preparation**, **sequence-to-sequence model inference**, and **optional chemistry post-processing**.

2. **Primary user-facing entry points**
   The main executable paths are:
   - `predict.py` for single-BGC prediction
   - `train.py` for model training
   - `calculate_molecular_properties.py` for post-processing predicted molecules  
   These scripts are the orchestration layer; most of the logic beneath them is shared across preprocessing, tokenization, and model code.

3. **Input boundary: annotated BGC files, not raw genomes**
   The model does **not** take raw genome FASTA directly. It expects BGC annotations from:
   - **antiSMASH** in GenBank format
   - **DeepBGC** in FASTA format  
   This is an important architecture boundary: external annotation tools sit upstream of DeepSeMS.

4. **Preprocessing and feature extraction layer**
   `data_processing.py` likely handles conversion of BGC annotations into model-ready features. The README also requires a **Pfam database** and `hmmer`, which implies the preprocessing layer maps BGC elements to Pfam/domain features before inference. That makes Pfam/HMMER an external bioinformatics dependency in the input pipeline.

5. **Tokenization and vocabularies**
   `tokenizer/tokenizer.py` and the `vocabs/` directory define the symbolic interface used by the model:
   - `vocabs/bgc_features_vocab.csv` for BGC-side features
   - `vocabs/smiles-vocab.pt` for SMILES tokenization  
   This is the translation boundary between biological annotations and the Transformer’s discrete token space.

6. **Core model subsystem**
   `models/Transformer.py` contains the neural architecture. Based on the repo description, this is a **PyTorch Transformer-based sequence model** that maps BGC-derived token sequences to SMILES token sequences. The model is the central computational boundary between biological input features and chemical output strings.

7. **Ensemble inference and checkpointing**
   `checkpoints/` stores pretrained weights, and the README describes **top-10 submodels** whose predictions are combined. So inference is not a single model pass; it is an **ensemble/ranking pipeline** over multiple checkpoints, producing a consensus score and confidence-like score for each candidate SMILES.

8. **Prediction flow**
   `predict.py` is the runtime orchestrator:
   - reads one annotated BGC file
   - preprocesses and tokenizes it
   - loads the model checkpoint ensemble
   - generates candidate SMILES
   - ranks them by model score and consensus
   - writes a result CSV under `test/outputs/`  
   This script is the main diagram node for end-to-end inference.

9. **Output artifacts**
   The primary output is a **CSV result file** containing ranked predictions with:
   - predicted SMILES
   - predicted score
   - consensus count  
   The output directory is structured per input file, so prediction results are file-scoped rather than stored in a centralized database or service.

10. **Optional chemistry post-processing**
   `calculate_molecular_properties.py` is a downstream analysis step that operates on DeepSeMS result CSVs. It depends on **RDKit** and the separately downloaded **SA score** resources (`sascorer.py`, `fpscores.pkl.gz`) to compute molecular properties after prediction, not during model inference.

11. **Training data and offline model development**
   `train.py` and `data/data_set.csv` support the training path. This is a separate subsystem from inference, using the same tokenization/vocabulary assets but reading from a curated dataset rather than user-supplied BGC files. The presence of checkpoints indicates trained models are serialized back into `checkpoints/`.

12. **Environment and reproducibility**
   `environment.yml` captures the local Conda environment, while the README also recommends Docker for reproducible execution. The repo is therefore designed around a **portable offline scientific ML workflow** rather than a long-running service architecture.

13. **External dependencies that matter architecturally**
   The major runtime dependencies are:
   - **PyTorch/TorchText** for the Transformer and decoding
   - **HMMER + Pfam** for annotation/domain feature mapping
   - **Biopython** for parsing biological file formats
   - **RDKit** for chemical property calculations  
   These libraries define the boundaries between biological parsing, ML inference, and chemical analysis.

14. **High-level data flow**
   The end-to-end flow is:
   **annotated BGC file → preprocessing/Pfam feature extraction → tokenization → Transformer ensemble inference → ranked SMILES output → optional molecular property calculation**.  
   For an architecture diagram, the main subsystems to show are: input annotations, preprocessing/tokenization, model ensemble, result ranking/output, and optional post-processing.

Main entry points: [`train.py`](https://github.com/lab-of-biochemai/deepsems/blob/main/train.py) (train.py), [`data_processing.py`](https://github.com/lab-of-biochemai/deepsems/blob/main/data_processing.py) (Prep), [`predict.py`](https://github.com/lab-of-biochemai/deepsems/blob/main/predict.py) (predict.py), [`tokenizer/tokenizer.py`](https://github.com/lab-of-biochemai/deepsems/blob/main/tokenizer/tokenizer.py) (Tokenizer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_inference["Inference Pipeline"]
  node_predict_py["predict.py<br/>orchestrator<br/>[predict.py]"]
  node_data_processing_py["Prep<br/>[data_processing.py]"]
  node_tokenizer_py["Tokenizer<br/>tokenization<br/>[tokenizer.py]"]
  node_transformer_model["Transformer<br/>seq2seq model<br/>[Transformer.py]"]
  node_checkpoint_model[("Checkpoint<br/>weights<br/>[checkpoint.ckpt]")]
  node_rank_output["Ranked CSV<br/>result"]
  node_ensemble_logic{{"Ensemble<br/>model selection"}}
end

subgraph group_g_training["Training"]
  node_train_py["train.py<br/>trainer<br/>[train.py]"]
  node_training_data["Dataset<br/>training data<br/>[data_set.csv]"]
end

subgraph group_g_analysis["Post-processing"]
  node_calculate_props_py["Props<br/>analysis"]
  node_rdkit_sa[("RDKit/SA<br/>chemistry deps")]
end

subgraph group_g_assets["Assets"]
  node_bgc_vocab["BGC vocab<br/>feature vocab"]
  node_smiles_vocab["SMILES vocab<br/>token vocab<br/>[smiles-vocab.pt]"]
end

subgraph group_g_external["External Tools"]
  node_bgc_inputs["BGC files<br/>annotated input"]
  node_antismash["antiSMASH<br/>upstream annotator"]
  node_deepbgc["DeepBGC<br/>upstream annotator<br/>[DeepBGC_example.fa]"]
  node_hmmer_pfam{{"HMMER/Pfam<br/>bioinformatics deps"}}
end

node_antismash -->|"produces"| node_bgc_inputs
node_deepbgc -->|"produces"| node_bgc_inputs
node_bgc_inputs -->|"input"| node_predict_py
node_predict_py -->|"calls"| node_data_processing_py
node_data_processing_py -.->|"uses"| node_hmmer_pfam
node_data_processing_py -->|"encodes"| node_bgc_vocab
node_data_processing_py -->|"passes"| node_tokenizer_py
node_tokenizer_py -->|"uses"| node_smiles_vocab
node_predict_py -->|"orchestrates"| node_ensemble_logic
node_ensemble_logic -->|"loads"| node_checkpoint_model
node_ensemble_logic -->|"runs"| node_transformer_model
node_transformer_model -->|"writes"| node_rank_output
node_train_py -->|"reads"| node_training_data
node_train_py -->|"reuses"| node_tokenizer_py
node_train_py -->|"fits"| node_transformer_model
node_train_py -->|"saves"| node_checkpoint_model
node_calculate_props_py -->|"reads"| node_rank_output
node_calculate_props_py -->|"uses"| node_rdkit_sa

click node_predict_py "https://github.com/lab-of-biochemai/deepsems/blob/main/predict.py"
click node_data_processing_py "https://github.com/lab-of-biochemai/deepsems/blob/main/data_processing.py"
click node_tokenizer_py "https://github.com/lab-of-biochemai/deepsems/blob/main/tokenizer/tokenizer.py"
click node_transformer_model "https://github.com/lab-of-biochemai/deepsems/blob/main/models/Transformer.py"
click node_checkpoint_model "https://github.com/lab-of-biochemai/deepsems/blob/main/checkpoints/checkpoint.ckpt"
click node_train_py "https://github.com/lab-of-biochemai/deepsems/blob/main/train.py"
click node_training_data "https://github.com/lab-of-biochemai/deepsems/blob/main/data/data_set.csv"
click node_bgc_vocab "https://github.com/lab-of-biochemai/deepsems/blob/main/vocabs/bgc_features_vocab.csv"
click node_smiles_vocab "https://github.com/lab-of-biochemai/deepsems/blob/main/vocabs/smiles-vocab.pt"
click node_calculate_props_py "https://github.com/lab-of-biochemai/deepsems/blob/main/calculate_molecular_properties.py"
click node_bgc_inputs "https://github.com/lab-of-biochemai/deepsems/blob/main/test/antiSMASH_example.gbk"
click node_deepbgc "https://github.com/lab-of-biochemai/deepsems/blob/main/test/DeepBGC_example.fa"

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_predict_py,node_data_processing_py,node_tokenizer_py,node_transformer_model,node_checkpoint_model,node_rank_output,node_ensemble_logic toneBlue
class node_train_py,node_training_data toneAmber
class node_calculate_props_py,node_rdkit_sa toneMint
class node_bgc_vocab,node_smiles_vocab toneRose
class node_bgc_inputs,node_antismash,node_deepbgc,node_hmmer_pfam toneIndigo
```

## Components

### Inference Pipeline

Single-BGC prediction flow from annotated input to ranked SMILES output.

- [predict.py](https://github.com/lab-of-biochemai/deepsems/blob/main/predict.py): orchestrator · Entry point for end-to-end single-BGC inference. · `predict.py`
- [Prep](https://github.com/lab-of-biochemai/deepsems/blob/main/data_processing.py): preprocess · Converts annotated BGC files into model-ready features. · `data_processing.py`
- [Tokenizer](https://github.com/lab-of-biochemai/deepsems/blob/main/tokenizer/tokenizer.py): tokenization · Maps biological features and SMILES symbols into discrete token sequences. · `tokenizer/tokenizer.py`
- [Transformer](https://github.com/lab-of-biochemai/deepsems/blob/main/models/Transformer.py): seq2seq model · PyTorch model that generates SMILES tokens from BGC tokens. · `models/Transformer.py`
- [Checkpoint](https://github.com/lab-of-biochemai/deepsems/blob/main/checkpoints/checkpoint.ckpt): weights · Pretrained model weights used during inference. · `checkpoints/checkpoint.ckpt`
- **Ranked CSV**: result · Final prediction file with SMILES, scores, and consensus counts.
- **Ensemble**: model selection · Top-k checkpoint scoring and consensus-based ranking during inference.

### Training

Offline model development and checkpoint generation.

- [train.py](https://github.com/lab-of-biochemai/deepsems/blob/main/train.py): trainer · Entry point for model training. · `train.py`
- [Dataset](https://github.com/lab-of-biochemai/deepsems/blob/main/data/data_set.csv): training data · Curated paired data used to fit the model. · `data/data_set.csv`

### Post-processing

Optional chemical property calculation on prediction outputs.

- [Props](https://github.com/lab-of-biochemai/deepsems/blob/main/calculate_molecular_properties.py): analysis · Post-processing script that computes molecular properties from prediction CSVs. · `calculate_molecular_properties.py`
- **RDKit/SA**: chemistry deps · Chemical analysis dependencies including RDKit and SA score resources.

### Assets

Vocabulary, dataset, and checkpoint artifacts used by the model.

- [BGC vocab](https://github.com/lab-of-biochemai/deepsems/blob/main/vocabs/bgc_features_vocab.csv): feature vocab · Vocabulary of BGC-side features used by preprocessing and tokenization. · `vocabs/bgc_features_vocab.csv`
- [SMILES vocab](https://github.com/lab-of-biochemai/deepsems/blob/main/vocabs/smiles-vocab.pt): token vocab · Token vocabulary for SMILES decoding and generation. · `vocabs/smiles-vocab.pt`

### External Tools

Upstream annotation and runtime dependencies outside the repository.

- [BGC files](https://github.com/lab-of-biochemai/deepsems/blob/main/test/antiSMASH_example.gbk): annotated input · Accepted annotated inputs from antiSMASH GenBank or DeepBGC FASTA files. · `test/antiSMASH_example.gbk`
- **antiSMASH**: upstream annotator · External tool producing annotated GenBank BGC input.
- [DeepBGC](https://github.com/lab-of-biochemai/deepsems/blob/main/test/DeepBGC_example.fa): upstream annotator · External tool producing annotated FASTA BGC input. · `test/DeepBGC_example.fa`
- **HMMER/Pfam**: bioinformatics deps · External feature-mapping layer for domain and Pfam-based preprocessing.

## Connections

- antiSMASH → BGC files: produces · Generates annotated GenBank BGC files.
- DeepBGC → BGC files: produces · Generates annotated FASTA BGC files.
- BGC files → predict.py: input · Single annotated BGC file is passed to inference.
- predict.py → Prep: calls · Runs preprocessing on the annotated input.
- Prep → HMMER/Pfam: uses · Maps BGC elements to Pfam/domain features.
- Prep → BGC vocab: encodes · Looks up biological feature symbols.
- Prep → Tokenizer: passes · Hands preprocessed features to tokenization.
- Tokenizer → SMILES vocab: uses · Consumes SMILES token vocabulary for decoding space.
- predict.py → Ensemble: orchestrates · Coordinates multi-checkpoint candidate generation and ranking.
- Ensemble → Checkpoint: loads · Uses pretrained weights for scoring.
- Ensemble → Transformer: runs · Executes the seq2seq Transformer for candidate generation.
- Transformer → Ranked CSV: writes · Produces ranked SMILES predictions and scores.
- train.py → Dataset: reads · Consumes curated training pairs.
- train.py → Tokenizer: reuses · Shares tokenization logic with inference.
- train.py → Transformer: fits · Trains the Transformer architecture.
- train.py → Checkpoint: saves · Serializes trained weights to checkpoint storage.
- Props → Ranked CSV: reads · Consumes prediction CSVs for analysis.
- Props → RDKit/SA: uses · Computes molecular properties and synthetic accessibility metrics.
