HTSLib.jl

HTSLib.jl calls htslib, the C library behind bgzip, tabix and samtools, in-process, from Julia. It loads htslib from htslib_jll, so there is nothing to install besides the package.

Its main use is as a reference implementation: a way to ask htslib what it makes of a file, and to compare a Julia package's answers, or its speed, with htslib's, without starting a process for each call. So it reports what htslib does, as htslib does it: its positions, its parsing, its corrections and its messages.

What this package covers

  • BGZF: compressing and decompressing files, as bgzip does.
  • Tabix: building TBI and CSI indexes, with htslib's presets or columns of your own, as tabix does, and reading the records of an indexed file, by region or from start to end.
  • SAM, BAM and CRAM: reading alignments and their headers, by region with an index, or from start to end, as samtools view does.
  • Records: records that own their bytes, or views of htslib's buffers that read a file without allocating.
  • Low-level bindings: HTSLib.Bindings, a ccall of each C function the package uses, and others that packages need, with htslib's structs and constants.

A tabix record is a line with htslib's interval: the package doesn't parse its other columns, whatever the format, BED, GFF, VCF or another. It doesn't read BCF files, write alignments, or read FASTA files, except as htslib does for CRAM references.

Installation and platforms

using Pkg
Pkg.add("HTSLib")

The versions of Julia and of htslib that HTSLib.jl supports are the compat bounds of its Project.toml. Its bindings are generated from the one minor version of htslib that those bounds admit, and HTSLib.version() gives the version that's loaded.

htslibjll builds htslib for Linux, macOS and FreeBSD, but not for Windows. On Windows, the package loads, so that packages that depend on it do too, but `HTSLib.isavailable()isfalse`, and every call into htslib fails.

The manual

First steps

A BED file, compressed and indexed, then read by region:

using HTSLib
using HTSLib: bgzip, tabix_index, overlaps

directory = mktempdir()
path = joinpath(directory, "features.bed")
write(path, "chr1\t1000\t1500\tfirst\nchr1\t1999\t2100\tsecond\nchr2\t0\t50\tthird\n")

compressed = bgzip(path)
tabix_index(compressed)

TabixFile(compressed) do file
    overlaps(file, "chr1:1001-2000")
end
2-element Vector{TabixRecord{HTSLib.Owned}}:
 TabixRecord("chr1", 1000, 1500, "chr1\t1000\t1500\tfirst")
 TabixRecord("chr1", 1999, 2100, "chr1\t1999\t2100\tsecond")

Each record has its sequence, its interval and its line. The interval is htslib's, 0-based and half-open, whatever the file's own positions are; Positions and intervals describes how each part of the package counts.

The example's second line imports the functions it calls by name. HTSLib.jl exports its types, such as TabixFile and AlignmentRecord, but none of its functions, since their common names would clash with those of packages loaded alongside, such as GenomicFeatures' each_overlap and htslib_jll's bgzip. So a program imports the functions it calls, as the example does, or qualifies them, as in HTSLib.overlaps(file, region).