BAM
Streaming SAM and BAM for Elixir. The library reads and writes BGZF-compressed BAM, including the header, alignment records, BAI and CSI indexes, and a SAM text bridge. Inflation and deflation go through OTP :zlib. There is no NIF and no HTSlib dependency.
This is version 0.1.0 of a library that has been exercised by ExUnit and, on the machine where those tests ran, by samtools 1.25. It is not a claim of production readiness or of full SAMv1 conformance. See docs/compatibility.md.
What SAM and BAM are
A sequencer emits reads: strings of bases and a confidence for each base. An aligner then places each read on a reference genome and records how it matched, including insertions, deletions, and clipped ends. That placement is an alignment.
SAM is the text form of those alignments. A header names the reference sequences. Each following line is one aligned segment, with its name, flags, reference, position, mapping quality, CIGAR edit string, mate, sequence, base qualities, and optional tags. BAM is the same records in binary, compressed with BGZF so a program can seek to a genomic interval instead of reading the file from the start. Positions in SAM text are one-based. Positions in a BAM record are zero-based.
People use these files after alignment and before variant calling, coverage summaries, and genome-browser review. SAM is the form you read and pipe. BAM is the form you store and query. CRAM is often the smaller archive when the reference genome is available. The longer account of who uses the formats, at which step, and which alternatives fit which job is What SAM and BAM are for.
Install
The package is not published. In a Mix project that vendors this repository:
{:bam, path: "path/to/bam-elixir"}
The application targets Elixir ~> 1.18. Runtime dependencies: none.
Read a file
{:ok, handle} = BAM.open("sample.bam")
{:ok, info} = BAM.info(handle)
names = Enum.map(info.references, & &1.name)
{:ok, handle} = BAM.open("sample.bam")
BAM.stream(handle)
|> Stream.map(& &1.qname)
|> Enum.take(10)
info/1 stops at the header. stream/2 is lazy and closes the handle when enumeration stops. Positions on an alignment are zero-based BAM coordinates. pos is -1 when the segment has no position.
Ask for a region
Regions passed to BAM.read/3 are zero-based and half-open. A samtools region string is one-based and inclusive; BAM.SAM.region/1 converts it.
{:ok, handle} = BAM.open("sample.bam")
{:ok, reads} = BAM.read(handle, "chr1:1-1000")
Enum.to_list(reads)
A path handle looks for sample.bam.bai and then sample.bam.csi. BAI covers references whose length is at most 512 Mbp. A longer reference needs CSI.
Write
{:ok, header} = BAM.Header.new([%{name: "chr1", length: 1000}])
:ok = BAM.write("out.bam", header, records, index: :bai)
Records used to build an index must be grouped by reference and sorted by position.
examples/gnosis_context.exs writes a tiny in-memory BAM and prints a JSON object. It does not depend on Gnosis.
Develop
mix test --cover
mix format
mix credo --strict
mix bam.inspect test/fixtures/tiny.bam
mix bam.tutorials
mix bam.bench
mix test --cover fails when total coverage drops below 90%. mix ci runs the same checks as GitHub Actions.
samtools is optional. When it is absent, differential checks are recorded as not run. On 2026-11-30, samtools 1.25 was installed and those checks ran. The numbers from mix bam.bench are in docs/benchmarks.md.
The tutorial drafts live in tutorials/. They are unpublished.
The license is MIT. The text is in the LICENSE file.