Packages
Streaming reader and writer for SAM and BAM alignment files, including BGZF, BAI, and CSI.
Current section
Files
Jump to
Current section
Files
README.md
# BAM
[](https://github.com/baoulo/bam-elixir/actions/workflows/ci.yml)
[](https://hex.pm/packages/bam)
[](https://hexdocs.pm/bam)
[](https://github.com/baoulo/bam-elixir/blob/main/LICENSE)
<img width="800" height="447" alt="bam" src="https://github.com/user-attachments/assets/3402be87-3b05-4107-9ed6-e8d8a3b4cb2b" />
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](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](docs/sam-and-bam.md).
## Install
Add `bam` to your Mix dependencies:
```elixir
{:bam, "~> 0.1.0"}
```
The application targets Elixir `~> 1.18`. Runtime dependencies: none.
## Read a file
```elixir
{: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.
```elixir
{: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
```elixir
{: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
```text
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](docs/benchmarks.md).
The tutorial drafts live in [tutorials/](tutorials/README.md). They are unpublished.
The license is MIT. The text is in the `LICENSE` file.