pub struct IndexedReader { /* private fields */ }


Create a new Reader from path.

  • path - the path to open.

Define the region from which .read() or .records will retrieve reads.

Both iterating (with .records()) and looping without allocation (with .read() are a two stage process:

  1. ‘fetch’ the region of interest
  2. iter/loop trough the reads.


use rust_htslib::bam::{IndexedReader, Read};
let mut bam = IndexedReader::from_path(&"test/test.bam").unwrap();
bam.fetch(("chrX", 10000, 20000)); // coordinates 10000..20000 on reference named "chrX"
for read in bam.records() {
    println!("read name: {:?}", read.unwrap().qname());

The arguments may be anything that can be converted into a FetchDefinition such as

  • fetch(tid: u32) -> fetch everything on this reference
  • fetch(reference_name: &u8 | &str) -> fetch everything on this reference
  • fetch((tid: i32, start: i64, stop: i64)): -> fetch in this region on this tid
  • fetch((reference_name: &u8 | &str, start: i64, stop: i64) -> fetch in this region on this tid
  • fetch(FetchDefinition::All) or fetch(“.”) -> Fetch overything
  • fetch(FetchDefinition::Unmapped) or fetch(“*”) -> Fetch unmapped (as signified by the ‘unmapped’ flag in the BAM - might be unreliable with some aligners.

The start / stop coordinates will take i64 (the correct type as of htslib’s ‘large coordinates’ expansion), i32, u32, and u64 (with a possible panic! if the coordinate won’t fit an i64).

This replaces the old fetch and fetch_str implementations.

Set the reference path for reading CRAM files.

  • path - path to the FASTA reference

Trait Implementations

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Iterator over the records of the fetched region. Note that, while being convenient, this is less efficient than pre-allocating a Record and reading into it with the read method, since every iteration involves the allocation of a new Record.

Read next BAM record into given record. Use this method in combination with a single allocated record to avoid the reallocations occurring with the iterator. Read more

Records iterator using an Rc to avoid allocating a Record each turn. This is about 1% slower than the read based API in micro benchmarks, but has nicer ergonomics (and might not actually be slower in your applications). Read more

Iterator over pileups.

Return the htsFile struct

Return the header.

Use a shared thread-pool for writing. This permits controlling the total thread count when multiple readers and writers are working simultaneously. A thread pool can be created with crate::tpool::ThreadPool::new(n_threads) Read more

Seek to the given virtual offset in the file

Report the current virtual offset

Activate multi-threaded BAM read support in htslib. This should permit faster reading of large BAM files. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.