Code Monkey home page Code Monkey logo

hts-nim's Introduction

hts-nim

badge Build Status

This is a wrapper for htslib in nim.

Nim is a fast, garbage-collected language that compiles to C and has a syntax that's not too different to python.

If you use this library, please cite the paper

Projects using hts-nim are accumulating in the wiki

Installation

See Section Below

Usage

Examples of hts-nim tools are available in the hts-nim-tools repo

below are examples of the syntax in this library see the docs for more info:

Also see examples and other repos using hts-nim in the wiki

BAM / CRAM / SAM

See API docs here

import hts

# open a bam/cram and look for the index.
var b:Bam
open(b, "tests/HG02002.cramam", index=true, fai="/data/human/g1k_v37_decoy.fa")

for record in b:
  if record.mapping_quality > 10u:
    echo record.chrom, record.start, record.stop

# regional queries:
for record in b.query("6", 30816675, 32816675):
  if record.flag.proper_pair and record.flag.reverse:
    # cigar is an iterable of operations:
    for op in record.cigar:
      # $op gives the string repr of the operation, e.g. '151M'
      echo $op, " ", op.consumes.reference, " ", op.consumes.query

    # tags are pulled by type `ta`
    var mismatches = tag[int](record, "NM")
    if not mismatches.isNone and mismatches.get < 3:
      var rg = tag[string](record, "RG")
      if not rg.isNone: echo rg.get

VCF / BCF

See API docs here

import hts

var tsamples = @["101976-101976", "100920-100920", "100231-100231", "100232-100232", "100919-100919"]
# VCF and BCF supported
var v:VCF
doAssert(open(v, "tests/test.bcf", samples=tsamples))

var afs = new_seq[float32](5) # size doesn't matter. this will be re-sized as needed
var acs = new_seq[int32](5) # size doesn't matter. this will be re-sized as needed
var csq = new_string_of_cap(20)
for rec in v:
  echo rec, " qual:", rec.QUAL, " filter:", rec.FILTER
  var info = rec.info
  # accessing stuff from the INFO field is meant to be as fast as possible, allowing
  # the user to re-use memory as needed.
  info.get("CSQ", csq) # string
  info.get("AC", acs) # ints
  info.get("AF", afs) # floats
  echo acs, afs, csq, info.has_flag("IN_EXAC")

  # accessing format fields is similar
  var dps = new_seq[int32](len(v.samples))
  doAssert rec.format.ints("DP", dps) == Status.OK

# open a VCF for writing
var wtr:VCF
doAssert(open(wtr, "tests/outv.vcf", mode="w"))
wtr.header = v.header
doAssert(wtr.write_header())

# regional queries look for index. works for VCF and BCF
for rec in v.query("1:15600-18250"):
  echo rec.CHROM, ":", $rec.POS
  # adjust some values in the INFO
  var val = 22.3
  check rec.info.set("VQSLOD", val) == Status.OK
  doAssert(wtr.write_variant(rec))

Setup / Installation

hts-nim requires that htslib is installed and the shared library is available (use LD_LIBRARY_PATH if it is not in a standard location).

If you use docker, you can use one of these images to get Nim installed.

Or you can copy the Dockerfile from this repo

If you don't use docker, you can use choosenim to quickly install Nim and nimble.

Users can also either follow or run scripts/simple-install.sh which sets up Nim and nimble ready for use and shows the needed adjustments to $PATH.

Once Nim is set up, hts-nim can be installed with nimble install -y from the root of this repository.

In all cases, it's recommended to use nim version 0.18.0 or more recent.

Then, from this repo you can run nimble test and nimble install and then you can save the above snippets into some.nim and run them with nim c -d:release -r some.nim. This will run them and save an executable named some.

Static Builds

hts-nim is meant to simplify and speed development and distribution. To that end, there is some machinery to help create truly static binaries for linux from nim-projects and for simple nim scripts. This means that there is no dependency on libhts.so. These builds only require docker and this static binary.

For a single file application that does not have a nimble file we can specify the dependencies using --deps:

hts_nim_static_builder -s vcf_cleaner.nim --deps "hts@>=0.2.7" --deps "binaryheap"

This will create a static binary at ./vcf_cleaner.

Projects with .nimble files can use that directly to indicate dependencies. For example, to build slivar, we can do:

hts_nim_static_builder -s ../slivar/src/slivar.nim -n ../slivar/slivar.nimble

After this finishes, a static slivar binary will appear in the current working directory.

We can verify that it is static using:

$ file ./slivar 
./slivar: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.18, BuildID[sha1]=c2b5b52cb7be7f81bf90355a4e44a08a08df91d8, not stripped

The docker image is based on alpine linux and uses musl to create truly static binaries. At this time, libcurl is not supported so only binaries built using this method will only be able to access local files (no http/https/s3/gcs).

The docker images does use libdeflate by default. That provides, for example, a 20% speed improvement when used to build mosdepth.

hts-nim's People

Contributors

brentp avatar ernfrid avatar andreas-wilm avatar mflevine avatar sbeaum avatar xbello avatar nellore avatar dmckean avatar jaudoux avatar

Watchers

Steffen Möller avatar James Cloos 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.