BAM.SAM (BAM v0.1.0)

Copy Markdown View Source

SAM text bridge.

SAM positions are one-based. A POS of zero and a RNAME of * both mean the segment has no reference coordinate; the BAM record stores -1. A region such as chr1:1-100 is one-based and inclusive. This module converts it to the zero-based half-open interval {start: 0, stop: 100} used by BAM.read/2.

chr1:1000 means "from base 1000 through the end of the reference", which is how samtools view reads a single coordinate. A name that contains a colon is left intact when the text after the colon is not a coordinate.

Summary

Functions

Formats a header and alignments as SAM text.

Formats one alignment as a SAM line without a newline.

Parses a complete SAM document into a header and a list of alignments.

Parses one alignment line. line_number is recorded on errors.

Parses a SAM region.

Types

region()

@type region() :: %{
  reference: binary(),
  start: non_neg_integer(),
  stop: non_neg_integer() | :end
}

Functions

format(header, records)

@spec format(BAM.Header.t(), Enumerable.t()) ::
  {:ok, binary()} | {:error, BAM.Error.t()}

Formats a header and alignments as SAM text.

format_alignment(alignment, header)

@spec format_alignment(BAM.Alignment.t(), BAM.Header.t()) ::
  {:ok, binary()} | {:error, BAM.Error.t()}

Formats one alignment as a SAM line without a newline.

parse(text, opts \\ [])

@spec parse(binary(), keyword()) ::
  {:ok, BAM.Header.t(), [BAM.Alignment.t()]} | {:error, BAM.Error.t()}

Parses a complete SAM document into a header and a list of alignments.

parse_alignment(line, header, opts \\ [])

@spec parse_alignment(binary(), BAM.Header.t(), keyword()) ::
  {:ok, BAM.Alignment.t()} | {:error, BAM.Error.t()}

Parses one alignment line. line_number is recorded on errors.

region(spec)

@spec region(binary()) :: {:ok, region()} | {:error, BAM.Error.t()}

Parses a SAM region.

Examples

iex> BAM.SAM.region("chr1:1-100")
{:ok, %{reference: "chr1", start: 0, stop: 100}}

iex> BAM.SAM.region("chr1:1000")
{:ok, %{reference: "chr1", start: 999, stop: :end}}

iex> BAM.SAM.region("chr1")
{:ok, %{reference: "chr1", start: 0, stop: :end}}