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

Builds a BAI or CSI binning index while a BAM file is written.

Records must arrive grouped by reference id and, within a reference, sorted
by `pos`. Mapped records become chunks. A run of records that share a bin
is one chunk, from the virtual offset of the first record to the virtual
offset of the following record. Placed unmapped reads increment the
reference's unmapped count and do not open a chunk. Reads with no reference
increment `n_no_coor`.

The BAM bin field itself always uses the 14/5 scheme. A CSI builder may
use another `min_shift` and `depth` for the index only.

# `t`

```elixir
@type t() :: %BAM.Index.Builder{
  depth: term(),
  format: term(),
  last_pos: term(),
  last_ref: term(),
  min_shift: term(),
  n_no_coor: term(),
  open: term(),
  refs: term()
}
```

# `finish`

```elixir
@spec finish(t(), non_neg_integer(), non_neg_integer()) :: {:ok, BAM.Index.t()}
```

Closes the last chunk at `final_voff` and returns an index with one slot
per reference in the header.

# `new`

```elixir
@spec new(keyword()) :: t()
```

New builder. `:format` is `:bai` or `:csi`. CSI defaults match BAI
(`min_shift` 14, `depth` 5) unless overridden.

# `push`

```elixir
@spec push(t(), BAM.Alignment.t(), non_neg_integer()) ::
  {:ok, t()} | {:error, BAM.Error.t()}
```

Accounts for one alignment whose first byte is at `voff`.

---

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