-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
151 lines (117 loc) · 3.24 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <getopt.h>
#include <string>
#include <fstream>
#include <memory>
#include "BasicStatsCollector.h"
using namespace std;
using namespace VcfStatsAlive;
static struct option getopt_options[] =
{
/* These options set a flag. */
{"update-rate", required_argument, 0, 'u'},
{"first-update", required_argument, 0, 'f'},
{"qual-lower-val", optional_argument, 0, 'q'},
{"qual-upper-val", optional_argument, 0, 'Q'},
{"log-scale-af", optional_argument, 0, 'l'},
{"batch", optional_argument, 0, 'b'},
{0, 0, 0, 0}
};
static unsigned int updateRate;
static unsigned int firstUpdateRate;
static int qualHistLowerVal;
static int qualHistUpperVal;
void printStatsJansson(AbstractStatCollector* rootStatCollector);
int main(int argc, char* argv[]) {
string filename;
updateRate = 1000;
firstUpdateRate = 0;
qualHistLowerVal = 1;
qualHistUpperVal = 200;
bool logScaleAF = false;
bool batch = false;
int option_index = 0;
int ch;
while((ch = getopt_long (argc, argv, "f:u:q:Q:l", getopt_options, &option_index)) != -1) {
switch(ch) {
case 0:
break;
case 'u':
updateRate = strtol(optarg, NULL, 10);
break;
case 'f':
firstUpdateRate = strtol(optarg, NULL, 10);
break;
case 'q':
qualHistLowerVal = strtol(optarg, NULL, 10);
if(qualHistLowerVal < 0) {
cerr<<"Invalid quality histogram lowerbound value "<<qualHistLowerVal<<endl;
exit(1);
}
break;
case 'Q':
qualHistUpperVal = strtol(optarg, NULL, 10);
if(qualHistUpperVal < 0) {
cerr<<"Invalid quality histogram upperbound value "<<qualHistUpperVal<<endl;
exit(1);
}
break;
case 'l':
logScaleAF = true;
break;
case 'b':
batch = true;
break;
default:
break;
}
}
if(qualHistUpperVal < qualHistLowerVal) {
cerr<<"Quality histogram upperbound is lower than lowerbound"<<endl;
exit(1);
}
argc -= optind;
argv += optind;
htsFile *fp;
if (argc == 0) {
fp = hts_open("-", "r");
}
else {
filename = *argv;
fp = hts_open(filename.c_str(), "r");
}
if (fp == NULL) {
std::cerr<<"Unable to open vcf file / stream"<<std::endl;
exit(1);
}
BasicStatsCollector *bsc = new BasicStatsCollector(qualHistLowerVal, qualHistUpperVal, logScaleAF);
unsigned long totalVariants = 0;
bcf_hdr_t* hdr = bcf_hdr_read(fp);
bcf1_t* line = bcf_init();
while(bcf_read(fp, hdr, line) == 0) {
// Unpack alternates and info block
if (bcf_unpack(line, BCF_UN_STR) != 0) {
std::cerr<<"Error unpacking"<<std::endl;
}
bsc->processVariant(hdr, line);
totalVariants++;
if( !batch && ((totalVariants > 0 && totalVariants % updateRate == 0) ||
(firstUpdateRate > 0 && totalVariants >= firstUpdateRate))) {
printStatsJansson(bsc);
// disable first update after it has been fired.
if(firstUpdateRate > 0) firstUpdateRate = 0;
}
}
printStatsJansson(bsc);
delete bsc;
return 0;
}
void printStatsJansson(AbstractStatCollector* rootStatCollector) {
// Create the root object that contains everything
json_t * j_root = json_object();
// Let the root object of the collector tree create Json
rootStatCollector->appendJson(j_root);
// Dump the json
cout<<json_dumps(j_root, JSON_COMPACT | JSON_ENSURE_ASCII | JSON_PRESERVE_ORDER)<<";"<<endl;
json_decref(j_root);
}