hts.cr v0.3.0

HTSlib bindings for Crystal

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

  • Crystal
  • HTSlib
    • Ubuntu : apt install libhts-dev
    • macOS : brew install htslib
    • Any OS : Build from source code
    • Make sure that pkg-config can detect htslib: pkg-config --libs htslib

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

  • High level API - Classes include Bam, Bcf, Tabix, Faidx, etc.
  • LibHTS - Native C bindings to HTSLib
  • For more information, please see API documentation.
 ┌──────────────────── HTS ────────────────────┐
 │                                             │
 │ ┌─ Bam ────────┬─ Bcf ───────┬─ Tabix ────┐ │
 │ │ SAM BAM CRAM │ VCF BCF     │ TABIX      │ │
 │ └──────────────┴─────────────┴────────────┘ │
 │                     ┌─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

🚀 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

Repository

hts.cr

Owner
Statistic
  • 14
  • 1
  • 1
  • 6
  • 0
  • about 10 hours ago
  • March 26, 2021
License

MIT License

Links
Synced at

Wed, 24 Jun 2026 07:00:28 GMT

Languages