-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckphase.cpp
More file actions
1401 lines (1296 loc) · 64.6 KB
/
checkphase.cpp
File metadata and controls
1401 lines (1296 loc) · 64.6 KB
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018-2025 by Lars Wienbrandt,
* Institute of Clinical Molecular Biology, Kiel University
*
* This file is part of Checkphase.
*
* Checkphase is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Checkphase is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Checkphase. If not, see <https://www.gnu.org/licenses/>.
*/
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
//#include <omp.h>
#include <htslib/vcf.h>
#include <htslib/synced_bcf_reader.h>
#include "Args.h"
#include "utils.h"
using namespace std;
inline size_t getNumVariantsFromIndex(const string &vcffilename) {
size_t numvars = 0;
htsFile* file = hts_open(vcffilename.c_str(), "r");
if (!file) {
cerr << "ERROR: Could not read file " << vcffilename << endl;
exit(EXIT_FAILURE);
}
bcf_hdr_t *hdr = bcf_hdr_read(file);
if (!hdr) {
cerr << "ERROR: Could not read header from " << vcffilename << endl;
exit(EXIT_FAILURE);
}
tbx_t *tbx = NULL;
hts_idx_t *idx = NULL;
if (hts_get_format(file)->format == vcf) {
tbx = tbx_index_load(vcffilename.c_str());
if (!tbx) {
cerr << "ERROR: Could not read reference index file." << endl;
exit(EXIT_FAILURE);
}
}
else if (hts_get_format(file)->format == bcf)
{
idx = bcf_index_load(vcffilename.c_str());
if (!idx) {
cerr << "ERROR: Could not read reference index file." << endl;
exit(EXIT_FAILURE);
}
}
else
{
cerr << "ERROR: Could not detect the reference file type as VCF or BCF." << endl;
exit(EXIT_FAILURE);
}
int nseq;
// get the number and names of sequences stored in the target file
const char** seq = tbx ? tbx_seqnames(tbx, &nseq) : bcf_index_seqnames(idx, hdr, &nseq);
// we only pick the first sequence that fits the convention, i.e. a number or literals optionally preceding with "chr" (but no special chars such as "_", ".", ...) and nrecords > 0
for (int i = 0; i < nseq; i++) {
uint64_t nrecords, unmapped;
string seqstring(seq[i]);
// if (seqstring.substr(0,3).compare("chr") == 0) // seqname starts with "chr"
// seqstring = seqstring.substr(3);
if (seqstring.find_first_not_of("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") == string::npos) { // only number or literals
hts_idx_get_stat(tbx ? tbx->idx : idx, i, &nrecords, &unmapped);
// cerr << "seq: " << i << " seqname: " << seq[i] << " nrecords: " << nrecords << " unmapped: " << unmapped << endl;
if (nrecords) {
numvars = nrecords;
break;
}
}
}
free(seq); // allocated by HTSlib
hts_close(file);
bcf_hdr_destroy(hdr);
if (tbx)
tbx_destroy(tbx);
if (idx)
hts_idx_destroy(idx);
return numvars;
}
inline void updateStatus(const char* statfile, float r, float q) {
if (!statfile)
return;
ofstream stat(statfile, ofstream::trunc);
if (!stat.fail())
stat << "R: " << r << "\nQ: " << q << endl;
}
double calc_r2_hard(size_t n, size_t sumx, size_t sumx2, size_t sumy, size_t sumy2, size_t sumxy) {
int64_t r_num = n * sumxy - sumx * sumy;
int64_t r2_denom_ref = n * sumx2 - sumx * sumx;
int64_t r2_denom_q = n * sumy2 - sumy * sumy;
double r2 = (r_num / (double)r2_denom_ref) * (r_num / (double)r2_denom_q);
return r2;
}
double calc_r2_soft(size_t n, size_t sumx, size_t sumx2, double sumy, double sumy2, double sumxy) {
double r_num = n * sumxy - sumx * sumy;
double r2_denom_ref = n * sumx2 - sumx * sumx;
double r2_denom_q = n * sumy2 - sumy * sumy;
double r2 = (r_num / r2_denom_ref) * (r_num / r2_denom_q);
return r2;
}
int main(int argc, char *argv[]) {
Args args = Args::parseArgs(argc, argv);
const string& reffile = args.vcfRef;
const string& queryfile = args.vcfQuery;
const string& sharedfile = args.vcfShared;
const char* statfile = args.statfile.empty() ? NULL : args.statfile.c_str();
bool dump = args.dump;
if (statfile)
cout << "Statfile: " << statfile << endl;
if (dump)
cout << "--dump enabled. Will dump phase error positions to stderr." << endl;
cout << endl;
updateStatus(statfile, 0, 0);
size_t Mrefidx = getNumVariantsFromIndex(reffile);
size_t Mqidx = getNumVariantsFromIndex(queryfile);
size_t Msharedidx = sharedfile.empty() ? 0 : getNumVariantsFromIndex(sharedfile);
bool useshared = Msharedidx ? true : false;
bcf_srs_t *sr = bcf_sr_init();
bcf_sr_set_opt(sr, BCF_SR_PAIR_LOGIC, BCF_SR_PAIR_BOTH_REF);
bcf_sr_set_opt(sr, BCF_SR_REQUIRE_IDX);
if (!bcf_sr_add_reader(sr, reffile.c_str())) {
cerr << "ERROR: Could not open reference for reading: " << bcf_sr_strerror(sr->errnum) << endl;
exit(EXIT_FAILURE);
}
if (!bcf_sr_add_reader(sr, queryfile.c_str())) {
cerr << "ERROR: Could not open query for reading: " << bcf_sr_strerror(sr->errnum) << endl;
exit(EXIT_FAILURE);
}
if (useshared) {
if (!bcf_sr_add_reader(sr, sharedfile.c_str())) {
cerr << "ERROR: Could not open shared file for reading: " << bcf_sr_strerror(sr->errnum) << endl;
exit(EXIT_FAILURE);
}
}
bcf_hdr_t *ref_hdr = bcf_sr_get_header(sr, 0);
bcf_hdr_t *q_hdr = bcf_sr_get_header(sr, 1);
bcf_hdr_t *s_hdr = bcf_sr_get_header(sr, 2);
size_t Nref = bcf_hdr_nsamples(ref_hdr);
size_t Nquery = bcf_hdr_nsamples(q_hdr);
// Read sample IDs
vector<string> refIDs;
vector<string> qIDs;
refIDs.reserve(Nref);
qIDs.reserve(Nquery);
for (size_t i = 0; i < Nref; i++)
refIDs.push_back(ref_hdr->samples[i]);
for (size_t i = 0; i < Nquery; i++)
qIDs.push_back(q_hdr->samples[i]);
cout << "Input files:" << endl;
cout << " Reference: " << reffile << endl;
cout << " Query: " << queryfile << endl;
if (useshared)
cout << " Shared variants file: " << sharedfile << endl;
cout << " Reference variants (from index): Mref = " << Mrefidx << endl;
cout << " Reference samples: Nref = " << Nref << endl;
cout << " Query variants (from index): Mquery = " << Mqidx << endl;
cout << " Query samples: Nquery = " << Nquery << endl;
if (useshared)
cout << " Shared file variants (from index): Ms = " << Msharedidx << endl;
cout << endl;
// extract shared samples (shared samples have to be in the same order! all query samples have to be in the reference!)
vector<size_t> map2ref(Nquery); // index mapping for shared samples from query to reference
{
size_t q = 0;
for (size_t r = 0; r < Nref && q < Nquery; r++) {
if (qIDs[q].compare(refIDs[r]) == 0) { // equal IDS -> add index to map and continue
map2ref[q] = r;
q++;
}
}
if (q < Nquery) {
cerr << "ERROR: Not all queries could be find in the reference." << endl;
exit(EXIT_FAILURE);
}
}
cout << "Passed samples check." << endl;
cout << "Checking shared variants for phase and genotype errors..." << flush;
size_t Mref = 0; // reference variants
size_t Mq = 0; // query variants
size_t Ms = 0; // variants from shared file
size_t Mshared = 0; // shared variants
size_t Mmaf01 = 0; // shared variants with RefPanelMAF >= 0.1
size_t Mmaf001 = 0; // shared variants with RefPanelMAF >= 0.01
size_t Mmaf0001 = 0; // shared variants with RefPanelMAF >= 0.001
size_t Mmaf00001 = 0; // shared variants with RefPanelMAF >= 0.0001
size_t Mtyped = 0; // shared variants with TYPED tag
int mref_gt = 0; void *ref_gt = NULL; // will be allocated once in bcf_get_genotypes() and then reused for each marker (need void* because of htslib)
int mtgt_gt = 0; void *tgt_gt = NULL; // will be allocated once in bcf_get_genotypes() and then reused for each marker (need void* because of htslib)
int ndosbuf = 0; void *dosbuf = NULL;
size_t MrefError = 0;
size_t MqError = 0;
size_t MRefAltSwap = 0;
size_t MStrandFlip = 0;
size_t MRefAltSwapAndStrandFlip = 0;
size_t MAlleleDiff = 0;
// for each tested sample the number of missing and unphased sites
vector<size_t> MrefMissing (Nquery, 0);
vector<size_t> MqMissing (Nquery, 0);
vector<size_t> MrefUnphasedHet(Nquery, 0);
vector<size_t> MqUnphasedHet (Nquery, 0);
vector<bool> initialized(Nquery, false);
vector<bool> initialized_typed(Nquery, false);
vector<bool> switched(Nquery, false);
vector<bool> switched_typed(Nquery, false);
vector<size_t> switchErrors(Nquery, 0);
vector<size_t> switchErrors_typed(Nquery, 0);
vector<bool> matPatSwitches(Nquery, false);
vector<bool> matPatSwitches_typed(Nquery, false);
vector<size_t> gtErrors(Nquery, 0);
vector<size_t> gtErrors_maf01(Nquery, 0);
vector<size_t> gtErrors_maf001(Nquery, 0);
vector<size_t> gtErrors_maf0001(Nquery, 0);
vector<size_t> gtErrors_maf00001(Nquery, 0);
vector<double> gtErrorsSoft(Nquery, 0.0);
vector<double> gtErrorsSoft_maf01(Nquery, 0.0);
vector<double> gtErrorsSoft_maf001(Nquery, 0.0);
vector<double> gtErrorsSoft_maf0001(Nquery, 0.0);
vector<double> gtErrorsSoft_maf00001(Nquery, 0.0);
// correlation r2
vector<size_t> gtSumRef(Nquery, 0.0);
vector<size_t> gt2SumRef(Nquery, 0.0);
vector<size_t> gtSumQ(Nquery, 0.0);
vector<size_t> gt2SumQ(Nquery, 0.0);
vector<size_t> gtSumRefQ(Nquery, 0.0);
vector<vector<double>> gtDosSumQ; // bins for a couple of variants, then for each query
vector<vector<double>> gtDos2SumQ; // bins for a couple of variants, then for each query
vector<vector<double>> gtDosSumRefQ; // bins for a couple of variants, then for each query
vector<size_t> gtSumRef_maf01(Nquery, 0.0);
vector<size_t> gt2SumRef_maf01(Nquery, 0.0);
vector<size_t> gtSumQ_maf01(Nquery, 0.0);
vector<size_t> gt2SumQ_maf01(Nquery, 0.0);
vector<size_t> gtSumRefQ_maf01(Nquery, 0.0);
vector<vector<double>> gtDosSumQ_maf01;
vector<vector<double>> gtDos2SumQ_maf01;
vector<vector<double>> gtDosSumRefQ_maf01;
vector<size_t> gtSumRef_maf001(Nquery, 0.0);
vector<size_t> gt2SumRef_maf001(Nquery, 0.0);
vector<size_t> gtSumQ_maf001(Nquery, 0.0);
vector<size_t> gt2SumQ_maf001(Nquery, 0.0);
vector<size_t> gtSumRefQ_maf001(Nquery, 0.0);
vector<vector<double>> gtDosSumQ_maf001;
vector<vector<double>> gtDos2SumQ_maf001;
vector<vector<double>> gtDosSumRefQ_maf001;
vector<size_t> gtSumRef_maf0001(Nquery, 0.0);
vector<size_t> gt2SumRef_maf0001(Nquery, 0.0);
vector<size_t> gtSumQ_maf0001(Nquery, 0.0);
vector<size_t> gt2SumQ_maf0001(Nquery, 0.0);
vector<size_t> gtSumRefQ_maf0001(Nquery, 0.0);
vector<vector<double>> gtDosSumQ_maf0001;
vector<vector<double>> gtDos2SumQ_maf0001;
vector<vector<double>> gtDosSumRefQ_maf0001;
vector<size_t> gtSumRef_maf00001(Nquery, 0.0);
vector<size_t> gt2SumRef_maf00001(Nquery, 0.0);
vector<size_t> gtSumQ_maf00001(Nquery, 0.0);
vector<size_t> gt2SumQ_maf00001(Nquery, 0.0);
vector<size_t> gtSumRefQ_maf00001(Nquery, 0.0);
vector<vector<double>> gtDosSumQ_maf00001;
vector<vector<double>> gtDos2SumQ_maf00001;
vector<vector<double>> gtDosSumRefQ_maf00001;
vector<double> r2Sum; // sum of per variant r2, bins for each couple of queries
vector<double> r2Sum_maf01; // sum of per variant r2 for variants of MAF>=0.1, bins for each couple of queries
vector<double> r2Sum_maf001; // sum of per variant r2 for variants of MAF>=0.01, bins for each couple of queries
vector<double> r2Sum_maf0001; // sum of per variant r2 for variants of MAF>=0.001, bins for each couple of queries
vector<double> r2Sum_maf00001; // sum of per variant r2 for variants of MAF>=0.0001, bins for each couple of queries
vector<double> r2SoftSum; // sum of per variant r2, bins for each couple of queries
vector<double> r2SoftSum_maf01; // sum of per variant r2 for variants of MAF>=0.1, bins for each couple of queries
vector<double> r2SoftSum_maf001; // sum of per variant r2 for variants of MAF>=0.01, bins for each couple of queries
vector<double> r2SoftSum_maf0001; // sum of per variant r2 for variants of MAF>=0.001, bins for each couple of queries
vector<double> r2SoftSum_maf00001; // sum of per variant r2 for variants of MAF>=0.0001, bins for each couple of queries
// used only when --dump is set
vector<vector<size_t>> errPos(Nquery);
// for counting haploid samples
bool hapsset_ref = false;
bool hapsset_tgt = false;
size_t Nrefhap = 0;
size_t Nqhap = 0;
bool havedosages = false;
bool havetyped = false;
// for progress
size_t linesperpercentref = divideRounded(Mrefidx, (size_t)100);
size_t linesperpercentq = divideRounded(Mqidx, (size_t)100);
if (!linesperpercentref) linesperpercentref = 1;
if (!linesperpercentq) linesperpercentq = 1;
updateStatus(statfile, 0, 0);
// need to allocate memory for the return value for getting AF and TYPED info from htslib
int af_size = sizeof(float);
float *af_ptr = (float*) malloc(af_size);
int typed_size = sizeof(int);
int *typed_ptr = (int*) malloc(typed_size);
// we sum up dosages in bins to compensate for numeric instability
size_t currbin = 0;
while (bcf_sr_next_line(sr)) { // read data SNP-wise in positional sorted order from query, reference and optional shared file
bcf1_t *ref = bcf_sr_get_line(sr, 0); // read one line of reference, if available at current position (otherwise NULL)
bcf1_t *tgt = bcf_sr_get_line(sr, 1); // read one line of query, if available at current position (otherwise NULL)
bcf1_t *shd = NULL;
if (useshared)
shd = bcf_sr_get_line(sr, 2); // read one line of shared file, if available at current position (otherwise NULL)
if (ref)
Mref++;
if (tgt)
Mq++;
if (shd)
Ms++;
if ((ref && Mref % linesperpercentref == 0) || (tgt && Mq % linesperpercentq == 0)) {
if (ref && Mref % linesperpercentref == 0) {
size_t p = (size_t)(100*Mref/(float)Mrefidx);
if (p%5 == 0)
cout << p << "%.." << flush;
}
updateStatus(statfile, Mref/(float)Mrefidx, Mq/(float)Mqidx);
}
if (!ref || !tgt || (useshared && !shd)) { // query or reference only variant, or not in shared file
continue;
}
// shared variant
if (Mshared % 1024 == 0) {
currbin = Mshared/1024;
gtDosSumQ.push_back(vector<double>(Nquery, 0.0));
gtDos2SumQ.push_back(vector<double>(Nquery, 0.0));
gtDosSumRefQ.push_back(vector<double>(Nquery, 0.0));
gtDosSumQ_maf01.push_back(vector<double>(Nquery, 0.0));
gtDos2SumQ_maf01.push_back(vector<double>(Nquery, 0.0));
gtDosSumRefQ_maf01.push_back(vector<double>(Nquery, 0.0));
gtDosSumQ_maf001.push_back(vector<double>(Nquery, 0.0));
gtDos2SumQ_maf001.push_back(vector<double>(Nquery, 0.0));
gtDosSumRefQ_maf001.push_back(vector<double>(Nquery, 0.0));
gtDosSumQ_maf0001.push_back(vector<double>(Nquery, 0.0));
gtDos2SumQ_maf0001.push_back(vector<double>(Nquery, 0.0));
gtDosSumRefQ_maf0001.push_back(vector<double>(Nquery, 0.0));
gtDosSumQ_maf00001.push_back(vector<double>(Nquery, 0.0));
gtDos2SumQ_maf00001.push_back(vector<double>(Nquery, 0.0));
gtDosSumRefQ_maf00001.push_back(vector<double>(Nquery, 0.0));
r2Sum.push_back(0.0);
r2Sum_maf01.push_back(0.0);
r2Sum_maf001.push_back(0.0);
r2Sum_maf0001.push_back(0.0);
r2Sum_maf00001.push_back(0.0);
r2SoftSum.push_back(0.0);
r2SoftSum_maf01.push_back(0.0);
r2SoftSum_maf001.push_back(0.0);
r2SoftSum_maf0001.push_back(0.0);
r2SoftSum_maf00001.push_back(0.0);
}
Mshared++;
// exclude monomorphic or multi-allelic markers
if (ref->n_allele != 2 || tgt->n_allele != 2) {
if (ref->n_allele != 2)
MrefError++;
if (tgt->n_allele != 2)
MqError++;
continue;
}
// decode genotypes/haplotypes
size_t nref_gt = bcf_get_genotypes(ref_hdr, ref, &ref_gt, &mref_gt); // calls bcf_unpack() within
size_t ntgt_gt = bcf_get_genotypes(q_hdr, tgt, &tgt_gt, &mtgt_gt); // calls bcf_unpack() within
// imputation R2
float maf = -1.0; // default if no MAF is present
if (bcf_get_info_float(q_hdr, tgt, "RefPanelAF", (void*)&af_ptr, &af_size) >= 0 // successfully read RefPanelAF tag from query
|| (useshared && bcf_get_info_float(s_hdr, shd, "RefPanelAF", (void*)&af_ptr, &af_size) >= 0)) { // or successfully read RefPanelAF tag from shared file
if (*af_ptr <= 0.5)
maf = *af_ptr; // if there's more than one RefPanelAF entry, take the first.
else
maf = 1.0 - *af_ptr;
}
// genotyped or not?
bool typed = false;
if (bcf_get_info_flag(q_hdr, tgt, "TYPED", (void*)&typed_ptr, &typed_size) > 0) { // successfully read TYPED tag and thus, it was set
// if (!useshared || bcf_get_info_flag(s_hdr, shd, "TYPED", (void*)&typed_ptr, &typed_size) > 0) { // also successfully read TYPED tag from shared variants file if required
typed = true;
havetyped = true;
// }
}
// get dosages, if present
int nret = bcf_get_format_values(q_hdr,tgt,"ADS",(void**)&dosbuf,&ndosbuf,BCF_HT_REAL); // ADS type
if (nret <= 0) nret = bcf_get_format_values(q_hdr,tgt,"HDS",(void**)&dosbuf,&ndosbuf,BCF_HT_REAL); // HDS type
if (nret > 0) {
// check number of dosages
if ((size_t)nret != 2*Nquery && (size_t)nret != Nquery) {
cerr << "ERROR: called dosage number is not as expected! nret = " << nret << " (expected: " << Nquery << " or " << 2*Nquery << ")" << endl;
exit(EXIT_FAILURE);
}
havedosages = true;
}
// check alleles
bool refaltswap = false;
if (strcmp(tgt->d.allele[0], ref->d.allele[0]) == 0 && strcmp(tgt->d.allele[1], ref->d.allele[1]) == 0) { // all good
// nothing to do here
} else if (strcmp(tgt->d.allele[0], ref->d.allele[1]) == 0 && strcmp(tgt->d.allele[1], ref->d.allele[0]) == 0) { // switched alleles
refaltswap = true;
MRefAltSwap++;
} else if (reverseComplement(tgt->d.allele[0]).compare(ref->d.allele[0]) == 0 && reverseComplement(tgt->d.allele[1]).compare(ref->d.allele[1]) == 0) { // strand flip
MStrandFlip++;
} else if (reverseComplement(tgt->d.allele[0]).compare(ref->d.allele[1]) == 0 && reverseComplement(tgt->d.allele[1]).compare(ref->d.allele[0]) == 0) { // strand flip + switched alleles
refaltswap = true;
MRefAltSwapAndStrandFlip++;
} else { // different alleles -> next variant
MAlleleDiff++;
continue;
}
// check alleles also with shared vars file
if (useshared) {
if (strcmp(tgt->d.allele[0], shd->d.allele[0]) == 0 && strcmp(tgt->d.allele[1], shd->d.allele[1]) == 0) { // all good
} else if (strcmp(tgt->d.allele[0], shd->d.allele[1]) == 0 && strcmp(tgt->d.allele[1], shd->d.allele[0]) == 0) { // switched alleles
} else if (reverseComplement(tgt->d.allele[0]).compare(shd->d.allele[0]) == 0 && reverseComplement(tgt->d.allele[1]).compare(shd->d.allele[1]) == 0) { // strand flip
} else if (reverseComplement(tgt->d.allele[0]).compare(shd->d.allele[1]) == 0 && reverseComplement(tgt->d.allele[1]).compare(shd->d.allele[0]) == 0) { // strand flip + switched alleles
} else { // different alleles -> next variant
MAlleleDiff++;
continue;
}
}
// check number of called genotypes
if ((nref_gt != 2*Nref && nref_gt != Nref) || (ntgt_gt != 2*Nquery && ntgt_gt != Nquery)) {
cerr << "ERROR: called genotype number is not as expected! nref_gt = " << nref_gt << " (expected: " << Nref << " or " << 2*Nref << ") ntgt_gt = " << ntgt_gt << " (expected: " << Nquery << " or " << 2*Nquery << ")" << endl;
exit(EXIT_FAILURE);
}
// check if variant is haploid
bool haploid_ref = nref_gt == Nref;
bool haploid_tgt = ntgt_gt == Nquery;
if (haploid_ref) {
if (!hapsset_ref) {
hapsset_ref = true;
Nrefhap = Nref;
} // else could perhaps throw an error if numbers don't match??
}
if (haploid_tgt) {
if (!hapsset_tgt) {
hapsset_tgt = true;
Nqhap = Nquery;
} // else could perhaps throw an error if numbers don't match??
}
// count in MAF categories
if (maf >= 0.1)
Mmaf01++;
if (maf >= 0.01)
Mmaf001++;
if (maf >= 0.001)
Mmaf0001++;
if (maf >= 0.0001)
Mmaf00001++;
// count checked typed variants
if (typed)
Mtyped++;
// for variant-wise correlation (PCC)
size_t gtsumref = 0;
size_t gt2sumref = 0;
size_t gtsumq = 0;
size_t gt2sumq = 0;
size_t gtsumrefq = 0;
double gtdossumq = 0.0;
double gtdos2sumq = 0.0;
double gtdossumrefq = 0.0;
// check samples
for (size_t q = 0; q < Nquery; q++) {
// haploid variants will be encoded as homozygous diploid
int32_t refmat_i = haploid_ref ? ((int32_t*)ref_gt)[map2ref[q]] : ((int32_t*)ref_gt)[2*map2ref[q]];
int32_t refpat_i = haploid_ref ? refmat_i : ((int32_t*)ref_gt)[2*map2ref[q]+1];
int32_t qmat_i = haploid_tgt ? ((int32_t*)tgt_gt)[q] : ((int32_t*)tgt_gt)[2*q];
int32_t qpat_i = haploid_tgt ? qmat_i : ((int32_t*)tgt_gt)[2*q+1];
float qdosmat = 0.0, qdospat = 0.0;
if (havedosages) {
qdosmat = haploid_tgt ? ((float*)dosbuf)[q] : ((float*)dosbuf)[2*q];
qdospat = haploid_tgt ? qdosmat : ((float*)dosbuf)[2*q+1];
}
// check if missing
if (bcf_gt_is_missing(refmat_i) || bcf_gt_is_missing(refpat_i) || bcf_gt_is_missing(qmat_i) || bcf_gt_is_missing(qpat_i)) {
if (bcf_gt_is_missing(refmat_i) || bcf_gt_is_missing(refpat_i))
MrefMissing[q]++;
if (bcf_gt_is_missing(qmat_i) || bcf_gt_is_missing(qpat_i))
MqMissing[q]++;
continue;
}
// check if unphased or haploid -> only gt check
bool phased = true;
bool diploid = true;
if (refpat_i == bcf_int32_vector_end || qpat_i == bcf_int32_vector_end || !bcf_gt_is_phased(refpat_i) || !bcf_gt_is_phased(qpat_i)) {
phased = false;
if (refpat_i == bcf_int32_vector_end) {
refpat_i = refmat_i; // treat as homozygous diploid
if (!hapsset_ref)
Nrefhap++;
diploid = false;
}
if (qpat_i == bcf_int32_vector_end) {
qpat_i = qmat_i; // treat as homozygous diploid
qdospat = qdosmat;
if (!hapsset_tgt)
Nqhap++;
diploid = false;
}
}
// decode haplotypes
bool refmat = bcf_gt_allele(refmat_i);
bool refpat = bcf_gt_allele(refpat_i);
bool qmat = bcf_gt_allele(qmat_i);
bool qpat = bcf_gt_allele(qpat_i);
// check if dosage is a number, otherwise set dosage corresponding to allele
if (isnan(qdosmat)) qdosmat = qmat ? 1.0 : 0.0;
if (isnan(qdospat)) qdospat = qpat ? 1.0 : 0.0;
// apply ref/alt swap
if (refaltswap) {
qmat = !qmat;
qpat = !qpat;
qdosmat = 1.0 - qdosmat;
qdospat = 1.0 - qdospat;
}
// gt check
int refgt = (refmat ? 1 : 0) + (diploid ? (refpat ? 1 : 0) : 0);
int qgt = (qmat ? 1 : 0) + (diploid ? (qpat ? 1 : 0) : 0);
// for complete and sample-wise correlation
gtSumRef[q] += refgt;
gt2SumRef[q] += refgt*refgt;
gtSumQ[q] += qgt;
gt2SumQ[q] += qgt*qgt;
gtSumRefQ[q] += refgt * qgt;
if (maf >= 0.1) {
gtSumRef_maf01[q] += refgt;
gt2SumRef_maf01[q] += refgt*refgt;
gtSumQ_maf01[q] += qgt;
gt2SumQ_maf01[q] += qgt*qgt;
gtSumRefQ_maf01[q] += refgt * qgt;
}
if (maf >= 0.01) {
gtSumRef_maf001[q] += refgt;
gt2SumRef_maf001[q] += refgt*refgt;
gtSumQ_maf001[q] += qgt;
gt2SumQ_maf001[q] += qgt*qgt;
gtSumRefQ_maf001[q] += refgt * qgt;
}
if (maf >= 0.001) {
gtSumRef_maf0001[q] += refgt;
gt2SumRef_maf0001[q] += refgt*refgt;
gtSumQ_maf0001[q] += qgt;
gt2SumQ_maf0001[q] += qgt*qgt;
gtSumRefQ_maf0001[q] += refgt * qgt;
}
if (maf >= 0.0001) {
gtSumRef_maf00001[q] += refgt;
gt2SumRef_maf00001[q] += refgt*refgt;
gtSumQ_maf00001[q] += qgt;
gt2SumQ_maf00001[q] += qgt*qgt;
gtSumRefQ_maf00001[q] += refgt * qgt;
}
// for variant-wise correlation
gtsumref += refgt;
gt2sumref += refgt*refgt;
gtsumq += qgt;
gt2sumq += qgt*qgt;
gtsumrefq += refgt * qgt;
// from here haploids are treated as homozygous diploid
if (!diploid) {
refgt += (refpat ? 1 : 0);
qgt += (qpat ? 1 : 0);
}
if (refgt == 1 && !phased)
MrefUnphasedHet[q]++;
if (qgt == 1 && !phased)
MqUnphasedHet[q]++;
// soft check
if (havedosages) {
double err;
switch (refgt) {
case 0: // homozygous wild
//this is wrong! gtErrorsSoft[q] += diploid ? qdosmat + qdospat : qdosmat;
if (!diploid)
err = qdosmat;
else {
double gp = (1.0-qdosmat)*(1.0-qdospat); // gp0
err = 1.0 - gp;
}
break;
case 2: // homozygous variant
//this is wrong! gtErrorsSoft[q] += diploid ? 2.0 - qdosmat - qdospat : 1.0 - qdosmat;
if (!diploid)
err = 1.0 - qdosmat;
else {
double gp = qdosmat*qdospat; // gp2
err = 1.0 - gp;
}
break;
default: // heterozygous (not applicable for haploid)
//this is wrong!
//float err1 = 1.0 - qdosmat + qdospat; // if mat=1 and pat=0
//float err2 = 1.0 - qdospat + qdosmat; // if mat=0 and pat=1
//gtErrorsSoft[q] += min(err1, err2); // we count the minimum deviation as we compare only the genotype and do not consider a phase switch here
err = (1.0-qdosmat)*(1.0-qdospat) + qdosmat*qdospat; // 1-gp1 = 1-(1-gp0-gp2) = gp0+gp2
}
gtErrorsSoft[q] += err;
if (maf >= 0.1)
gtErrorsSoft_maf01[q] += err;
if (maf >= 0.01)
gtErrorsSoft_maf001[q] += err;
if (maf >= 0.001)
gtErrorsSoft_maf0001[q] += err;
if (maf >= 0.0001)
gtErrorsSoft_maf00001[q] += err;
// genotype dosage = ads0 + ads1
double gtdos = qdosmat + (diploid ? qdospat : 0);
gtDosSumQ[currbin][q] += gtdos;
gtDos2SumQ[currbin][q] += gtdos*gtdos;
gtDosSumRefQ[currbin][q] += (refgt * gtdos) / (diploid ? 1 : 2); // need to reduce homozygous diploid representation back to haploid in the case of a haploid sample
if (maf >= 0.1) {
gtDosSumQ_maf01[currbin][q] += gtdos;
gtDos2SumQ_maf01[currbin][q] += gtdos*gtdos;
gtDosSumRefQ_maf01[currbin][q] += (refgt * gtdos) / (diploid ? 1 : 2); // need to reduce homozygous diploid representation back to haploid in the case of a haploid sample
}
if (maf >= 0.01) {
gtDosSumQ_maf001[currbin][q] += gtdos;
gtDos2SumQ_maf001[currbin][q] += gtdos*gtdos;
gtDosSumRefQ_maf001[currbin][q] += (refgt * gtdos) / (diploid ? 1 : 2); // need to reduce homozygous diploid representation back to haploid in the case of a haploid sample
}
if (maf >= 0.001) {
gtDosSumQ_maf0001[currbin][q] += gtdos;
gtDos2SumQ_maf0001[currbin][q] += gtdos*gtdos;
gtDosSumRefQ_maf0001[currbin][q] += (refgt * gtdos) / (diploid ? 1 : 2); // need to reduce homozygous diploid representation back to haploid in the case of a haploid sample
}
if (maf >= 0.0001) {
gtDosSumQ_maf00001[currbin][q] += gtdos;
gtDos2SumQ_maf00001[currbin][q] += gtdos*gtdos;
gtDosSumRefQ_maf00001[currbin][q] += (refgt * gtdos) / (diploid ? 1 : 2); // need to reduce homozygous diploid representation back to haploid in the case of a haploid sample
}
gtdossumq += gtdos;
gtdos2sumq += gtdos*gtdos;
gtdossumrefq += (refgt * gtdos) / (diploid ? 1 : 2);
}
// hard check
if (refgt != qgt) { // genotype error -> continue with next sample
gtErrors[q]++;
if (maf >= 0.1)
gtErrors_maf01[q]++;
if (maf >= 0.01)
gtErrors_maf001[q]++;
if (maf >= 0.001)
gtErrors_maf0001[q]++;
if (maf >= 0.0001)
gtErrors_maf00001[q]++;
continue;
}
// phase check
if (refgt == 1 && phased) { // only proceed on heterozygous site and if site is phased (gt errors are already excluded above)
if (initialized[q]) { // this is not the first het for this sample -> wrong phase is a switch error!
//bool swerr = (refmat != qmat) xor switched[q];
bool swerr = switched[q] ? (refmat == qmat) : (refmat != qmat);
if (swerr) {
switched[q] = !switched[q];
switchErrors[q]++;
if (dump)
errPos[q].push_back(Mq-1); // error position is 0-based
}
} else { // first het site -> simply set the switched-flag according to the current phases in ref and query
initialized[q] = true;
if (refmat != qmat) {
switched[q] = true;
matPatSwitches[q] = true;
if (dump)
errPos[q].push_back(0); // indicates the difference at the first het, for debugging
}
}
if (typed) {
if (initialized_typed[q]) { // this is not the first het for this sample -> wrong phase is a switch error!
//bool swerr = (refmat != qmat) xor switched[q];
bool swerr = switched_typed[q] ? (refmat == qmat) : (refmat != qmat);
if (swerr) {
switched_typed[q] = !switched_typed[q];
switchErrors_typed[q]++;
}
} else { // first het site -> simply set the switched-flag according to the current phases in ref and query
initialized_typed[q] = true;
if (refmat != qmat) {
switched_typed[q] = true;
matPatSwitches_typed[q] = true;
}
}
}
}
} // end for every sample
// variant-wise correlation r2
double r2 = calc_r2_hard(Nquery, gtsumref, gt2sumref, gtsumq, gt2sumq, gtsumrefq);
if (!isnan(r2)) { // add only if it is a number, otherwise it's treated as zero
r2Sum[currbin] += r2;
if (maf >= 0.1)
r2Sum_maf01[currbin] += r2;
if (maf >= 0.01)
r2Sum_maf001[currbin] += r2;
if (maf >= 0.001)
r2Sum_maf0001[currbin] += r2;
if (maf >= 0.0001)
r2Sum_maf00001[currbin] += r2;
}
double r2soft = 0.0;
if (havedosages) {
r2soft = calc_r2_soft(Nquery, gtsumref, gt2sumref, gtdossumq, gtdos2sumq, gtdossumrefq);
if (!isnan(r2soft)) { // add only if it is a number, otherwise it's treated as zero
r2SoftSum[currbin] += r2soft;
if (maf >= 0.1)
r2SoftSum_maf01[currbin] += r2soft;
if (maf >= 0.01)
r2SoftSum_maf001[currbin] += r2soft;
if (maf >= 0.001)
r2SoftSum_maf0001[currbin] += r2soft;
if (maf >= 0.0001)
r2SoftSum_maf00001[currbin] += r2soft;
}
}
if (!hapsset_ref) {
if (Nrefhap)
hapsset_ref = true;
} // else could perhaps throw an error if numbers don't match??
if (!hapsset_tgt) {
if (Nqhap)
hapsset_tgt = true;
} // else could perhaps throw an error if numbers don't match??
} // end while read line
cout << " done." << endl;
updateStatus(statfile, 1, 1);
free(af_ptr);
free(typed_ptr);
size_t Mexclude = MrefError + MqError + MAlleleDiff;
size_t Mcheck = Mshared - Mexclude;
cout << "\nSummary:\n" << endl;
cout << " Haploid ref samples: " << Nrefhap << endl;
cout << " Haploid query samples: " << Nqhap << endl;
cout << endl;
cout << " Reference variants: " << Mref << endl;
cout << " Query variants: " << Mq << endl;
cout << " Shared variants: " << Mshared << endl;
cout << " Checked variants: " << Mcheck << endl;
cout << " Checked MAF>=0.1: " << Mmaf01 << endl;
cout << " Checked MAF>=0.01: " << Mmaf001 << endl;
cout << " Checked MAF>=0.001: " << Mmaf0001 << endl;
cout << " Checked MAF>=0.0001: " << Mmaf00001 << endl;
cout << " Checked typed variants: " << Mtyped << endl;
cout << endl;
cout << " Excluded shared: " << Mexclude << endl;
cout << " Not biallelic in ref: " << MrefError << endl;
cout << " Not biallelic in query: " << MqError << endl;
cout << " Alleles do not match: " << MAlleleDiff << endl;
cout << endl;
cout << " Ref/Alt swaps: " << MRefAltSwap << endl;
cout << " Strand flips: " << MStrandFlip << endl;
cout << " Ref/Alt + Strand flip: " << MRefAltSwapAndStrandFlip << endl;
cout << endl;
size_t totalRefMissing = 0;
for (size_t v : MrefMissing)
totalRefMissing += v;
size_t totalQMissing = 0;
for (size_t v : MqMissing)
totalQMissing += v;
size_t totalRefUnphasedHet = 0;
for (size_t v : MrefUnphasedHet)
totalRefUnphasedHet += v;
size_t totalQUnphasedHet = 0;
for (size_t v : MqUnphasedHet)
totalQUnphasedHet += v;
cout << " Missing or unphased sites:" << endl;
cout << " Total missing in ref: " << totalRefMissing << endl;
cout << " Total missing in query: " << totalQMissing << endl;
cout << " Total unphased het in ref: " << totalRefUnphasedHet << endl;
cout << " Total unphased het in query: " << totalQUnphasedHet << endl;
cout << endl;
cout << fixed; // omit scientific notation
cout << setprecision(8);
// for correlation (PCC)
size_t gtsumref = 0;
size_t gt2sumref = 0;
size_t gtsumq = 0;
size_t gt2sumq = 0;
size_t gtsumrefq = 0;
double gtdossumq = 0.0;
double gtdos2sumq = 0.0;
double gtdossumrefq = 0.0;
size_t gtsumref_maf01 = 0;
size_t gt2sumref_maf01 = 0;
size_t gtsumq_maf01 = 0;
size_t gt2sumq_maf01 = 0;
size_t gtsumrefq_maf01 = 0;
double gtdossumq_maf01 = 0.0;
double gtdos2sumq_maf01 = 0.0;
double gtdossumrefq_maf01 = 0.0;
size_t gtsumref_maf001 = 0;
size_t gt2sumref_maf001 = 0;
size_t gtsumq_maf001 = 0;
size_t gt2sumq_maf001 = 0;
size_t gtsumrefq_maf001 = 0;
double gtdossumq_maf001 = 0.0;
double gtdos2sumq_maf001 = 0.0;
double gtdossumrefq_maf001 = 0.0;
size_t gtsumref_maf0001 = 0;
size_t gt2sumref_maf0001 = 0;
size_t gtsumq_maf0001 = 0;
size_t gt2sumq_maf0001 = 0;
size_t gtsumrefq_maf0001 = 0;
double gtdossumq_maf0001 = 0.0;
double gtdos2sumq_maf0001 = 0.0;
double gtdossumrefq_maf0001 = 0.0;
size_t gtsumref_maf00001 = 0;
size_t gt2sumref_maf00001 = 0;
size_t gtsumq_maf00001 = 0;
size_t gt2sumq_maf00001 = 0;
size_t gtsumrefq_maf00001 = 0;
double gtdossumq_maf00001 = 0.0;
double gtdos2sumq_maf00001 = 0.0;
double gtdossumrefq_maf00001 = 0.0;
vector<double> gtdossumqv(Nquery, 0.0);
vector<double> gtdos2sumqv(Nquery, 0.0);
vector<double> gtdossumrefqv(Nquery, 0.0);
vector<double> gtdossumqv_maf01(Nquery, 0.0);
vector<double> gtdos2sumqv_maf01(Nquery, 0.0);
vector<double> gtdossumrefqv_maf01(Nquery, 0.0);
vector<double> gtdossumqv_maf001(Nquery, 0.0);
vector<double> gtdos2sumqv_maf001(Nquery, 0.0);
vector<double> gtdossumrefqv_maf001(Nquery, 0.0);
vector<double> gtdossumqv_maf0001(Nquery, 0.0);
vector<double> gtdos2sumqv_maf0001(Nquery, 0.0);
vector<double> gtdossumrefqv_maf0001(Nquery, 0.0);
vector<double> gtdossumqv_maf00001(Nquery, 0.0);
vector<double> gtdos2sumqv_maf00001(Nquery, 0.0);
vector<double> gtdossumrefqv_maf00001(Nquery, 0.0);
for (size_t q = 0; q < Nquery; q++) {
gtsumref += gtSumRef[q];
gt2sumref += gt2SumRef[q];
gtsumq += gtSumQ[q];
gt2sumq += gt2SumQ[q];
gtsumrefq += gtSumRefQ[q];
gtsumref_maf01 += gtSumRef_maf01[q];
gt2sumref_maf01 += gt2SumRef_maf01[q];
gtsumq_maf01 += gtSumQ_maf01[q];
gt2sumq_maf01 += gt2SumQ_maf01[q];
gtsumrefq_maf01 += gtSumRefQ_maf01[q];
gtsumref_maf001 += gtSumRef_maf001[q];
gt2sumref_maf001 += gt2SumRef_maf001[q];
gtsumq_maf001 += gtSumQ_maf001[q];
gt2sumq_maf001 += gt2SumQ_maf001[q];
gtsumrefq_maf001 += gtSumRefQ_maf001[q];
gtsumref_maf0001 += gtSumRef_maf0001[q];
gt2sumref_maf0001 += gt2SumRef_maf0001[q];
gtsumq_maf0001 += gtSumQ_maf0001[q];
gt2sumq_maf0001 += gt2SumQ_maf0001[q];
gtsumrefq_maf0001 += gtSumRefQ_maf0001[q];
gtsumref_maf00001 += gtSumRef_maf00001[q];
gt2sumref_maf00001 += gt2SumRef_maf00001[q];
gtsumq_maf00001 += gtSumQ_maf00001[q];
gt2sumq_maf00001 += gt2SumQ_maf00001[q];
gtsumrefq_maf00001 += gtSumRefQ_maf00001[q];
}
if (havedosages) {
for (size_t bin = 0; bin < gtDosSumQ.size(); bin++) {
for (size_t q = 0; q < Nquery; q++) {
gtdossumqv[q] += gtDosSumQ[bin][q];
gtdos2sumqv[q] += gtDos2SumQ[bin][q];
gtdossumrefqv[q] += gtDosSumRefQ[bin][q];
gtdossumqv_maf01[q] += gtDosSumQ_maf01[bin][q];
gtdos2sumqv_maf01[q] += gtDos2SumQ_maf01[bin][q];
gtdossumrefqv_maf01[q] += gtDosSumRefQ_maf01[bin][q];
gtdossumqv_maf001[q] += gtDosSumQ_maf001[bin][q];
gtdos2sumqv_maf001[q] += gtDos2SumQ_maf001[bin][q];
gtdossumrefqv_maf001[q] += gtDosSumRefQ_maf001[bin][q];
gtdossumqv_maf0001[q] += gtDosSumQ_maf0001[bin][q];
gtdos2sumqv_maf0001[q] += gtDos2SumQ_maf0001[bin][q];
gtdossumrefqv_maf0001[q] += gtDosSumRefQ_maf0001[bin][q];
gtdossumqv_maf00001[q] += gtDosSumQ_maf00001[bin][q];
gtdos2sumqv_maf00001[q] += gtDos2SumQ_maf00001[bin][q];
gtdossumrefqv_maf00001[q] += gtDosSumRefQ_maf00001[bin][q];
}
}
for (size_t q = 0; q < Nquery; q++) {
gtdossumq += gtdossumqv[q];
gtdos2sumq += gtdos2sumqv[q];
gtdossumrefq += gtdossumrefqv[q];
gtdossumq_maf01 += gtdossumqv_maf01[q];
gtdos2sumq_maf01 += gtdos2sumqv_maf01[q];
gtdossumrefq_maf01 += gtdossumrefqv_maf01[q];
gtdossumq_maf001 += gtdossumqv_maf001[q];
gtdos2sumq_maf001 += gtdos2sumqv_maf001[q];
gtdossumrefq_maf001 += gtdossumrefqv_maf001[q];
gtdossumq_maf0001 += gtdossumqv_maf0001[q];
gtdos2sumq_maf0001 += gtdos2sumqv_maf0001[q];
gtdossumrefq_maf0001 += gtdossumrefqv_maf0001[q];
gtdossumq_maf00001 += gtdossumqv_maf00001[q];
gtdos2sumq_maf00001 += gtdos2sumqv_maf00001[q];
gtdossumrefq_maf00001 += gtdossumrefqv_maf00001[q];
}
}
{
size_t totalGtErrors = 0;
size_t totalGtErrors_maf01 = 0;
size_t totalGtErrors_maf001 = 0;
size_t totalGtErrors_maf0001 = 0;
size_t totalGtErrors_maf00001 = 0;
size_t gtErrorMin = 0xffffffffffffffffull;
size_t gtErrorMax = 0;
// calc total errors and identify min and max
for (size_t err : gtErrors) {
totalGtErrors += err;
if (gtErrorMin > err) {
gtErrorMin = err;
}
if (gtErrorMax < err) {
gtErrorMax = err;
}
}