-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvcf2beagle.cpp
86 lines (83 loc) · 3.3 KB
/
vcf2beagle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// -*- compile-command: "g++ vcf2beagle.cpp -std=c++11 -g -O3 -Wall -lhts -lz" -*-
#include "vcfpp.h"
#include <zlib.h>
#include <cmath>
using namespace std;
using namespace vcfpp;
int main(int argc, char * argv[])
{
// ========= helper message and parameters parsing ============================
std::vector<std::string> args(argv + 1, argv + argc);
if(argc <= 1 || args[0] == "-h" || args[0] == "-help" || args[0] == "--help")
{
std::cout << "Author: Zilong-Li ([email protected])\n"
<< "Description:\n"
<< " convert vcf with PL or GL tag to beagle file\n\n"
<< "Usage example:\n"
<< " " + (std::string)argv[0] + " -i in.bcf -o beagle.gz -t PL\n"
<< " " + (std::string)argv[0] + " -i in.bcf -o beagle.gz -t GL -s ^S1,S2 -r chr1:1-1000 \n"
<< "\nOptions:\n"
<< " -i input vcf/bcf file\n"
<< " -o ouput vcf/bcf file [stdout]\n"
<< " -s list of samples to be included or excluded\n"
<< " -r specific region to be included\n"
<< " -t tag of genotype likelihoods in VCF. [PL or GL]\n"
<< std::endl;
return 1;
}
std::string invcf, outfile = "begale.gz", samples = "-", region = "", tag = "PL";
for(size_t i = 0; i < args.size(); i++)
{
if(args[i] == "-i") invcf = args[++i];
if(args[i] == "-o") outfile = args[++i];
if(args[i] == "-s") samples = args[++i];
if(args[i] == "-r") region = args[++i];
if(args[i] == "-t") tag = args[++i];
}
// ========= core calculation part ===========================================
BcfReader vcf(invcf, region, samples);
BcfRecord var(vcf.header);
int nsamples = vcf.nsamples;
vector<int> pl;
vector<float> gl;
std::string hdr{"marker\tallele1\tallele2"}, line;
for(auto s : vcf.header.getSamples()) hdr += "\t" + s + "\t" + s + "\t" + s;
hdr += "\n";
gzFile gzfp = gzopen(outfile.c_str(), "wb");
gzwrite(gzfp, hdr.c_str(), hdr.size());
vector<double> out;
while(vcf.getNextVariant(var))
{
// if(!var.isSNP()) continue;
if(tag == "PL")
{
var.getFORMAT("PL", pl); // try get PL values
out.resize(pl.size());
for(size_t i = 0; i < pl.size(); i++) out[i] = std::pow(10, -(double)pl[i] / 10.0);
}
else if(tag == "GL")
{
var.getFORMAT("GL", gl); // try get GL values
out.resize(gl.size());
for(size_t i = 0; i < gl.size(); i++) out[i] = std::pow(10, (double)gl[i]);
}
else
{
throw runtime_error("please specify -t PL or GL\n");
}
// normalize it
line = var.CHROM() + "_" + to_string(var.POS()) + "\t" + var.REF() + "\t" + var.ALT();
for(int i = 0; i < 3 * nsamples; i+=3)
{
double s = out[i] + out[i + 1] + out[i + 2];
out[i] /= s;
out[i + 1] /= s;
out[i + 2] /= s;
line += "\t" + to_string(out[i]) + "\t" + to_string(out[i + 1]) + "\t" + to_string(out[i + 2]);
}
line += "\n";
gzwrite(gzfp, line.c_str(), line.size());
}
gzclose(gzfp);
return 0;
}