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

Bin numbers for BAI and CSI.

BAI is the special case `min_shift = 14`, `depth = 5`. That scheme covers
reference positions whose exclusive end is at most `2^29` (512 Mbp). The
on-disk BAM record stores this BAI bin in a 16-bit field. CSI uses the
same recurrence with a caller-chosen shift and depth, and it stores bin
numbers in the index as 32-bit integers.

Intervals are zero-based and half-open. The formulas are the ones in the
CSI specification, which reduce to the SAM specification's `reg2bin` for
the BAI parameters. A CIGAR that consumes no reference bases is given
length 1 before the bin is chosen, matching the January 2021 SAM note and
`bam_endpos`.

# `bai_bin`

```elixir
@spec bai_bin(non_neg_integer(), non_neg_integer()) :: non_neg_integer()
```

BAI bin for `[beg, end)`, after the zero-reference-consumption rule.

`reference_length` is the number of reference bases the CIGAR consumes.
Zero is treated as one.

# `bai_bin_limit`

```elixir
@spec bai_bin_limit() :: pos_integer()
```

Number of addressable BAI bins. Valid bin numbers are `0` until this
value, exclusive.

# `bai_depth`

```elixir
@spec bai_depth() :: 5
```

BAI depth. The level of 16 kbp bins is 5, and level 0 is the single bin
that spans the whole addressable range.

# `bai_max_pos`

```elixir
@spec bai_max_pos() :: pos_integer()
```

Largest exclusive end a BAI index can store, `2^29`.

# `bai_meta_bin`

```elixir
@spec bai_meta_bin() :: pos_integer()
```

Pseudo-bin used by HTSlib to store mapped and unmapped counts. For BAI
this is 37450. Those chunks are metadata, not file ranges.

# `bai_reference?`

```elixir
@spec bai_reference?(non_neg_integer()) :: boolean()
```

True when a reference of this length fits in a BAI index.

Length `2^29` fits, because the exclusive end of a base at the last
position is `2^29`. A longer reference needs CSI.

# `bai_shift`

```elixir
@spec bai_shift() :: 14
```

BAI `min_shift`.

# `bin_limit`

```elixir
@spec bin_limit(non_neg_integer(), non_neg_integer()) :: non_neg_integer()
```

First bin number that is not a real bin for these parameters. The
metadata pseudo-bin, when present, is `bin_limit + 1`.

# `first_bin`

```elixir
@spec first_bin(non_neg_integer()) :: non_neg_integer()
```

First bin number at `level`. Level 0 starts at 0.

# `interval`

```elixir
@spec interval(non_neg_integer(), non_neg_integer(), non_neg_integer()) ::
  {non_neg_integer(), non_neg_integer()}
```

Half-open reference interval covered by `bin`.

# `max_pos`

```elixir
@spec max_pos(non_neg_integer(), non_neg_integer()) :: pos_integer()
```

Largest exclusive coordinate a CSI parameter pair can name.

# `meta_bin`

```elixir
@spec meta_bin(non_neg_integer(), non_neg_integer()) :: non_neg_integer()
```

Metadata pseudo-bin number.

# `overlaps?`

```elixir
@spec overlaps?(
  non_neg_integer(),
  non_neg_integer(),
  pos_integer(),
  non_neg_integer(),
  non_neg_integer()
) :: boolean()
```

True when the bin's interval overlaps `[beg, end)`.

# `reg2bin`

```elixir
@spec reg2bin(non_neg_integer(), pos_integer(), non_neg_integer(), non_neg_integer()) ::
  non_neg_integer()
```

Bin that contains the half-open interval `[beg, end)`.

## Examples

    iex> BAM.Index.Bin.reg2bin(0, 1, 14, 5)
    4681

    iex> BAM.Index.Bin.reg2bin(0, 16_385, 14, 5)
    585

    iex> BAM.Index.Bin.reg2bin(0, 536_870_912, 14, 5)
    0

# `reg2bins`

```elixir
@spec reg2bins(non_neg_integer(), pos_integer(), non_neg_integer(), non_neg_integer()) ::
  [
    non_neg_integer()
  ]
```

Bin numbers that can overlap `[beg, end)`.

The list follows the CSI `reg2bins` recurrence. Callers that already hold
an index should intersect it with the bins that index actually contains.

---

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