#sam #read-write #pileup #htslib #reader-writer #genomics #bgzip

bam

Crate that allows to read and write BAM, SAM and BGZIP files, written completely in Rust

20 releases

0.1.4 Apr 19, 2021
0.1.3 Feb 17, 2021
0.1.2 Dec 2, 2020
0.1.1 Sep 4, 2020
0.0.11 Nov 15, 2019

#189 in Compression

Download history 53/week @ 2024-08-12 54/week @ 2024-08-19 90/week @ 2024-08-26 42/week @ 2024-09-02 48/week @ 2024-09-09 32/week @ 2024-09-16 78/week @ 2024-09-23 56/week @ 2024-09-30 25/week @ 2024-10-07 54/week @ 2024-10-14 53/week @ 2024-10-21 50/week @ 2024-10-28 62/week @ 2024-11-04 61/week @ 2024-11-11 79/week @ 2024-11-18 41/week @ 2024-11-25

246 downloads per month
Used in 5 crates

MIT license

250KB
5K SLoC

bam is a crate that allows to read and write BAM, SAM and BGZIP files, written completely in Rust.

Why?

Having a crate written completely in Rust reduces the number of dependencies and compilation time. Additionally, it removes the need to install additional C libraries.

Errors produced by this crate are more readable and easier to catch and fix on-the-fly.

Overview

Currently, there are three readers and two writers:

  • bam::IndexedReader - fetches records from random genomic regions.
  • bam::BamReader - reads a BAM file consecutively.
  • bam::SamReader - reads a SAM file consecutively.
  • bam::BamWriter - writes a BAM file.
  • bam::SamWriter - writes a SAM file.

BAM readers and writers have single-thread and multi-thread modes.

You can construct pileups from all readers using Pileup.

You can use bgzip module to interact directly with bgzip files (BGZF).

The crate also allows to conviniently work with SAM/BAM records and their fields, such as CIGAR or tags.

Usage

The following code would load BAM file in.bam and its index in.bam.bai, take all records from 3:600001-700000 and print them on the stdout.

extern crate bam;

use std::io;
use bam::RecordWriter;

fn main() {
    let mut reader = bam::IndexedReader::from_path("in.bam").unwrap();
    let output = io::BufWriter::new(io::stdout());
    let mut writer = bam::SamWriter::build()
        .write_header(false)
        .from_stream(output, reader.header().clone()).unwrap();

    for record in reader.fetch(&bam::Region::new(2, 600_000, 700_000)).unwrap() {
        let record = record.unwrap();
        writer.write(&record).unwrap();
    }
}

You can find more detailed usage here.

Changelog

You can find changelog here.

Issues

Please submit issues here or send them to timofey.prodanov[at]gmail.com.

Dependencies

~580KB