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.
- Crystal
- HTSlib
- Ubuntu :
apt install libhts-dev - macOS :
brew install htslib - Any OS : Build from source code
- Make sure that
pkg-configcan detect htslib:pkg-config --libs htslib
- Ubuntu :
Add hts to your shard.yml:
dependencies:
hts:
github: bio-cr/hts.cr
branch: developRun shards install
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
endRead 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["DP"],
genotypes: r.format.get_string("GT")
end
end- 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.
The Crystal language is suited for creating efficient command-line tools. The Ruby language, on the other hand, is suited for exploratory analysis.
🚀 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.
https://github.com/brentp/vcf-bench
code: https://github.com/kojix2/vcf-bench/blob/kojix2/crystal-htslib/read.cr