Skip to content

Repository files navigation

CytoBERT: A Foundation Model for Variable-Panel Cytometry

This repository contains the implementation of CytoBERT, a Transformer-based foundation model for single-cell cytometry data. The project supports:

  • Self-supervised pretraining on large-scale CyTOF datasets
  • Fine-tuning for sample-level classification

The repository also provides utilities for downloading, preprocessing, and standardizing CyTOF datasets from ImmPort.


Quickstart

Clone the repository. Download the cytobert-data.zip file from Zenodo and unpack it. It contains preprocessed datasets and model weights.

Run as Dev Container

The code is meant to be executed in a vscode devcontainer, using the .devcontainer/devcontainer.json configuration file.

For configuring the container's environment, please customize .devcontainer/.env. Most importantly adjust the path to the extracted cytobert artifacts, and the host's CUDA version:

$ cp .devcontainer/.env.sample .devcontainer/.env
$ vim .devcontainer/.env

CYTOBERT_CUDA=${YOUR_CUDA_VERSION}
CYTOBERT_DATA=${YOUR_DATA_PATH}

Build the devcontainer and reopen the repository in the container. The devcontainer uses conda to install dependencies in the environment cytobert. The postCreateCommand will attempt to install the cytobert package into the conda environment.

Check if the setup was successful:

$ conda activate cytobert
$ python -c "import cytobert"

Please follow the notebooks for different downstream tasks:

  • Tutorial_Sample_Classification.ipynb

Select cytobert as Python environment for each notebook. These tutorials demonstrate how to use pretrained CytoBERT checkpoints for different downstream tasks.

Run Docker Image

You can also run the code from a Docker Image:

wget https://zenodo.org/records/21298697/files/image.tar
docker load -i image.tar

# forward jupyter lab port, provide gpu and mount data artifacts
# make sure that the data artifacts are readable and writable to all users (chmod a+rw cytobert-data)
docker run --rm -p 8888:8888 --gpus="all" --mount ${CYTOBERT_DATA_PATH}:/workspaces/cytobert/data cytobert

You can now access localhost:8888 and run the notebooks in the notebooks folder.


Repository Structure

.
├── .devcontainer/                         # Reproducible Docker/devcontainer environment
│
├── data_download/
│   ├── data/                              # Full Dataset manifests with meta infor and paths
│   ├── data_paths/                        # Dataset path extracted from manifests for ImmPort downloads
│   ├── Readme.md                          # Instructions for downloading CyTOF datasets
│   ├── cytof_anndata.py                   # Script for downloading CyTOF datasets
│   └── standardize_markers.ipynb          # Standardizes marker names across datasets
│
├── readfcs/                               # Modified readfcs package used for FCS parsing
│
├── src/
│   ├── cytobert/                       # Main CytoBERT package    
| 
├── notebooks/
│       ├── Tutorial_Sample_Classification     # Tutorial for sample classification
│       ├── Tutorial_Reference_Mapping         # Notebook tutorial for reference mapping
│       └── Tutorial_Cell_Classification       # Tutorial for cell classification
|
├── scripts/
│       ├── pretrain.py                        # Self-supervised pretraining pipeline
│       ├── run_pretrain.sh                    # Example pretraining shell script
│       ├── finetune.py                        # Fine-tuning and benchmarking script
│       └── run_finetune.sh                    # Shell script for main.py with different configurations
|                 
└── README.md

Pretraining from Scratch

The complete workflow for pretraining CytoBERT is outlined below.

Activate the environment cytobert (already created during container building)

1. Download CyTOF datasets

See the data_download folder for downloading and standardizing CyTOF datasets. Follow the instructions in data_download/Readme.md.


2. Pretrain CytoBERT

Run:

./scripts/run_pretrain.sh

This performs self-supervised pretraining on the downloaded CyTOF datasets.

Important:

  • Use the provided vocabulary file:
pretrain_cytof/vocab.json
  • The preprocessing statistics (bin boundaries) and checkpoints will be saved automatically.

3. Fine-tuning and benchmarking

Run:

./scripts/run_finetune.sh

Important:

  • Before running the script, update the required paths inside scripts/run_finetune.sh, especially::
pretrain_run_dir/
├── checkpoints__uniform                    # checkpoint directory
└──   └── cytobert_epoch_50.pt      # pretrained CytoBERT checkpoint
├── scaler__uniform /                     # Uniform scaling (min and max value per marker)
└──   └── .npy files              # bin boundary files used during pretraining                  

label_csv/
├── SDY1535.csv
├── SDY1708.csv
├── SDY1733.csv
└── ...
  • Fine-tuning on downstream datasets
  • Benchmarking against baseline models
  • Cross-validation experiments
  • Metric generation and result saving

The script supports different:

  • pooling strategies (mean, max, attention)
  • dataset splits (grouped, ungrouped)
  • binning schemes (unifrom, quantile)
  • model configurations (cytobert, cytobert_scratch, cytoset, logistic)

Other Resources

About

A foundation model for cytometry data

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages