-
Notifications
You must be signed in to change notification settings - Fork 38
/
profile_na12878.cpp
646 lines (554 loc) · 21.8 KB
/
profile_na12878.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* The MIT License
Copyright (c) 2014 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 "profile_na12878.h"
namespace
{
class BroadKBStats
{
public:
uint32_t tp, fp, tn, fn;
BroadKBStats()
{
tp = 0;
fp = 0;
tn = 0;
fn = 0;
};
};
class OverlapStats
{
public:
uint32_t a,ab,b,a_ins,ab_ins,b_ins,a_del,ab_del,b_del;
OverlapStats()
{
a = 0;
ab = 0;
b = 0;
a_ins = 0;
a_del = 0;
ab_ins = 0;
ab_del = 0;
b_ins = 0;
b_del = 0;
};
};
class ConcordanceStats
{
public:
int32_t geno[4][4];
ConcordanceStats()
{
for (int32_t i=0; i<4; ++i)
{
for (int32_t j=0; j<4; ++j)
{
geno[i][j] = 0;
}
}
};
};
class Igor : Program
{
public:
std::string version;
///////////
//options//
///////////
std::string input_vcf_file;
std::vector<std::string> input_vcf_files;
std::string ref_fasta_file;
std::string ref_data_sets_list;
std::vector<GenomeInterval> intervals;
std::string interval_list;
std::vector<std::string> dataset_labels;
std::vector<std::string> dataset_types;
BroadKBStats kbstats;
std::vector<OverlapStats> stats;
std::vector<ConcordanceStats> concordance;
std::string gencode_gtf_file;
bool gencode_exists;
///////
//i/o//
///////
BCFSyncedReader *sr;
bcf1_t *v;
kstring_t line;
//////////
//filter//
//////////
std::string fexp;
Filter filter;
bool filter_exists;
/////////
//stats//
/////////
uint32_t no_variants;
uint32_t no_positive_variants;
uint32_t no_negative_variants;
uint32_t nfs;
uint32_t fs;
uint32_t rare_nfs;
uint32_t rare_fs;
uint32_t common_nfs;
uint32_t common_fs;
////////////////
//common tools//
////////////////
VariantManip *vm;
GENCODE *gc;
Igor(int argc, char ** argv)
{
//////////////////////////
//options initialization//
//////////////////////////
try
{
std::string desc = "profile NA12878";
version = "0.5";
TCLAP::CmdLine cmd(desc, ' ', version);
VTOutput my; cmd.setOutput(&my);
TCLAP::ValueArg<std::string> arg_ref_fasta_file("r", "r", "reference sequence fasta file []", true, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_intervals("i", "i", "intervals []", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_interval_list("I", "I", "file containing list of intervals []", false, "", "file", cmd);
TCLAP::ValueArg<std::string> arg_fexp("f", "f", "filter expression []", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_ref_data_sets_list("g", "g", "file containing list of reference datasets []", false, "", "file", cmd);
TCLAP::UnlabeledValueArg<std::string> arg_input_vcf_file("<in.vcf>", "input VCF file", true, "","file", cmd);
cmd.parse(argc, argv);
ref_fasta_file = arg_ref_fasta_file.getValue();
fexp = arg_fexp.getValue();
parse_intervals(intervals, arg_interval_list.getValue(), arg_intervals.getValue());
ref_data_sets_list = arg_ref_data_sets_list.getValue();
input_vcf_file = arg_input_vcf_file.getValue();
///////////////////////
//parse input VCF files
///////////////////////
}
catch (TCLAP::ArgException &e)
{
std::cerr << "error: " << e.error() << " for arg " << e.argId() << "\n";
abort();
}
};
void initialize()
{
//////////////////////
//reference data set//
//////////////////////
//# This file contains information on how to process reference data sets.
//#
//# dataset - name of data set, this label will be printed.
//# type - True Positives (TP) and False Positives (FP)
//# overlap percentages labeled as (Precision, Sensitivity) and (False Discovery Rate, Type I Error) respectively
//# - annotation
//# file is used for GENCODE annotation of frame shift and non frame shift Indels
//# filter - filter applied to variants for this particular data set
//# path - path of indexed BCF file
//#dataset type filter path
//broad.kb TP PASS /net/fantasia/home/atks/dev/vt/bundle/public/grch37/broad.kb.241365variants.genotypes.bcf
//illumina.platinum TP PASS /net/fantasia/home/atks/dev/vt/bundle/public/grch37/NA12878.illumina.platinum.5284448variants.genotypes.bcf
//gencode.v19 annotation . /net/fantasia/home/atks/dev/vt/bundle/public/grch37/gencode.v19.annotation.gtf.gz
input_vcf_files.push_back(input_vcf_file);
dataset_labels.push_back("data");
dataset_types.push_back("ref");
filter.parse(fexp.c_str(), true);
htsFile *hts = hts_open(ref_data_sets_list.c_str(), "r");
kstring_t s = {0,0,0};
std::vector<std::string> vec;
while (hts_getline(hts, '\n', &s)>=0)
{
if (s.s[0] == '#')
continue;
std::string line(s.s);
split(vec, " ", line);
if (vec[1] == "TP" || vec[1] == "FP")
{
dataset_labels.push_back(vec[0]);
dataset_types.push_back(vec[1]);
input_vcf_files.push_back(vec[3]);
}
else if (vec[1] == "annotation")
{
gencode_gtf_file = vec[3];
}
else
{
std::cerr << "Reference data set type: \"" << vec[1] << "\" not recognised\n";
exit(1);
}
}
hts_close(hts);
if (s.m) free(s.s);
//////////////////////
//i/o initialization//
//////////////////////
sr = new BCFSyncedReader(input_vcf_files, intervals, false);
///////////////////////
//tool initialization//
///////////////////////
vm = new VariantManip(ref_fasta_file);
gencode_exists = false;
if (gencode_gtf_file != "")
{
gencode_exists = true;
gc = new GENCODE(gencode_gtf_file, ref_fasta_file);
}
////////////////////////
//stats initialization//
////////////////////////
no_variants = 0;
no_positive_variants = 0;
no_negative_variants = 0;
fs = 0;
nfs = 0;
}
void profile_na12878()
{
//for combining the alleles
std::vector<bcfptr*> current_recs;
std::vector<Interval*> overlaps;
Variant variant;
int32_t no_overlap_files = input_vcf_files.size();
std::vector<int32_t> presence(no_overlap_files, 0);
std::vector<bcfptr*> presence_bcfptr(no_overlap_files, NULL);
stats.resize(no_overlap_files);
concordance.resize(no_overlap_files);
int32_t *gts = NULL;
int32_t n = 0;
int32_t x1, x2, x, k0;
int32_t na12878_index[no_overlap_files];
//get NA12878 gt position from reference file
for (int32_t i=0; i<no_overlap_files; ++i)
{
na12878_index[i] = bcf_hdr_id2int(sr->hdrs[i], BCF_DT_SAMPLE, "NA12878");
}
int32_t discordance_filter = 0;
while(sr->read_next_position(current_recs))
{
//check first variant
bcf1_t *v = current_recs[0]->v;
bcf_hdr_t *h = current_recs[0]->h;
int32_t vtype = vm->classify_variant(h, v, variant);
//if (filter.apply(h, v, &variant))
//if (bcf_has_filter(h,v,"PASS")!=1)
//if (bcf_get_n_allele(v)!=2 || !(vtype==VT_INDEL || vtype==(VT_SNP|VT_INDEL)) )
//if (bcf_get_n_allele(v)!=2 )
//if (bcf_get_n_allele(v)!=2 || !(vtype==VT_INDEL) || bcf_has_filter(h,v,"PASS")!=1 )
// if (bcf_get_n_allele(v)!=2 || !(vtype==VT_INDEL || vtype==(VT_SNP|VT_INDEL)) )
// if (bcf_get_n_allele(v)!=2 || !(vtype==VT_SNP) )
//if (bcf_get_n_allele(v)!=2 || !(vtype==VT_INDEL) || variant.alleles[0].dlen!=1)
//bool v1 = (bcf_get_n_allele(v)!=2 || variant.alleles[0].dlen==0);
//bool v1 = !(bcf_get_n_allele(v)==2 && variant.alleles[0].dlen==1);
bool v1 = false;
//bool v2 = false;
bool v2 = !filter.apply(h, v, &variant);
if (false && v1!=v2)
{
++discordance_filter;
std::cerr << "discordance in filter:" << "v1:" << v1 << " v2:" << v2 << "\n";
bcf_print(h,v);
variant.print();
}
if (v2)
{
continue;
}
std::string chrom = bcf_get_chrom(h,v);
int32_t start1 = bcf_get_pos1(v);
int32_t end1 = bcf_get_end_pos1(v);
//check existence
for (uint32_t i=0; i<current_recs.size(); ++i)
{
++presence[current_recs[i]->file_index];
presence_bcfptr[current_recs[i]->file_index] = current_recs[i];
}
//annotate
if (presence[0])
{
if (gencode_exists)
{
gc->search(chrom, start1+1, end1, overlaps);
bool cds_found = false;
bool is_fs = false;
for (int32_t i=0; i<overlaps.size(); ++i)
{
GENCODERecord *rec = (GENCODERecord *) overlaps[i];
if (rec->feature==GC_FT_CDS)
{
cds_found = true;
if (abs(variant.alleles[0].dlen)%3!=0)
{
is_fs = true;
break;
}
}
}
if (cds_found)
{
if (is_fs)
{
++fs;
}
else
{
++nfs;
}
}
}
}
int32_t ins = variant.alleles[0].ins;
int32_t del = 1-ins;
if (presence[0])
{
++stats[0].a;
stats[0].a_ins += ins;
stats[0].a_del += del;
bcf_unpack(v, BCF_UN_STR);
int k = bcf_get_genotypes(presence_bcfptr[0]->h, presence_bcfptr[0]->v, >s, &n);
x1 = bcf_gt_allele(gts[na12878_index[0]*2]);
x2 = bcf_gt_allele(gts[na12878_index[0]*2+1]);
x = x1+x2;
if (x<0)
{
x = 3;
}
}
//update overlap stats
for (uint32_t i=1; i<no_overlap_files; ++i)
{
//******************
//for BROAD KB stats
//******************
if (i==1)
{
int32_t y1 = -1;
int32_t y2 = -1;
int32_t y = -2;
int32_t xt = -2;
char* dst = NULL;
int32_t ndst = 0;
if (presence[1])
{
bcf_unpack(presence_bcfptr[i]->v, BCF_UN_IND);
k0 = bcf_get_genotypes(presence_bcfptr[1]->h, presence_bcfptr[1]->v, >s, &n);
bcf_get_info_string(presence_bcfptr[1]->h, presence_bcfptr[1]->v, "TruthStatus", &dst, &ndst);
y1 = bcf_gt_allele(gts[na12878_index[i]*2]);
y2 = bcf_gt_allele(gts[na12878_index[i]*2+1]);
y = y1+y2;
}
if (presence[0])
{
xt = x1 + x2;
++no_variants;
if (xt>0) ++no_positive_variants;
if (xt==0) ++no_negative_variants;
}
if (presence[0] && presence[1])
{
if (strcmp(dst, "TRUE_POSITIVE")==0)
{
if (xt>0 && y>0)
{
++kbstats.tp;
}
else if (xt>0 && y==0)
{
++kbstats.fp;
}
else if (xt==0 && y>0)
{
++kbstats.fn;
}
}
else if (strcmp(dst, "FALSE_POSITIVE")==0)
{
if (xt>0)
{
++kbstats.fp;
}
else if (xt==0)
{
++kbstats.tn;
}
}
}
else if (presence[0] && !presence[1])
{
//++kbstats.fp;
}
else if (!presence[0] && presence[1])
{
if (strcmp(dst, "TRUE_POSITIVE")==0)
{
if (y>0)
{
++kbstats.fn;
}
else if (y==0)
{
++kbstats.tn;
}
}
else if (strcmp(dst, "FALSE_POSITIVE")==0)
{
++kbstats.tn;
}
}
else
{
//++kbstats.tn;
}
if (ndst) free(dst);
}
if (presence[0] && !presence[i])
{
++stats[i].a;
stats[i].a_ins += ins;
stats[i].a_del += del;
}
else if (presence[0] && presence[i])
{
++stats[i].ab;
stats[i].ab_ins += ins;
stats[i].ab_del += del;
bcf_unpack(presence_bcfptr[i]->v, BCF_UN_IND);
int k = bcf_get_genotypes(presence_bcfptr[i]->h, presence_bcfptr[i]->v, >s, &n);
int32_t y1 = bcf_gt_allele(gts[na12878_index[i]*2]);
int32_t y2 = bcf_gt_allele(gts[na12878_index[i]*2+1]);
int32_t y = y1+y2;
if (y<0)
{
y = 3;
}
// bcf_print(presence_bcfptr[0]->h, presence_bcfptr[0]->v);
// bcf_print(presence_bcfptr[i]->h, presence_bcfptr[i]->v);
if (x2==bcf_int32_missing)
{
std::cerr << "x2 encodes int32_t missing\n";
}
//std::cerr << "k0 " << k0 << " " << "k " << k << " " << x << " " << "(" << x1 << "," << x2 << ") " << y << "\n";
++concordance[i].geno[x][y];
}
else if (!presence[0] && presence[i])
{
++stats[i].b;
stats[i].b_ins += ins;
stats[i].b_del += del;
}
else
{
//not in either, do nothing
}
presence[i]=0;
presence_bcfptr[i]=NULL;
}
presence[0] = 0;
}
std::cerr << "NO DISCORDANCE FILTERS " << discordance_filter << "\n";
};
void print_options()
{
std::clog << "profile_na12878 v" << version << "\n\n";
std::clog << "\n";
std::clog << "Options: input VCF File " << input_vcf_file << "\n";
std::clog << " [g] reference data sets list file " << ref_data_sets_list << "\n";
std::clog << " [r] reference FASTA file " << ref_fasta_file << "\n";
print_int_op(" [i] intervals ", intervals);
std::clog << "\n";
}
void print_stats()
{
fprintf(stderr, "\n");
fprintf(stderr, " %s\n", "data set");
fprintf(stderr, " No Variants : %10d [%.2f]\n", stats[0].a, (float)stats[0].a_ins/(stats[0].a_del));
fprintf(stderr, " FS/NFS : %10.2f (%d/%d)\n", (float)fs/(fs+nfs), fs, nfs);
fprintf(stderr, "\n");
fprintf(stderr, " Broad KB\n");
fprintf(stderr, " TP %10d \n", kbstats.tp);
fprintf(stderr, " FP %10d \n", kbstats.fp);
fprintf(stderr, " TN %10d \n", kbstats.tn);
fprintf(stderr, " FN %10d \n", kbstats.fn);
fprintf(stderr, " P %10d \n", kbstats.tp+kbstats.fp);
fprintf(stderr, " N %10d \n", kbstats.tn+kbstats.fn);
fprintf(stderr, " + %10d \n", no_positive_variants);
fprintf(stderr, " - %10d \n", no_negative_variants);
fprintf(stderr, " TDR %5.2f (TP/(TP+FP))\n", (float)kbstats.tp/(kbstats.tp+kbstats.fp)*100);
fprintf(stderr, " FNR %5.2f (FN/(TN+FN))\n", (float)kbstats.fn/(kbstats.fn+kbstats.tn)*100);
fprintf(stderr, "\n");
for (int32_t i=1; i<dataset_labels.size(); ++i)
{
fprintf(stderr, " %s\n", dataset_labels[i].c_str());
fprintf(stderr, " A-B %10d [%.2f]\n", stats[i].a, (float)stats[i].a_ins/(stats[i].a_del));
fprintf(stderr, " A&B %10d [%.2f]\n", stats[i].ab, (float)stats[i].ab_ins/stats[i].ab_del);
fprintf(stderr, " B-A %10d [%.2f]\n", stats[i].b, (float)stats[i].b_ins/(stats[i].b_del));
if (dataset_types[i]=="TP")
{
fprintf(stderr, " Precision %4.1f%%\n", 100*(float)stats[i].ab/(stats[i].a+stats[i].ab));
fprintf(stderr, " Sensitivity %4.1f%%\n", 100*(float)stats[i].ab/(stats[i].b+stats[i].ab));
}
else
{
fprintf(stderr, " FDR %4.1f%%\n", 100*(float)stats[i].ab/(stats[i].a+stats[i].ab));
fprintf(stderr, " Type I Error %4.1f%%\n", 100*(float)stats[i].ab/(stats[i].b+stats[i].ab));
}
fprintf(stderr, "\n");
}
for (int32_t i=1; i<dataset_labels.size(); ++i)
{
int32_t (&geno)[4][4] = concordance[i].geno;
int32_t total = 0;
int32_t concordance = 0;
for (int32_t i=0; i<3; ++i)
{
for (int32_t j=0; j<3; ++j)
{
total += geno[i][j];
if (i==j)
{
concordance += geno[i][j];
}
}
}
int32_t discordance = total-concordance;
fprintf(stderr, " %s\n", dataset_labels[i].c_str());
fprintf(stderr, " R/R R/A A/A ./.\n");
fprintf(stderr, " R/R %8d %8d %8d %8d\n", geno[0][0], geno[0][1], geno[0][2], geno[0][3]);
fprintf(stderr, " R/A %8d %8d %8d %8d\n", geno[1][0], geno[1][1], geno[1][2], geno[1][3]);
fprintf(stderr, " A/A %8d %8d %8d %8d\n", geno[2][0], geno[2][1], geno[2][2], geno[2][3]);
fprintf(stderr, " ./. %8d %8d %8d %8d\n", geno[3][0], geno[3][1], geno[3][2], geno[3][3]);
fprintf(stderr, "\n");
fprintf(stderr, " Total genotype pairs : %8d\n", total);
fprintf(stderr, " Concordance : %5.2f%% (%d)\n", (float)concordance/total*100, concordance);
fprintf(stderr, " Discordance : %5.2f%% (%d)\n", (float)discordance/total*100, discordance);
fprintf(stderr, "\n");
}
};
~Igor()
{
};
private:
};
}
void profile_na12878(int argc, char ** argv)
{
Igor igor(argc, argv);
igor.print_options();
igor.initialize();
igor.profile_na12878();
igor.print_stats();
}