# fmoonlights/nabp-bert architecture

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

- Interactive diagram: https://gitdiagram.com/fmoonlights/nabp-bert
- Repository: https://github.com/fmoonlights/nabp-bert

## Overview

##### Project Type and Purpose
- From the README and file tree, NABP‑BERT is an Open‑Source ML Pipeline for Nanobody‑Antigen Binding Prediction.
- It is not a classic web front‑end/back‑end app, but a batch‑style data‑processing and model‑training system built in Python/TensorFlow and orchestrated by shell scripts.
- Components cover:
- Data ingestion & cleaning (nanobody‑antigen sequences and PPI data)
- Dataset construction (TSV, TFRecord)
- Self‑supervised pre‑training (PROT‑BERT)
- Supervised fine‑tuning (PPI‑PROT‑BERT, NABP‑BERT)
- Evaluation & inference

##### File Structure
- Top‑level Python scripts map to pipeline stages:
- Data‑prep: CleanNbAgSeqs.py, CleanHINTdatabase.py, ConstructNbAgTrainValTestDatasets.py, ExtractingProteinSeqsFromTrembleDatabase.py, PrepearPPIDatasetSequences.py, Remove_Homology.py, ConstructPPINegativeSamples.py
- Format conversion: createTSV.py, tsv2record.py
- Pre‑train data prep: Prepear_PreTrain_Dataset.py, create_data.sh
- Model code: modeling.py, optimization.py, tokenization.py, bert_config_3.json, vocab/*
- Training & entry points: run_pretraining.py + pre_train.sh, run_fine_tune.sh, run_classifier.py / run_classifier_fatma.py, run_test.sh
- Utility: saveAsFASTA.py, createDatabase.py, get_unmappedKeysInSwissPortDatabase.py, createTSV.py
- Data directory (data/) holds raw and intermediate CSV/TSV.
- Shell scripts sequence the pipeline: create_data.sh → pre_train.sh → run_fine_tune.sh → run_test.sh.

##### README for Architecture Insights
- Four high‑level stages (A–D) of data prep
- Pre‑training stage using TFRecord + bert_config
- Fine‑tuning and evaluation stage
- Dependencies: UniProt Trembl/Sprot, HINT PPI data, Google Drive model downloads

##### Main Components & Their Interactions
a. Data Sources (External)
- HINT PPI binary files
- UniProt FASTA (Trembl, Swiss‑Prot)
- Nanobody‑Antigen CSV (in repo)
b. Data Preparation Module
- Sequence extraction, cleaning, homology removal
- Negative sample construction for PPI
- Output: cleaned FASTA, CSV
c. Dataset Builder
- createTSV.py → raw CSV/FASTA → TSV files
- tsv2record.py → TSV → TFRecord for TensorFlow input
d. Vocabulary & Tokenization
- vocab_3kmer.txt + tokenization.py break sequences into k‑mers
e. Model Definition
- bert_config_3.json defines model layers
- modeling.py + optimization.py implement PROT‑BERT and task heads
f. Pre‑training Pipeline
- run_pretraining.py invoked via pre_train.sh
- Consumes TFRecord, outputs PROT‑BERT checkpoints
g. Fine‑tuning Pipeline
- run_fine_tune.sh → run_classifier.py (PPI/PPI‑PROT or NB‑Ag)
- Produces task‑specific checkpoints & evaluation results
h. Testing & Inference
- run_test.sh → run_classifier_test_fatma.py → metrics, saved models

##### Architectural Patterns & Design Principles
- Modular pipeline: each stage produces artifacts consumed by the next
- Reusable model core (PROT‑BERT) with plug‑in heads for different tasks (PPI, NABP)
- Shell scripts as orchestration layer for reproducible experiments
- TFRecord as standard data interchange format for performance

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Data Sources
    subgraph "External Data Sources"
        direction TB
        HINT["HINT PPI Files"]:::external
        UniProt["UniProt FASTA (Trembl/Swiss‑Prot)"]:::external
        NbAgCSV["Nanobody‑Antigen CSV"]:::external
        NbAgDist["Antibodies Pairwise Distance Matrix CSV"]:::external
    end

    %% Data Preparation Layer
    subgraph "Data Preparation" 
        direction TB
        CleanNbAgSeqs["CleanNbAgSeqs.py"]:::dataPrep
        CleanHINT["CleanHINTdatabase.py"]:::dataPrep
        ExtractTrembl["ExtractingProteinSeqsFromTrembleDatabase.py"]:::dataPrep
        PrepPPISeq["PrepearPPIDatasetSequences.py"]:::dataPrep
        RemoveHomology["Remove_Homology.py"]:::dataPrep
        NegSample["ConstructPPINegativeSamples.py"]:::dataPrep
        CreateDB["createDatabase.py"]:::dataPrep
        Unmapped["get_unmappedKeysInSwissPortDatabase.py"]:::dataPrep
        SaveFasta["saveAsFASTA.py"]:::dataPrep
        NbAgDataset["ConstructNbAgTrainValTestDatasets.py"]:::dataPrep
    end

    %% Intermediate Data Stores
    subgraph "Cleaned Data Stores"
        direction TB
        CleanedFASTA["Cleaned FASTA Files"]:::dataStore
        CleanedCSV["Cleaned CSV Files"]:::dataStore
        NbAgCleanCSV["Nb-Ag Dataset CSV"]:::dataStore
    end

    %% Dataset Builder Layer
    subgraph "Dataset Builder"
        direction TB
        CreateTSV["createTSV.py"]:::dataPrep
        TSV["TSV Files"]:::dataStore
        TSV2Record["tsv2record.py"]:::dataPrep
        TFRecord["TFRecord Files"]:::dataStore
    end

    %% Model Definition & Vocabulary
    subgraph "Model Definition & Vocabulary"
        direction TB
        Tokenization["tokenization.py"]:::modelPre
        Vocab["vocab_3kmer.txt"]:::modelPre
        BertConfig["bert_config_3.json"]:::modelPre
        Modeling["modeling.py"]:::modelPre
        Optimization["optimization.py"]:::modelPre
    end

    %% Pre-Training Orchestration
    subgraph "Pre-Training"
        direction TB
        PrepDataScript["Prepear_PreTrain_Dataset.py"]:::modelPre
        CreateDataSh["create_data.sh"]:::orchestr
        PreTrainScript["run_pretraining.py"]:::modelPre
        PreTrainSh["pre_train.sh"]:::orchestr
        PTCheckpoints["PROT‑BERT Checkpoints"]:::dataStore
    end

    %% Fine-Tuning & Testing
    subgraph "Fine-Tuning & Testing"
        direction TB
        FineTuneSh["run_fine_tune.sh"]:::fineTest
        Classifier["run_classifier.py"]:::fineTest
        ClassifierFatma["run_classifier_fatma.py"]:::fineTest
        TestSh["run_test.sh"]:::fineTest
        ClassifierTestFatma["run_classifier_test_fatma.py"]:::fineTest
        FTCheckpoints["Task-specific Checkpoints"]:::dataStore
        EvalResults["Evaluation Metrics"]:::dataStore
    end

    %% Connections
    HINT --> CleanHINT
    UniProt --> ExtractTrembl
    UniProt --> Unmapped
    NbAgCSV --> CleanNbAgSeqs
    NbAgDist --> NbAgDataset

    CleanNbAgSeqs --> CleanedFASTA
    CleanHINT --> CleanedCSV
    ExtractTrembl --> CleanedFASTA
    PrepPPISeq --> CleanedCSV
    RemoveHomology --> CleanedCSV
    NegSample --> CleanedCSV
    CreateDB --> CleanedCSV
    Unmapped --> CleanedCSV
    SaveFasta --> CleanedFASTA
    NbAgDataset --> NbAgCleanCSV

    CleanedFASTA --> CreateTSV
    CleanedCSV --> CreateTSV
    NbAgCleanCSV --> CreateTSV

    CreateTSV --> TSV
    TSV --> TSV2Record
    TSV2Record --> TFRecord

    TFRecord --> PrepDataScript
    PrepDataScript --> CreateDataSh
    CreateDataSh -.-> PreTrainSh
    PreTrainSh --> PreTrainScript
    PreTrainScript --> PTCheckpoints

    PTCheckpoints --> FineTuneSh
    FineTuneSh --> Classifier
    Classifier --> FTCheckpoints
    FineTuneSh --> ClassifierFatma
    ClassifierFatma --> FTCheckpoints

    FTCheckpoints --> TestSh
    TestSh --> ClassifierTestFatma
    ClassifierTestFatma --> EvalResults

    %% Click Events
    click CleanNbAgSeqs "https://github.com/fmoonlights/nabp-bert/blob/main/CleanNbAgSeqs.py"
    click CleanHINT "https://github.com/fmoonlights/nabp-bert/blob/main/CleanHINTdatabase.py"
    click ExtractTrembl "https://github.com/fmoonlights/nabp-bert/blob/main/ExtractingProteinSeqsFromTrembleDatabase.py"
    click PrepPPISeq "https://github.com/fmoonlights/nabp-bert/blob/main/PrepearPPIDatasetSequences.py"
    click RemoveHomology "https://github.com/fmoonlights/nabp-bert/blob/main/Remove_Homology.py"
    click NegSample "https://github.com/fmoonlights/nabp-bert/blob/main/ConstructPPINegativeSamples.py"
    click CreateDB "https://github.com/fmoonlights/nabp-bert/blob/main/createDatabase.py"
    click Unmapped "https://github.com/fmoonlights/nabp-bert/blob/main/get_unmappedKeysInSwissPortDatabase.py"
    click SaveFasta "https://github.com/fmoonlights/nabp-bert/blob/main/saveAsFASTA.py"
    click NbAgDataset "https://github.com/fmoonlights/nabp-bert/blob/main/ConstructNbAgTrainValTestDatasets.py"
    click NbAgCSV "https://github.com/fmoonlights/nabp-bert/blob/main/data/Nb_Ag_data/Ag_NbBindingDatawithSequences_Sheet1.csv"
    click NbAgDist "https://github.com/fmoonlights/nabp-bert/blob/main/data/Nb_Ag_data/Antibodies_Pairwise_Distance_Matrix.csv"
    click CreateTSV "https://github.com/fmoonlights/nabp-bert/blob/main/createTSV.py"
    click TSV2Record "https://github.com/fmoonlights/nabp-bert/blob/main/tsv2record.py"
    click Tokenization "https://github.com/fmoonlights/nabp-bert/blob/main/tokenization.py"
    click Vocab "https://github.com/fmoonlights/nabp-bert/blob/main/vocab/vocab_3kmer.txt"
    click BertConfig "https://github.com/fmoonlights/nabp-bert/blob/main/bert_config_3.json"
    click Modeling "https://github.com/fmoonlights/nabp-bert/blob/main/modeling.py"
    click Optimization "https://github.com/fmoonlights/nabp-bert/blob/main/optimization.py"
    click PrepDataScript "https://github.com/fmoonlights/nabp-bert/blob/main/Prepear_PreTrain_Dataset.py"
    click CreateDataSh "https://github.com/fmoonlights/nabp-bert/blob/main/create_data.sh"
    click PreTrainScript "https://github.com/fmoonlights/nabp-bert/blob/main/run_pretraining.py"
    click PreTrainSh "https://github.com/fmoonlights/nabp-bert/blob/main/pre_train.sh"
    click FineTuneSh "https://github.com/fmoonlights/nabp-bert/blob/main/run_fine_tune.sh"
    click Classifier "https://github.com/fmoonlights/nabp-bert/blob/main/run_classifier.py"
    click ClassifierFatma "https://github.com/fmoonlights/nabp-bert/blob/main/run_classifier_fatma.py"
    click TestSh "https://github.com/fmoonlights/nabp-bert/blob/main/run_test.sh"
    click ClassifierTestFatma "https://github.com/fmoonlights/nabp-bert/blob/main/run_classifier_test_fatma.py"

    %% Style Definitions
    classDef external fill:#e2e3e5,stroke:#333,stroke-width:1px
    classDef dataPrep fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef dataStore fill:#f8d7da,stroke:#333,stroke-width:1px,shape:cylinder
    classDef modelPre fill:#d4edda,stroke:#333,stroke-width:1px
    classDef orchestr fill:#d1ecf1,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef fineTest fill:#fff3cd,stroke:#333,stroke-width:1px
```
