Exporting Pseudobulks for ChromBPNet with MarchR
Eric Liu
marchr_chrombpnet.RmdTutorial is currently a work in progress (WIP).
Introduction
ChromBPNet learns base-resolution chromatin accessibility from DNA sequence while factoring out Tn5 sequence bias. It trains one model per pseudobulk, and for single-cell data it is suggested to aggregate the fragments by cluster instead of sample to train cluster-specific ChromBPNet models.
This tutorial provides instructions to setup your single-cell ATAC datasets from your MarchRProject directly for ChromBPNet model training. It continues from the end of the MarchR Hematopoiesis Tutorial and exports the files that the ChromBPNet pipeline reads. We highly recommend you read the ChromBPNet Wiki prior to starting this tutorial.
| ChromBPNet input | Flag | Source |
|---|---|---|
| Fragments per cluster | -ifrag |
write_pseudobulk_fragments() |
| Peaks per cluster (10-column narrowPeak) | -p |
write_pseudobulk_peaks() |
| Chromosome sizes | -c |
write_pseudobulk_chrom_sizes() |
| Blacklist |
-br (prep nonpeaks) |
get_blacklist() |
| Reference genome FASTA | -g |
Downloaded |
| Train / validation / test chromosome folds | -fl |
chrombpnet prep splits |
| GC-matched background regions | -n |
chrombpnet prep nonpeaks |
| Bias model | -b |
Pre-trained, or chrombpnet bias pipeline
|
The last three come from ChromBPNet’s own tools, so they always match the ChromBPNet release you train with. Training needs a GPU and is not run in this tutorial; the shell commands are shown for reference.
Setup
Load MarchR and set the session thread count.
library(MarchR)
set_threads(4)The Hematopoiesis Tutorial saved its project to
cache_dir. The export is written next to it, in
export_dir.
cache_dir <- path.expand("~/.cache/marchr_hematopoiesis")
export_dir <- file.path(cache_dir, "chrombpnet_tutorial")
proj <- load_project(file.path(cache_dir, "saved_project"))
projOutput
## MarchRProject Object
## Directory: ~/.cache/marchr_hematopoiesis/saved_project
## 3 Datasets:
## scATAC_BMMC_R1:
## 4696 cells: scATAC_BMMC_R1#TAGGAGGGTTAACCGT-1, scATAC_BMMC_R1#CCTTGGTAGTTCTCCC-1, scATAC_BMMC_R1#GCCAGCAGTTCTGAGT-1, scATAC_BMMC_R1#TAGCATGGTGACCAGA-1 ... scATAC_BMMC_R1#GTGGATTCATCACAGT-1
## Assays: TileMatrix, GeneScoreMatrix, IterativeLSI
## Fragments: ATAC
## Cell metadata: 3 features (atac_clusters, UMAP_1, UMAP_2)
## scATAC_CD34_BMMC_R1:
## 3173 cells: scATAC_CD34_BMMC_R1#ATTTGTCTCAATCGTG-1, scATAC_CD34_BMMC_R1#CACATGAGTGATAAGT-1, scATAC_CD34_BMMC_R1#GCATTCCTCGTTGTTT-1, scATAC_CD34_BMMC_R1#TGCTTTAGTAACAATG-1 ... scATAC_CD34_BMMC_R1#AACGAGGTCTGAAAGA-1
## Assays: TileMatrix, GeneScoreMatrix, IterativeLSI
## Fragments: ATAC
## Cell metadata: 3 features (atac_clusters, UMAP_1, UMAP_2)
## scATAC_PBMC_R1:
## 2402 cells: scATAC_PBMC_R1#CAGGGCTTCTTTCGAT-1, scATAC_PBMC_R1#GATGATCCACGTTACA-1, scATAC_PBMC_R1#TTTGCGCTCTATCTAC-1, scATAC_PBMC_R1#ATGTACGAGTATGCTC-1 ... scATAC_PBMC_R1#TTCGGTCTCATTCGGA-1
## Assays: TileMatrix, GeneScoreMatrix, IterativeLSI
## Fragments: ATAC
## Cell metadata: 3 features (atac_clusters, UMAP_1, UMAP_2)
## All datasets aligned.
## Assays:
## TileMatrix:
## 6072620 features
## GeneScoreMatrix:
## 20561 features
## IterativeLSI:
## 29 features
## Genome object:
## Chromosomes:
## chr1, chr2 ... chr21
## Annotation names:
## transcripts, genes ... blacklist
## Sequence:
## BSgenome.Hsapiens.UCSC.hg19
## DimReductions: IterativeLSI
## Impute Weights: 2 blocks (embedding = IterativeLSI)Calling Peaks per Cluster
ChromBPNet trains each cluster’s model on that cluster’s own peaks.
add_group_coverages() builds pseudobulk replicates for each
cluster, and add_reproducible_peak_set() calls peaks on
them with MACS3. It
stores the merged peak set in proj$peak_set and each
cluster’s reproducible peaks in proj$group_peak_sets, which
is what gets exported below.
ChromBPNet recommends
relaxed peak calls (“MACS2 with p-value parameter threshold set to
0.01”), so -p 0.01 is appended to the default MACS
arguments.
proj <- add_group_coverages(proj, group_by = "atac_clusters", verbose = TRUE)
proj <- add_reproducible_peak_set(
proj,
group_by = "atac_clusters",
additional_params = paste(
"--call-summits --keep-dup all --shift -75 --extsize 150 --nomodel --nolambda",
"-p 0.01"
),
verbose = TRUE
)Output
## 2026-12-04 01:31:00 Group coverages: 10,271 cells in 11 groups by 'atac_clusters', 3 samples
## Split 11 groups into 22 replicates (56-500 cells each) [0.0s]
## 2026-12-04 01:31:00 Group coverages finished [0.0s]: added group coverages 'atac_clusters' (11 groups, 22 replicates)
## 2026-12-04 01:31:00 Peak calling: 22 replicates in 11 groups (6,584 cells), macs3, reproducibility 2
## C4_Rep1: called 179,996 summits from 500 cells [25.6s]
## C4_Rep2: called 148,396 summits from 500 cells [21.7s]
## C8_Rep1: called 510,005 summits from 247 cells [49.8s]
## C8_Rep2: called 482,310 summits from 168 cells [48.6s]
## C2_Rep1: called 777,719 summits from 500 cells [74.9s]
## C2_Rep2: called 168,118 summits from 65 cells [16.9s]
## C9_Rep1: called 500,637 summits from 193 cells [48.9s]
## C9_Rep2: called 514,360 summits from 193 cells [50.3s]
## C10_Rep1: called 562,769 summits from 167 cells [56.4s]
## C10_Rep2: called 306,035 summits from 143 cells [31.9s]
## C6_Rep1: called 197,121 summits from 500 cells [27.3s]
## C6_Rep2: called 240,438 summits from 339 cells [32.4s]
## C1_Rep1: called 195,388 summits from 500 cells [31.2s]
## C1_Rep2: called 506,244 summits from 263 cells [52.1s]
## C5_Rep1: called 189,624 summits from 500 cells [26.2s]
## C5_Rep2: called 587,875 summits from 191 cells [57.0s]
## C3_Rep1: called 157,364 summits from 500 cells [24.0s]
## C3_Rep2: called 156,883 summits from 56 cells [15.5s]
## C11_Rep1: called 649,255 summits from 322 cells [65.7s]
## C11_Rep2: called 256,136 summits from 87 cells [24.8s]
## C7_Rep1: called 171,844 summits from 500 cells [24.8s]
## C7_Rep2: called 357,999 summits from 150 cells [35.3s]
## C4: kept 73,080 reproducible peaks (2 replicates) [15.4s]
## C8: kept 81,863 reproducible peaks (2 replicates) [75.6s]
## C2: kept 51,086 reproducible peaks (2 replicates) [64.3s]
## C9: kept 96,302 reproducible peaks (2 replicates) [74.9s]
## C10: kept 94,421 reproducible peaks (2 replicates) [62.1s]
## C6: kept 98,467 reproducible peaks (2 replicates) [22.6s]
## C1: kept 77,086 reproducible peaks (2 replicates) [46.5s]
## C5: kept 89,712 reproducible peaks (2 replicates) [48.9s]
## C3: kept 38,904 reproducible peaks (2 replicates) [9.8s]
## C11: kept 68,420 reproducible peaks (2 replicates) [79.4s]
## C7: kept 72,119 reproducible peaks (2 replicates) [28.6s]
## Merged 841,460 group peaks into 298,564 peaks [3.6s]
## Annotated 298,564 peaks: 111,477 Distal, 160,516 Intronic, 26,571 Promoter [5.0s]
## 2026-12-04 01:53:15 Peak calling finished [1394.0s]: added peak set (298,564 peaks) and 11 group peak setsGrouping Fragments by Cluster
Each of the atac_clusters from the Hematopoiesis
Tutorial becomes one pseudobulk, one fragment file, and one ChromBPNet
model.
plot_embedding(proj, col_name = "atac_clusters", title = "UMAP - Clusters")
pseudobulk_fragments() groups cells by a metadata column
and returns one lazy fragment set per group. Fragments are pooled across
the three datasets and pass through the same blacklist and chromosome
filters as peak calling, so the exported fragments and peaks cover the
same regions.
pb <- pseudobulk_fragments(proj, group_by = "atac_clusters", verbose = TRUE)
pbOutput
## 2026-12-04 01:53:16 Pseudobulk: 10,271 cells in 11 groups by 'atac_clusters', 3 datasets
## 2026-12-04 01:53:16 Pseudobulk finished [0.0s]: 11 fragment sets (37,183,949 fragments)
## MarchRPseudobulk: 11 groups (by atac_clusters)
## # A tibble: 11 × 5
## group n_cells n_fragments median_tss_enrichment passed_depth_gate
## <chr> <int> <dbl> <dbl> <lgl>
## 1 C4 2559 8833019 16.3 NA
## 2 C8 421 1298533 19.2 NA
## 3 C2 1357 3077258 19.5 NA
## 4 C9 386 1273594 15.8 NA
## 5 C10 319 1205706 14.6 NA
## 6 C6 1199 5193773 14.6 NA
## 7 C1 840 4399296 20.7 NA
## 8 C5 829 3013265 16.5 NA
## 9 C3 597 2840957 22.5 NA
## 10 C11 409 1294367 16.9 NA
## 11 C7 1355 4754181 17.7 NAThe manifest reports cells, fragments, and median TSS enrichment for
each cluster. Each ChromBPNet model learns its own count scale, so
clusters do not need matched depth. To leave shallow clusters out of the
export, set min_fragments with
on_low_depth = "drop". ChromBPNet does not publish a
minimum depth, so pick the floor for your own data.
Writing Fragment Files
write_pseudobulk_fragments() writes one gzipped,
10x-style fragment file per cluster to
export_dir/fragments/. These are the -ifrag
inputs.
pb <- write_pseudobulk_fragments(pb, export_dir, verbose = TRUE)Output
## 2026-12-04 01:53:16 Fragment export: 11 groups to '~/.cache/marchr_hematopoiesis/chrombpnet_tutorial'
## C4: wrote fragments for 2,559 cells [5.2s]
## C8: wrote fragments for 421 cells [1.3s]
## C2: wrote fragments for 1,357 cells [2.1s]
## C9: wrote fragments for 386 cells [1.2s]
## C10: wrote fragments for 319 cells [1.2s]
## C6: wrote fragments for 1,199 cells [3.4s]
## C1: wrote fragments for 840 cells [2.7s]
## C5: wrote fragments for 829 cells [2.2s]
## C3: wrote fragments for 597 cells [2.0s]
## C11: wrote fragments for 409 cells [1.2s]
## C7: wrote fragments for 1,355 cells [3.0s]
## 2026-12-04 01:53:40 Fragment export finished [25.6s]: added manifest column 'fragments_path' (11 files)
read.delim(pb$manifest$fragments_path[[1]], header = FALSE, nrows = 5)Output
## V1 V2 V3 V4
## 1 chr1 750947 751028 scATAC_PBMC_R1#GCGCATGTCTCTCAAT-1
## 2 chr1 750989 751069 scATAC_BMMC_R1#AATGGCTTCTTGTGCC-1
## 3 chr1 751650 751736 scATAC_PBMC_R1#CAAGCTAAGTGCCCTG-1
## 4 chr1 751658 751726 scATAC_BMMC_R1#TTCTGTAGTAATGTAG-1
## 5 chr1 751663 751804 scATAC_PBMC_R1#CATAACGTCGAGCGCT-1The columns are chromosome, start, end, and cell barcode. ChromBPNet reads only the first three: its fragment reader expects “tsv with columns chr, start, end and optionally more columns”, so Cell Ranger’s fifth duplicate-count column is not needed. Coordinates are written unshifted, because ChromBPNet estimates and corrects the Tn5 shift itself; its guide notes that “you do not need to worry about pre-shifting the fragments”.
ArchR’s getGroupFragments() writes the same four
columns. One difference is that MarchR has already removed fragments in
blacklisted regions and on chrM.
Writing Peaks
write_pseudobulk_peaks() writes each cluster’s peaks to
export_dir/peaks/. scope = "group" skips the
merged peak set; the default, "both", also writes it as
union.narrowPeak.
pb <- write_pseudobulk_peaks(pb, export_dir, scope = "group", verbose = TRUE)
read.delim(pb$manifest$peaks_path[[1]], header = FALSE, nrows = 5)Output
## 2026-12-04 01:53:40 Peak export: 11 groups to '~/.cache/marchr_hematopoiesis/chrombpnet_tutorial'
## Wrote 11 group peak files (38,904-98,467 peaks) [2.5s]
## 2026-12-04 01:53:42 Peak export finished [2.5s]: added manifest column 'peaks_path' (11 files)
## V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
## 1 chr3 48506897 48507398 C4_1 1000 . 306.145 306.145 -1 250
## 2 chr1 31229488 31229989 C4_2 1000 . 219.173 219.173 -1 250
## 3 chr19 48018320 48018821 C4_3 1000 . 219.173 219.173 -1 250
## 4 chr8 103251249 103251750 C4_4 1000 . 303.254 303.254 -1 250
## 5 chr17 76194921 76195422 C4_5 1000 . 205.590 205.590 -1 250ChromBPNet’s -p takes a “10 column bed file of peaks”
and centers each training window at start (column 2) plus summit (column
10). MarchR peaks are 501 bp wide around the MACS summit, and column 10
holds the stored summit offset.
plot_browser_track() shows the signal each cluster’s
model will learn. At the CD14 locus, nearby gene promoters are open in
every cluster, while several distal peaks are open mainly in C8, so each
cluster’s model trains on a different accessibility profile. The peaks
track shows the merged peak set.
region <- gene_region(
get_gene_annotations(proj, "genes"),
gene_symbol = "CD14",
upstream = 50000L,
downstream = 50000L
)
plot_browser_track(
proj,
region = region,
group_by = "atac_clusters",
frags_name = "ATAC",
tracks = c("coverage", "peaks", "gene"),
bins = 400L,
norm_method = "ReadsInTSS",
title = "CD14"
)
Chromosome Sizes and Blacklist
chrombpnet prep splits assigns every chromosome in the
chromosome sizes file to a fold, so the file should list only the
chromosomes to train on. This project’s chromosome sizes already cover
just chr1-22, chrX, and chrY, the same set the ChromBPNet tutorial
uses.
pb <- write_pseudobulk_chrom_sizes(pb, export_dir, get_chrom_sizes(proj))
readLines(pb$params$chrom_sizes_path, n = 3)Output
## [1] "chr1\t249250621" "chr2\t243199373" "chr3\t198022430"ChromBPNet uses the blacklist twice: to filter peaks and to keep background regions out of blacklisted sequence. Writing the project’s own blacklist keeps it consistent with the fragment filtering above.
blacklist <- get_blacklist(proj)
write.table(
blacklist[, c("chr", "start", "end")],
file.path(export_dir, "blacklist.bed"),
sep = "\t", quote = FALSE, row.names = FALSE, col.names = FALSE
)Writing the Manifest
write_pseudobulk_manifest() writes
manifest.tsv, which works as a samplesheet for workflow
managers, and manifest.json, which also records the export
parameters and package versions. Each writer above added its file paths
to the manifest as a new column.
write_pseudobulk_manifest(pb, export_dir)
colnames(pb$manifest)Output
## [1] "group" "n_cells" "n_fragments"
## [4] "median_tss_enrichment" "passed_depth_gate" "fragments_path"
## [7] "peaks_path"
list.files(export_dir, recursive = TRUE)Output
## [1] "blacklist.bed" "chrom.sizes" "fragments/C1.tsv.gz"
## [4] "fragments/C10.tsv.gz" "fragments/C11.tsv.gz" "fragments/C2.tsv.gz"
## [7] "fragments/C3.tsv.gz" "fragments/C4.tsv.gz" "fragments/C5.tsv.gz"
## [10] "fragments/C6.tsv.gz" "fragments/C7.tsv.gz" "fragments/C8.tsv.gz"
## [13] "fragments/C9.tsv.gz" "manifest.json" "manifest.tsv"
## [16] "peaks/C1.narrowPeak" "peaks/C10.narrowPeak" "peaks/C11.narrowPeak"
## [19] "peaks/C2.narrowPeak" "peaks/C3.narrowPeak" "peaks/C4.narrowPeak"
## [22] "peaks/C5.narrowPeak" "peaks/C6.narrowPeak" "peaks/C7.narrowPeak"
## [25] "peaks/C8.narrowPeak" "peaks/C9.narrowPeak"If you train your own bias model (below), use a deep cluster so the model sees as many background reads as possible. The deepest cluster here is:
pb$manifest$group[which.max(pb$manifest$n_fragments)]Output
## [1] "C4"Preparing ChromBPNet Inputs
The remaining steps run in a shell where ChromBPNet is installed, and follow its Preprocessing, Bias model training, and ChromBPNet training pages. The tutorial fragments are aligned to hg19, so every reference below is hg19, whereas the ChromBPNet tutorial uses hg38.
DATA=~/.cache/marchr_hematopoiesis/chrombpnet_tutorial
mkdir -p $DATA/downloads
wget https://hgdownload.soe.ucsc.edu/goldenPath/hg19/bigZips/hg19.fa.gz -O $DATA/downloads/hg19.fa.gz
gunzip $DATA/downloads/hg19.fa.gzChromBPNet reads 2,114 bp of sequence around each summit, so peaks must also stay clear of the blacklist “extended by 1057 bp on both sides”:
bedtools slop -i $DATA/blacklist.bed -g $DATA/chrom.sizes -b 1057 > $DATA/blacklist_slop1057.bed
mkdir -p $DATA/peaks_no_blacklist
for peaks in $DATA/peaks/*.narrowPeak; do
bedtools intersect -v -a $peaks -b $DATA/blacklist_slop1057.bed \
> $DATA/peaks_no_blacklist/$(basename $peaks)
doneSplit chromosomes into training, validation, and test sets. This is fold 0 of the standard ENCODE folds used by the ChromBPNet tutorial:
mkdir -p $DATA/splits
chrombpnet prep splits -c $DATA/chrom.sizes -tcr chr1 chr3 chr6 -vcr chr8 chr20 \
-op $DATA/splits/fold_0Generate GC-matched background regions for each cluster. Each run
writes nonpeaks/<cluster>/output_negatives.bed:
for peaks in $DATA/peaks_no_blacklist/*.narrowPeak; do
group=$(basename $peaks .narrowPeak)
mkdir -p $DATA/nonpeaks/$group
chrombpnet prep nonpeaks \
-g $DATA/downloads/hg19.fa \
-p $peaks \
-c $DATA/chrom.sizes \
-fl $DATA/splits/fold_0.json \
-br $DATA/blacklist.bed \
-o $DATA/nonpeaks/$group/output
doneChoosing a Bias Model
The bias model captures Tn5 sequence preference so the main model can learn TF activity apart from it. There are two options.
Pre-trained. ChromBPNet’s FAQ advises picking “a pre-trained bias model that is similar in experimental setting to the current dataset”. The ChromBPNet tutorial’s ATAC bias model is:
mkdir -p $DATA/bias_model
wget https://storage.googleapis.com/chrombpnet_data/input_files/bias_models/ATAC/ENCSR868FGK_bias_fold_0.h5 \
-O $DATA/bias_model/ENCSR868FGK_bias_fold_0.h5
BIAS=$DATA/bias_model/ENCSR868FGK_bias_fold_0.h5Trained on one cluster. Train a single bias model on
a deep cluster and reuse it for every cluster, as the HDMA
atlas does. -b is the bias threshold factor, for which
ChromBPNet recommends starting at 0.5 for ATAC.
BIAS_GROUP=C8
mkdir -p $DATA/bias_model/$BIAS_GROUP
chrombpnet bias pipeline \
-ifrag $DATA/fragments/$BIAS_GROUP.tsv.gz \
-d "ATAC" \
-g $DATA/downloads/hg19.fa \
-c $DATA/chrom.sizes \
-p $DATA/peaks_no_blacklist/$BIAS_GROUP.narrowPeak \
-n $DATA/nonpeaks/$BIAS_GROUP/output_negatives.bed \
-fl $DATA/splits/fold_0.json \
-b 0.5 \
-o $DATA/bias_model/$BIAS_GROUP/
BIAS=$DATA/bias_model/$BIAS_GROUP/models/bias.h5Read evaluation/overall_report.html before using a
trained bias model. It should learn the expected Tn5 bias motif and no
TF motifs; the FAQ explains how to tune -b when it does
not.
Training ChromBPNet per Cluster
Train one bias-factorized model per cluster. Each run is a GPU job, so on a cluster submit one job per cluster instead of running the loop as written.
for frag in $DATA/fragments/*.tsv.gz; do
group=$(basename $frag .tsv.gz)
mkdir -p $DATA/chrombpnet_model/$group
chrombpnet pipeline \
-ifrag $frag \
-d "ATAC" \
-g $DATA/downloads/hg19.fa \
-c $DATA/chrom.sizes \
-p $DATA/peaks_no_blacklist/$group.narrowPeak \
-n $DATA/nonpeaks/$group/output_negatives.bed \
-fl $DATA/splits/fold_0.json \
-b $BIAS \
-o $DATA/chrombpnet_model/$group/
doneEach model’s evaluation/overall_report.html is its
quality check. The bias-corrected model,
models/chrombpnet_nobias.h5, should learn the expected TF
motifs and no enzyme bias. For production models, the FAQ recommends
training all five folds and averaging the predictions and contribution
scores across them.
Optional: Coverage Tracks
ChromBPNet builds its own signal tracks from the fragment files, so no coverage file is needed for training. To view observed signal next to model predictions in a genome browser, MarchR can write per-cluster insertion coverage in a single pass over the fragments:
pb <- write_pseudobulk_coverage(pb, export_dir)
pb <- write_pseudobulk_bigwig(pb, export_dir, get_chrom_sizes(proj))