Skip to main content

CountMut

Unified ultra-fast strand-aware mutation counter — C backend, Python wrapper.

CountMut counts base/substitution ratios from BAM files with fast C core and a thin Python wrapper. It fuses the two classic ways of walking a BAM:

  • pileup-based (bam_mplp_auto / pysam pileup) — fast, sees indels/ref-skips, general.
  • read-walk (countmut's "no pileup") — walk reads directly, only touch the target sites.

Both produce identical output, and the tool can process whole genomes in parallel (threads).

Why it's fast & correct

  • The hot loop (BAM read, pileup, per-(site,strand) base counting, mate-overlap dedup, quality/conversion classification) is in C (backend/countmut_core, built on the self-contained htslib subset from lh3/minipileup).
  • Strand-aware (countmut biological-strand rule for paired-end reads).
  • Paired-end overlap dedup: at an overlapping position a fragment is counted once, choosing the best mate by (mapq, read1, base-qual) — the thing minipileup gets wrong.
  • Parallel: divides the genome into bins and processes them across threads (--threads).
  • Memory-clean (verified under AddressSanitizer).

Install

pip install -e .
# or, to prebuild the C core:
make backend

Quick start

# strand-aware A->G mutation count (bisulfite / m6A style)
countmut -i in.bam -r ref.fa -o mut.tsv --ref-base A --mut-base G

# per-site base counts (perbase/mpileup style)
countmut -i in.bam -r ref.fa --mode base -o depth.tsv

# alleles -> VCF (minipileup style)
countmut -i in.bam -r ref.fa --mode allele --vcf -o allele.vcf

Modes

Mode Output
mutation chrom pos strand motif u0 u1 u2 m0 m1 m2 [o0 o1 o2] (strand-aware substitution table)
base chrom pos [strand] ref depth a c g t n [ins del ref_skip fail]
allele chrom pos ref depth ref_count alt alt_count, or VCF with --vcf

Filtering with expressions (-e / -p)

Filtering is done with samtools-style filter expressions — there are no separate --min-mapq/--min-baseq/--trim-* flags; write them as expressions instead.

  • -e, --expression <STR> — per-base read filter (samtools SAM fields).
  • -p, --pile-expression <STR> — per-site filter (pileup fields).

Grammar is the samtools filter=STRING expression language (C-style precedence, &&/||/!, bit fields, tags, regex). See docs/filter_grammar.md.

# keep high-quality, non-5prime, properly paired reads
countmut -i x.bam -r ref.fa -e "mapq >= 20 && bq >= 20 && dist5 >= 2 && flag & PROPER_PAIR"

# restrict to one RG group
countmut -i x.bam -r ref.fa -e "tag('RG') == 'sampleA'"

# report only A-reference sites with depth >= 5 and > 2 G alleles
countmut -i x.bam -r ref.fa -p "ref == 'A' && depth >= 5 && g > 2"

Read variables: mapq, flag (+ flag.dup, flag.unmap, ...), qname, pos, endpos, pnext, rname, mrname, tlen, qlen, rlen, ncigar, seq, qual, sclen, hclen, bq, dist5/dist3, strand, [NM]/[RG] tags, avg(qual), exists([NM]), sqrt(mapq), ...

Site variables: depth, pos, ref, a c g t n, ins, del, ref_skip, fail.

When -e/-p is given, counting runs on the Python engine (the C core cannot evaluate strings). Without expressions, the fast C backend is used.

Engine selection

--engine {auto|read-walk|pileup} (default auto):

  • auto → read-walk for mutation (targeted sites), pileup for base/allele.
  • read-walk / pileup → force a strategy.

Options

-i/--input, -r/--reference, -o/--output
--mode {mutation,base,allele}   --engine {auto,read-walk,pileup}
--region, --threads/-t
--ref-base, --mut-base, --pad, --save-rest
--split-strand, --count-indels, --min-depth, --min-allele-support, --vcf
-e/--expression, -p/--pile-expression

Design

countmut/
  cli.py               rich CLI (routes to the backend)
  backend.py           builds/loads the C binary; calls it; Python fallback
  pipeline.py          region binning + parallel dispatch
  model.py             FilterConfig / MutationConfig / StrandConfig / EngineConfig
  engine_readwalk.py   read-walk engine
  engine_pileup.py     pileup engine
  formatter.py         TSV/VCF renderers
  expression.py        samtools-style filter-expression engine
backend/
  countmut_core.c      the computation core (htslib subset)
  countmut_core_main.c CLI wrapper
  Makefile             builds the `countmut_core` binary

Both engines fill the same SiteColumn (per-site, per-strand base counts), so the two "ways" are interchangeable; the C backend implements the pileup engine.

References this tool learns from

  • minipileup — pileup walk, filters, allele counting
  • perbase / pbr — mate-aware overlap dedup, base counts
  • countmut — biological strand, bisulfite NS/Zf/Yf tiers
  • mpileup / cpup — base-count output
  • samtools --input-fmt-option filter=STRING — the expression grammar

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

countmut-0.1.1.tar.gz (54.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

countmut-0.1.1-py3-none-any.whl (49.0 kB view details)

Uploaded Python 3

File details

Details for the file countmut-0.1.1.tar.gz.

File metadata

  • Download URL: countmut-0.1.1.tar.gz
  • Upload date:
  • Size: 54.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for countmut-0.1.1.tar.gz
Algorithm Hash digest
SHA256 cda99102e3b66b6b387bfc87d187ce00474fb7a1028511bc8a75510500f988b1
MD5 56922cfee6fec78eee2177d0e2264bf8
BLAKE2b-256 39cc7c6a5661c2ed23bf593e5fc142a18377299ab23512f72d9de017958722bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for countmut-0.1.1.tar.gz:

Publisher: publish.yml on y9c/countmut

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file countmut-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: countmut-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 49.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for countmut-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c921d3af1fd9a5fb8972f550d9857a6d54d0c6bfd59ca6f1d296b03f974bbf41
MD5 29f9824b30e7eb63e35d6af17db4e28d
BLAKE2b-256 412984d901a0b612413c8357785ce3873da2349e22664ab71aaf1d8734da1936

See more details on using hashes here.

Provenance

The following attestation bundles were made for countmut-0.1.1-py3-none-any.whl:

Publisher: publish.yml on y9c/countmut

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.0

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

This release

0.1.1 This release

2 files

0.1.0

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page