Read CRAM 3.0 and 3.1 container structure from Elixir.
open/2, info/1, header/1, and containers/1 walk the file definition,
the SAM header, and block boundaries. They do not reconstruct alignments.
stream/2 decodes records through the pinned noodles-cram NIF.
read/2 with :region uses the sibling .crai file.
write/3 returns reason: :unsupported.
Versions other than 3.0 and 3.1 open far enough to report the version.
Walking their containers returns reason: :unsupported_version.
Summary
Functions
Release the file descriptor held by handle.
Lazy containers, including the EOF container when it is present.
Small metadata map for a downstream tool.
Parsed SAM header from the first container.
Version, file id, container count, and reference names from the SAM header.
Open a local path or an in-memory CRAM binary.
Read alignments into a list.
Lazy alignments from a CRAM 3.0 or 3.1 file.
Library version, the same string as mix.exs.
CRAM writing is not implemented.
Types
Functions
@spec close(CRAM.Handle.t()) :: :ok | {:error, CRAM.Error.t()}
Release the file descriptor held by handle.
Returns :ok. A later call returns {:error, %CRAM.Error{reason: :closed}}.
Closing twice is that error, not an exception.
iex> {:ok, handle} = CRAM.open("test/fixtures/tiny.cram")
iex> CRAM.close(handle)
:ok
iex> {:error, error} = CRAM.close(handle)
iex> error.reason
:closed
@spec containers(CRAM.Handle.t()) :: {:ok, Enumerable.t()} | {:error, CRAM.Error.t()}
Lazy containers, including the EOF container when it is present.
Returns {:ok, stream}. The stream raises CRAM.Error if a later
container is truncated or fails its checksum. The handle stays open;
call close/1 when finished. CRAM 2.1 returns
{:error, %CRAM.Error{reason: :unsupported_version}}.
iex> {:ok, handle} = CRAM.open("test/fixtures/tiny.cram")
iex> {:ok, stream} = CRAM.containers(handle)
iex> stream |> Enum.map(& &1.eof) |> Enum.count(& &1)
1
iex> CRAM.close(handle)
:ok
@spec export_context(CRAM.Handle.t()) :: {:ok, map()} | {:error, CRAM.Error.t()}
Small metadata map for a downstream tool.
Returns {:ok, map} with :format, :version, :file_id, :byte_size,
:container_count, and :references. Each reference is %{name, length, md5}.
The map does not contain sequence bases. Gnosis is not a dependency.
CRAM 2.1 still returns the map, with an empty reference list.
iex> {:ok, handle} = CRAM.open("test/fixtures/tiny.cram")
iex> {:ok, context} = CRAM.export_context(handle)
iex> context.format
"CRAM"
iex> hd(context.references).name
"sq0"
iex> CRAM.close(handle)
:ok
@spec header(CRAM.Handle.t()) :: {:ok, CRAM.Header.t()} | {:error, CRAM.Error.t()}
Parsed SAM header from the first container.
Returns {:ok, %CRAM.Header{}}. The raw text is kept on the struct.
CRAM 2.1 returns {:error, %CRAM.Error{reason: :unsupported_version}}.
A header container with no FILE_HEADER block is reason: :bad_header.
iex> {:ok, handle} = CRAM.open("test/fixtures/tiny.cram")
iex> {:ok, header} = CRAM.header(handle)
iex> hd(header.sequences).name
"sq0"
iex> CRAM.close(handle)
:ok
@spec info(CRAM.Handle.t()) :: {:ok, CRAM.Info.t()} | {:error, CRAM.Error.t()}
Version, file id, container count, and reference names from the SAM header.
This reads every container header. It does not decode records. CRAM 2.1
returns supported: false and leaves the container fields nil instead of
walking a layout this library does not read.
iex> {:ok, handle} = CRAM.open("test/fixtures/tiny.cram")
iex> {:ok, info} = CRAM.info(handle)
iex> info.eof
true
iex> info.container_count
4
iex> CRAM.close(handle)
:ok
@spec metadata(CRAM.Handle.t()) :: {:ok, CRAM.Metadata.t()} | {:error, CRAM.Error.t()}
Returns {:ok, %CRAM.Metadata{}}. When the version is not 3.0 or 3.1,
header is nil and info.supported is false. Other header failures
return {:error, %CRAM.Error{}}.
iex> {:ok, handle} = CRAM.open("test/fixtures/tiny.cram")
iex> {:ok, metadata} = CRAM.metadata(handle)
iex> metadata.info.version
{3, 0}
iex> metadata.header.version
"1.6"
iex> CRAM.close(handle)
:ok
@spec open(source(), keyword()) :: {:ok, CRAM.Handle.t()} | {:error, CRAM.Error.t()}
Open a local path or an in-memory CRAM binary.
source is a path, {:file, path}, or {:binary, iodata}.
Options:
:mode—:strict(default) or:permissive:max_container_bytes— largest container body that will be read (default 64 MiB):max_block_bytes— largest declared block size that will be accepted (default 64 MiB):max_blocks— largest block count in one container (default 100_000):max_landmarks— largest landmark count in one container (default 100_000):max_header_bytes— largest SAM header that will be inflated (default 16 MiB):reference— aCRAM.ReferenceProviderstored for a later decode
Returns {:ok, handle} or {:error, %CRAM.Error{}}. A file that does not
start with CRAM is reason: :bad_magic. A short definition is
reason: :truncated. Inspection does not call :reference.
iex> {:ok, handle} = CRAM.open("test/fixtures/tiny.cram")
iex> {:ok, info} = CRAM.info(handle)
iex> info.version
{3, 0}
iex> CRAM.close(handle)
:ok
iex> {:error, short} = CRAM.open({:binary, "CRAM"})
iex> short.reason
:truncated
iex> {:error, magic} = CRAM.open({:binary, String.duplicate("N", 26)})
iex> magic.reason
:bad_magic
@spec read(CRAM.Handle.t(), keyword()) :: {:ok, [CRAM.Alignment.t()]} | {:error, CRAM.Error.t()}
Read alignments into a list.
Accepts the same options as stream/2. Returns {:ok, [%CRAM.Alignment{}]}.
A later container that cannot be decoded returns {:error, %CRAM.Error{}}
instead of raising. :limit stops after that many records. :region is
"name:start-end" or :unmapped.
iex> {:ok, handle} = CRAM.open("test/fixtures/decode/no_ref.cram")
iex> {:ok, [first | _]} = CRAM.read(handle, limit: 1)
iex> first.qname
"read1"
iex> first.pos
1
iex> CRAM.close(handle)
:ok
iex> {:ok, handle} = CRAM.open("test/fixtures/regions/sample.cram")
iex> {:ok, records} = CRAM.read(handle, region: "sq0:1-4")
iex> Enum.map(records, & &1.qname)
["read1"]
iex> {:error, error} = CRAM.read(handle, region: "sq0")
iex> error.reason
:invalid_range
iex> CRAM.close(handle)
:ok
@spec stream(CRAM.Handle.t(), keyword()) :: {:ok, Enumerable.t()} | {:error, CRAM.Error.t()}
Lazy alignments from a CRAM 3.0 or 3.1 file.
Returns {:ok, stream} of %CRAM.Alignment{}. The native decoder reads at
most :batch_size records per call (default 256, a positive integer).
Halting the stream sets a cancellation flag checked between records.
Options:
:batch_size— records requested from the NIF per call:limit— non-negative maximum yielded by the stream:reference— provider used instead of the one stored byopen/2:region—"name:start-end"(1-based, inclusive) or:unmapped:index— CRAI path. The default for a file ispath <> ".crai"
A missing NIF returns reason: :native_unavailable. A bad region string is
reason: :invalid_range. CRAM 2.1 is reason: :unsupported_version.
A missing reference is reason: :reference_missing. Inspection does not
use this function. The stream raises CRAM.Error if a later container
cannot be decoded.
iex> {:ok, handle} = CRAM.open("test/fixtures/decode/no_ref.cram")
iex> {:ok, stream} = CRAM.stream(handle, batch_size: 1, limit: 1)
iex> Enum.map(stream, & &1.qname)
["read1"]
iex> CRAM.close(handle)
:ok
@spec version() :: String.t()
Library version, the same string as mix.exs.
iex> CRAM.version()
"0.1.0"
@spec write(Path.t(), term(), keyword()) :: {:error, CRAM.Error.t()}
CRAM writing is not implemented.
path is stored on the error. data and opts are ignored. This function
does not create a file and does not raise.
iex> {:error, error} = CRAM.write("out.cram", <<>>, [])
iex> error.reason
:unsupported