# `BAM.SAM`
[🔗](https://github.com/baoulo/bam-elixir/blob/v0.1.0/lib/bam/sam.ex#L1)

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.

# `region`

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

# `format`

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

Formats a header and alignments as SAM text.

# `format_alignment`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@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}}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
