BAM.BGZF (BAM v0.1.0)

Copy Markdown View Source

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.

Summary

Functions

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

Compresses data into BGZF members followed by the empty terminator.

Decompresses a whole BGZF stream, including a CSI index.

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

Maximum uncompressed payload, from the SAM specification.

Parses one member from the front of binary.

Reads the member that starts at offset.

Splits a virtual offset into {file_offset, uncompressed_offset}.

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

Functions

build_member(uncompressed, opts \\ [])

@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(data, opts \\ [])

@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(binary)

@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()

@spec eof_block() :: binary()

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

max_uncompressed()

@spec max_uncompressed() :: 65536

Maximum uncompressed payload, from the SAM specification.

parse_member(binary, opts \\ [])

@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(module, state, offset, context \\ %{})

@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(voffset)

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

Splits a virtual offset into {file_offset, uncompressed_offset}.

virtual_offset(coffset, uoffset)

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