Packages

Read CRAM 3.0 and 3.1 alignment files from Elixir.

Current section

Files

Jump to
cram docs guide.md
Raw

docs/guide.md

# Guide

This page is the shape of a CRAM file and the calls that read one. Function arguments and return values are in the module docs, starting at `CRAM`.

## The file

A CRAM 3 file is, in order:

1. The file definition: the bytes `CRAM`, a major and minor version, and a 20-byte file id (CRAMv3 §6).
2. Containers. The first holds the SAM header. Later containers hold slices of records.
3. A 38-byte EOF container (CRAMv3 §9). A file that ends before it is `reason: :truncated` in both modes.

`CRAM.open/2` reads the definition and keeps the source open. `CRAM.info/1` walks container headers. `CRAM.header/1` inflates the SAM header block when it is raw or gzip. Neither call decodes a record.

## Opening

```elixir
{:ok, handle} = CRAM.open(path, mode: :strict, max_block_bytes: 1_048_576)
```

`path` may also be `{:file, path}` or `{:binary, iodata}`.

`mode` is `:strict` or `:permissive`. Strict mode returns `{:error, %CRAM.Error{}}` for a bad container CRC, a truncated file, and an unknown block method needed to read the header. Permissive mode still refuses truncated input and a size above the cap. It records an unknown external-block method on the container and continues. Neither mode repairs bytes.

`:max_container_bytes`, `:max_block_bytes`, `:max_blocks`, `:max_landmarks`, and `:max_header_bytes` bound what a declared size is allowed to allocate. A declared uncompressed size above the cap is `reason: :allocation_limit` before that buffer is allocated.

`:reference` stores a `CRAM.ReferenceProvider` for a later decode. `CRAM.info/1` and `CRAM.containers/1` do not call it.

`CRAM.close/1` stops the handle. A later call returns `reason: :closed`.

## Inspection

`CRAM.info/1` returns version, file id, container count, whether EOF was present, block methods, and the `@SQ` names, lengths, and `M5` checksums. `CRAM.header/1` keeps the raw SAM text. `CRAM.metadata/1` is both. `CRAM.containers/1` yields one container at a time, including EOF.

`CRAM.export_context/1` returns reference names, lengths, and MD5 strings. It does not return bases.

`mix cram.info path` prints the same summary and does not decode reads.

CRAM 2.1 opens far enough for `CRAM.info/1` to report the version with `supported: false`. `CRAM.header/1`, `CRAM.containers/1`, and `CRAM.read/2` then return `reason: :unsupported_version`.

## References

A slice that needs an external reference is not fetched during inspection. Decode asks the provider for each `@SQ` line that has an `M5`.

`CRAM.Reference.Local` reads a FASTA file, uses a sibling `.fai` when the name is present, and checks the SAM MD5 (uppercase, whitespace removed) before it returns bases. `CRAM.Reference.Cache` stores an accepted sequence under a directory the caller created, named by the checksum, and hashes it again on a hit. `CRAM.Reference.Refget` speaks a sequence endpoint only when `allow_network: true`. The default open options do not build it. Without that flag the result is `reason: :network_disabled` and no connection.

A missing name is `reason: :reference_missing`. A checksum that does not match is `reason: :reference_checksum_mismatch`. Those errors carry the name and the checksum, not the bases.

## Records

`CRAM.stream/2` and `CRAM.read/2` decode CRAM 3.0 and 3.1 through a Rustler NIF pinned to noodles-cram 0.103.0. Elixir resolves references first and passes `{name, md5, sequence}` in. The NIF builds an in-memory FASTA repository and does not open a socket.

Each call returns at most `:batch_size` records (default 256). Halting the stream sets a cancellation flag checked between records. `%CRAM.Alignment` positions are SAM 1-based. A missing position, sequence, or quality is `nil`. Unknown tags are kept.

`:limit` stops after that many records. If the shared library does not load, inspection still works and decode returns `reason: :native_unavailable`. A container the crate cannot decode returns `%CRAM.Error{}` instead of aborting the VM.

The codecs are delegated. This repository does not reimplement them. The synthetic fixtures exercise raw, gzip, and rans4x16. The other block methods are named and not separately tested.

## Regions

`CRAM.read/2` accepts `:region`. `"sq0:1-4"` is a 1-based inclusive interval, the same shape `samtools view` uses. `:unmapped` reads the unmapped bin. The default index is `path.cram.crai`. A binary source has no sibling path, so pass `:index`.

`CRAM.Crai` parses the six text columns. Sequence id `-1` is the unmapped bin. Alignment start `1` is SAM position 1. Start `0` means the row has no position. A non-integer is `reason: :bad_header`. A line over 4096 bytes is `reason: :allocation_limit`. A missing file is `reason: :index_missing`.

The NIF calls noodles `Reader::query` or `query_unmapped` with that index and the same reference repository. An empty region is an empty list, not an error.

## Writing

`CRAM.write/3` returns `{:error, %CRAM.Error{reason: :unsupported}}`. This version does not write CRAM.

## What a result means

Public calls return `{:ok, value}` or `{:error, %CRAM.Error{}}`. `reason` is a fixed atom. It is never built from bytes in the file. `path` and `offset` say where the reader was. Bang functions are not the public style of this library. A stream may raise `CRAM.Error` for a later container. `CRAM.read/2` turns that into `{:error, %CRAM.Error{}}`.