Skip to content

Instantly share code, notes, and snippets.

View elowy01's full-sized avatar

Ernesto Lowy elowy01

  • Biofidelity
  • Cambridge, UK
View GitHub Profile
@elowy01
elowy01 / Parse_VCF_BCFs_with_htslib_C++.md
Created November 16, 2020 10:28 — forked from gatoravi/Parse_VCF_BCFs_with_htslib_C++.md
Parse VCF/BCFs with htslib (C++)

#Gist of the gist This is a simple example showing how htslib can be used to parse VCF/BCF files in C++. Compiling options are shown in compile_and_run.sh, this assumes that you have compiled htslib into a static library - libhts.a, post a comment if you'd like help doing that.

The program takes a VCF file and spits out chromosome, position and number_of_alleles for each record in the VCF.

This test in the Samtools repo - [https://github.com/samtools/htslib/blob/1.2.1/test/test-vcf-api.c] also has some useful examples, note you might have to switch to the master branch version of that file to keep up with the currently rapidly evolving htslib API.

#Acknowledgements

@PoisonAlien
PoisonAlien / readBam.C
Last active November 9, 2023 21:21
reading bam files in C using htslib
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <htslib/sam.h>
int main(int argc, char *argv[]){
samFile *fp_in = hts_open(argv[1],"r"); //open bam file
bam_hdr_t *bamHdr = sam_hdr_read(fp_in); //read header
bam1_t *aln = bam_init1(); //initialize an alignment
@arq5x
arq5x / test.sh
Last active November 30, 2023 12:50
Compress and then Decompress a string with zlib.
# compile
$ g++ zlib-example.cpp -lz -o zlib-example
# run
$ ./zlib-example
Uncompressed size is: 36
Uncompressed string is: Hello Hello Hello Hello Hello Hello!
----------