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

Blocked GNU Zip (BGZF) members.

A BAM file is a sequence of BGZF members, not an arbitrary gzip stream.
Each member is a gzip member whose extra field contains a `BC` subfield.
`BSIZE` in that subfield is the total member length minus one, and the
uncompressed payload is at most 65_536 bytes. The file ends with a fixed
28-byte empty member.

A virtual offset is not a byte offset. The high bits are the file offset
of the member that contains the byte. The low 16 bits are the offset of
that byte within the member's uncompressed payload.

Inflation and deflation use OTP `:zlib`. Members are independent and
bounded, so a NIF is not required for the format to be correct.

# `build_member`

```elixir
@spec build_member(binary(), keyword()) ::
  {:ok, binary()} | {:error, BAM.Error.t() | :expanded}
```

Builds one member. Returns `:expanded` when the gzip member would exceed
65_536 bytes, which is the `BSIZE` limit.

# `compress`

```elixir
@spec compress(binary(), keyword()) :: {:ok, binary()} | {:error, BAM.Error.t()}
```

Compresses `data` into BGZF members followed by the empty terminator.

`:compression_level` is the zlib level, 0 through 9. Level 0 stores the
bytes. The member boundaries depend on the level and on the zlib version,
so two OTP builds may not produce identical compressed bytes. The
uncompressed payloads and the terminator do not depend on zlib.

# `decompress_all`

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

Decompresses a whole BGZF stream, including a CSI index.

The empty terminator is not included in the result.

# `eof_block`

```elixir
@spec eof_block() :: binary()
```

The 28-byte empty member that terminates a BGZF stream.

# `max_uncompressed`

```elixir
@spec max_uncompressed() :: 65536
```

Maximum uncompressed payload, from the SAM specification.

# `parse_member`

```elixir
@spec parse_member(binary(), keyword()) ::
  {:ok, BAM.BGZF.Block.t(), binary()} | {:error, BAM.Error.t()}
```

Parses one member from the front of `binary`.

Returns the member and the unparsed tail. The tail may begin the next
member. `opts` accepts `:offset`, recorded on the block as the file
position of this member.

# `read_member`

```elixir
@spec read_member(module(), term(), non_neg_integer(), map()) ::
  {:ok, BAM.BGZF.Block.t(), non_neg_integer()} | {:error, BAM.Error.t()}
```

Reads the member that starts at `offset`.

The read is sized from the `BC` subfield. It does not pull the following
member.

# `split_virtual_offset`

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

Splits a virtual offset into `{file_offset, uncompressed_offset}`.

# `virtual_offset`

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

Packs a file offset and an uncompressed offset into a virtual offset.

## Examples

    iex> BAM.BGZF.virtual_offset(100, 7)
    {:ok, 6_553_607}

    iex> BAM.BGZF.split_virtual_offset(6_553_607)
    {100, 7}

---

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