The tbx module provides a Reader for reading tabix-indexed text files (like BED) in a line-based, format-agnostic way.
To use it, follow this general workflow:
- Create a
tbx::Reader using from_path or from_url. - Resolve a chromosome/contig name to its numeric ID using
tid(name). - Seek to a specific genomic region using
fetch(tid, start, end). - Iterate over the records in that region using the
records() iterator or the more efficient read(&mut record) method.
Note: For accessing tabix-indexed VCF files, it is recommended to use the bcf module instead, as tbx only provides raw lines which require manual parsing.
use rust_htslib::tbx::{self, Read};
// Create a tabix reader for reading a tabix-indexed BED file.
let path_bed = "file.bed.gz";
let mut tbx_reader = tbx::Reader::from_path(&path_bed)
.expect(&format!("Could not open {}", path_bed));
// Resolve chromosome name to numeric ID.
let tid = match tbx_reader.tid("chr1") {
Ok(tid) => tid,
Err(_) => panic("Could not resolve 'chr1' to contig ID"),
};
// Set region to fetch (0-based start and end).
tbx_reader
.fetch(tid, 0, 100_000)
.expect("Could not seek to chr1:1-100,000");
// Read through all records in region.
for record in tbx_reader.records() {
// ... actually do some work
}