Skip to content

Instantly share code, notes, and snippets.

@gatoravi
Last active November 16, 2020 10:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gatoravi/cad922bdf2b625a91126 to your computer and use it in GitHub Desktop.
Save gatoravi/cad922bdf2b625a91126 to your computer and use it in GitHub Desktop.
Parse VCF/BCFs with htslib (C++)
#! /bin/bash
#Compile
g++ -g -I path/to/htslib/ -D__STDC_LIMIT_MACROS -L path/to/htslib/ ./test_htslib_vcfparser.cc -lhtslib -lz -lgcov -lpthread -o test_htslib_vcfparser
#Execute
./test_htslib_vcfparser ./test_bcftools.vcf

#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 This repo was a good reference for how to use the library. Thanks @junjiezhujason

/* test_htslib_vcfparser.cc
Copyright (c) 2015, Avinash Ramu
Author: Avinash Ramu <aramu@genome.wustl.edu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#include <iostream>
#include <stdexcept>
#include "vcf.h"
int usage() {
std::cerr << "Usage: ./test_htslib_vcfparser example.vcf" << std::endl;
return 1;
}
int main(int argc, char* argv[]) {
if(argc == 2) {
htsFile *test_bcf = NULL;
bcf_hdr_t *test_header = NULL;
bcf1_t *test_record = bcf_init();
test_bcf = bcf_open(argv[1], "r");
if(test_bcf == NULL) {
throw std::runtime_error("Unable to open file.");
}
test_header = bcf_hdr_read(test_bcf);
if(test_header == NULL) {
throw std::runtime_error("Unable to read header.");
}
std::cout << "chromosome\tposition\tnum_alleles" << std::endl;
while(bcf_read(test_bcf, test_header, test_record) == 0) {
std::cout << bcf_hdr_id2name(test_header, test_record->rid) << "\t" <<
test_record->pos << "\t" <<
test_record->n_allele << "\t" <<
std::endl;
}
bcf_hdr_destroy(test_header);
bcf_destroy(test_record);
bcf_close(test_bcf);
} else {
return usage();
}
return 0;
}
@sinamajidian
Copy link

Hi
Thanks for your effort.
I've run the .sh file but I've face an error about ./test_htslib_vcfparser.cc. Could you please help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment