# Specification notes

Pinned on 2026-11-30, while this tree was written. The specification wins when a tool disagrees with it. HTSlib was read as a second description of layouts that the PDFs leave implicit, and it was not vendored.

## Documents

| Document | What was pinned |
| --- | --- |
| SAMv1 | Format 1.6. The PDF at <https://samtools.github.io/hts-specs/SAMv1.pdf> was dated 12 Aug 2025. The hts-specs printing recorded with that file is `b5341fb`. |
| SAMtags | <https://samtools.github.io/hts-specs/SAMtags.pdf>, consulted the same day for the standard optional-field types. |
| CSIv1 | <https://samtools.github.io/hts-specs/CSIv1.pdf>. BAI is the CSI parameter pair `min_shift = 14`, `depth = 5`. |
| HTSlib | Develop sources for `bam_endpos`, `reg2bin` / `reg2bins`, and the BAI and CSI on-disk layouts. Used to check edge cases. Not compiled into this project. |
| samtools | 1.25, linked to htslib 1.25, installed at `/opt/homebrew/bin/samtools` on the machine that ran the differential tests. |

## BGZF and `:zlib`

A BGZF member is a gzip member with an extra-field `BC` subfield. `BSIZE` is the member length minus one, and the uncompressed payload is at most 65,536 bytes. Members are independent. OTP `:zlib` inflates and deflates one member at a time with raw deflate (`windowBits` -15). CRC32 uses `:erlang.crc32`. A NIF is not required to implement the format. This choice is recorded here so a later measurement can revisit it. The Stage 5 bench on a 500-record synthetic file did not show `:zlib` to be a reason to add one. See `docs/benchmarks.md`.

`:zlib` is a module in the ERTS distribution on the OTP used here. It is not an OTP application, so it is not listed in `extra_applications`.

## Coordinates

`BAM.Alignment.pos` is the on-disk value: zero-based, or `-1` when the segment has no position. `end_pos` follows HTSlib `bam_endpos`: the CIGAR reference consumption, with unmapped reads and zero-reference CIGARs treated as length 1, so a placed read ends at `pos + 1` in that case. SAM `POS` is one-based and writes `0` for an unplaced segment.

`BAM.read/3` intervals are zero-based and half-open. `BAM.SAM.region/1` parses `chr:1-100` as `%{start: 0, stop: 100}`. `chr1:1000` runs from base 1000 through the end of the reference. A name that contains a colon stays intact when the suffix is not a coordinate.

## Raw bytes

Flag bits, CIGAR words, sequence nybbles, quality bytes, float32 tag bytes, and unknown header lines are kept. Tag names and reference names stay binaries. The decoder does not create atoms from file bytes. CIGAR operation names are the fixed binaries `M I D N S H P = X`.

## Caps

`BAM.Limits` documents the default caps for header text, reference count, record `block_size`, CIGAR operations, auxiliary bytes, and index tables. Callers override them with `:limits`. A `BSIZE` that does not frame its gzip member is rejected. Parsers walk the file iteratively.

## BAI and CSI

BAI addresses an exclusive end up to `2^29` (512 Mbp). A reference of length `2^29` fits. Length `2^29 + 1` needs CSI. The 16-bit bin stored on a BAM record is always the BAI `reg2bin(14, 5)` value. An alignment whose exclusive end passes `2^29` is rejected on encode.

## What was not taken as a dependency

The Hex package `vcf` 1.0.0 exists and is a different project. This library does not depend on it. BGZF stays in `BAM.BGZF` until a second library in this set needs the same code.
