Skip to content
powellgenomicslabPublic

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bam2mtx logo

bam2mtx

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.

Requirements

  • 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) and GX (gene ID(s) a read confidently overlaps) tags.

Installation

git clone https://github.com/powellgenomicslab/bam2mtx.git
cd bam2mtx
pip install .

This installs the bam2mtx command.

Usage

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.

Output

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.

xf-mode filtering

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: bits 1 (confidently mapped to the transcriptome) and 16 (maps to exactly one feature) set, bit 2 (discarded in favour of a different feature with higher read support) clear.
  • strict — xf == 17 (1+16) or xf == 25 (1+8+16) exactly.
  • off — ignore xf entirely; rely only on CB+UB+GX presence and single-gene GX.

Exon / intron matrices

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.

Validation

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.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages