Skip to content

Commit

Permalink
Updated readme with fixed examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskoester committed Nov 2, 2015
1 parent e3ea929 commit c2d79fa
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,60 @@ If you only want to use the library, there is no need to clone the repository. G
To use Rust-HTSlib in your Rust project, import the crate from your source code:

```rust
extern crate rust_htslib as htslib;
use htslib::bam;
extern crate rust_htslib;
```

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

```rust
let bam = bam::Reader::new("path/to/some.bam").ok().expect("Error opening bam.");
let out = bam::Writer::with_template("path/to/some.bam", "reversereads.bam").ok().expect("Error opening bam.");
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);
out.write(&record).ok().expect("Error writing BAM file.");
}
}
```

Pileups can be performed with

```rust
let bam = bam::Reader::new("path/to/some.bam").ok().expect("Error opening bam.");
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 {}", bam.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()])
}
}
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:

```rust
let bam = bam::IndexedReader::new("path/to/some.bam");
use rust_htslib::bam;

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

// seek to chr1:50000-100000
bam.seek(bam.header.tid(b"chr1"), 50000, 100000).ok().expect("Error seeking BAM file.");
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
```

Expand Down

0 comments on commit c2d79fa

Please sign in to comment.