forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bam2bcf.c
1148 lines (1051 loc) · 41.7 KB
/
bam2bcf.c
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
/* bam2bcf.c -- variant calling.
Copyright (C) 2010-2012 Broad Institute.
Copyright (C) 2012-2021 Genome Research Ltd.
Author: Heng Li <[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 <math.h>
#include <stdint.h>
#include <assert.h>
#include <float.h>
#include <htslib/hts.h>
#include <htslib/sam.h>
#include <htslib/kstring.h>
#include <htslib/kfunc.h>
#include "bam2bcf.h"
extern void ks_introsort_uint32_t(size_t n, uint32_t a[]);
#define CALL_DEFTHETA 0.83
#define DEF_MAPQ 20
#define CAP_DIST 25
bcf_callaux_t *bcf_call_init(double theta, int min_baseQ, int max_baseQ,
int delta_baseQ)
{
bcf_callaux_t *bca;
if (theta <= 0.) theta = CALL_DEFTHETA;
bca = (bcf_callaux_t*) calloc(1, sizeof(bcf_callaux_t));
bca->capQ = 60;
bca->openQ = 40; bca->extQ = 20; bca->tandemQ = 100;
bca->min_baseQ = min_baseQ;
bca->max_baseQ = max_baseQ;
bca->delta_baseQ = delta_baseQ;
bca->e = errmod_init(1. - theta);
bca->min_frac = 0.002;
bca->min_support = 1;
bca->per_sample_flt = 0;
bca->npos = 100;
bca->ref_pos = (int*) malloc(bca->npos*sizeof(int));
bca->alt_pos = (int*) malloc(bca->npos*sizeof(int));
bca->iref_pos= (int*) malloc(bca->npos*sizeof(int));
bca->ialt_pos= (int*) malloc(bca->npos*sizeof(int));
bca->nqual = 60;
bca->ref_mq = (int*) malloc(bca->nqual*sizeof(int));
bca->alt_mq = (int*) malloc(bca->nqual*sizeof(int));
bca->iref_mq = (int*) malloc(bca->nqual*sizeof(int));
bca->ialt_mq = (int*) malloc(bca->nqual*sizeof(int));
bca->ref_bq = (int*) malloc(bca->nqual*sizeof(int));
bca->alt_bq = (int*) malloc(bca->nqual*sizeof(int));
bca->fwd_mqs = (int*) malloc(bca->nqual*sizeof(int));
bca->rev_mqs = (int*) malloc(bca->nqual*sizeof(int));
return bca;
}
void bcf_call_destroy(bcf_callaux_t *bca)
{
if (bca == 0) return;
errmod_destroy(bca->e);
if (bca->npos) {
free(bca->ref_pos); free(bca->alt_pos);
free(bca->iref_pos); free(bca->ialt_pos);
bca->npos = 0;
}
free(bca->ref_mq); free(bca->alt_mq);
free(bca->iref_mq); free(bca->ialt_mq);
free(bca->ref_bq); free(bca->alt_bq);
free(bca->fwd_mqs); free(bca->rev_mqs);
bca->nqual = 0;
free(bca->bases); free(bca->inscns); free(bca);
}
// position in the sequence with respect to the aligned part of the read
static int get_position(const bam_pileup1_t *p, int *len,
int *sc_len, int *sc_dist) {
int i, j, edist = p->qpos + 1;
int sc_left = 0, sc_right = 0;
int sc_left_dist = -1, sc_right_dist = -1;
// left end
for (i = 0; i < p->b->core.n_cigar; i++) {
int cig = bam_get_cigar(p->b)[i] & BAM_CIGAR_MASK;
if (cig == BAM_CHARD_CLIP)
continue;
else if (cig == BAM_CSOFT_CLIP)
sc_left += bam_get_cigar(p->b)[i] >> BAM_CIGAR_SHIFT;
else
break;
}
if (sc_left)
sc_left_dist = p->qpos+1 - sc_left;
edist -= sc_left;
// right end
for (j = p->b->core.n_cigar-1; j >= i; j--) {
int cig = bam_get_cigar(p->b)[j] & BAM_CIGAR_MASK;
if (cig == BAM_CHARD_CLIP)
continue;
else if (cig == BAM_CSOFT_CLIP)
sc_right += bam_get_cigar(p->b)[j] >> BAM_CIGAR_SHIFT;
else
break;
}
if (sc_right)
sc_right_dist = p->b->core.l_qseq - sc_right - p->qpos;
// Distance to nearest soft-clips and length of that clip.
if (sc_left_dist >= 0) {
if (sc_right_dist < 0 || sc_left_dist < sc_right_dist) {
*sc_len = sc_left;
*sc_dist = sc_left_dist;
}
} else if (sc_right_dist >= 0) {
*sc_len = sc_right;
*sc_dist = sc_right_dist;
} else {
*sc_len = 0;
*sc_dist = 0;
}
*len = p->b->core.l_qseq - sc_left - sc_right;
return edist;
}
void bcf_callaux_clean(bcf_callaux_t *bca, bcf_call_t *call)
{
memset(bca->ref_pos,0,sizeof(int)*bca->npos);
memset(bca->alt_pos,0,sizeof(int)*bca->npos);
memset(bca->iref_pos,0,sizeof(int)*bca->npos);
memset(bca->ialt_pos,0,sizeof(int)*bca->npos);
memset(bca->ref_mq,0,sizeof(int)*bca->nqual);
memset(bca->alt_mq,0,sizeof(int)*bca->nqual);
memset(bca->iref_mq,0,sizeof(int)*bca->nqual);
memset(bca->ialt_mq,0,sizeof(int)*bca->nqual);
memset(bca->ref_bq,0,sizeof(int)*bca->nqual);
memset(bca->alt_bq,0,sizeof(int)*bca->nqual);
memset(bca->fwd_mqs,0,sizeof(int)*bca->nqual);
memset(bca->rev_mqs,0,sizeof(int)*bca->nqual);
if ( call->ADF ) memset(call->ADF,0,sizeof(int32_t)*(call->n+1)*B2B_MAX_ALLELES);
if ( call->ADR ) memset(call->ADR,0,sizeof(int32_t)*(call->n+1)*B2B_MAX_ALLELES);
if ( call->SCR ) memset(call->SCR,0,sizeof(*call->SCR)*(call->n+1));
memset(call->QS,0,sizeof(*call->QS)*call->n*B2B_MAX_ALLELES);
memset(bca->ref_scl, 0, 100*sizeof(int));
memset(bca->alt_scl, 0, 100*sizeof(int));
memset(bca->iref_scl, 0, 100*sizeof(int));
memset(bca->ialt_scl, 0, 100*sizeof(int));
}
/*
Notes:
- Called from bam_plcmd.c by mpileup. Amongst other things, sets the bcf_callret1_t.QS frequencies
which are carried over via bcf_call_combine and bcf_call2bcf to the output BCF as the INFO/QS and FMT/QS annotations.
Later it's used for multiallelic calling by `call -m`, `call -mG` and `+trio-dnm`.
- ref_base is the 4-bit representation of the reference base. It is negative if we are looking at an indel.
*/
/*
* This function is called once for each sample.
* _n is number of pilesups pl contributing reads to this sample
* pl is pointer to array of _n pileups (one pileup per read)
* ref_base is the 4-bit representation of the reference base. It is negative if we are looking at an indel.
* bca is the settings to perform calls across all samples
* r is the returned value of the call
*/
int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r)
{
int i, n, ref4, is_indel, ori_depth = 0;
// clean from previous run
r->ori_depth = 0;
r->mq0 = 0;
memset(r->anno,0,sizeof(double)*16);
memset(r->p,0,sizeof(float)*25);
r->SCR = 0;
if (ref_base >= 0) {
ref4 = seq_nt16_int[ref_base];
is_indel = 0;
} else ref4 = 4, is_indel = 1;
if (_n == 0) return -1;
// enlarge the bases array if necessary
if (bca->max_bases < _n) {
bca->max_bases = _n;
kroundup32(bca->max_bases);
bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
}
// fill the bases array
double nqual_over_60 = bca->nqual / 60.0;
int ADR_ref_missed[4] = {0};
int ADF_ref_missed[4] = {0};
for (i = n = 0; i < _n; ++i) {
const bam_pileup1_t *p = pl + i;
int q, b, mapQ, baseQ, is_diff, min_dist, seqQ;
if ( bca->fmt_flag&(B2B_INFO_SCR|B2B_FMT_SCR) && PLP_HAS_SOFT_CLIP(p->cd.i) ) r->SCR++;
if (p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
if (p->is_del && !is_indel) continue;
++ori_depth;
if (is_indel)
{
b = p->aux>>16&0x3f;
seqQ = q = (p->aux & 0xff); // mp2 + builtin indel-bias
if (q < bca->min_baseQ)
{
if (!p->indel && b < 4)
{
if (bam_is_rev(p->b))
ADR_ref_missed[b]++;
else
ADF_ref_missed[b]++;
}
continue;
}
if (p->indel == 0 && (q < _n/2 || _n > 20)) {
// high quality indel calls without p->indel set aren't
// particularly indicative of being a good REF match either,
// at least not in low coverage. So require solid coverage
// before we start utilising such quals.
b = 0;
q = (int)bam_get_qual(p->b)[p->qpos];
seqQ = (3*seqQ + 2*q)/8;
}
if (_n > 20 && seqQ > 40) seqQ = 40;
baseQ = p->aux>>8&0xff;
is_diff = (b != 0);
}
else
{
b = bam_seqi(bam_get_seq(p->b), p->qpos); // base
b = seq_nt16_int[b? b : ref_base]; // b is the 2-bit base
// Lowest of this and neighbour quality values
uint8_t *qual = bam_get_qual(p->b);
q = qual[p->qpos];
if (p->qpos > 0 &&
q > qual[p->qpos-1]+bca->delta_baseQ)
q = qual[p->qpos-1]+bca->delta_baseQ;
if (p->qpos+1 < p->b->core.l_qseq &&
q > qual[p->qpos+1]+bca->delta_baseQ)
q = qual[p->qpos+1]+bca->delta_baseQ;
if (q < bca->min_baseQ) continue;
if (q > bca->max_baseQ) q = bca->max_baseQ;
baseQ = q;
seqQ = 99;
is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
}
mapQ = p->b->core.qual < 255? p->b->core.qual : DEF_MAPQ; // special case for mapQ==255
if ( !mapQ ) r->mq0++;
if (q > seqQ) q = seqQ;
mapQ = mapQ < bca->capQ? mapQ : bca->capQ;
if (q > mapQ) q = mapQ;
if (q > 63) q = 63;
if (q < 4) q = 4; // MQ=0 reads count as BQ=4
bca->bases[n++] = q<<5 | (int)bam_is_rev(p->b)<<4 | b;
// collect annotations
if (b < 4)
{
r->QS[b] += q;
if ( r->ADF )
{
if ( bam_is_rev(p->b) )
r->ADR[b]++;
else
r->ADF[b]++;
}
}
++r->anno[0<<2|is_diff<<1|bam_is_rev(p->b)];
min_dist = p->b->core.l_qseq - 1 - p->qpos;
if (min_dist > p->qpos) min_dist = p->qpos;
if (min_dist > CAP_DIST) min_dist = CAP_DIST;
r->anno[1<<2|is_diff<<1|0] += baseQ;
r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
r->anno[2<<2|is_diff<<1|0] += mapQ;
r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
r->anno[3<<2|is_diff<<1|0] += min_dist;
r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
// collect for bias tests
if ( baseQ > 59 ) baseQ = 59;
if ( mapQ > 59 ) mapQ = 59;
int len, epos = 0, sc_len = 0, sc_dist = 0;
if ( bca->fmt_flag & (B2B_INFO_RPB|B2B_INFO_VDB|B2B_INFO_SCB) )
{
int pos = get_position(p, &len, &sc_len, &sc_dist);
epos = (double)pos/(len+1) * bca->npos;
if (sc_len) {
sc_len = 15.0*sc_len / sc_dist;
if (sc_len > 99) sc_len = 99;
}
}
int imq = mapQ * nqual_over_60;
int ibq = baseQ * nqual_over_60;
if ( bam_is_rev(p->b) )
bca->rev_mqs[imq]++;
else
bca->fwd_mqs[imq]++;
if ( bam_seqi(bam_get_seq(p->b),p->qpos) == ref_base )
{
bca->ref_pos[epos]++;
bca->ref_bq[ibq]++;
bca->ref_mq[imq]++;
bca->ref_scl[sc_len]++;
}
else
{
bca->alt_pos[epos]++;
bca->alt_bq[ibq]++;
bca->alt_mq[imq]++;
bca->alt_scl[sc_len]++;
}
}
// Compensate for AD not being counted on low quality REF indel matches.
if ( r->ADF && bca->ambig_reads==B2B_INC_AD0 )
{
for (i=0; i<4; i++)
{
r->ADR[0] += ADR_ref_missed[i];
r->ADF[0] += ADF_ref_missed[i];
}
}
else if ( r->ADF && bca->ambig_reads==B2B_INC_AD )
{
int dp = 0, dp_ambig = 0;
for (i=0; i<4; i++) dp += r->ADR[i];
for (i=0; i<4; i++) dp_ambig += ADR_ref_missed[i];
if ( dp )
for (i=0; i<4; i++) r->ADR[i] += lroundf((float)dp_ambig * r->ADR[i]/dp);
dp = 0, dp_ambig = 0;
for (i=0; i<4; i++) dp += r->ADF[i];
for (i=0; i<4; i++) dp_ambig += ADF_ref_missed[i];
if ( dp )
for (i=0; i<4; i++) r->ADF[i] += lroundf((float)dp_ambig * r->ADF[i]/dp);
}
r->ori_depth = ori_depth;
// glfgen
errmod_cal(bca->e, n, 5, bca->bases, r->p); // calculate PL of each genotype
return n;
}
/*
* calc_vdb() - returns value between zero (most biased) and one (no bias)
* on success, or HUGE_VAL when VDB cannot be calculated because
* of insufficient depth (<2x)
*
* Variant Distance Bias tests if the variant bases are positioned within the
* reads with sufficient randomness. Unlike other tests, it looks only at
* variant reads and therefore gives different kind of information than Read
* Position Bias for instance. VDB was developed for detecting artefacts in
* RNA-seq calls where reads from spliced transcripts span splice site
* boundaries. The current implementation differs somewhat from the original
* version described in supplementary material of PMID:22524474, but the idea
* remains the same. (Here the random variable tested is the average distance
* from the averaged position, not the average pairwise distance.)
*
* For coverage of 2x, the calculation is exact but is approximated for the
* rest. The result is most accurate between 4-200x. For 3x or >200x, the
* reported values are slightly more favourable than those of a true random
* distribution.
*/
double calc_vdb(int *pos, int npos)
{
// Note well: the parameters were obtained by fitting to simulated data of
// 100bp reads. This assumes rescaling to 100bp in bcf_call_glfgen().
const int readlen = 100;
assert( npos==readlen );
#define nparam 15
const float param[nparam][3] = { {3,0.079,18}, {4,0.09,19.8}, {5,0.1,20.5}, {6,0.11,21.5},
{7,0.125,21.6}, {8,0.135,22}, {9,0.14,22.2}, {10,0.153,22.3}, {15,0.19,22.8},
{20,0.22,23.2}, {30,0.26,23.4}, {40,0.29,23.5}, {50,0.35,23.65}, {100,0.5,23.7},
{200,0.7,23.7} };
int i, dp = 0;
float mean_pos = 0, mean_diff = 0;
for (i=0; i<npos; i++)
{
if ( !pos[i] ) continue;
dp += pos[i];
mean_pos += pos[i]*i;
}
if ( dp<2 ) return HUGE_VAL; // one or zero reads can be placed anywhere
mean_pos /= dp;
for (i=0; i<npos; i++)
{
if ( !pos[i] ) continue;
mean_diff += pos[i] * fabs(i - mean_pos);
}
mean_diff /= dp;
int ipos = mean_diff; // tuned for float-to-int implicit conversion
if ( dp==2 )
return (2*readlen-2*(ipos+1)-1)*(ipos+1)/(readlen-1)/(readlen*0.5);
if ( dp>=200 )
i = nparam; // shortcut for big depths
else
{
for (i=0; i<nparam; i++)
if ( param[i][0]>=dp ) break;
}
float pshift, pscale;
if ( i==nparam )
{
// the depth is too high, go with 200x
pscale = param[nparam-1][1];
pshift = param[nparam-1][2];
}
else if ( i>0 && param[i][0]!=dp )
{
// linear interpolation of parameters
pscale = (param[i-1][1] + param[i][1])*0.5;
pshift = (param[i-1][2] + param[i][2])*0.5;
}
else
{
pscale = param[i][1];
pshift = param[i][2];
}
return 0.5*kf_erfc(-(mean_diff-pshift)*pscale);
}
double calc_chisq_bias(int *a, int *b, int n)
{
int na = 0, nb = 0, i, ndf = n;
for (i=0; i<n; i++) na += a[i];
for (i=0; i<n; i++) nb += b[i];
if ( !na || !nb ) return HUGE_VAL;
double chisq = 0;
for (i=0; i<n; i++)
{
if ( !a[i] && !b[i] ) ndf--;
else
{
double tmp = a[i] - b[i];
chisq += tmp*tmp/(a[i]+b[i]);
}
}
/*
kf_gammq: incomplete gamma function Q(a,x) = 1 - P(a,x) = Gamma(a,x)/Gamma(a)
1 if the distributions are identical, 0 if very different
*/
double prob = kf_gammaq(0.5*ndf, 0.5*chisq);
return prob;
}
static double mann_whitney_1947_(int n, int m, int U)
{
if (U<0) return 0;
if (n==0||m==0) return U==0 ? 1 : 0;
return (double)n/(n+m)*mann_whitney_1947_(n-1,m,U-m) + (double)m/(n+m)*mann_whitney_1947_(n,m-1,U);
}
double mann_whitney_1947(int n, int m, int U)
{
#include "mw.h"
assert(n >= 2 && m >= 2);
return (n < 8 && m < 8 && U < 50)
? mw[n-2][m-2][U]
: mann_whitney_1947_(n,m,U);
}
double mann_whitney_1947_cdf(int n, int m, int U)
{
int i;
double sum = 0;
for (i=0; i<=U; i++)
sum += mann_whitney_1947(n,m,i);
return sum;
}
double calc_mwu_bias_cdf(int *a, int *b, int n)
{
int na = 0, nb = 0, i;
double U = 0;
//double ties = 0;
for (i=0; i<n; i++)
{
na += a[i];
U += a[i] * (nb + b[i]*0.5);
nb += b[i];
// if ( a[i] && b[i] )
// {
// double tie = a[i] + b[i];
// ties += (tie*tie-1)*tie;
// }
}
if ( !na || !nb ) return HUGE_VAL;
// Always work with the smaller U
double U_min = ((double)na * nb) - U;
if ( U < U_min ) U_min = U;
if ( na==1 ) return 2.0 * (floor(U_min)+1) / (nb+1);
if ( nb==1 ) return 2.0 * (floor(U_min)+1) / (na+1);
// Normal approximation, very good for na>=8 && nb>=8 and reasonable if na<8 or nb<8
if ( na>=8 || nb>=8 )
{
double mean = ((double)na*nb)*0.5;
// Correction for ties:
// double N = na+nb;
// double var2 = (N*N-1)*N-ties;
// if ( var2==0 ) return 1.0;
// var2 *= ((double)na*nb)/N/(N-1)/12.0;
// No correction for ties:
double var2 = ((double)na*nb)*(na+nb+1)/12.0;
double z = (U_min - mean)/sqrt(2*var2); // z is N(0,1)
return 2.0 - kf_erfc(z); // which is 1 + erf(z)
}
// Exact calculation
double pval = 2*mann_whitney_1947_cdf(na,nb,U_min);
return pval>1 ? 1 : pval;
}
double calc_mwu_bias(int *a, int *b, int n, int left)
{
int na = 0, nb = 0, i;
double U = 0;
// double ties = 0;
for (i=0; i<n; i++)
{
if (!a[i]) {
if (!b[i]) continue;
nb += b[i];
} else if (!b[i]) {
na += a[i];
U += a[i] * nb;
} else {
na += a[i];
U += a[i] * (nb + b[i]*0.5);
nb += b[i];
// double tie = a[i] + b[i];
// ties += (tie*tie-1)*tie;
}
}
if ( !na || !nb ) return HUGE_VAL;
if ( na==1 || nb==1 ) return 1.0; // Flat probability, all U values are equally likely
double mean = ((double)na*nb)*0.5;
if (left && U > mean) return 1; // for MQB which is asymmetrical
if ( na==2 || nb==2 )
{
// Linear approximation
return U>mean ? (2.0*mean-U)/mean : U/mean;
}
// Correction for ties:
// double N = na+nb;
// double var2 = (N*N-1)*N-ties;
// if ( var2==0 ) return 1.0;
// var2 *= ((double)na*nb)/N/(N-1)/12.0;
// No correction for ties:
double var2 = ((double)na*nb)*(na+nb+1)/12.0;
if ( na>=8 || nb>=8 )
{
// Normal approximation, very good for na>=8 && nb>=8 and reasonable if na<8 or nb<8
return exp(-0.5*(U-mean)*(U-mean)/var2);
}
// Exact calculation
return mann_whitney_1947(na,nb,U) * sqrt(2*M_PI*var2);
}
// A Z-score version of the above function.
//
// See "Normal approximation and tie correction" at
// https://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U_test
//
// The Z score is the number of standard deviations above or below the mean
// with 0 being equality of the two distributions and +ve/-ve from there.
//
// This is a more robust score to filter on.
double calc_mwu_biasZ(int *a, int *b, int n, int left_only, int do_Z) {
int i;
int64_t t;
// Optimisation
for (i = 0; i < n; i++)
if (b[i])
break;
int b_empty = (i == n);
// Count equal (e), less-than (l) and greater-than (g) permutations.
int e = 0, l = 0, na = 0, nb = 0;
if (b_empty) {
for (t = 0, i = n-1; i >= 0; i--) {
na += a[i];
t += (a[i]*a[i]-1)*a[i]; // adjustment score for ties
}
} else {
for (t = 0, i = n-1; i >= 0; i--) {
// Combinations of a[i] and b[j] for i==j
e += a[i]*b[i];
// nb is running total of b[i+1]..b[n-1].
// Therefore a[i]*nb is the number of combinations of a[i] and b[j]
// for all i < j.
l += a[i]*nb; // a<b
na += a[i];
nb += b[i];
int p = a[i]+b[i];
t += (p*p-1)*p; // adjustment score for ties
}
}
if (!na || !nb)
return HUGE_VAL;
double U, m;
U = l + e*0.5; // Mann-Whitney U score
m = na*nb / 2.0;
// With ties adjustment
double var2 = (na*nb)/12.0 * ((na+nb+1) - t/(double)((na+nb)*(na+nb-1)));
// var = na*nb*(na+nb+1)/12.0; // simpler; minus tie adjustment
if (var2 <= 0)
return do_Z ? 0 : 1;
if (do_Z) {
// S.D. normalised Z-score
//Z = (U - m - (U-m >= 0 ? 0.5 : -0.5)) / sd; // gatk method?
return (U - m) / sqrt(var2);
}
// Else U score, which can be asymmetric for some data types.
if (left_only && U > m)
return HUGE_VAL; // one-sided, +ve bias is OK, -ve is not.
if (na >= 8 || nb >= 8) {
// Normal approximation, very good for na>=8 && nb>=8 and
// reasonable if na<8 or nb<8
return exp(-0.5*(U-m)*(U-m)/var2);
}
// Exact calculation
if (na==1 || nb == 1)
return mann_whitney_1947_(na, nb, U) * sqrt(2*M_PI*var2);
else
return mann_whitney_1947(na, nb, U) * sqrt(2*M_PI*var2);
}
static inline double logsumexp2(double a, double b)
{
if ( a>b )
return log(1 + exp(b-a)) + a;
else
return log(1 + exp(a-b)) + b;
}
void calc_SegBias(const bcf_callret1_t *bcr, bcf_call_t *call)
{
call->seg_bias = HUGE_VAL;
if ( !bcr ) return;
int nr = call->anno[2] + call->anno[3]; // number of observed non-reference reads
if ( !nr ) return;
int avg_dp = (call->anno[0] + call->anno[1] + nr) / call->n; // average depth
double M = floor((double)nr / avg_dp + 0.5); // an approximate number of variants samples in the population
if ( M>call->n ) M = call->n; // clamp M at the number of samples
else if ( M==0 ) M = 1;
double f = M / 2. / call->n; // allele frequency
double p = (double) nr / call->n; // number of variant reads per sample expected if variant not real (poisson)
double q = (double) nr / M; // number of variant reads per sample expected if variant is real (poisson)
double sum = 0;
const double log2 = log(2.0);
// fprintf(stderr,"M=%.1f p=%e q=%e f=%f dp=%d\n",M,p,q,f,avg_dp);
int i;
for (i=0; i<call->n; i++)
{
int oi = bcr[i].anno[2] + bcr[i].anno[3]; // observed number of non-ref reads
double tmp;
if ( oi )
{
// tmp = log(f) + oi*log(q/p) - q + log(2*(1-f) + f*pow(2,oi)*exp(-q)) + p; // this can under/overflow
tmp = logsumexp2(log(2*(1-f)), log(f) + oi*log2 - q);
tmp += log(f) + oi*log(q/p) - q + p;
}
else
tmp = log(2*f*(1-f)*exp(-q) + f*f*exp(-2*q) + (1-f)*(1-f)) + p;
sum += tmp;
// fprintf(stderr,"oi=%d %e\n", oi,tmp);
}
call->seg_bias = sum;
}
/**
* bcf_call_combine() - sets the PL array and VDB, RPB annotations, finds the top two alleles
* @n: number of samples
* @calls: each sample's calls
* @bca: auxiliary data structure for holding temporary values
* @ref_base: the reference base
* @call: filled with the annotations
*
* Combines calls across the various samples being studied
* 1. For each allele at each base across all samples the quality is summed so
* you end up with a set of quality sums for each allele present 2. The quality
* sums are sorted.
* 3. Using the sorted quality sums we now create the allele ordering array
* A\subN. This is done by doing the following:
* a) If the reference allele is known it always comes first, otherwise N
* comes first.
* b) Then the rest of the alleles are output in descending order of quality
* sum (which we already know the qsum array was sorted). Any allelles with
* qsum 0 will be excluded.
* 4. Using the allele ordering array we create the genotype ordering array.
* In the worst case with an unknown reference this will be: A0/A0 A1/A0 A1/A1
* A2/A0 A2/A1 A2/A2 A3/A0 A3/A1 A3/A2 A3/A3 A4/A0 A4/A1 A4/A2 A4/A3 A4/A4
* 5. The genotype ordering array is then used to extract data from the error
* model 5*5 matrix and is used to produce a Phread likelihood array for each
* sample.
*/
int bcf_call_combine(int n, const bcf_callret1_t *calls, bcf_callaux_t *bca, int ref_base /*4-bit*/, bcf_call_t *call)
{
int ref4, i, j;
float qsum[B2B_MAX_ALLELES] = {0,0,0,0,0};
if (ref_base >= 0) {
call->ori_ref = ref4 = seq_nt16_int[ref_base];
if (ref4 > 4) ref4 = 4;
} else call->ori_ref = -1, ref4 = 0;
// calculate qsum, this is done by summing normalized qsum across all samples,
// to account for differences in coverage
for (i = 0; i < n; ++i)
{
float sum = 0;
for (j = 0; j < 4; ++j) sum += calls[i].QS[j];
if ( sum )
for (j = 0; j < 4; j++) qsum[j] += (float)calls[i].QS[j] / sum;
}
// sort qsum in ascending order (insertion sort)
float *ptr[5], *tmp;
for (i=0; i<5; i++) ptr[i] = &qsum[i];
for (i=1; i<4; i++)
for (j=i; j>0 && *ptr[j] < *ptr[j-1]; j--)
tmp = ptr[j], ptr[j] = ptr[j-1], ptr[j-1] = tmp;
// Set the reference allele and alternative allele(s)
for (i=0; i<5; i++) call->a[i] = -1;
for (i=0; i<B2B_MAX_ALLELES; i++) call->qsum[i] = 0;
call->unseen = -1;
call->a[0] = ref4;
for (i=3, j=1; i>=0; i--) // i: alleles sorted by QS; j, a[j]: output allele ordering
{
int ipos = ptr[i] - qsum; // position in sorted qsum array
if ( ipos==ref4 )
call->qsum[0] = qsum[ipos]; // REF's qsum
else
{
if ( !qsum[ipos] ) break; // qsum is 0, this and consequent alleles are not seen in the pileup
call->qsum[j] = qsum[ipos];
call->a[j++] = ipos;
}
}
if (ref_base >= 0)
{
// for SNPs, find the "unseen" base
if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
call->unseen = j, call->a[j++] = ptr[i] - qsum;
call->n_alleles = j;
}
else
{
call->n_alleles = j;
if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
}
/*
* Set the phread likelihood array (call->PL) This array is 15 entries long
* for each sample because that is size of an upper or lower triangle of a
* worst case 5x5 matrix of possible genotypes. This worst case matrix will
* occur when all 4 possible alleles are present and the reference allele
* is unknown. The sides of the matrix will correspond to the reference
* allele (if known) followed by the alleles present in descending order of
* quality sum
*/
{
int x, g[15], z;
double sum_min = 0.;
x = call->n_alleles * (call->n_alleles + 1) / 2;
// get the possible genotypes
// this is done by creating an ordered list of locations g for call (allele a, allele b) in the genotype likelihood matrix
for (i = z = 0; i < call->n_alleles; ++i) {
for (j = 0; j <= i; ++j) {
g[z++] = call->a[j] * 5 + call->a[i];
}
}
// for each sample calculate the PL
for (i = 0; i < n; ++i)
{
int32_t *PL = call->PL + x * i;
const bcf_callret1_t *r = calls + i;
float min = FLT_MAX;
for (j = 0; j < x; ++j) {
if (min > r->p[g[j]]) min = r->p[g[j]];
}
sum_min += min;
for (j = 0; j < x; ++j) {
int y;
y = (int)(r->p[g[j]] - min + .499);
if (y > 255) y = 255;
PL[j] = y;
}
}
if ( call->DP4 )
{
for (i=0; i<n; i++)
{
call->DP4[4*i] = calls[i].anno[0];
call->DP4[4*i+1] = calls[i].anno[1];
call->DP4[4*i+2] = calls[i].anno[2];
call->DP4[4*i+3] = calls[i].anno[3];
}
}
if ( call->SCR )
{
for (i=0; i<n; i++)
{
call->SCR[0] += calls[i].SCR;
call->SCR[1+i] = calls[i].SCR;
}
}
if ( call->ADF )
{
assert( call->n_alleles<=B2B_MAX_ALLELES ); // this is always true for SNPs and so far for indels as well
// reorder ADR,ADF to match the allele ordering at this site
int32_t tmp[B2B_MAX_ALLELES];
int32_t *adr = call->ADR + B2B_MAX_ALLELES, *adr_out = call->ADR + B2B_MAX_ALLELES;
int32_t *adf = call->ADF + B2B_MAX_ALLELES, *adf_out = call->ADF + B2B_MAX_ALLELES;
int32_t *adr_tot = call->ADR; // the first bin stores total counts per site
int32_t *adf_tot = call->ADF;
for (i=0; i<n; i++)
{
for (j=0; j<call->n_alleles; j++)
{
tmp[j] = adr[ call->a[j] ];
adr_tot[j] += tmp[j];
}
for (j=0; j<call->n_alleles; j++) adr_out[j] = tmp[j];
for (j=0; j<call->n_alleles; j++)
{
tmp[j] = adf[ call->a[j] ];
adf_tot[j] += tmp[j];
}
for (j=0; j<call->n_alleles; j++) adf_out[j] = tmp[j];
adf_out += call->n_alleles;
adr_out += call->n_alleles;
adr += B2B_MAX_ALLELES;
adf += B2B_MAX_ALLELES;
}
}
if ( bca->fmt_flag & B2B_FMT_QS )
{
assert( call->n_alleles<=B2B_MAX_ALLELES ); // this is always true for SNPs and so far for indels as well
// reorder QS to match the allele ordering at this site
int32_t tmp[B2B_MAX_ALLELES];
int32_t *qs = call->QS, *qs_out = call->QS;
for (i=0; i<n; i++)
{
for (j=0; j<call->n_alleles; j++) tmp[j] = qs[ call->a[j] ];
for (j=0; j<call->n_alleles; j++) qs_out[j] = tmp[j] < BCF_MAX_BT_INT32 ? tmp[j] : BCF_MAX_BT_INT32;
qs_out += call->n_alleles;
qs += B2B_MAX_ALLELES;
}
}
// if (ref_base < 0) fprintf(stderr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
call->shift = (int)(sum_min + .499);
}
// combine annotations
memset(call->anno, 0, 16 * sizeof(double));
call->ori_depth = 0;
call->depth = 0;
call->mq0 = 0;
for (i = 0; i < n; ++i) {
call->depth += calls[i].anno[0] + calls[i].anno[1] + calls[i].anno[2] + calls[i].anno[3];
call->ori_depth += calls[i].ori_depth;
call->mq0 += calls[i].mq0;
for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
}
calc_SegBias(calls, call);
// calc_chisq_bias("XPOS", call->bcf_hdr->id[BCF_DT_CTG][call->tid].key, call->pos, bca->ref_pos, bca->alt_pos, bca->npos);
// calc_chisq_bias("XMQ", call->bcf_hdr->id[BCF_DT_CTG][call->tid].key, call->pos, bca->ref_mq, bca->alt_mq, bca->nqual);
// calc_chisq_bias("XBQ", call->bcf_hdr->id[BCF_DT_CTG][call->tid].key, call->pos, bca->ref_bq, bca->alt_bq, bca->nqual);
if (bca->fmt_flag & B2B_INFO_ZSCORE) {
// U z-normalised as +/- number of standard deviations from mean.
if (call->ori_ref < 0) {
if (bca->fmt_flag & B2B_INFO_RPB)
call->mwu_pos = calc_mwu_biasZ(bca->iref_pos, bca->ialt_pos,
bca->npos, 0, 1);
call->mwu_mq = calc_mwu_biasZ(bca->iref_mq, bca->ialt_mq,
bca->nqual,1,1);
if ( bca->fmt_flag & B2B_INFO_SCB )
call->mwu_sc = calc_mwu_biasZ(bca->iref_scl, bca->ialt_scl,
100, 0,1);
} else {
if (bca->fmt_flag & B2B_INFO_RPB)
call->mwu_pos = calc_mwu_biasZ(bca->ref_pos, bca->alt_pos,
bca->npos, 0, 1);
call->mwu_mq = calc_mwu_biasZ(bca->ref_mq, bca->alt_mq,
bca->nqual,1,1);
call->mwu_bq = calc_mwu_biasZ(bca->ref_bq, bca->alt_bq,
bca->nqual,0,1);
call->mwu_mqs = calc_mwu_biasZ(bca->fwd_mqs, bca->rev_mqs,
bca->nqual,0,1);
if ( bca->fmt_flag & B2B_INFO_SCB )
call->mwu_sc = calc_mwu_biasZ(bca->ref_scl, bca->alt_scl,
100, 0,1);
}
} else {
// Old method; U as probability between 0 and 1
if ( bca->fmt_flag & B2B_INFO_RPB )
call->mwu_pos = calc_mwu_biasZ(bca->ref_pos, bca->alt_pos,
bca->npos, 0, 0);
call->mwu_mq = calc_mwu_biasZ(bca->ref_mq, bca->alt_mq,
bca->nqual, 1, 0);
call->mwu_bq = calc_mwu_biasZ(bca->ref_bq, bca->alt_bq,
bca->nqual, 0, 0);
call->mwu_mqs = calc_mwu_biasZ(bca->fwd_mqs, bca->rev_mqs,
bca->nqual, 0, 0);
}
#if CDF_MWU_TESTS
// CDF version of MWU tests is not calculated by default
if ( bca->fmt_flag & B2B_INFO_RPB )
call->mwu_pos_cdf = calc_mwu_bias_cdf(bca->ref_pos, bca->alt_pos, bca->npos);
call->mwu_mq_cdf = calc_mwu_bias_cdf(bca->ref_mq, bca->alt_mq, bca->nqual);
call->mwu_bq_cdf = calc_mwu_bias_cdf(bca->ref_bq, bca->alt_bq, bca->nqual);
call->mwu_mqs_cdf = calc_mwu_bias_cdf(bca->fwd_mqs, bca->rev_mqs, bca->nqual);
#endif
if ( bca->fmt_flag & B2B_INFO_VDB )
call->vdb = calc_vdb(bca->alt_pos, bca->npos);
return 0;
}
int bcf_call2bcf(bcf_call_t *bc, bcf1_t *rec, bcf_callret1_t *bcr, int fmt_flag, const bcf_callaux_t *bca, const char *ref)
{
extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
int i, j, nals = 1;
bcf_hdr_t *hdr = bc->bcf_hdr;
rec->rid = bc->tid;
rec->pos = bc->pos;
rec->qual = 0;
bc->tmp.l = 0;
if (bc->ori_ref < 0) // indel
{
// REF
kputc(ref[bc->pos], &bc->tmp);
for (j = 0; j < bca->indelreg; ++j) kputc(ref[bc->pos+1+j], &bc->tmp);
// ALT
for (i=1; i<4; i++)
{
if (bc->a[i] < 0) break;
kputc(',', &bc->tmp); kputc(ref[bc->pos], &bc->tmp);
if (bca->indel_types[bc->a[i]] < 0) { // deletion
for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++j)