Code accompanying Dancer: DisentANgling within-Cell-type hetERogeneity from transcript contamination in imaging-based spatial transcriptomics.
Dancer uses a labeled single-cell reference to learn cell-type-specific expression programs and separate intrinsic spatial expression from neighboring-cell and background contamination. This repository provides a human breast cancer example, from public data download to corrected counts, program loadings/activities and basic evaluation.
This example uses the 10x Genomics breast cancer Xenium dataset, together with the matched single-cell reference and annotations from the accompanying data deposit, described in Janesick et al. (2023).
Use a fresh Python 3.12 environment, preferably with a CUDA-capable GPU.
git clone https://github.com/jerryliu357/Dancer.git
cd Dancer
conda create -n dancer python=3.12 -y
conda activate dancerInstall one of the following PyTorch builds:
GPU:
pip install torch==2.6.0 --index-url https://download.pytorch.org/whl/cu124CPU:
pip install torch==2.6.0 --index-url https://download.pytorch.org/whl/cpuThen install the remaining dependencies and Dancer:
pip install -r requirements.txt
pip install -e . --no-depsRun the following steps from the Dancer directory.
# 1. Download public data and extract the required matrix/cell table.
python scripts/download_data.py --config configs/breast_cancer.yaml
# 2. Prepare labeled reference.h5ad and spatial.h5ad on shared genes.
python scripts/preprocess.py --config configs/breast_cancer.yaml
# 3. Select program numbers, train and export (default: up to 5 idle GPUs for selection).
python scripts/run_model.py --config configs/breast_cancer.yamlFor step 3, you can use one of these alternatives instead:
# Use two automatically selected idle GPUs.
python scripts/run_model.py --config configs/breast_cancer.yaml --gpus 2
# OR: use specific visible GPUs (training and export use the first listed GPU).
python scripts/run_model.py --config configs/breast_cancer.yaml --devices 0,2
# OR: run selection, training and export on the CPU.
python scripts/run_model.py --config configs/breast_cancer.yaml --cpuThen evaluate the exported results:
# 4. Select evaluation markers, calculate metrics and generate plots/report.
python scripts/evaluate.py --config configs/breast_cancer.yamlEdit configs/breast_cancer.yaml to change paths, selection thresholds, training parameters, export settings or evaluation options.
Relative paths are resolved against project_root in the configuration. For a new experiment, change paths.results to a new directory.
All directories are created automatically. Downloads go into data/raw/breast_cancer/, processed inputs into data/processed/breast_cancer/, and results into results/breast_cancer/.
The corrected expression matrix is saved in results/breast_cancer/outputs/corrected.h5ad.
After evaluation, please open results/breast_cancer/report.html to view the results. You can also load model outputs in an interactive Python session:
# Inspect corrected_counts, h, u and L interactively.
python -i scripts/load_outputs.py --config configs/breast_cancer.yamlDancer/
├── configs/breast_cancer.yaml # Example configuration
├── scripts/
│ ├── download_data.py # Download data
│ ├── preprocess.py # Prepare model inputs
│ ├── run_model.py # Select programs, train and export
│ ├── evaluate.py # Calculate metrics and generate plots
│ └── load_outputs.py # Load saved matrices and activities
├── src/dancer/ # Model and analysis code
├── data/ # Downloaded and processed data
└── results/ # Model outputs and evaluation results