Reader and writer for BAM alignment files.

A BAM file is BGZF-compressed SAM. open/2 accepts a local path or {:memory, binary}. info/1 reports the header and the reference dictionary and does not decode alignments. stream/2 yields alignments one record at a time. The stream closes the handle when enumeration stops, including when the caller stops early.

pos on each alignment is the BAM coordinate: zero-based, or -1 when the segment has no position. BAM.read/3 regions use the same zero-based half-open interval. BAM.SAM.region/1 converts a one-based inclusive samtools region such as chr1:1-100.

mode: :strict is the default. A strict stream raises %BAM.Error{} at the first record it cannot decode. mode: :permissive yields {:ok, alignment} and {:error, %BAM.Error{}} and keeps going when the record boundary is still known. Length fields that would allocate past BAM.Limits return category: :resource_limit in either mode.

write/4 emits BGZF, including the empty terminator. Pass index: :bai or index: :csi to write a sidecar next to a path. BAI refuses a reference longer than 512 Mbp. CSI is the index for those references.

Summary

Functions

Releases the file or memory held by handle. Closing a closed handle returns :ok.

Closes handle or raises.

Header summary. Alignments are not decoded.

Header summary or raises.

Parsed header, including the raw text and the reference dictionary.

Parsed header or raises.

Opens a local BAM file or an in-memory binary.

Opens a BAM input or raises %BAM.Error{}.

Alignments that overlap a region.

Region query or raises.

Lazy alignment stream.

Writes records as a BAM file.

Writes a BAM file or raises.

Types

handle()

@type handle() :: BAM.Handle.t()

Functions

close(handle)

@spec close(handle()) :: :ok | {:error, BAM.Error.t()}

Releases the file or memory held by handle. Closing a closed handle returns :ok.

close!(handle)

@spec close!(handle()) :: :ok

Closes handle or raises.

info(handle)

@spec info(handle()) :: {:ok, map()} | {:error, BAM.Error.t()}

Header summary. Alignments are not decoded.

The map includes :version, :sort_order, :references, :read_groups, :programs, and :comments.

info!(handle)

@spec info!(handle()) :: map()

Header summary or raises.

metadata(handle)

@spec metadata(handle()) :: {:ok, BAM.Header.t()} | {:error, BAM.Error.t()}

Parsed header, including the raw text and the reference dictionary.

metadata!(handle)

@spec metadata!(handle()) :: BAM.Header.t()

Parsed header or raises.

open(input, opts \\ [])

@spec open(Path.t() | {:memory, binary()}, keyword()) ::
  {:ok, handle()} | {:error, BAM.Error.t()}

Opens a local BAM file or an in-memory binary.

Options:

  • :mode — :strict or :permissive. Defaults to :strict.
  • :limits — a BAM.Limits struct or a keyword of overrides.
  • :source — {module, state} for a custom BAM.Source. path is then only the name recorded on errors.

Examples

iex> {:ok, handle} = BAM.open({:memory, <<>>})
iex> {:error, error} = BAM.info(handle)
iex> :ok = BAM.close(handle)
iex> error.category
:truncated

open!(input, opts \\ [])

@spec open!(Path.t() | {:memory, binary()}, keyword()) :: handle()

Opens a BAM input or raises %BAM.Error{}.

read(handle, region, opts \\ [])

@spec read(handle(), map() | binary(), keyword()) ::
  {:ok, Enumerable.t()} | {:error, BAM.Error.t()}

Alignments that overlap a region.

region is %{reference: name, start: start, stop: stop} with start and stop zero-based and stop exclusive. stop: :end runs to the end of the reference. A samtools region string is accepted and parsed with BAM.SAM.region/1.

Options include every stream/2 option plus :index, which is a path, {:memory, binary}, or a %BAM.Index{}. When :index is omitted, a file handle looks for path.bai and then path.csi.

The result is {:ok, stream}. The stream closes the handle when it stops.

read!(handle, region, opts \\ [])

@spec read!(handle(), map() | binary(), keyword()) :: Enumerable.t()

Region query or raises.

stream(handle, opts \\ [])

@spec stream(handle(), keyword()) :: Enumerable.t()

Lazy alignment stream.

Options:

  • :mode — overrides the handle mode for this stream.
  • :reference — a reference name or ref_id to keep.
  • :flag_require — bits that must all be set.
  • :flag_exclude — bits that must all be unset.
  • :limits — overrides for this stream.
  • :max_records — stop after this many yielded records.

A strict stream yields %BAM.Alignment{}. A permissive stream yields {:ok, alignment} or {:error, error}. Creating the stream does not read the file. The first pull reads the header.

write(dest, header, records, opts \\ [])

@spec write(Path.t() | {:memory, :binary}, BAM.Header.t(), Enumerable.t(), keyword()) ::
  :ok | {:ok, binary()} | {:error, BAM.Error.t()}

Writes records as a BAM file.

dest is a path or {:memory, :binary}. A path is replaced only after the terminator is written. index: :bai or index: :csi writes path.bai or path.csi. Records used for an index must be grouped by reference and sorted by position.

:compression_level is 0..9. The default is 6.

write!(dest, header, records, opts \\ [])

@spec write!(Path.t() | {:memory, :binary}, BAM.Header.t(), Enumerable.t(), keyword()) ::
  :ok | binary()

Writes a BAM file or raises.