# `CRAM.Crai`
[🔗](https://github.com/baoulo/cram-elixir/blob/v0.1.0/lib/cram/crai.ex#L1)

A CRAM index (`.crai`) row.

The six columns are the reference sequence id, alignment start, alignment
span, container offset, slice offset, and slice size. samtools 1.25 writes
the alignment start as a SAM 1-based position. `0` means the row has no
position, which is how the unmapped bin is stored. Sequence id `-1` is the
unmapped bin and is kept as `nil`.

This module reads the text. It does not decode records.

# `t`

```elixir
@type t() :: %CRAM.Crai{
  alignment_span: non_neg_integer(),
  alignment_start: non_neg_integer(),
  container_offset: non_neg_integer(),
  reference_sequence_id: non_neg_integer() | nil,
  slice_offset: non_neg_integer(),
  slice_size: non_neg_integer()
}
```

One index row.

  * `:reference_sequence_id` — sequence id, or `nil` for the unmapped bin
  * `:alignment_start` — 1-based start, or `0` when the row has no position
  * `:alignment_span` — span in bases
  * `:container_offset` — byte offset of the container
  * `:slice_offset` — offset of the slice inside the container
  * `:slice_size` — slice size in bytes

    iex> %CRAM.Crai{reference_sequence_id: nil, alignment_start: 0, alignment_span: 0, container_offset: 0, slice_offset: 0, slice_size: 0}.reference_sequence_id
    nil

# `parse`

```elixir
@spec parse(binary(), Path.t() | nil) :: {:ok, [t()]} | {:error, CRAM.Error.t()}
```

Parse CRAI text or gzip bytes.

`bytes` is the file contents. `path` is optional and is stored on errors.
Six tab-separated columns are required. Sequence id `-1` becomes `nil`.
A start of `0` stays `0`. A bad column is `reason: :bad_header`. Invalid
gzip is the same reason. Does not raise.

    iex> {:ok, [row]} = CRAM.Crai.parse("0\t1\t8\t370\t196\t125\n")
    iex> row.alignment_start
    1
    iex> {:ok, [unmapped]} = CRAM.Crai.parse("-1\t0\t0\t0\t0\t0\n")
    iex> unmapped.reference_sequence_id
    nil
    iex> {:error, error} = CRAM.Crai.parse("0\tnope\n")
    iex> error.reason
    :bad_header

# `read`

```elixir
@spec read(Path.t()) :: {:ok, [t()]} | {:error, CRAM.Error.t()}
```

Read a `.crai` file.

`path` is the index path. The bytes may be gzip or plain text. Returns
`{:ok, rows}`. A missing file is `reason: :index_missing`. A line above
4096 bytes, or text above 16 MiB, is `reason: :allocation_limit`. Does not
decode records and does not raise.

    iex> {:ok, [mapped | _]} = CRAM.Crai.read("test/fixtures/regions/sample.cram.crai")
    iex> mapped.alignment_start
    1
    iex> {:error, error} = CRAM.Crai.read("test/fixtures/missing.crai")
    iex> error.reason
    :index_missing

---

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