-
Notifications
You must be signed in to change notification settings - Fork 3
/
vcf_flipGT.cpp
59 lines (56 loc) · 2.13 KB
/
vcf_flipGT.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
// -*- compile-command: "g++ flipGT.cpp -std=c++11 -g -O3 -Wall -lhts -lz -lm -lbz2 -llzma -lcurl" -*-
#include "vcfpp.h"
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"
<< " swap REF and ALT and flip corresponding genotypes for only biallelics \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);
BcfRecord var(vcf.header);
BcfWriter bw(outvcf, vcf.header);
std::vector<int> gts;
while (vcf.getNextVariant(var))
{
var.getGenotypes(gts);
var.swap_REF_ALT();
for (auto& g : gts)
{
if(g == -9) continue;
g = !g;
}
var.setGenotypes(gts);
bw.writeRecord(var);
}
return 0;
}