-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvcf_addINFO.cpp
73 lines (70 loc) · 2.82 KB
/
vcf_addINFO.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
// -*- compile-command: "g++ vcf_addINFO.cpp -std=c++11 -g -O3 -Wall -I.. -lhts" -*-
#include "vcfpp.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"
<< " calculate INFO score per site given GP from input vcf file\n\n"
<< "Usage example:\n"
<< " " + (std::string)argv[0] + " -i in.bcf \n"
<< " " + (std::string)argv[0] + " -i in.bcf -o out.bcf -s ^S1,S2 -r chr1:1-1000 \n"
<< " bcftools view in.bcf | " + (std::string)argv[0] + " -i - -o out.bcf \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"
<< std::endl;
return 1;
}
std::string invcf, outvcf = "-", samples = "-", region = "";
for(size_t i = 0; i < args.size(); i++)
{
if(args[i] == "-i") invcf = args[++i];
if(args[i] == "-o") outvcf = args[++i];
if(args[i] == "-s") samples = args[++i];
if(args[i] == "-r") region = args[++i];
}
// ========= core calculation part ===========================================
BcfReader vcf(invcf, region, samples);
BcfWriter bw(outvcf);
bw.copyHeader(invcf, samples);
bw.header.addINFO("INFO", "1", "Float", "INFO score given genotype probability");
bw.header.addINFO("EAF", "1", "Float", "Estimated Allele Frequency");
int N = vcf.nsamples, i{0};
vector<float> gps;
float info, eaf, eij, fij, a0, a1, thetaHat;
BcfRecord var(bw.header);
while(vcf.getNextVariant(var))
{
var.getFORMAT("GP", gps); // try get GP values
for(eij = 0.0, fij = 0.0, i = 0; i < N; i++)
{
a0 = gps[i * 3 + 1] + gps[i * 3 + 2] * 2;
a1 = gps[i * 3 + 1] + gps[i * 3 + 2] * 4;
eij += a0;
fij += a1 - a0 * a0;
}
eaf = eij / 2 / N;
info = 1.0 - fij / (eij * (1 - eaf));
thetaHat = std::round(1e2 * eaf) / 1e2;
if(thetaHat == 0 || thetaHat == 1)
info = 1.0;
else if(info < 0)
info = 0.0;
else
info = std::round(1e3 * info) / 1e3;
eaf = std::round(1e6 * eaf) / 1e6;
var.setINFO("INFO", info);
var.setINFO("EAF", eaf);
bw.writeRecord(var);
}
return 0;
}