bacha is a fast, memory-efficient method to measure chromatin accessibility
scores aggregated across a set of genomic loci, relative to a matched set of
control regions in the cell.
These scores are conceptually similar to those from ChromVAR, but with several advantages.
bachauses DNA accessibilities directly surrounding the motif sites, rather than entire peak regions- The score for each cell is completely independent from other cells in the dataset. This makes the
bachascores stable (they do not change upon subsetting or merging datasets) and more easily comparable across datasets. bachascores are independent of peak calling, and so do not change according to the set of peaks used.bachahas a highly efficient implementation and is much faster to compute with lower memory requirements compared to ChromVAR.
Bacha is a command-line Rust tool. It can be compiled from source by cloning the git repository:
git clone git@github.com:stuart-lab/bacha.git
cd bacha; cargo install --path .Compiling Bacha from source requires that you have the Rust compiler installed: https://rust-lang.org/tools/install/
Alternatively, you can download a pre-compiled binary file from the GitHub repository.
We provide an R package to enable easily running Bacha and adding the results to a Seurat object. To install the R package:
remotes::install_github("stuart-lab/bacha", subdir="bachaR")Note that building new Bacha index files is currently only supported through
the BachaIndex() function in the R package.
A Python package (bacha-py) is also provided for use with AnnData / scverse
workflows. It wraps the bacha command-line tool and returns
per-cell-per-motif scores attached to an AnnData object.
The package shells out to the bacha binary, so install the command-line tool
first (see above) and make sure it is on your PATH (or pass bacha_path=...).
Then install the Python package (requires Python >= 3.10):
pip install -e ./bacha-pyTo quantify Bacha scores per cell from an existing index file:
bacha count -f <fragments.tsv.gz> -i <bacha.index> -c <cells.txt> -o <output.csv.gz> To process multiple fragment files in a single bacha call, provide a tab-separated
manifest instead of -f/-c. This will output a single matrix containing scores
for all of the cells:
bacha count -m <manifest.tsv> -i <bacha.index> -o <output.csv.gz>Where manifest.tsv follows the following structure:
sample fragments cells
FN1 lung1/atac_fragments.tsv.gz lung1_cells.txt
FN2 lung2/atac_fragments.tsv.gz lung2_cells.txt
The header row is optional, blank lines and lines starting with # are ignored, and relative
paths are resolved against the manifest's own directory.
Every cell must belong to exactly one fragment file. Barcodes that repeat across samples (shared barcode whitelists) are fine: give the two cells distinct output names using the two-column form of the cells file, where the first column is the barcode as it appears in the fragment file and the second is the name to use in the output.
AAACGAAAGATTGGCT-1 sample1_AAACGAAAGATTGGCT-1
Full argument reference for Bacha:
Bias-adjusted aggregation of chromatin accessibility data
Usage: bacha <COMMAND>
count— Count Tn5 insertions and compute activity scoresview— View index file contents in plain textmotifs— Export the motif matrices stored in an index, in JASPAR formatsummary— Print summary information about an index file (header + per-TF site and control counts) without reading the site position blocks
Count Tn5 insertions and compute activity scores
Usage: bacha count [OPTIONS] --index <INDEX> --output <OUTPUT>
-
-f,--fragments <FRAGMENTS>— Path to the fragment file -
-i,--index <INDEX>— Bacha index file -
-c,--cells <CELLS>— File containing cell barcodes to include. One barcode per line, or two tab-separated columns where the first is the barcode as it appears in the fragment file and the second is the name to give that cell in the output. -
-m,--manifest <MANIFEST>— Tab-separated manifest listing several fragment files to score into one output matrix, as an alternative to -f/-c. Columns are sample, fragments and cells; a header row naming exactly those three is optional. Blank lines and lines beginning with '#' are ignored, and relative paths are resolved against the manifest's own directory. Files are counted one at a time, each using all --threads workers, and the Bacha index is loaded once for the whole run. Every cell must belong to exactly one fragment file: a Bacha score is a non-linear function of a cell's insertion counts, so counts for one cell split across two files cannot be combined. -
-o,--output <OUTPUT>— Output file name. Output will be a CSV file. If the file extension is .gz, the file will be compressed with gzip. -
-t,--threads <THREADS>— Total number of threads to use. Against a BGZF (bgzip-compressed) fragment file the budget is split: 2 threads inflate the file and the rest count it, so the two pools share --threads rather than adding to it. A plain gzip file is inflated inline by the reader thread, so every thread goes to counting. Also sets the number of gzip compression threads when the output path ends in .gz.Default value:
6 -
-l,--lambda <LAMBDA>— Shrinkage strength parameter (lambda)Default value:
10 -
-p,--pseudocount <PSEUDOCOUNT>— Pseudocount added to counts before log fold change calculation to avoid log(0)Default value:
1 -
--shrink-to <SHRINK_TO>— Shrinkage target.zero(default) is the posterior mean under a prior centred on no enrichment, so every score is pulled toward 0 by 1 - prec / (prec + lambda).meancentres the prior on each feature's pseudobulk log2 fold change, computed from the counts summed over every scored cell.Default value:
zeroPossible values:
zero: Shrink toward a log fold change of 0mean: Shrink toward the feature's pseudobulk log fold change over all scored cells
-
--feature-names <FEATURE_NAMES>— Label for each feature (row) in the output.name(default) uses the display name stored in the index, such as a TF gene symbol; when several features share a name, each of them is written asNAME (ID)so rows stay unique.iduses the feature's unique ID, such as a JASPAR matrix ID.Default value:
namePossible values:
name: The feature's display name (e.g. a gene symbol). A name shared by several features gets the ID appended to each, asNAME (ID), so every row stays uniqueid: The feature's unique ID (e.g. a JASPAR matrix ID)
-
--no-bounds-check— Disable the genome/assembly mismatch check. By default the run aborts on the first fragment coordinate that lies beyond the end of its chromosome in the index (an impossible position unless the fragment file and index were built on different genome assemblies). Pass this flag to skip the check and process such coordinates anyway.
View index file contents in plain text
Usage: bacha view [OPTIONS] <INDEX>
<INDEX>— Bacha index file to view
-H,--header-only— Print header information only
Export the motif matrices stored in an index, in JASPAR format. Each record's header line is >ID<TAB>NAME<TAB>TYPE, where TYPE is the value type of the matrix as given to the index builder: counts, probabilities, logodds or other. Features without a matrix are left out.
Usage: bacha motifs <INDEX>
<INDEX>— Bacha index file
Print summary information about an index file (header + per-TF site and control counts) without reading the site position blocks
Usage: bacha summary <INDEX>
<INDEX>— Bacha index file to summarise
library(bacha)
index <- "hg38.h14.v1.bi"
fragment_file <- "fragments.tsv.gz"
cells <- c("AAACGAAAGAGAGGTA-1", "AAACGAAAGCAGGAGG-1", "AAACGAAAGGAAGAAC-1", "AAACGAAAGTCGACCC-1", "AAACGAACAAGCACTT-1")
bacha_matrix <- Bacha(fragment_file, cells, index)To run on a Seurat object containing a Signac ChromatinAssay or ChromatinAssay5:
library(bacha)
library(Signac)
library(Seurat)
index <- "hg38.h14.v1.bi"
obj <- readRDS("object.rds") # load a Seurat object containing a ChromatinAssay
obj <- Bacha(obj, index)import anndata as ad
import bacha
adata = ad.read_h5ad("pbmc.h5ad") # .obs_names are the cell barcodes
# Return a new (cells x motifs) AnnData of scores
result = bacha.count(
adata,
fragments="fragments.tsv.gz",
index="hg38.h14.v1.bi",
inplace=False,
)
# Or write the scores back into the input object:
# adata.obsm["bacha"] -> (cells x motifs) score matrix
# adata.uns["bacha_motifs"] -> motif IDs (the columns)
bacha.count(
adata,
fragments="fragments.tsv.gz",
index="hg38.h14.v1.bi",
inplace=True,
)We provide pre-built index files for a variety of species here: https://github.com/stuart-lab/bacha-index