Hex.pm Docs CI License

A squirrel in an Elixir CRAM shirt storing DNA-marked walnuts in a tree

The CRAM format (Compressed Reference-oriented Alignment Map) stores sequence alignments by recording how each read differs from a reference. A file starts with the bytes CRAM, a version, and a file id, then containers of compressed blocks and a SAM header. BAM stores the alignments themselves inside BGZF. CRAM can be smaller, and a reader that wants the original bases may need the reference whose MD5 the file names.

This project reads CRAM 3.0 and 3.1 from Elixir. Inspection of the container and the SAM header is Elixir. Record decoding and region filtering go through a Rustler NIF pinned to noodles-cram 0.103.0. Writing is not implemented.

When to use it

CRAM is for alignment files that were compressed against a reference, or that keep the bases inside the file and still use the CRAM container.

  • You need the reference names, lengths, checksums, and container boundaries without decoding every read.
  • You need SAM-shaped records, in batches, from a 3.0 or 3.1 file.
  • You need the reads that overlap a 1-based interval, using the .crai index beside the file.
  • The reference, when one is required, is a FASTA you already have. Network lookup stays off unless you opt in.

When not to use it

This library does not write CRAM. CRAM.write/3 returns reason: :unsupported. It does not decode CRAM 2.1. It does not fetch a reference unless you pass allow_network: true to CRAM.Reference.Refget. It is not a BAM reader. Do not point it at patient-derived sequence.

Quick Start

Install

def deps do
  [
    {:cram, "~> 0.1.0"}
  ]
end

Requires Elixir ~> 1.18. A prod build downloads a precompiled NIF for NIF version 2.15. The targets are aarch64-apple-darwin, x86_64-apple-darwin, x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu, and x86_64-pc-windows-msvc. :dev and :test compile from source, as does CRAM_BUILD=1. A source build needs Rust 1.91 or newer. This checkout pins Elixir 1.20.2 / OTP 28.3 and Rust 1.97.1.

Open and inspect

CRAM.open/2 reads the file definition. CRAM.info/1 walks container headers and the SAM @SQ lines. It does not decode records.

{:ok, handle} = CRAM.open("test/fixtures/decode/no_ref.cram")
{:ok, info} = CRAM.info(handle)
{:ok, header} = CRAM.header(handle)
:ok = CRAM.close(handle)

On that synthetic file, info.version is {3, 1}, info.eof is true, and the header names sq0 with length 8. mix cram.info path prints the same summary.

{:binary, bytes} skips the filesystem: CRAM.open({:binary, bytes}).

Read records

CRAM.read/2 returns %CRAM.Alignment{} records. Positions are SAM 1-based. A reference-free file needs no provider.

{:ok, handle} = CRAM.open("test/fixtures/decode/no_ref.cram")
{:ok, [first | _]} = CRAM.read(handle, limit: 1)
:ok = CRAM.close(handle)

first.qname is "read1". first.pos is 1. A file that stored differences instead of bases needs reference: CRAM.Reference.Local for the matching FASTA. A missing reference is reason: :reference_missing and the error does not contain the bases.

Read a region

region: "sq0:1-4" is 1-based and inclusive. The sibling sample.cram.crai is the index.

{:ok, handle} = CRAM.open("test/fixtures/regions/sample.cram")
{:ok, records} = CRAM.read(handle, region: "sq0:1-4")
:ok = CRAM.close(handle)

Those records are the read named read1. :unmapped returns the unmapped bin. sq0:9-9 on that file is an empty list.

Close

CRAM.close/1 stops the handle. A later call returns {:error, %CRAM.Error{reason: :closed}}.

Roadmap

0.1.0 reads CRAM 3.0 and 3.1: container inspection, offline references, record decoding, and CRAI region queries. Checksums, size caps, and the refusal to write stay in every release.

Outside this version

  • CRAM.write/3
  • Decoding CRAM 2.1
  • A network reference, unless the caller passes allow_network: true
  • Elixir implementations of the data-series codecs. Those stay delegated to the pinned noodles-cram crate
  • A claim that one synthetic file matching samtools view is a conformance test