Code Monkey home page Code Monkey logo

rust-htslib's Introduction

Crates.io Crates.io Crates.io docs.rs Travis

HTSlib bindings for Rust

This library provides HTSlib bindings and a high level Rust API for reading and writing BAM files.

To clone this repository, issue

git clone --recursive https://github.com/rust-bio/rust-htslib.git

ensuring that the HTSlib submodule is fetched, too. If you only want to use the library, there is no need to clone the repository. Go on to the Usage section in this case.

Resources

Usage

To use Rust-HTSlib in your Rust project, import the crate from your source code:

extern crate rust_htslib;

Rust-HTSlib provides a high level BAM API. Reading and writing BAM files is as easy as

use rust_htslib::bam;
use rust_htslib::bam::Read;

let bam = bam::Reader::new(&"test/test.bam").ok().expect("Error opening bam.");
let mut out = bam::Writer::with_template(&"test/test.bam", &"test/out.bam").ok().expect("Error opening bam.");

// copy reverse reads to new BAM file
for r in bam.records() {
    let record = r.ok().expect("Error reading BAM file.");
    if record.is_reverse() {
        out.write(&record).ok().expect("Error writing BAM file.");
    }
}

Pileups can be performed with

use rust_htslib::bam;
use rust_htslib::bam::Read;

let bam = bam::Reader::new(&"test/test.bam").ok().expect("Error opening bam.");

// pileup over all covered sites
for p in bam.pileup() {
 let pileup = p.ok().expect("Error reading BAM file.");
 println!("{}:{} depth {}", pileup.tid(), pileup.pos(), pileup.depth());

 for alignment in pileup.alignments() {
     match alignment.indel() {
         bam::pileup::Indel::Ins(len) => println!("Insertion of length {}", len),
         bam::pileup::Indel::Del(len) => println!("Deletion of length {}", len),
         _ => println!("Base {}", alignment.record().seq()[alignment.qpos()])
     }
 }
}

In both cases, indexed BAM files can be seeked for specific regions, constraining either the record iterator or the pileups:

use rust_htslib::bam;

let mut bam = bam::IndexedReader::new(&"test/test.bam").ok().expect("Error opening indexed BAM.");

// seek to chr1:50000-100000
let tid = bam.header.tid(b"CHROMOSOME_I").unwrap();
bam.seek(tid, 0, 20).ok().expect("Error seeking BAM file.");
// afterwards, read or pileup in this region

Authors

Contributors

License

Licensed under the MIT license http://opensource.org/licenses/MIT. This project may not be copied, modified, or distributed except according to those terms.

rust-htslib's People

Contributors

johanneskoester avatar christopher-schroeder avatar pmarks avatar gusevfe avatar andrelmartins avatar luthfianto avatar

Watchers

Riley Doyle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.