Reference
HTSLib.HTSLib — Module
HTSLibJulia bindings to htslib, the C library behind bgzip, tabix and samtools, from htslib_jll.
HTSLib compresses files with BGZF, builds and loads tabix indexes, and reads records by region or from start to end, as the bgzip and tabix command-line tools do. It reads SAM, BAM and CRAM files, from start to end and by region, as samtools does. Its use is as a reference implementation, to validate and benchmark Julia packages against htslib in-process, so it reports what htslib finds rather than offering an API of its own for the formats. The low-level bindings are in HTSLib.Bindings.
HTSLib.AbstractHTSFile — Type
AbstractHTSFileAbstract supertype for all HTSLib file handles (AlignmentFile, TabixFile).
HTSLib.AbstractHTSRecord — Type
AbstractHTSRecordAbstract supertype for all HTSLib records: TabixRecord and AlignmentRecord, each of which owns its bytes, as a TabixRecord{HTSLib.Owned}, or borrows a buffer of htslib, as a TabixRecordView, which is TabixRecord{HTSLib.Borrowed}.
HTSLib.AlignmentFile — Type
AlignmentFile(path::AbstractString; index = nothing, reference = nothing)
AlignmentFile(f, path::AbstractString; kwargs...)An open SAM, BAM or CRAM file, as samtools opens it, with its header.
index: the path of the index, BAI, CSI or CRAI, for queries; by default, the index next to the file. The index is loaded at the first query.reference: the path of the FASTA reference of a CRAM file. Without it, htslib looks the reference up as samtools does, through theREF_PATHandREF_CACHEenvironment variables, which may download it.
As samtools does, htslib adds MD and NM tags computed from the reference to the records of a CRAM file.
Call close when finished, or pass a function f to call with the file, which is closed afterwards.
HTSLib.AlignmentHeader — Type
AlignmentHeaderThe header of a SAM, BAM or CRAM file, as htslib read it. String(header) gives its text, and sequence_names and sequence_lengths its reference sequences. It reads the names of its reference sequences once, when it is made, so that every record shares them, and rname of a record allocates nothing. The garbage collector frees it once no file or record refers to it.
HTSLib.AlignmentRecord — Type
AlignmentRecord{O}An alignment that htslib read: the fixed-length fields of its bam1_core_t, its variable-length data, and the header of its file. O is its ownership:
- an
AlignmentRecordView, which isAlignmentRecord{HTSLib.Borrowed}, borrows a buffer of htslib that the next record overwrites, so it is only valid until the next iteration; - an
AlignmentRecord{HTSLib.Owned}owns its data, in Julia's memory, so it stays valid after its file is closed.
copy(view) gives an owned record, which allocates only its data, and since an owned record is immutable, copy(record) is record itself. Two records are equal if they have the same fields and come from the same header, so a view equals its copy.
string(record) is the SAM line of the record, as samtools view prints it, with every field. data(record) gives the variable-length fields, QNAME, CIGAR, SEQ, QUAL and the tags, as htslib lays them out, as an ImmutableMemoryView{UInt8}. The accessors take the names of the SAM specification's fields, with aliases where the specification names a field otherwise:
| Accessor | Value | Aliases |
|---|---|---|
qname | QNAME, "Query template NAME", as a StringView of the record's data, valid as long as the record is | template_name |
rname | RNAME, "Reference sequence NAME", as the header's String, or nothing without one | reference_name |
pos | POS, "1-based leftmost mapping POSition", or 0 without one | position |
end_position | the last reference base that the CIGAR covers, 1-based, as htslib's bam_endpos computes it: an unmapped record, or one without a CIGAR, covers one base, and a record without a position ends at 0; derived, not a field | — |
reference_index | the 1-based index of RNAME into sequence_names, or 0 without one; derived, not a field | — |
A record overlaps a region from left to right if pos(record) <= right and end_position(record) >= left. With GenomicFeatures loaded, group_name, left_position and right_position give rname, pos and end_position.
HTSLib.AlignmentRecordView — Type
AlignmentRecordViewAn AlignmentRecord that borrows a buffer of htslib, AlignmentRecord{HTSLib.Borrowed}, which is only valid until the next iteration.
HTSLib.Borrowed — Type
HTSLib.BorrowedThe type parameter of a record that borrows a buffer of htslib, which the next record read overwrites, so that it is only valid until the next iteration: TabixRecordView, which is TabixRecord{Borrowed}, and AlignmentRecordView, which is AlignmentRecord{Borrowed}.
HTSLib.Owned — Type
HTSLib.OwnedThe type parameter of a record that owns its bytes, in Julia's memory, so that it stays valid after the next iteration and after its file is closed: TabixRecord{Owned} and AlignmentRecord{Owned}. copy gives one from a view.
HTSLib.TabixFile — Type
TabixFile(path::AbstractString; index = nothing, threads = 0)
TabixFile(f, path::AbstractString; kwargs...)An open file, BGZF-compressed or plain, with its tabix index, as tabix opens it. The index is found next to the file, unless index gives its path. A plain file needs index, such as that of its compressed copy, and can be read from start to end with each_record, but not queried. threads is the number of threads that decompress the file, or none, for the file and for each query.
Each query, and each read from start to end, opens the file again and reads through a handle of its own, so that they may interleave.
Call close when finished, or pass a function f to call with the file, which is closed afterwards.
HTSLib.TabixIndex — Type
TabixIndex(path::AbstractString; index = nothing)The tabix index, TBI or CSI, of the BGZF-compressed file path, loaded with htslib. The index next to the file is found as tabix finds it, unless index gives its path. Call close when finished; the garbage collector closes it otherwise.
HTSLib.TabixRecord — Type
TabixRecord{O}A record that htslib read from a tabix-indexed file: the 1-based index of its sequence in sequence_names(file), its interval, and its line. O is its ownership:
- a
TabixRecordView, which isTabixRecord{HTSLib.Borrowed}, borrows a buffer of htslib that the next record overwrites, so it is only valid until the next iteration; - a
TabixRecord{HTSLib.Owned}owns its line, in Julia's memory, so it stays valid after its file is closed.
copy(view) gives an owned record, which allocates only its line, and since an owned record is immutable, copy(record) is record itself. A view equals its copy. TabixRecord(sequence, start, stop, line) makes an owned record from strings.
The fields start and stop are the interval, 0-based and half-open, as htslib reports it: start is the position of the first base, and stop the position after the last. sequence_name(record) gives its sequence name, from the column that the tabix specification calls col_seq, the "Column for the sequence name". position(record) gives the 1-based position of the first base, and interval_0based(record) the interval. line(record) gives the line as an ImmutableMemoryView{UInt8}, without its line ending, and String(record) copies it. The line keeps the positions of the file, which HTSLib.is_zero_based(file) tells. With GenomicFeatures loaded, group_name, left_position and right_position give the sequence name and the 1-based, inclusive interval.
HTSLib.TabixRecordView — Type
TabixRecordViewA TabixRecord that borrows a buffer of htslib, TabixRecord{HTSLib.Borrowed}, which is only valid until the next iteration.
HTSLib.bgzip — Function
bgzip(source::AbstractString, destination::AbstractString = source * ".gz"; level = nothing, threads = 0) -> StringCompress the file source into the BGZF file destination with htslib, as bgzip does, and return destination. Unlike bgzip, source is kept.
level is the compression level, from 0 to 9, or htslib's default; threads is the number of threads that compress blocks, or none.
HTSLib.bgzip_decompress — Function
bgzip_decompress(source::AbstractString, destination::AbstractString = (endswith(source, ".gz") ? source[1:(end - 3)] : source * ".decompressed"); threads = 0) -> StringDecompress the BGZF-compressed file source into destination with htslib, and return destination. threads is the number of decompression threads, or 0.
HTSLib.data — Method
HTSLib.data(record::AlignmentRecord) -> ImmutableMemoryView{UInt8}The variable-length fields of record, QNAME, CIGAR, SEQ, QUAL and the tags, as htslib lays them out in its bam1_t, as bytes that can't be written through the view. A view's data is in a buffer of htslib, so it is only valid until the next iteration; copy(record) keeps it.
HTSLib.each_overlap — Method
each_overlap(file::AlignmentFile, region::AbstractString; copy = true)
each_overlap(file::AlignmentFile, sequence::AbstractString, left::Integer, right::Integer; copy = true)
each_overlap(file::AlignmentFile, sequence::AbstractString, range::AbstractUnitRange; copy = true)Iterate over the records of file that overlap a region, as samtools view file region does, yielding an AlignmentRecord{HTSLib.Owned} of each, which owns its data. The region is a samtools region string, such as "chr1:1001-2000", "chr1" or "*" for the records without a position, or a sequence name with 1-based, inclusive positions. As in htslib, a sequence that the header does not have has no records. Queries need the index of the file, and a BAM, CRAM, or SAM file compressed with bgzip. Each query opens the file again and reads through a handle of its own, so queries of the same file may interleave.
With copy = false, it yields an AlignmentRecordView of each, which allocates nothing, and is only valid until the next iteration; copy keeps a record. collect gives owned records either way.
HTSLib.each_overlap — Method
each_overlap(file::TabixFile, region::AbstractString; copy = true)
each_overlap(file::TabixFile, sequence::AbstractString, left::Integer, right::Integer; copy = true)
each_overlap(file::TabixFile, sequence::AbstractString, range::AbstractUnitRange; copy = true)Iterate over the records of file that overlap a region, as tabix file region does, yielding a TabixRecord{HTSLib.Owned} of each, which owns its line. The file must be BGZF-compressed. The region is a tabix region string, such as "chr1:1001-2000" or "chr1", or a sequence name with 1-based, inclusive positions. As in htslib, a sequence that the index does not have, or a region that htslib cannot parse, has no records. Each query opens the file again and reads through a handle of its own, so queries of the same file may interleave.
With copy = false, it yields a TabixRecordView of each, which allocates nothing, and is only valid until the next iteration; copy keeps a record. collect gives owned records either way.
HTSLib.each_record — Method
each_record(file::AlignmentFile; copy = true)Iterate over every record of file from its start, as samtools view reads them, yielding an AlignmentRecord{HTSLib.Owned} of each, which owns its data. The file is opened again, so iterating does not disturb queries.
With copy = false, it yields an AlignmentRecordView of each, which allocates nothing, and is only valid until the next iteration; copy keeps a record. collect gives owned records either way.
HTSLib.each_record — Method
each_record(file::TabixFile; copy = true)Iterate over every record of file from its start, as htslib reads them with tbx_readrec, yielding a TabixRecord{HTSLib.Owned} of each, which owns its line. Header lines, the first line_skip lines and those that start with the meta character of the index, are skipped, as when the index was built. The file is opened again, so iterating does not disturb queries.
With copy = false, it yields a TabixRecordView of each, which allocates nothing, and is only valid until the next iteration; copy keeps a record. collect gives owned records either way.
HTSLib.end_position — Method
HTSLib.end_position(record::AlignmentRecord) -> IntThe 1-based position of the last reference base that the CIGAR covers, as htslib's bam_endpos computes it. As htslib does, an unmapped record, or one without a CIGAR, covers one base, pos(record), and a record without a position ends at 0.
HTSLib.format — Method
HTSLib.format(file::AlignmentFile) -> SymbolThe format of the file, :sam, :bam or :cram, as htslib detects it from its content.
HTSLib.interval_0based — Method
HTSLib.interval_0based(record::AlignmentRecord) -> Tuple{Int, Int}The interval of record as htslib gives it, 0-based and half-open: (pos(record) - 1, end_position(record)), which is (-1, 0) for a record without a position, as htslib indexes it.
HTSLib.interval_0based — Method
HTSLib.interval_0based(record::TabixRecord) -> Tuple{Int, Int}The interval of record as htslib gives it, 0-based and half-open, (record.start, record.stop), whatever the positions of the file.
HTSLib.is_available — Method
HTSLib.is_available() -> BoolWhether htslib_jll provides htslib for this platform.
HTSLib.is_zero_based — Method
HTSLib.is_zero_based(index::TabixIndex) -> Bool
HTSLib.is_zero_based(file::TabixFile) -> BoolWhether the positions of the indexed file are 0-based and half-open, as the BED preset and tabix -0 make them, rather than 1-based and inclusive. This is htslib's TBX_UCSC flag of the index. Whatever it is, htslib gives each record a 0-based, half-open interval, start and stop; the line of a record keeps the positions of the file, so this tells how to read its columns.
HTSLib.line — Method
HTSLib.line(record::TabixRecord) -> ImmutableMemoryView{UInt8}The line of record, without its line ending, as bytes that can't be written through the view. A view's line is in a buffer of htslib, so it is only valid until the next iteration; String(record) copies it. split_each(line(record), UInt8('\t')), of MemoryViews, gives its columns without copying them, and StringView(line(record)), of StringViews, gives it as a string without copying it.
HTSLib.overlaps — Method
overlaps(file::AlignmentFile, region) -> Vector{AlignmentRecord{HTSLib.Owned}}
overlaps(file::AlignmentFile, sequence, left, right) -> Vector{AlignmentRecord{HTSLib.Owned}}The records of file that overlap a region, as collect(each_overlap(file, region...)) gives them. query is an alias of overlaps.
HTSLib.overlaps — Method
overlaps(file::TabixFile, region) -> Vector{TabixRecord{HTSLib.Owned}}
overlaps(file::TabixFile, sequence, left, right) -> Vector{TabixRecord{HTSLib.Owned}}The records of file that overlap a region, as collect(each_overlap(file, region...)) gives them. query is an alias of overlaps.
HTSLib.pos — Method
HTSLib.pos(record::AlignmentRecord) -> IntPOS, the SAM specification's "1-based leftmost mapping POSition" of the first base that the CIGAR covers, or 0 for a record without a position. position is an alias.
HTSLib.position — Method
HTSLib.position(record::AlignmentRecord) -> IntAn alias of HTSLib.pos, after the SAM specification's "mapping POSition".
HTSLib.position — Method
HTSLib.position(record::TabixRecord) -> IntThe 1-based position of the first base of record, record.start + 1, whatever the positions of the file.
HTSLib.qname — Method
HTSLib.qname(record::AlignmentRecord) -> StringViewQNAME, the SAM specification's "Query template NAME", as a StringView of the record's data, so that reading it allocates nothing. It is valid as long as the record is: until the next iteration for a view; String(qname(record)) keeps it. template_name is an alias.
HTSLib.query — Function
query(file, region...)An alias of overlaps, after htslib's region queries, tbx_itr_querys and sam_itr_querys, and the region query of tabix and samtools view.
HTSLib.records — Method
records(file::AlignmentFile) -> Vector{AlignmentRecord{HTSLib.Owned}}Every record of file, as collect(each_record(file)) gives them.
HTSLib.records — Method
records(file::TabixFile) -> Vector{TabixRecord{HTSLib.Owned}}Every record of file, as collect(each_record(file)) gives them.
HTSLib.reference_index — Method
HTSLib.reference_index(record::AlignmentRecord) -> IntThe 1-based index of RNAME into sequence_names of the file, or 0 for a record without one.
HTSLib.reference_name — Method
HTSLib.reference_name(record::AlignmentRecord) -> Union{String, Nothing}An alias of HTSLib.rname, after the SAM specification's "Reference sequence NAME".
HTSLib.rname — Method
HTSLib.rname(record::AlignmentRecord) -> Union{String, Nothing}RNAME, the SAM specification's "Reference sequence NAME", or nothing for a record without one, whose RNAME is *. It is the header's String, shared by the records of the file, so reading it allocates nothing. reference_name is an alias.
HTSLib.sequence_lengths — Method
sequence_lengths(header::AlignmentHeader) -> Vector{Int}
sequence_lengths(file::AlignmentFile) -> Vector{Int}The lengths of the reference sequences, in the order of sequence_names.
HTSLib.sequence_name — Method
HTSLib.sequence_name(record::TabixRecord) -> StringThe sequence name of record, from the column that the tabix specification calls col_seq, the "Column for the sequence name". It is the file's String, shared by its records, so reading it allocates nothing.
HTSLib.sequence_names — Method
sequence_names(index::TabixIndex) -> Vector{String}
sequence_names(file::TabixFile) -> Vector{String}The sequence names of the index, in the order of the file.
HTSLib.tabix_index — Method
tabix_index(path::AbstractString; preset = nothing, kwargs...) -> StringBuild an index of the BGZF-compressed, sorted file path with htslib, as tabix does, and return the path of the index.
preset: the columns of the file, astabix -p::gff,:bed,:psltbl,:samor:vcf. By default, it is detected astabixdetects it: from a name that ends in.gff.gz,.bed.gz,.sam.gzor.vcf.gz, ignoring case, or else from the contents of a VCF file. Wheretabixwould use the GFF preset for a file that it cannot detect,tabix_indexthrows anArgumentError. BAM, CRAM and BCF files, whichtabixdetects but indexes with htslib's other indexers,bam_index_buildandbcf_index_build, throw anArgumentErrortoo, sincetabix_indexbuilds only tabix indexes.sequence_column,begin_column,end_column: 1-based columns that replace those of the preset, astabix -s,-band-e.zero_based: whether positions are 0-based and half-open, instead of 1-based and inclusive; by default, as the preset says.trueistabix -0, andfalsemakes a 0-based preset 1-based, as givingtabix -s,-band-ewithout-0does. htslib reads only the begin column differently, since the last base of a 1-based, inclusive interval is the end of the 0-based, half-open one.meta_char: the character that starts header and comment lines, astabix -c.line_skip: the number of lines to skip at the start of the file, astabix -S.csi: build a CSI index,path * ".csi", astabix -C, instead of a TBI index,path * ".tbi".min_shift: the bins of a CSI index at the finest level are2^min_shiftbases, astabix -m; 14 by default.index: the path of the index, instead of the default.threads: the number of threads that decompress the file, or none.
Whatever the positions of the file, htslib gives each record a 0-based, half-open interval, and HTSLib.is_zero_based tells which positions the index was built for. As htslib does, a start before the first base is read as the first base, and htslib warns "Coordinate <= 0 detected. Did you forget to use the -0 option?", where -0 is zero_based = true.
HTSLib.template_name — Method
HTSLib.template_name(record::AlignmentRecord) -> StringViewAn alias of HTSLib.qname, after the SAM specification's "Query template NAME".
HTSLib.version — Method
HTSLib.version() -> VersionNumberThe version of the loaded htslib.