HTS.cr

CI Slack Docs Latest Ask DeepWiki Lines of Code DOI

HTS.cr provides Crystal bindings for HTSlib that allow you to read and write file formats commonly used in genomics, such as SAM, BAM, VCF, and BCF.

Requirements

Installation

Add hts to your shard.yml:

dependencies:
  hts:
    github: bio-cr/hts.cr
    branch: develop

Run shards install

Usage

Read SAM / BAM / CRAM

require "hts/bam"

HTS::Bam.open(bam_path) do |bam|
  bam.each do |r|
    tags = r.aux

    p name: r.qname,
      chrom: r.chrom,
      start: r.pos + 1,
      cigar: r.cigar.to_s,
      seq: r.seq,
      qual: r.qual_string,
      nm: tags.get_int("NM"),
      mc: tags.get_string("MC")
  end
end

Read VCF / BCF

require "hts/bcf"

HTS::Bcf.open(bcf_path) do |bcf|
  bcf.each do |r|
    p chrom: r.chrom,
      pos: r.pos + 1,
      id: r.id,
      qual: r.qual,
      filter: r.filter,
      ref: r.ref,
      alt: r.alt,
      info_dp: r.info.get_int("DP"),
      genotypes: r.format.get_string("GT")
  end
end

API Overview

 ┌──────────────────── HTS ────────────────────┐
 │                                             │
 │ ┌─ Bam ────────┬─ Bcf ───────┬─ Tabix ────┐ │
 │ │ SAM BAM CRAMVCF BCFTABIX      │ │
 │ └──────────────┴─────────────┴────────────┘ │
 │                     ┌─LibHTS2───────────┐   │
 │ ┌─LibHTS────────────┤ Macro functions   ├─┐ │
 │ │ Native C bindings └───────────────────┘ │ │
 │ └─────────────────────────────────────────┘ │
 └─────────────────────────────────────────────┘

LibHTS2: Since methods cannot be added to Lib in the Crystal language, macro functions are implemented in the LibHTS2 module. This is different from Ruby-htslib.

Looking for flexibility?

The Crystal language is suited for creating efficient command-line tools. The Ruby language, on the other hand, is suited for exploratory analysis.

Contributing

:rocket: Feel free to fork it!

git clone https://github.com/bio-cr/hts.cr
cd hts.cr
crystal spec

Bug reports and pull requests are welcome.

Benchmark

https://github.com/brentp/vcf-bench

code: https://github.com/kojix2/vcf-bench/blob/kojix2/crystal-htslib/read.cr