-
Notifications
You must be signed in to change notification settings - Fork 37
/
compute_concordance.cpp
420 lines (339 loc) · 12.9 KB
/
compute_concordance.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* The MIT License
Copyright (c) 2013 Adrian Tan <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "compute_concordance.h"
namespace
{
#define RR 0;
#define RA 1;
#define AA 2;
#define NA 3;
class VariantConcordanceStats
{
public:
int32_t concordance[4][4];
VariantConcordanceStats()
{
for (uint32_t i=0; i<4; ++i)
{
for (uint32_t j=0; j<4; ++j)
{
concordance[i][j] = 0;
}
}
};
void update_stats(bcf_hdr_t *h1, bcf1_t *v1, bcf_hdr_t *h2, bcf1_t *v2)
{
};
};
class ConcordanceStats
{
public:
int32_t concordance[4][4];
ConcordanceStats()
{
for (uint32_t i=0; i<4; ++i)
{
for (uint32_t j=0; j<4; ++j)
{
concordance[i][j] = 0;
}
}
};
void reset()
{
for (uint32_t i=0; i<4; ++i)
{
for (uint32_t j=0; j<4; ++j)
{
concordance[i][j] = 0;
}
}
}
};
class Igor : Program
{
public:
std::string version;
///////////
//options//
///////////
std::vector<std::string> input_vcf_files;
std::string filters;
std::vector<GenomeInterval> intervals;
std::string interval_list;
std::string variant_concordance_txt_file;
std::string sample_concordance_txt_file;
std::vector<ConcordanceStats> stats;
int32_t variant_type;
///////
//i/o//
///////
htsFile *variant_concordance_txt;
htsFile *sample_concordance_txt;
BCFSyncedReader *sr;
bcf1_t *v;
Filter *filter;
Filter *rare_filter;
/////////
//stats//
/////////
uint32_t no_candidate_snps;
uint32_t no_candidate_indels;
void intersect_samples(bcf_hdr_t* h1, bcf_hdr_t *h2, std::vector<std::string>& s, std::vector<int32_t>& a, std::vector<int32_t>& b)
{
vdict_t *d2 = (vdict_t*)h2->dict[BCF_DT_SAMPLE];
int32_t nsamples = bcf_hdr_get_n_sample(sr->hdrs[0]);
char** samples = bcf_hdr_get_samples(sr->hdrs[0]);
std::map<std::string, int32_t> m;
for (int32_t i=0; i<nsamples; ++i)
{
khint_t k = kh_get(vdict, d2, samples[i]);
if ( k != kh_end(d2) )
{
s.push_back(samples[i]);
a.push_back(i);
b.push_back(kh_val(d2, k).id);
}
}
}
Igor(int argc, char ** argv)
{
//////////////////////////
//options initialization//
//////////////////////////
try
{
std::string desc =
"Compute Concordance.\n\
Each VCF file is required to have the FORMAT flags E and N and should have exactly one sample.\n\
e.g. vt profile_snps_variants -o - NA19130.vcf.gz HG00096.vcf.gz\n\n";
version = "0.5";
TCLAP::CmdLine cmd(desc, ' ', version);
TCLAP::ValueArg<std::string> arg_intervals("i", "intervals", "Intervals", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_interval_list("I", "interval-list", "File containing list of intervals", false, "", "file", cmd);
TCLAP::ValueArg<std::string> arg_variant_type("v", "variant", "Variant Type ", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_variant_concordance_txt_file("m", "variant-concordance-txt-file", "Variant concordance text file", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_sample_concordance_txt_file("s", "sample-concordance-txt-file", "Sample concordance text file", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_filters("f", "filters", "Filter (e.g. AF>0.3) ", false, "", "str", cmd);
TCLAP::UnlabeledMultiArg<std::string> arg_input_vcf_files("input-vcf-file", "Input VCF File", true, "str", cmd);
cmd.parse(argc, argv);
if (arg_input_vcf_files.getValue().size()!=2)
{
std::cerr << "Requires 2 VCF files as input\n";
exit(1);
}
input_vcf_files = arg_input_vcf_files.getValue();
parse_intervals(intervals, arg_interval_list.getValue(), arg_intervals.getValue());
filters = arg_filters.getValue();
variant_concordance_txt_file = arg_variant_concordance_txt_file.getValue();
sample_concordance_txt_file = arg_sample_concordance_txt_file.getValue();
// filter=NULL;
// if (filters!="")
// {
// filter = new Filter();
// filter->parse(filters);
// }
if (arg_variant_type.getValue()=="snp")
{
variant_type = VCF_SNP;
}
else if (arg_variant_type.getValue()=="indel")
{
variant_type = VCF_INDEL;
}
}
catch (TCLAP::ArgException &e)
{
std::cerr << "error: " << e.error() << " for arg " << e.argId() << "\n";
abort();
}
};
void initialize()
{
//////////////////////
//i/o initialization//
//////////////////////
//input vcfs
sr = new BCFSyncedReader(input_vcf_files, intervals);
variant_concordance_txt = hts_open(variant_concordance_txt_file.c_str(), "w");
sample_concordance_txt = hts_open(sample_concordance_txt_file.c_str(), "w");
////////////////////////
//stats initialization//
////////////////////////
no_candidate_snps = 0;
no_candidate_indels = 0;
}
void compute_concordance()
{
///////////////////////
//find common samples//
///////////////////////
char** samples1 = bcf_hdr_get_samples(sr->hdrs[0]);
char** samples2 = bcf_hdr_get_samples(sr->hdrs[1]);
std::vector<std::string> s;
std::vector<int32_t> a;
std::vector<int32_t> b;
intersect_samples(sr->hdrs[0], sr->hdrs[1], s, a, b);
stats.resize(s.size());
kstring_t *line = &variant_concordance_txt->line;
ConcordanceStats variant_stat;
kputs("variant\tRR_RR\tRR_RA\tRR_AA\tRR_NA\tRA_RR\tRA_RA\tRA_AA\tRA_NA\tAA_RR\tAA_RA\tAA_AA\tAA_NA\tNA_RR\tNA_RA\tNA_AA\tNA_NA\n", line);
//hts_write(variant_concordance_txt);
//for combining the alleles
std::vector<bcfptr*> current_recs;
std::map<std::string, bcfptr*> variants;
std::stringstream ss;
while(sr->read_next_position(current_recs))
{
if (current_recs.size()>=2)
{
variants.clear();
bool printed = false;
for (uint32_t i=0; i<current_recs.size(); ++i)
{
ss.str("");
int32_t d = current_recs[i]->file_index;
bcf1_t *v = current_recs[i]->v;
//bcf_set_variant_types(v);
if (bcf_get_var_type(v)!=variant_type || bcf_get_n_allele(v)!=2)
{
continue;
}
ss << bcf_get_alt(v, 0) << ":" << bcf_get_alt(v, 1);
if (d==0)
{
printed = true;
variants[ss.str()] = current_recs[i];
}
else //d==1
{
printed = true;
if (variants.find(ss.str())!=variants.end())
{
variant_stat.reset();
bcf1_t *v1 = variants[ss.str()]->v;
bcf_hdr_t *h1 = sr->hdrs[0];
bcf_fmt_t* f1 = bcf_get_fmt(h1, v1, "GT");
bcf1_t *v2 = v;
bcf_hdr_t *h2 = sr->hdrs[1];
bcf_fmt_t* f2 = bcf_get_fmt(h2, v2, "GT");
for (uint32_t i=0; i<s.size(); ++i)
{
int8_t *x1 = (int8_t*)(f1->p + a[i] * f1->size);
int8_t *x2 = (int8_t*)(f2->p + b[i] * f2->size);
int32_t g1 = 0;
int32_t g2 = 0;
int32_t l = 0;
for (l = 0; l < f1->n && x1[l]!=INT8_MIN; ++l)
{
if (x1[l] == 0)
{
g1 = 3;
break;
}
else if (x1[l]>>1)
{
g1 += (x1[l]>>1) - 1;
}
}
for (l = 0; l < f2->n && x1[l]!=INT8_MIN; ++l)
{
if (x2[l] == 0)
{
g2 = 3;
break;
}
else if (x2[l]>>1)
{
g2 += (x2[l]>>1) - 1;
}
}
++stats[i].concordance[g1][g2];
++variant_stat.concordance[g1][g2];
}
line->l = 0;
kputs(sr->get_current_sequence().c_str(), line);
kputc(':', line);
kputw(sr->get_current_pos1(), line);
kputc(':', line);
kputs(ss.str().c_str(), line);
kputc('\t', line);
for (uint32_t j=0; j<4; ++j)
{
for (uint32_t k=0; k<4; ++k)
{
kputw(variant_stat.concordance[j][k], line);
kputs("\t", line);
}
}
kputs("\n", line);
//hts_write(variant_concordance_txt);
}
}
}
}
}
/////////////////////////////////////////////
//output concordance matrix for individuals//
/////////////////////////////////////////////
line = &sample_concordance_txt->line;
kputs("sample\tRR_RR\tRR_RA\tRR_AA\tRR_NA\tRA_RR\tRA_RA\tRA_AA\tRA_NA\tAA_RR\tAA_RA\tAA_AA\tAA_NA\tNA_RR\tNA_RA\tNA_AA\tNA_NA\n", line);
//hts_write(sample_concordance_txt);
for (uint32_t i=0; i<s.size(); ++i)
{
line->l = 0;
kputs(s[i].c_str(), line);
kputc('\t', line);
for (uint32_t j=0; j<4; ++j)
{
for (uint32_t k=0; k<4; ++k)
{
kputw(stats[i].concordance[j][k], line);
kputs("\t", line);
}
}
kputs("\n", line);
//hts_write(sample_concordance_txt);
}
};
void print_options()
{
std::clog << "compute_concordance v" << version << "\n\n";
std::clog << "Options: Input VCF File " << input_vcf_files.size() << "\n";
std::clog << " [i] Intervals " << intervals.size() << " intervals\n";
}
void print_stats()
{
};
~Igor()
{
hts_close(variant_concordance_txt);
hts_close(sample_concordance_txt);
};
private:
};
}
void compute_concordance(int argc, char ** argv)
{
Igor igor(argc, argv);
igor.print_options();
igor.initialize();
igor.compute_concordance();
igor.print_stats();
}