BAM.Index.Bin (BAM v0.1.0)

Copy Markdown View Source

Bin numbers for BAI and CSI.

BAI is the special case min_shift = 14, depth = 5. That scheme covers reference positions whose exclusive end is at most 2^29 (512 Mbp). The on-disk BAM record stores this BAI bin in a 16-bit field. CSI uses the same recurrence with a caller-chosen shift and depth, and it stores bin numbers in the index as 32-bit integers.

Intervals are zero-based and half-open. The formulas are the ones in the CSI specification, which reduce to the SAM specification's reg2bin for the BAI parameters. A CIGAR that consumes no reference bases is given length 1 before the bin is chosen, matching the January 2021 SAM note and bam_endpos.

Summary

Functions

BAI bin for [beg, end), after the zero-reference-consumption rule.

Number of addressable BAI bins. Valid bin numbers are 0 until this value, exclusive.

BAI depth. The level of 16 kbp bins is 5, and level 0 is the single bin that spans the whole addressable range.

Largest exclusive end a BAI index can store, 2^29.

Pseudo-bin used by HTSlib to store mapped and unmapped counts. For BAI this is 37450. Those chunks are metadata, not file ranges.

True when a reference of this length fits in a BAI index.

BAI min_shift.

First bin number that is not a real bin for these parameters. The metadata pseudo-bin, when present, is bin_limit + 1.

First bin number at level. Level 0 starts at 0.

Half-open reference interval covered by bin.

Largest exclusive coordinate a CSI parameter pair can name.

Metadata pseudo-bin number.

True when the bin's interval overlaps [beg, end).

Bin that contains the half-open interval [beg, end).

Bin numbers that can overlap [beg, end).

Functions

bai_bin(pos, reference_length)

BAI bin for [beg, end), after the zero-reference-consumption rule.

reference_length is the number of reference bases the CIGAR consumes. Zero is treated as one.

bai_bin_limit()

@spec bai_bin_limit() :: pos_integer()

Number of addressable BAI bins. Valid bin numbers are 0 until this value, exclusive.

bai_depth()

@spec bai_depth() :: 5

BAI depth. The level of 16 kbp bins is 5, and level 0 is the single bin that spans the whole addressable range.

bai_max_pos()

@spec bai_max_pos() :: pos_integer()

Largest exclusive end a BAI index can store, 2^29.

bai_meta_bin()

@spec bai_meta_bin() :: pos_integer()

Pseudo-bin used by HTSlib to store mapped and unmapped counts. For BAI this is 37450. Those chunks are metadata, not file ranges.

bai_reference?(length)

@spec bai_reference?(non_neg_integer()) :: boolean()

True when a reference of this length fits in a BAI index.

Length 2^29 fits, because the exclusive end of a base at the last position is 2^29. A longer reference needs CSI.

bai_shift()

@spec bai_shift() :: 14

BAI min_shift.

bin_limit(min_shift, depth)

@spec bin_limit(non_neg_integer(), non_neg_integer()) :: non_neg_integer()

First bin number that is not a real bin for these parameters. The metadata pseudo-bin, when present, is bin_limit + 1.

first_bin(level)

@spec first_bin(non_neg_integer()) :: non_neg_integer()

First bin number at level. Level 0 starts at 0.

interval(bin, min_shift, depth)

Half-open reference interval covered by bin.

max_pos(min_shift, depth)

@spec max_pos(non_neg_integer(), non_neg_integer()) :: pos_integer()

Largest exclusive coordinate a CSI parameter pair can name.

meta_bin(min_shift, depth)

@spec meta_bin(non_neg_integer(), non_neg_integer()) :: non_neg_integer()

Metadata pseudo-bin number.

overlaps?(bin, beg, stop, min_shift, depth)

True when the bin's interval overlaps [beg, end).

reg2bin(beg, stop, min_shift, depth)

Bin that contains the half-open interval [beg, end).

Examples

iex> BAM.Index.Bin.reg2bin(0, 1, 14, 5)
4681

iex> BAM.Index.Bin.reg2bin(0, 16_385, 14, 5)
585

iex> BAM.Index.Bin.reg2bin(0, 536_870_912, 14, 5)
0

reg2bins(beg, stop, min_shift, depth)

Bin numbers that can overlap [beg, end).

The list follows the CSI reg2bins recurrence. Callers that already hold an index should intersect it with the bins that index actually contains.