| Title: | Bioconductor-Friendly Multithreaded BAM Processing |
|---|---|
| Description: | Multithreaded sequential BAM processing built on top of the ompBAM C++ engine. BamScale provides user-friendly BAM read and scan interfaces designed for compatibility with existing Bioconductor workflows. |
| Authors: | Chirag Parsania [aut, cre] |
| Maintainer: | Chirag Parsania <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.99.14 |
| Built: | 2026-09-15 11:37:32 UTC |
| Source: | https://github.com/bioc/BamScale |
bam_count() provides a fast chromosome-level count summary, honoring key
filtering fields from ScanBamParam (mapqFilter, flag, and which).
bam_count( file, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE, include_unmapped = TRUE )bam_count( file, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE, include_unmapped = TRUE )
file |
BAM input ( |
param |
Optional |
threads |
Requested number of OpenMP threads. May be capped when
|
BPPARAM |
|
auto_threads |
Logical; when |
include_unmapped |
Whether to include an extra |
Parallelism behavior matches bam_read(): BPPARAM distributes work across
BAM files, while threads controls OpenMP work within each file. If
auto_threads = TRUE and BPPARAM has multiple workers, BamScale first
limits the number of concurrently active workers to preserve the requested
per-file thread count within the detected core budget, then caps per-file
OpenMP threads only if a single file would still oversubscribe the machine.
For one file: a data.frame with columns seqname, seqlength, count.
For multiple files: named list of such data.frames.
bam <- ompBAM::example_BAM("Unsorted") bam_count(bam, threads = 2)bam <- ompBAM::example_BAM("Unsorted") bam_count(bam, threads = 2)
Computes per-base read coverage by folding a per-contig difference array inside
the multithreaded C++ reader and run-length encoding the result, without ever
materialising the alignments as an R GAlignments object. This is the fast path
for coverage / bigWig generation on large BAMs: the standard route
(GenomicAlignments::coverage(readGAlignments(file))) builds a full
GAlignments in R first, which is the dominant cost on 100M+-read files.
bam_coverage( file, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE )bam_coverage( file, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE )
file |
A BAM file path, Rsamtools::BamFile, or a vector / |
param |
Optional Rsamtools::ScanBamParam (or a compatible list). Only its
|
threads |
Number of OpenMP threads for within-file decoding. |
BPPARAM |
A BiocParallel::BiocParallelParam for across-file parallelism
when |
auto_threads |
Logical; if |
The result is byte-identical (identical()) to
GenomicAlignments::coverage(GenomicAlignments::readGAlignments(file)): CIGAR
operations M, =, X and D contribute to coverage, N introduces a gap,
and I/S/H/P are ignored; only unmapped reads (SAM flag 0x4) are
dropped by default – secondary, supplementary, duplicate and QC-fail reads are
counted, exactly as readGAlignments() does.
An IRanges::RleList (SimpleRleList) of integer coverage, one element
per BAM-header sequence in header order, each of length equal to the header
sequence length (contigs with no reads are all-zero runs). For multiple input
files, a named list of such RleLists.
fragment_sizes(), mapq_dist(), bam_read().
Computes per-base coverage (identical to bam_coverage()) and writes it
directly to a bigWig file entirely in C++ via the bundled libBigWig, without
ever materialising the coverage as an R object. This is the fast path for
generating coverage tracks from large BAMs: the usual route
(rtracklayer::export.bw(bam_coverage(file), out)) round-trips a large
RleList through R, and the serialisation dominates. Only non-zero coverage
runs are written (uncovered bases are implicitly zero, the bigWig convention);
importing the file reconstructs full-length coverage from the header lengths.
bam_coverage_bigwig( file, outfile, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE, n_zooms = 10L, compress_level = -1L, parallel = TRUE, verbose = FALSE )bam_coverage_bigwig( file, outfile, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE, n_zooms = 10L, compress_level = -1L, parallel = TRUE, verbose = FALSE )
file |
A BAM file path, Rsamtools::BamFile, or a vector / |
outfile |
Output bigWig path(s); must have the same length as |
param |
Optional Rsamtools::ScanBamParam (or a compatible list). Only its
|
threads |
Number of OpenMP threads for within-file decoding. |
BPPARAM |
A BiocParallel::BiocParallelParam for across-file parallelism
when |
auto_threads |
Logical; if |
n_zooms |
Maximum number of bigWig zoom levels to generate (default |
compress_level |
Integer zlib deflate level for the bigWig blocks: |
parallel |
Logical; if |
verbose |
Logical; if |
The written values are identical to
GenomicAlignments::coverage(GenomicAlignments::readGAlignments(file))
(M/=/X/D contribute, N is a gap, I/S/H/P are ignored; only
unmapped reads are dropped by default). Coverage values are integers stored as
the bigWig float type, which is exact for depths up to 2^24.
The output path(s), invisibly.
bam_coverage() for the in-memory RleList.
bam_read() is a multithreaded sequential BAM reader built on top of
ompBAM. The interface is designed to be familiar to users of
Rsamtools::scanBam(), GenomicAlignments::readGAlignments(), and
GenomicAlignments::readGAlignmentPairs().
bam_read( file, param = NULL, what = NULL, tag = NULL, as = c("DataFrame", "data.frame", "GAlignments", "GAlignmentPairs", "scanBam"), seqqual_mode = c("compatible", "compact"), threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE, use.names = FALSE, with.which_label = FALSE, include_unmapped = TRUE )bam_read( file, param = NULL, what = NULL, tag = NULL, as = c("DataFrame", "data.frame", "GAlignments", "GAlignmentPairs", "scanBam"), seqqual_mode = c("compatible", "compact"), threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE, use.names = FALSE, with.which_label = FALSE, include_unmapped = TRUE )
file |
A BAM input. Supported values are:
|
param |
Optional |
what |
Character vector of fields to return, similar to
|
tag |
Character vector of 2-letter tag names to extract. |
as |
Output format:
|
seqqual_mode |
Controls representation of
|
threads |
Requested number of OpenMP threads used for
reading/decompression. May be capped when |
BPPARAM |
|
auto_threads |
Logical; when |
use.names |
Passed to alignment object conversion. When |
with.which_label |
Logical; if |
include_unmapped |
Logical; whether unmapped records are retained
(subject to |
bam_read() is intentionally column-compatible with common BAM fields used by
Bioconductor workflows and can be used as a fast drop-in reader before
conversion to downstream classes.
Parallelism model:
BPPARAM parallelizes across files (one file per BiocParallel worker).
threads parallelizes within each file via OpenMP.
Effective total concurrency is approximately
min(length(file), BiocParallel::bpnworkers(BPPARAM)) * threads.
If auto_threads = TRUE and BPPARAM has multiple workers, BamScale
first limits the number of concurrently active workers to preserve the
requested per-file thread count within the detected core budget, then
caps per-file OpenMP threads only if a single file would still
oversubscribe the machine.
Compatibility notes:
rname, mrnm, and strand are returned as factor columns, matching
Rsamtools::scanBam(). rname/mrnm levels are the BAM header reference
names; unmapped or unset references are NA (not "*"). strand has
levels c("+", "-", "*").
Region filtering via param$which is supported as a sequential filter
(not index-jump random access).
Flag filtering uses ScanBamFlag semantics by converting logical flag
requirements into required-set and required-unset bit masks.
Tag values are returned with their native BAM-derived type, matching
scanBam(): integer-valued tags (c, C, s, S, i, I) become
integer columns (or double when a value exceeds the R integer range),
floating-point (f) tags become double columns, and A/Z/H/B tags
become character columns. B tags are comma-separated character values.
Absent tag values are NA.
seqqual_mode = "compact" is optimized for throughput-oriented
benchmarking and returns raw list-columns for seq/qual, not ordinary
sequence or quality strings. In this representation, seq contains
BAM-packed nucleotide bytes and qual contains raw Phred bytes. Compact
output is intended for users who want to defer or avoid full
string-materialization costs; use decode_compact_seq(),
decode_compact_qual(), or decode_seqqual_compact() to decode compact
output back to standard string form when needed.
"GAlignments" and "GAlignmentPairs" output exclude unmapped records.
as = "scanBam" returns a strict scan-like list-of-lists:
without param$which, it returns one unnamed batch; with param$which,
it returns one batch per range label (including empty ranges), with
requested what fields and tag values under $tag.
In this output mode, seq and qual are returned as
Biostrings::DNAStringSet and Biostrings::PhredQuality for closer
scanBam() compatibility.
If file is length 1: one object in the format specified by as.
If file has length > 1 (or is a BamFileList): a named list of outputs,
one per BAM file.
bam <- ompBAM::example_BAM("Unsorted") # Familiar scanBam-like field selection x <- bam_read(bam, what = c("qname", "flag", "rname", "pos", "cigar")) # Include sequence + quality y <- bam_read(bam, what = c("qname", "seq", "qual"), threads = 2) # scanBam-shaped output z <- bam_read(bam, what = c("qname", "flag"), tag = "NM", as = "scanBam")bam <- ompBAM::example_BAM("Unsorted") # Familiar scanBam-like field selection x <- bam_read(bam, what = c("qname", "flag", "rname", "pos", "cigar")) # Include sequence + quality y <- bam_read(bam, what = c("qname", "seq", "qual"), threads = 2) # scanBam-shaped output z <- bam_read(bam, what = c("qname", "flag"), tag = "NM", as = "scanBam")
Decodes qual values returned by bam_read(..., seqqual_mode = "compact")
back to ASCII Phred-quality strings.
decode_compact_qual(qual)decode_compact_qual(qual)
qual |
A list (or list-column) of |
A character vector containing decoded quality strings. Entries with
all-missing quality bytes are returned as "*", matching BamScale's
compatibility mode.
decode_compact_seq(), decode_seqqual_compact(), bam_read()
decode_compact_qual( qual = list(as.raw(c(0L, 1L, 2L, 3L))) )decode_compact_qual( qual = list(as.raw(c(0L, 1L, 2L, 3L))) )
Decodes seq values returned by bam_read(..., seqqual_mode = "compact")
back to ordinary character strings.
decode_compact_seq(seq, qwidth)decode_compact_seq(seq, qwidth)
seq |
A list (or list-column) of |
qwidth |
Integer vector of read widths. This is required because compact sequence bytes use BAM's 4-bit packed encoding (two bases per byte). |
A character vector containing decoded sequence strings.
decode_compact_qual(), decode_seqqual_compact(), bam_read()
decode_compact_seq( seq = list(as.raw(c(0x12, 0x48))), qwidth = 4L )decode_compact_seq( seq = list(as.raw(c(0x12, 0x48))), qwidth = 4L )
seq and qual columns in BamScale outputConvenience wrapper for converting a compact bam_read() result back to
ordinary sequence and quality strings.
decode_seqqual_compact( x, seq_col = "seq", qual_col = "qual", qwidth_col = "qwidth" )decode_seqqual_compact( x, seq_col = "seq", qual_col = "qual", qwidth_col = "qwidth" )
x |
A |
seq_col |
Name of the compact sequence column. |
qual_col |
Name of the compact quality column. |
qwidth_col |
Name of the read-width column used to decode compact sequence bytes. |
x with compact seq and/or qual columns replaced by decoded
character vectors. The input class is preserved.
decode_compact_seq(), decode_compact_qual(), bam_read()
x <- data.frame(qwidth = 4L) x$seq <- I(list(as.raw(c(0x12, 0x48)))) x$qual <- I(list(as.raw(c(0L, 1L, 2L, 3L)))) decode_seqqual_compact(x)x <- data.frame(qwidth = 4L) x$seq <- I(list(as.raw(c(0x12, 0x48)))) x$qual <- I(list(as.raw(c(0L, 1L, 2L, 3L)))) decode_seqqual_compact(x)
Computes the paired-end fragment-size (insert-size) distribution – a table of
abs(TLEN) counts – by accumulating the histogram inside the multithreaded
C++ reader, without ever materialising the per-read insert sizes as an R
vector. This is the fast path for ATAC-seq / paired-end fragment-size QC on
large BAMs: the histogram is folded together inside the OpenMP threads and only
the compact (fragment_size, count) table crosses back into R (cf.
scanBam(what = "isize") followed by table(abs(isize)), which materialises
every insert size in R first).
fragment_sizes( file, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE, max_fragment = 100000L, drop_mate_unmapped = TRUE )fragment_sizes( file, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE, max_fragment = 100000L, drop_mate_unmapped = TRUE )
file |
A BAM file path, Rsamtools::BamFile, or a vector / |
param |
Optional Rsamtools::ScanBamParam (or a compatible list). Only
its |
threads |
Number of OpenMP threads for within-file decoding. |
BPPARAM |
A BiocParallel::BiocParallelParam for across-file parallelism
when |
auto_threads |
Logical; if |
max_fragment |
Integer; fragment sizes up to this value are tallied in a
dense histogram and larger ones in an overflow map. Every observed size is
returned exactly regardless of this value (default |
drop_mate_unmapped |
Logical; if |
A data.frame with columns fragment_size (integer abs(TLEN)) and
count (numeric), one row per observed fragment size in ascending order.
For multiple input files, a named list of such data.frames.
bam_read() for reading records, bam_count() for per-chromosome
counts.
Computes the MAPQ distribution – a table of per-value alignment-count – by
accumulating the histogram inside the multithreaded C++ reader, without ever
materialising the per-read MAPQ values as an R vector. This is the fast path
for mapping-quality QC on large BAMs: each thread tallies MAPQ into a private
256-slot histogram inside the OpenMP region and only the compact
(mapq, count) table crosses back into R (cf. scanBam(what = "mapq")
followed by table(mapq), which materialises every MAPQ in R first).
mapq_dist( file, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE )mapq_dist( file, param = NULL, threads = 1L, BPPARAM = BiocParallel::bpparam(), auto_threads = FALSE )
file |
A BAM file path, Rsamtools::BamFile, or a vector / |
param |
Optional Rsamtools::ScanBamParam (or a compatible list). Only
its |
threads |
Number of OpenMP threads for within-file decoding. |
BPPARAM |
A BiocParallel::BiocParallelParam for across-file parallelism
when |
auto_threads |
Logical; if |
A data.frame with columns mapq (integer, 0-255) and count
(numeric), one row per observed MAPQ value in ascending order. For multiple
input files, a named list of such data.frames.
fragment_sizes() for the fragment-size distribution, bam_count()
for per-chromosome counts.