-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvcf_vs_vcf.cpp
291 lines (192 loc) · 5.42 KB
/
vcf_vs_vcf.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) 2018, Nicola Prezza. All rights reserved.
// Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
#include <iostream>
#include <fstream>
#include <assert.h>
#include <vector>
#include <unistd.h>
#include <sstream>
#include <set>
#include <cstring>
#include "include.hpp"
#include <algorithm>
using namespace std;
int indel_window_def = 10;//if 2 indels are within this number of bases, keep only one of the two.
int indel_window = 0;
bool non_isolated = true;
void help(){
cout << "vcf_vs_vcf [OPTIONS]" << endl << endl <<
"Compute sensitivity and specificity of the calls specified with -1 against those specified with -2." << endl <<
"Options:" << endl <<
"-h Print this help." << endl <<
"-1 <arg> Input VCF file to be validated. REQUIRED" << endl <<
"-2 <arg> Ground truth VCF. REQUIRED" << endl <<
"-d <arg> Indel window tolerance. Consider a true match if 2 indels are within <arg> bases (default = " << indel_window_def << ")" << endl;
exit(0);
}
struct vcf_entry{
string chr;
uint64_t pos;
string REF;
string ALT;
bool indel;
bool flag;
bool operator<(const vcf_entry & a) const{
if(chr.compare(a.chr) < 0 ) return true;
else if(chr.compare(a.chr) == 0 ){
if(pos == a.pos){
string snp1 = REF;
snp1.append(ALT);
string snp2 = a.REF;
snp2.append(a.ALT);
return snp1.compare(snp2) < 0;
}
return pos < a.pos;
}
return false;
}
bool operator==(const vcf_entry & a) const{
//return chr.compare(a.chr)==0 and pos == a.pos and REF.compare(a.REF) == 0 and ALT.compare(a.ALT) == 0;
return chr.compare(a.chr)==0 and pos == a.pos;
}
};
vector<vcf_entry> read_vcf(string path){
vector<vcf_entry> vcf;
ifstream is(path);
string line;
while(getline(is, line)){
if(line[0] != '#'){
std::istringstream iss(line);
string chr;
string pos_s;
string id;
string ref;
string alt;
getline(iss, chr, '\t');
getline(iss, pos_s, '\t');
getline(iss, id, '\t');
getline(iss, ref, '\t');
getline(iss, alt, '\t');
uint64_t pos = atoi(pos_s.c_str());
string ALT;
std::istringstream iss2(alt);
while(getline(iss2,ALT,',')){
vcf.push_back(
{
chr,
pos,
ref,
ALT,
(ref.length()>1 or ALT.length()>1),
false
}
);
}
}
}
is.close();
return vcf;
}
int main(int argc, char** argv){
if(argc < 2) help();
string vcf1_path;
string vcf2_path;
int opt;
while ((opt = getopt(argc, argv, "d:1:2:h")) != -1){
switch (opt){
case 'h':
help();
break;
case 'd':
indel_window = atoi(optarg);
break;
case '1':
vcf1_path = string(optarg);
break;
case '2':
vcf2_path = string(optarg);
break;
default:
help();
return -1;
}
}
indel_window = indel_window==0 ? indel_window_def : indel_window;
if(vcf1_path.compare("")==0) help();
if(vcf2_path.compare("")==0) help();
cout << "Reading VCF 1 ... " << flush;
auto vcf1 = read_vcf(vcf1_path);
cout << "done." << endl;
cout << "Reading VCF 2 ... " << flush;
auto vcf2 = read_vcf(vcf2_path);
cout << "done." << endl;
cout << "Sorting VCF 1 ... " << flush;
sort(vcf1.begin(),vcf1.end());
cout << "done." << endl;
cout << "Sorting VCF 2 ... " << flush;
sort(vcf2.begin(),vcf2.end());
cout << "done." << endl;
/*for(auto v:vcf2){
cout << v.chr << "\t" << v.pos << "\t" << v.REF << "\t" << v.ALT << endl;
}*/
uint64_t TP_s = 0;
uint64_t FP_s = 0;
uint64_t FN_s = 0;
uint64_t TP_i = 0;
uint64_t FP_i = 0;
uint64_t FN_i = 0;
for(auto & v:vcf1){
if(v.indel){
vcf_entry cp = v;
cp.pos = cp.pos >= indel_window ? cp.pos - indel_window : 0;
auto it = std::lower_bound(vcf2.begin(), vcf2.end(), cp);
//find matching indels
while(it < vcf2.end() && it->chr.compare(v.chr)==0 && it->pos <= v.pos + indel_window ){
if(it->indel){
v.flag = true;
it->flag = true;
}
it++;
}
}else{
auto it = std::find(vcf2.begin(), vcf2.end(), v);
if(it != vcf2.end()){//found
//matched
v.flag = true;
it->flag = true;
}//else: nothing
}
}
for(auto v:vcf1){
if(v.indel){
TP_i += v.flag;
FP_i += (not v.flag);
}else{
TP_s += v.flag;
FP_s += (not v.flag);
}
}
for(auto v:vcf2){
if(v.indel){
FN_i += (not v.flag);
}else{
FN_s += (not v.flag);
}
}
cout << "TP (SNP) = " << TP_s << endl;
cout << "FP (SNP) = " << FP_s << endl;
cout << "FN (SNP) = " << FN_s << endl << endl;
cout << "TP (INDEL) = " << TP_i << endl;
cout << "FP (INDEL) = " << FP_i << endl;
cout << "FN (INDEL) = " << FN_i << endl << endl;
cout << "TP (TOT) = " << TP_i+TP_s << endl;
cout << "FP (TOT) = " << FP_i+FP_s << endl;
cout << "FN (TOT) = " << FN_i+FN_s << endl << endl;
cout << "sensitivity SNP = TP/(TP+FN) = " << 100*double(TP_s)/(double(TP_s)+double(FN_s)) << "%" << endl;
cout << "precision SNP = TP/(TP+FP) = " << 100*double(TP_s)/(double(TP_s)+double(FP_s)) << "%" << endl << endl;
cout << "sensitivity INDEL = TP/(TP+FN) = " << 100*double(TP_i)/(double(TP_i)+double(FN_i)) << "%" << endl;
cout << "precision INDEL = TP/(TP+FP) = " << 100*double(TP_i)/(double(TP_i)+double(FP_i)) << "%" << endl << endl;
cout << "sensitivity TOT = TP/(TP+FN) = " << 100*double(TP_s+TP_i)/(double(TP_s+TP_i)+double(FN_s+FN_i)) << "%" << endl;
cout << "precision TOT = TP/(TP+FP) = " << 100*double(TP_s+TP_i)/(double(TP_s+TP_i)+double(FP_s+FP_i)) << "%" << endl << endl;
}