Build a gene x cell UMI count matrix directly from a CellRanger-tagged BAM.
bam2mtx walks a BAM's alignment records once, groups reads by
(cell barcode, gene) using the CB/GX tags CellRanger wrote into
the file, and counts distinct UMIs (the UB tag) per group. Useful
when you have a CellRanger BAM but the matrix was never archived or
got lost.
- Python >= 3.9
- pysam >= 0.19
- A BAM produced by CellRanger's Gene Expression pipeline (or anything
writing the same tag conventions, e.g. STARsolo), specifically the
CB(corrected cell barcode),UB(corrected UMI) andGX(gene ID(s) a read confidently overlaps) tags.
git clone https://github.com/powellgenomicslab/bam2mtx.git
cd bam2mtx
pip install .This installs the bam2mtx command.
bam2mtx <bam> -o <out_dir> [--xf-mode {robust,strict,off}] [--threads N] [--top-n N] [--v2-format] [--split-regions]| Option | Description |
|---|---|
bam |
Path to a CellRanger Gene Expression BAM (CB/UB/GX/GN tagged). |
-o, --out-dir |
Output directory (required). |
--xf-mode |
How to use 10x's xf "extra flags" tag for read selection. See xf-mode filtering below. Default: robust. |
--threads |
BAM decompression threads. Default: 4. |
--top-n |
Keep only the top N barcodes by total UMI count. Default: keep every observed barcode (the "raw" matrix). |
--v2-format |
Write the legacy uncompressed CellRanger v2 layout (matrix.mtx/genes.tsv/barcodes.tsv, 2-column genes.tsv) instead of the modern gzipped v3 layout (matrix.mtx.gz/features.tsv.gz/barcodes.tsv.gz, 3-column features.tsv.gz). Default: v3. |
--split-regions |
Additionally write <out_dir>/exon/ and <out_dir>/intron/ matrices, partitioning the counted molecules by CellRanger's RE region tag. See Exon / intron matrices below. Default: off. |
Standard CellRanger MatrixMarket layout, loadable with scipy.io.mmread,
Seurat::Read10X, scanpy.read_10x_mtx, or any other standard 10x-format
reader. The gene ID -> gene symbol map is built from the BAM's own
GX/GN tags.
10x's xf tag is a per-read "extra flags" bitmask (1 confidently mapped,
2 discarded for a better-supported feature, 4 discordant gene pair, 8
representative read for a molecule, 16 maps to exactly one feature, 32
removed by targeted UMI filtering — see the
10x BAM tag docs).
bam2mtx uses it (in addition to requiring CB+UB+GX and a single-gene
GX) to decide which reads to count:
robust(default) — a bitmask test: bits1(confidently mapped to the transcriptome) and16(maps to exactly one feature) set, bit2(discarded in favour of a different feature with higher read support) clear.strict—xf == 17(1+16) orxf == 25(1+8+16) exactly.off— ignorexfentirely; rely only onCB+UB+GXpresence and single-geneGX.
With --split-regions, bam2mtx also partitions the counted molecules by
CellRanger's per-read RE region tag (E exonic, N intronic, I
intergenic) and writes two extra matrices alongside the total:
<out_dir>/ total (every counted molecule, exactly as without the flag)
<out_dir>/exon/ molecules whose counted reads were all exonic
<out_dir>/intron/ molecules with at least one intronic read
All three share identical gene rows and barcode columns (and honour --top-n
and --v2-format), so they can be loaded and stacked directly, e.g. as
spliced/unspliced layers for RNA velocity. The rule is per molecule, not per
read: a UMI whose reads include even one RE:N read is called intronic, on the
reasoning that intronic coverage is evidence the molecule was unspliced. A
molecule whose reads carry no RE tag at all is kept in the total but appears
in neither partition; the count of such molecules is reported on stderr.
This is only informative for BAMs produced with introns included (the
default from CellRanger 7.0, or --include-introns on CellRanger 5.x/6.x).
In exon-only runs CellRanger does not assign a GX gene tag to intronic
reads at all, so they are never counted and intron/ comes out empty (the
tool warns when this happens). CellRanger itself does not produce a
spliced/unspliced split, so there is no CellRanger output to validate the
partition against; the total matrix is unaffected by the flag.
I checked bam2mtx against a CellRanger run using 10x Genomics'
public "1k PBMCs from a Healthy Donor (v3 chemistry)"
dataset. Running it on CellRanger 3.0.0's own BAM with the default
--xf-mode robust reproduced CellRanger's own raw feature-barcode matrix
exactly.