forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrio-dnm2.c
1762 lines (1664 loc) · 74.2 KB
/
trio-dnm2.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
/* The MIT License
Copyright (c) 2018-2023 Genome Research Ltd.
Author: Petr Danecek <pd3@sanger.ac.uk>
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 <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <assert.h>
#include <getopt.h>
#include <math.h>
#include <unistd.h> // for isatty
#include <inttypes.h>
#include <htslib/hts.h>
#include <htslib/vcf.h>
#include <htslib/kstring.h>
#include <htslib/kseq.h>
#include <htslib/kfunc.h>
#include <htslib/synced_bcf_reader.h>
#include <htslib/vcfutils.h>
#include <assert.h>
#include <errno.h>
#include "bcftools.h"
#include "regidx.h"
#include "filter.h"
#define USE_DNG 1 // DeNovoGear model
#define USE_ACM 2 // the new "allele-centric model" which combines fixed DNG priors with allele centric approach
#define USE_NAIVE 3 // a naive calling model based on observed GT
// Logic of the filters: include or exclude sites which match the filters?
#define FLT_INCLUDE 1
#define FLT_EXCLUDE 2
#define iFATHER 0 // don't modify, QS calculations depend on this order!
#define iMOTHER 1
#define iCHILD 2
// output tag type
#define DNM_INT (1<<0)
#define DNM_FLOAT (1<<1)
#define DNM_LOG ((1<<2)|DNM_FLOAT)
#define DNM_PHRED ((1<<3)|DNM_INT)
#define DNM_PROB ((1<<4)|DNM_FLOAT)
#define DNM_FLAG ((1<<5)|DNM_INT)
typedef struct
{
int idx[3]; // VCF sample index for child, father, mother
int pass, // do all three pass the filters?
is_male; // male pattern of chrX inheritance?
}
trio_t;
typedef struct
{
// combines priors, mutation rates, genotype transmission probability; see init_priors()
double pprob[10][10][10]; // prior probability; the order is father,mother,child
uint8_t denovo[10][10][10]; // is the GT combination not compatible with normal inheritance (0) or is de novo (1)
uint8_t denovo_allele[10][10][10]; // which of the alleles is de novo for this configuration
}
priors_t;
typedef struct
{
double abs, frac;
double abs1, frac1; // applied only if allele observed in a single parent, but not when observed in both
}
pnoise_t;
typedef struct
{
int argc, filter_logic, regions_is_file, targets_is_file, output_type, record_cmd_line, clevel;
int regions_overlap, targets_overlap;
char *filter_str;
filter_t *filter;
char **argv, *ped_fname, *pfm, *output_fname, *fname, *regions, *targets;
htsFile *out_fh;
bcf_srs_t *sr;
bcf_hdr_t *hdr, *hdr_out;
char *chrX_list_str;
regidx_t *chrX_idx;
trio_t *trio;
int has_fmt_ad;
int ntrio, mtrio;
int32_t *pl,*ad,*qs,*gt, *dnm_qual_int, *dnm_allele, *vaf; // input FMT/PL,AD,QS,GT values, output DNM and VAF
float *dnm_qual_float;
int mpl, mad, mqs, mgt;
double min_score;
double *pl3; // normalized PLs converted to probs for iFATHER,iMOTHER,iCHILD
double *qs3; // QS converted to probs for iFATHER,iMOTHER,iCHILD
int maprob, mpl3, mqs3, midx, *idx, force_ad, use_model;
double *alt_tmp;
int *alt_idx;
int malt_tmp, malt_idx;
char *dnm_score_tag, // the argument of --dnm-tag, by default DNM:log
*dnm_vaf_tag,
*dnm_allele_tag;
int dnm_score_type; // given by e.g. --dnm-tag DNM:log
double mrate; // --mrate, mutation rate
pnoise_t pn_snv, pn_indel; // --pn and --pns for SNVs and indels
int with_ppl, with_pad; // --with-pPL or --with-pAD
int use_dng_priors; // --dng-priors
int need_QS;
int strictly_novel;
priors_t priors, priors_X, priors_XX;
char *index_fn;
int write_index;
}
args_t;
const char *about(void)
{
return "Screen variants for possible de-novo mutations in trios.\n";
}
static const char *usage_text(void)
{
return
"\n"
"About: Screen variants for possible de-novo mutations in trios\n"
"Usage: bcftools +trio-dnm2 [OPTIONS]\n"
"Common options:\n"
" -e, --exclude EXPR Exclude trios for which the expression is true (one matching sample invalidates a trio)\n"
" -i, --include EXPR Include trios for which the expression is true (one failing samples invalidates a trio)\n"
" -o, --output FILE Output file name [stdout]\n"
" -O, --output-type u|b|v|z[0-9] u/b: un/compressed BCF, v/z: un/compressed VCF, 0-9: compression level [v]\n"
" -r, --regions REG Restrict to comma-separated list of regions\n"
" -R, --regions-file FILE Restrict to regions listed in a file\n"
" --regions-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [1]\n"
" -t, --targets REG Similar to -r but streams rather than index-jumps\n"
" -T, --targets-file FILE Similar to -R but streams rather than index-jumps\n"
" --targets-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [0]\n"
" --no-version Do not append version and command line to the header\n"
"\n"
"General options:\n"
" -m, --min-score NUM Do not add FMT/DNM annotation if the score is smaller than NUM\n"
" -p, --pfm [1X:|2X:]P,F,M Sample names of child (the proband), father, mother; \"1X:\" for male pattern of chrX inheritance [2X:]\n"
" -P, --ped FILE PED file with the columns: <ignored>,proband,father,mother,sex(1:male,2:female)\n"
" -X, --chrX LIST List of regions with chrX inheritance pattern or one of the presets: [GRCh37]\n"
" GRCh37 .. X:1-60000,chrX:1-60000,X:2699521-154931043,chrX:2699521-154931043\n"
" GRCh38 .. X:1-9999,chrX:1-9999,X:2781480-155701381,chrX:2781480-155701381\n"
" --dnm-tag TAG[:type] Output tag with DNM quality score and its type [DNM:log]\n"
" log .. log-scaled quality (-inf,0; float)\n"
" flag .. is a DNM, implies --use-NAIVE (1; int)\n"
" phred .. phred quality (0-255; int)\n"
" prob .. probability (0-1; float)\n"
" --force-AD Calculate VAF even if the number of FMT/AD fields is incorrect. Use at your own risk!\n"
" --va TAG Output tag name for the variant allele [VA]\n"
" --vaf TAG Output tag name for variant allele fraction [VAF]\n"
"\n"
"Model options:\n"
" --dng-priors Use the original DeNovoGear priors (including bugs in prior assignment, but with chrX bugs fixed)\n"
" --mrate NUM Mutation rate [1e-8]\n"
" --pn FRAC[,NUM][:type] Tolerance to parental noise or mosaicity, given as fraction of QS or number of reads. The type is\n"
" 'snv', 'indel' or 'both' [--pn 0.005,0:snv --pn 0,0:indel]\n"
" --pns FRAC[,NUM][:type] Same as --pn but applied for alleles observed in a single parent [--pns 0.045,0:snv --pns 0,0:indel]\n"
" -n, --strictly-novel When Mendelian inheritance is violiated, score highly only novel alleles (e.g. in LoH regions)\n"
" --use-DNG The original DeNovoGear model, implies --dng-priors\n"
" --use-NAIVE A naive calling model which uses only FMT/GT to determine DNMs\n"
" --with-pAD Do not use FMT/QS but parental FMT/AD\n"
" --with-pPL Do not use FMT/QS but parental FMT/PL. Equals to DNG with bugs fixed (more FPs, fewer FNs)\n"
" -W, --write-index[=FMT] Automatically index the output files [off]\n"
"\n"
"Example:\n"
" # Annotate VCF with FORMAT/DNM, run for a single trio\n"
" bcftools +trio-dnm2 -p proband,father,mother file.bcf\n"
"\n"
" # Same as above, but read the trio(s) from a PED file\n"
" bcftools +trio-dnm2 -P file.ped file.bcf\n"
"\n"
" # Same as above plus extract a list of significant DNMs using the bcftools/query command\n"
" bcftools +trio-dnm2 -P file.ped file.bcf -Ou | bcftools query -i'DNM>10' -f'[%CHROM:%POS %SAMPLE %DNM\\n]'\n"
"\n"
" # A complete example with a variant calling step. Note that this is one long\n"
" # command and should be on a single line. Also note that a filtering step is\n"
" # recommended, e.g. by depth and VAF (not shown here):\n"
" bcftools mpileup -a AD,QS -f ref.fa -Ou proband.bam father.bam mother.bam |\n"
" bcftools call -mv -Ou |\n"
" bcftools +trio-dnm2 -p proband,father,mother -Oz -o output.vcf.gz\n"
"\n";
}
static int cmp_trios(const void *_a, const void *_b)
{
trio_t *a = (trio_t *) _a;
trio_t *b = (trio_t *) _b;
int i;
int amin = a->idx[0];
for (i=1; i<3; i++)
if ( amin > a->idx[i] ) amin = a->idx[i];
int bmin = b->idx[0];
for (i=1; i<3; i++)
if ( bmin > b->idx[i] ) bmin = b->idx[i];
if ( amin < bmin ) return -1;
if ( amin > bmin ) return 1;
return 0;
}
static void parse_ped(args_t *args, char *fname)
{
htsFile *fp = hts_open(fname, "r");
if ( !fp ) error("Could not read: %s\n", fname);
kstring_t str = {0,0,0};
if ( hts_getline(fp, KS_SEP_LINE, &str) <= 0 ) error("Empty file: %s\n", fname);
int moff = 0, *off = NULL;
do
{
// familyID sampleID paternalID maternalID sex phenotype population relationship siblings secondOrder thirdOrder children comment
// BB03 HG01884 HG01885 HG01956 2 0 ACB child 0 0 0 0
int ncols = ksplit_core(str.s,0,&moff,&off);
if ( ncols<4 ) error("Could not parse the ped file: %s\n", str.s);
int father = bcf_hdr_id2int(args->hdr,BCF_DT_SAMPLE,&str.s[off[2]]);
if ( father<0 ) continue;
int mother = bcf_hdr_id2int(args->hdr,BCF_DT_SAMPLE,&str.s[off[3]]);
if ( mother<0 ) continue;
int child = bcf_hdr_id2int(args->hdr,BCF_DT_SAMPLE,&str.s[off[1]]);
if ( child<0 ) continue;
int sex = 0;
if ( ncols>=5 )
{
char *tmp;
sex = strtol(&str.s[off[4]],&tmp,10);
if ( tmp==&str.s[off[4]] || *tmp ) error("Could not parse the PED file, the 5th column should be numeric: %s\n",str.s);
if ( sex!=1 && sex!=2 ) sex = 0;
}
args->ntrio++;
hts_expand0(trio_t,args->ntrio,args->mtrio,args->trio);
trio_t *trio = &args->trio[args->ntrio-1];
trio->idx[iFATHER] = father;
trio->idx[iMOTHER] = mother;
trio->idx[iCHILD] = child;
trio->is_male = sex==1 ? 1 : 0;
}
while ( hts_getline(fp, KS_SEP_LINE, &str)>=0 );
// sort the sample by index so that they are accessed more or less sequentially
qsort(args->trio,args->ntrio,sizeof(trio_t),cmp_trios);
// check for duplicates
int i;
for (i=1; i<args->ntrio; i++)
{
trio_t *ta = &args->trio[i-1];
trio_t *tb = &args->trio[i];
if ( ta->idx[0]==tb->idx[0] && ta->idx[1]==tb->idx[1] && ta->idx[2]==tb->idx[2] )
error("Error: duplicate trio entries detected in the PED file: %s\n",fname);
}
fprintf(stderr,"Identified %d complete trio%s in the VCF file\n", args->ntrio,args->ntrio==1?"":"s");
free(str.s);
free(off);
if ( hts_close(fp)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,fname);
}
static const uint8_t seq1[10] = {0,1,1,2,2,2,3,3,3,3};
static const uint8_t seq2[10] = {0,0,1,0,1,2,0,1,2,3};
static const int8_t seq3[13] = {-1,0,2,1,5,3,4,-1,9,6,7,-1,8}; // Lookup from (1<<ial)|(1<<jal) to iseq
typedef enum { include_ref, only_alts } count_unique_t;
static int count_unique_alleles(int ngt, int gt[3], count_unique_t count)
{
int i, als[4] = {0,0,0,0};
for (i=0; i<ngt; i++)
{
int igt = gt[i];
als[seq1[igt]] = 1;
als[seq2[igt]] = 1;
}
int nals = 0;
int ibeg = count==include_ref ? 0 : 1;
for (i=ibeg; i<4; i++) nals += als[i];
return nals;
}
// Parent genotype probability L(GM,GF)
// The FIGL model from the supplement "Variation in genome-wide mutation rates within and between human families",
// see also the actual implementation in https://github.com/ultimatesource/denovogear/blob/develop/src/dnm/makeLookup.cc
// This is the original method, including bugs in prior assignment.
static double init_DNG_mf_priors(args_t *args, int fi, int mi, int ci)
{
double gt_prior = 0; // parent genotype probability L(GM,GF)
int fa = seq1[fi];
int fb = seq2[fi];
int ma = seq1[mi];
int mb = seq2[mi];
int gts[3]; gts[0] = fi; gts[1] = mi; gts[2] = 0;
int nals_mf = count_unique_alleles(2,gts,include_ref);
int ca = seq1[ci];
int cb = seq2[ci];
gts[0] = fi; gts[1] = mi; gts[2] = ci;
int nals_mfc = count_unique_alleles(3,gts,include_ref);
int nref_mf = (fa==0 ? 1 : 0) + (fb==0 ? 1 : 0) + (ma==0 ? 1 : 0) + (mb==0 ? 1 : 0);
if ( nals_mfc>3 ) // 4 different alleles in the trio
gt_prior = 1e-26;
else if ( nals_mf>=3 ) // 3 different alleles in parents,
gt_prior = 0.002 * 0.002 / 414; // split equally amongst all triallelic cases
else if ( nals_mfc==3 ) // 3rd allele in the child
gt_prior = 1e-3 * 1e-3; // This is what g_PolyRate evaluates in DNG code
else if ( nref_mf==4 )
gt_prior = 0.995 * 0.998; // 4 copies of ref in parents
else if ( nref_mf==3 )
gt_prior = 0.995 * 0.002 * (3.0/5.0) * (4.0/5.0) * 0.5; // 3 copies of ref in parents
else if ( nref_mf==2 && fa==fb && ma==mb )
gt_prior = 0.995 * 0.002 * (2.0/5.0) * (1.0/5.0) * 0.5; // 2 copies of ref in parents, both homs
else if ( nref_mf==2 )
gt_prior = 0.995 * 0.002 * (2.0/5.0) * (2.0/5.0); // 2 copies of ref in parents, both hets
else if ( nref_mf==1 )
{
assert( nals_mf==2 && nals_mfc==2 );
gt_prior = 0.995 * 0.002 * (2.0/5.0) * (2.0/5.0) * 0.5; // 1 copy of ref in parents
}
else if ( nref_mf==0 )
{
if ( nals_mf==1 )
gt_prior = 0.995 * 0.002 * (3.0/5.0) * (1.0/5.0); // 1 alt allele in the trio
else if ( nals_mf==2 )
{
assert( ca!=0 && cb!=0 );
gt_prior = 0.002 * 0.002 / 414; // 2 alt alleles and 0 refs in the trio
}
else
error("Fixme: %s:%d\n",__FILE__,__LINE__);
}
else
error("Fixme: %s:%d\n",__FILE__,__LINE__);
return gt_prior;
}
// Parent genotype probability L(GM,GF), with DNG bugs fixed
static double init_mf_priors(args_t *args, int fi, int mi)
{
double gt_prior = 0; // parent genotype probability L(GM,GF)
int fa = seq1[fi];
int fb = seq2[fi];
int ma = seq1[mi];
int mb = seq2[mi];
int gts[3]; gts[0] = fi; gts[1] = mi; gts[2] = 0;
int nalt_mf = count_unique_alleles(2,gts,only_alts);
int nref_mf = (fa==0 ? 1 : 0) + (fb==0 ? 1 : 0) + (ma==0 ? 1 : 0) + (mb==0 ? 1 : 0);
const double p_homref = 0.998; // this assumes bi-allelic sites
const double p_poly = (1 - p_homref) * (1 - p_homref); // p of this occurring twice for a different allele
const double p_nonref = 1 - p_homref - p_poly;
if ( nalt_mf>=3 ) // penalize heavily sites with 3 unique ALTs
gt_prior = 1e-26;
else if ( nalt_mf>=2 ) // 2 unique ALTs, 19*3 = 57 cases
gt_prior = p_poly / 57.;
else if ( nref_mf==4 ) // 0 ALTs; 00,00
gt_prior = p_homref;
else if ( nref_mf==3 ) // this and all remaining have 1 unique ALT allele; 00,0x
gt_prior = p_nonref * (4.0/15.0) * (1.0/3.0);
else if ( nref_mf==2 && ma==mb ) // hom alt; 00,xx
gt_prior = p_nonref * (2.0/15.0) * (1.0/3.0);
else if ( nref_mf==2 ) // two hets; 0x,0x
gt_prior = p_nonref * (4.0/15.0) * (1.0/3.0);
else if ( nref_mf==1 ) // single ref; 0x,xx
gt_prior = p_nonref * (4.0/15.0) * (1.0/3.0);
else if ( nref_mf==0 ) // no ref; xx,xx
gt_prior = p_nonref * (1.0/15.0) * (1.0/3.0);
else
error("Fixme: %s:%d\n",__FILE__,__LINE__);
return gt_prior;
}
static double init_mf_priors_chrX(args_t *args, int mi)
{
double gt_prior = 0; // parent genotype probability L(GM)
int ma = seq1[mi];
int mb = seq2[mi];
int gts[3]; gts[0] = mi; gts[1] = 0; gts[2] = 0;
int nalt_m = count_unique_alleles(1,gts,only_alts);
int nref_m = (ma==0 ? 1 : 0) + (mb==0 ? 1 : 0);
const double p_homref = 0.999; // this assumes bi-allelic sites
const double p_poly = (1 - p_homref) * (1 - p_homref); // p of this occurring twice for a different allele
const double p_nonref = 1 - p_homref - p_poly;
if ( nalt_m>=2 ) // 2 unique ALTs, 3 cases
gt_prior = p_poly / 3.;
else if ( nref_m==2 ) // 00
gt_prior = p_homref;
else if ( nref_m==1 ) // single ref; 0x and x0
gt_prior = p_nonref * (2.0/3.0) * (1.0/3.0);
else if ( nref_m==0 ) // no ref; xx,xx
gt_prior = p_nonref * (1.0/3.0) * (1.0/3.0);
else
error("Fixme: %s:%d\n",__FILE__,__LINE__);
return gt_prior;
}
static double init_mf_priors_chrXX(args_t *args, int fi, int mi)
{
double gt_prior = 0; // parent genotype probability L(GM)
int fa = seq1[fi];
int fb = seq2[fi];
int ma = seq1[mi];
int mb = seq2[mi];
int gts[3]; gts[0] = fi; gts[1] = mi; gts[2] = 0;
int nalt_mf = count_unique_alleles(2,gts,only_alts);
int nref_mf = (fa==0 ? 1 : 0) + (fb==0 ? 1 : 0) + (ma==0 ? 1 : 0) + (mb==0 ? 1 : 0);
if ( fa!=fb ) return 0; // father can't be a het
if ( fa==0 ) nref_mf--;
else nalt_mf--;
const double p_homref = 0.998; // this assumes bi-allelic sites
const double p_poly = (1 - p_homref) * (1 - p_homref); // p of this occurring twice for a different allele
const double p_nonref = 1 - p_homref - p_poly;
if ( nalt_mf>=3 ) // 3 unique ALTs
gt_prior = 1e-26;
else if ( nalt_mf>=2 ) // 2 unique ALTs
gt_prior = p_poly * (1.0/9.0) * (1.0/3.0);
else if ( nref_mf==3 ) // 00,0
gt_prior = p_homref;
else if ( nref_mf==2 ) // 00,x; 0x,0; x0,0
gt_prior = p_nonref * (3.0/7.0) * (1.0/3.0);
else if ( nref_mf==1 ) // 0x,x; x0,x; xx,0
gt_prior = p_nonref * (3.0/7.0) * (1.0/3.0);
else if ( nref_mf==0 ) // no ref; xx,x
gt_prior = p_nonref * (1.0/7.0) * (1.0/3.0);
else
error("Fixme: %s:%d\n",__FILE__,__LINE__);
return gt_prior;
}
static void init_DNG_tprob_mprob(args_t *args, int fi, int mi, int ci, double *tprob, double *mprob, int *denovo_allele)
{
int fa = seq1[fi];
int fb = seq2[fi];
int ma = seq1[mi];
int mb = seq2[mi];
int gts[3]; gts[0] = fi; gts[1] = mi; gts[2] = 0;
int ca = seq1[ci];
int cb = seq2[ci];
gts[0] = fi; gts[1] = mi; gts[2] = ci;
int nals_mfc = count_unique_alleles(3,gts,include_ref);
*tprob = 1; // genotype transmission likelihood L(GC|GM,GF), 0 if not compatible with Mendelian inheritance
*mprob = 1 - args->mrate; // probability of mutation
*denovo_allele = ca!=fa && ca!=fb && ca!=ma && ca!=mb ? ca : cb;
if ( nals_mfc==4 )
*tprob = 0; // 4 unique alleles
else if ( nals_mfc==3 ) // 3 alleles
{
if ( ((ca==fa || ca==fb) && (cb==ma || cb==mb)) ||
((cb==fa || cb==fb) && (ca==ma || ca==mb)) )
{
if ( ca==cb ) *tprob = 0.25;
else if ( fa==fb || ma==mb ) *tprob = 0.5; // one parent is homozygous
else *tprob = 0.25;
}
else
{
if ( ca!=fa && ca!=fb && ca!=ma && ca!=mb &&
cb!=fa && cb!=fb && cb!=ma && cb!=mb ) *mprob = args->mrate * args->mrate; // two mutations
else
*mprob = args->mrate;
*tprob = 0;
}
}
else if ( nals_mfc==2 ) // 2 alleles
{
if ( fa!=fb && ma!=mb ) *tprob = 0.25; // both parents are hets
else if ( fa==fb && ma==mb ) // both parents are homs
{
if ( fa==ma && ca==cb ) *tprob = 0, *mprob = args->mrate * args->mrate; // parents same homs, child a hom, two alleles mutated
else if ( fa==ma ) *tprob = 0, *mprob = args->mrate; // parents same homs, child a het, one allele mutated
else if ( ca==cb ) *tprob = 0, *mprob = args->mrate; // parents diff homs, child a hom, one allele mutated
}
else if ( ca==cb && ((fa==fb && fa!=ca) || (ma==mb && ma!=ca)) )
*tprob = 0, *mprob = args->mrate; // child is (wrong) hom and one parent is hom
else
*tprob = 0.5;
}
}
static void init_tprob_mprob(args_t *args, int fi, int mi, int ci, double *tprob, double *mprob, int *denovo_allele)
{
int fa = seq1[fi];
int fb = seq2[fi];
int ma = seq1[mi];
int mb = seq2[mi];
int ca = seq1[ci];
int cb = seq2[ci];
*denovo_allele = ca!=fa && ca!=fb && ca!=ma && ca!=mb ? ca : cb;
// tprob .. genotype transmission probability L(GC|GM,GF), 0 if not compatible with Mendelian inheritance
// mprob .. probability of mutation
int is_novel;
if ( args->strictly_novel )
{
// account for LoH sites, see trio-dnm.11.vcf
// chr1:10000057 child=1/1 father=1/1 mother=0/0 .. LoH region
// chr1:10697377 child=0/1 father=1/1 mother=1/1 .. usually these are indel ambiguities
is_novel = ( (ca!=fa && ca!=fb && ca!=ma && ca!=mb) || (cb!=fa && cb!=fb && cb!=ma && cb!=mb) ) ? 1 : 0;
if ( is_novel && *denovo_allele==0 ) is_novel = 0;
}
else
{
is_novel = ( ((ca==fa||ca==fb) && (cb==ma||cb==mb)) || ((ca==ma||ca==mb) && (cb==fa||cb==fb)) ) ? 0 : 1;
}
if ( !is_novel )
{
if ( fa==fb && ma==mb ) *tprob = 1;
else if ( fa==fb || ma==mb ) *tprob = 0.5;
else *tprob = 0.25;
*mprob = 1 - args->mrate;
}
else
{
*tprob = 0;
if ( (ca==fa||ca==fb) || (ca==ma||ca==mb) || (cb==fa||cb==fb) || (cb==ma||cb==mb) ) *mprob = args->mrate;
else *mprob = args->mrate * args->mrate;
}
}
static void init_tprob_mprob_chrX(args_t *args, int mi, int ci, double *tprob, double *mprob, int *denovo_allele)
{
int ma = seq1[mi];
int mb = seq2[mi];
int ca = seq1[ci];
int cb = seq2[ci];
*denovo_allele = ca!=ma && ca!=mb ? ca : cb;
if ( ca!=cb ) // male cannot be heterozygous in X
*mprob = 0, *tprob = 0;
else if ( ca==ma || ca==mb ) // inherited
{
if ( ma==mb ) *tprob = 1;
else *tprob = 0.5;
*mprob = 1 - args->mrate;
}
else // de novo
*mprob = args->mrate, *tprob = 0;
}
static void init_tprob_mprob_chrXX(args_t *args, int fi, int mi, int ci, double *tprob, double *mprob, int *denovo_allele)
{
int fa = seq1[fi];
int fb = seq2[fi];
int ma = seq1[mi];
int mb = seq2[mi];
int ca = seq1[ci];
int cb = seq2[ci];
*denovo_allele = ca!=fa && ca!=fb && ca!=ma && ca!=mb ? ca : cb;
if ( fa!=fb )
{
// this must be a genotype error, father cannot be heterozygous in X; don't flag it as a DNM unless
// also autosomal inheritance fails
init_tprob_mprob(args,fi,mi,ci,tprob,mprob,denovo_allele);
}
else if ( (ca==fa && (cb==ma||cb==mb)) || (cb==fa && (ca==ma||ca==mb)) )
{
if ( ma==mb ) *tprob = 1;
else *tprob = 0.5;
*mprob = 1 - args->mrate;
}
else
{
*tprob = 0;
if ( (ca==fa || (ca==ma||ca==mb)) || (cb==fa || (cb==ma||cb==mb)) ) *mprob = args->mrate, *tprob = 0;
else *mprob = args->mrate * args->mrate;
}
}
typedef enum { autosomal, chrX, chrXX } init_priors_t;
static void init_priors(args_t *args, priors_t *priors, init_priors_t type)
{
// Based on the FIGL model from the supplement "Variation in genome-wide mutation rates within and between human families"
int fi,mi,ci;
for (fi=0; fi<10; fi++)
{
for (mi=0; mi<10; mi++)
{
for (ci=0; ci<10; ci++)
{
double gt_prior; // parent genotype probability L(GM,GF)
double tprob; // genotype transmission likelihood L(GC|GM,GF), 0 if not compatible with Mendelian inheritance
double mprob; // probability of mutation
int allele; // which of the alleles is de novo
if ( args->use_dng_priors )
gt_prior = init_DNG_mf_priors(args,fi,mi,ci);
else if ( type==autosomal )
gt_prior = init_mf_priors(args,fi,mi);
else if ( type==chrX )
gt_prior = init_mf_priors_chrX(args,mi);
else if ( type==chrXX )
gt_prior = init_mf_priors_chrXX(args,fi,mi);
else
error("Can't happen\n");
if ( args->use_dng_priors )
init_DNG_tprob_mprob(args,fi,mi,ci,&tprob,&mprob,&allele);
else if ( type==autosomal || args->strictly_novel )
init_tprob_mprob(args,fi,mi,ci,&tprob,&mprob,&allele);
else if ( type==chrX )
init_tprob_mprob_chrX(args,mi,ci,&tprob,&mprob,&allele);
else if ( type==chrXX )
init_tprob_mprob_chrXX(args,fi,mi,ci,&tprob,&mprob,&allele);
else
error("Can't happen\n");
priors->denovo_allele[fi][mi][ci] = tprob==0 ? allele : UINT8_MAX; // the latter should never happen, making it fail deliberately
priors->denovo[fi][mi][ci] = tprob==0 ? 1 : 0;
priors->pprob[fi][mi][ci] = log(gt_prior * mprob * (tprob==0 ? 1 : tprob));
}
}
}
}
static void init_data(args_t *args)
{
if ( !args->dnm_score_tag )
{
if ( args->use_model==USE_NAIVE ) args->dnm_score_tag = strdup("DNM:flag");
else args->dnm_score_tag = strdup("DNM:log");
}
char *ptr = strchr(args->dnm_score_tag,':');
if ( ptr )
{
if ( ptr==args->dnm_score_tag ) error("Error: could not parse --dnm-tag %s\n",ptr);
*ptr = 0;
if ( !strcasecmp(ptr+1,"log") ) args->dnm_score_type = DNM_LOG;
else if ( !strcasecmp(ptr+1,"phred") ) args->dnm_score_type = DNM_PHRED;
else if ( !strcasecmp(ptr+1,"prob") ) args->dnm_score_type = DNM_PROB;
else if ( !strcasecmp(ptr+1,"flag") ) args->dnm_score_type = DNM_FLAG;
else error("Error: the type \"%s\" is not supported\n",ptr+1);
}
else
args->dnm_score_type = DNM_LOG;
if ( args->dnm_score_type==DNM_FLAG )
{
if ( !args->use_model ) args->use_model = USE_NAIVE;
else if ( args->use_model!=USE_NAIVE ) error("The output type FLAG can be used only with --use-NAIVE\n");
}
if ( args->use_model==USE_NAIVE )
{
if ( !args->dnm_score_type ) args->dnm_score_type = DNM_FLAG;
else if ( args->dnm_score_type!=DNM_FLAG ) error("The output type FLAG is required with --use-NAIVE\n");
}
if ( !args->use_model ) args->use_model = USE_ACM;
args->sr = bcf_sr_init();
if ( args->regions )
{
args->sr->require_index = 1;
bcf_sr_set_opt(args->sr,BCF_SR_REGIONS_OVERLAP,args->regions_overlap);
if ( bcf_sr_set_regions(args->sr, args->regions, args->regions_is_file)<0 ) error("Failed to read the regions: %s\n",args->regions);
}
if ( args->targets )
{
bcf_sr_set_opt(args->sr,BCF_SR_TARGETS_OVERLAP,args->targets_overlap);
if ( bcf_sr_set_targets(args->sr, args->targets, args->targets_is_file, 0)<0 ) error("Failed to read the targets: %s\n",args->targets);
}
if ( !bcf_sr_add_reader(args->sr,args->fname) ) error("Error: %s\n", bcf_sr_strerror(args->sr->errnum));
args->hdr = bcf_sr_get_header(args->sr,0);
if ( args->filter_str ) args->filter = filter_init(args->hdr, args->filter_str);
int id;
if ( args->use_model==USE_NAIVE )
{
if ( (id=bcf_hdr_id2int(args->hdr, BCF_DT_ID, "GT"))<0 || !bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FMT,id) )
error("Error: the tag FORMAT/GT is not present in %s\n", args->fname);
}
else if ( (id=bcf_hdr_id2int(args->hdr, BCF_DT_ID, "PL"))<0 || !bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FMT,id) )
error("Error: the tag FORMAT/PL is not present in %s\n", args->fname);
args->need_QS = ( args->use_model==USE_ACM && !args->with_ppl && !args->with_pad ) ? 1 : 0;
if ( args->need_QS && ((id=bcf_hdr_id2int(args->hdr, BCF_DT_ID, "QS"))<0 || !bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FMT,id)) )
error(
"Error:\n"
" The FORMAT/QS tag is not present. If you want to proceed anyway, add either `--with-pAD` or\n"
" `--with-pPL` option, the latter at the cost of inflated false discovery rate. The QS annotation\n"
" can be generated at the mpileup step together with the AD annotation using the command\n"
" bcftools mpileup -a AD,QS -f ref.fa file.bam\n"); // Possible future todo: use AD as a proxy for QS?
if ( args->use_model!=USE_NAIVE )
{
if ( (id=bcf_hdr_id2int(args->hdr, BCF_DT_ID, "AD"))<0 || !bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FMT,id) )
fprintf(stderr, "Warning: the tag FORMAT/AD is not present in %s, the output tag FORMAT/VAF will not be added\n", args->fname);
else
args->has_fmt_ad = 1;
if ( args->with_pad && !args->has_fmt_ad )
error("Error: no FORMAT/AD is present in %s, cannot run with --with-pAD\n", args->fname);
}
init_priors(args,&args->priors,autosomal);
init_priors(args,&args->priors_X,chrX);
init_priors(args,&args->priors_XX,chrXX);
args->hdr_out = bcf_hdr_dup(args->hdr);
char *type = NULL;
if ( args->dnm_score_type==DNM_LOG ) type = "log scaled value (bigger value = bigger confidence)";
if ( args->dnm_score_type==DNM_PHRED ) type = "phred value (bigger value = bigger confidence)";
if ( args->dnm_score_type==DNM_PROB ) type = "probability";
if ( args->dnm_score_type==DNM_FLAG ) type = "1 for Mendelian-incompatible genotypes";
bcf_hdr_printf(args->hdr_out, "##FORMAT=<ID=%s,Number=1,Type=%s,Description=\"De-novo mutation score given as %s\">",args->dnm_score_tag,(args->dnm_score_type&DNM_INT)?"Integer":"Float",type);
bcf_hdr_printf(args->hdr_out, "##FORMAT=<ID=%s,Number=1,Type=Integer,Description=\"The de-novo allele\">",args->dnm_allele_tag);
if ( args->has_fmt_ad )
bcf_hdr_printf(args->hdr_out, "##FORMAT=<ID=%s,Number=1,Type=Integer,Description=\"The percentage of ALT reads\">",args->dnm_vaf_tag);
int i, n = 0;
char **list;
if ( args->pfm )
{
args->ntrio = 1;
args->trio = (trio_t*) calloc(1,sizeof(trio_t));
list = hts_readlist(args->pfm, 0, &n);
if ( n!=3 ) error("Expected three sample names with -t\n");
args->trio[0].idx[iCHILD] = bcf_hdr_id2int(args->hdr, BCF_DT_SAMPLE, list[0]);
args->trio[0].idx[iFATHER] = bcf_hdr_id2int(args->hdr, BCF_DT_SAMPLE, list[1]);
args->trio[0].idx[iMOTHER] = bcf_hdr_id2int(args->hdr, BCF_DT_SAMPLE, list[2]);
if ( args->trio[0].idx[iCHILD] < 0 )
{
if ( strlen(list[0])>3 && !strncasecmp(list[0],"1X:",3) )
{
args->trio[0].idx[iCHILD] = bcf_hdr_id2int(args->hdr, BCF_DT_SAMPLE, list[0]+3);
args->trio[0].is_male = 1;
}
else if ( strlen(list[0])>3 && !strncasecmp(list[0],"2X:",3) )
args->trio[0].idx[iCHILD] = bcf_hdr_id2int(args->hdr, BCF_DT_SAMPLE, list[0]+3);
}
for (i=0; i<n; i++)
{
if ( args->trio[0].idx[i] < 0 ) error("The sample is not present: %s\n", list[i]);
free(list[i]);
}
free(list);
}
else
{
parse_ped(args,args->ped_fname);
if ( !args->ntrio ) error("No complete trio present\n");
}
if ( !args->chrX_list_str || !strcasecmp("GRCh37",args->chrX_list_str) )
args->chrX_list_str = "X:1-60000,chrX:1-60000,X:2699521-154931043,chrX:2699521-154931043";
else if ( !strcasecmp("GRCh38",args->chrX_list_str) )
args->chrX_list_str = "X:1-9999,chrX:1-9999,X:2781480-155701381,chrX:2781480-155701381";
char *rmme = strdup(args->chrX_list_str), *tmp = rmme;
while ( *tmp )
{
if ( *tmp==',' ) *tmp = '\n';
tmp++;
}
args->chrX_idx = regidx_init_string(rmme, regidx_parse_reg, NULL, 0, NULL);
free(rmme);
if ( args->record_cmd_line )
bcf_hdr_append_version(args->hdr_out, args->argc, args->argv, "bcftools_trio-dnm2");
char wmode[8];
set_wmode(wmode,args->output_type,args->output_fname,args->clevel);
args->out_fh = hts_open(args->output_fname ? args->output_fname : "-", wmode);
if ( args->out_fh == NULL ) error("Can't write to \"%s\": %s\n", args->output_fname, strerror(errno));
if ( bcf_hdr_write(args->out_fh, args->hdr_out)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname);
if ( init_index2(args->out_fh,args->hdr_out,args->output_fname,
&args->index_fn, args->write_index)<0 )
error("Error: failed to initialise index for %s\n",args->output_fname);
if ( args->dnm_score_type & DNM_FLOAT )
args->dnm_qual_float = (float*) malloc(sizeof(*args->dnm_qual_float)*bcf_hdr_nsamples(args->hdr));
else
args->dnm_qual_int = (int32_t*) malloc(sizeof(*args->dnm_qual_int)*bcf_hdr_nsamples(args->hdr));
args->vaf = (int32_t*) malloc(sizeof(*args->vaf)*bcf_hdr_nsamples(args->hdr));
args->dnm_allele = (int32_t*) malloc(sizeof(*args->dnm_allele)*bcf_hdr_nsamples(args->hdr));
}
static void destroy_data(args_t *args)
{
if ( args->filter ) filter_destroy(args->filter);
regidx_destroy(args->chrX_idx);
free(args->dnm_score_tag);
free(args->dnm_vaf_tag);
free(args->dnm_allele_tag);
free(args->pl3);
free(args->alt_tmp);
free(args->alt_idx);
free(args->idx);
free(args->dnm_qual_int);
free(args->dnm_qual_float);
free(args->dnm_allele);
free(args->vaf);
free(args->trio);
free(args->gt);
free(args->pl);
free(args->ad);
free(args->qs);
free(args->qs3);
if ( args->write_index )
{
if ( bcf_idx_save(args->out_fh)<0 )
{
if ( hts_close(args->out_fh)!=0 ) error("Error: close failed .. %s\n", args->output_fname?args->output_fname:"stdout");
error("Error: cannot write to index %s\n", args->index_fn);
}
free(args->index_fn);
}
if ( hts_close(args->out_fh)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->output_fname);
bcf_hdr_destroy(args->hdr_out);
bcf_sr_destroy(args->sr);
free(args);
}
static inline double phred2num(double phred)
{
return pow(10,-0.1*phred);
}
static inline double log2phred(double num)
{
return fabs(4.3429 * num);
}
static inline double phred2log(double phred)
{
return -phred/4.3429;
}
#if 0
static inline double subtract_num_log(double a_num, double b_log)
{
return log(a_num - exp(b_log));
}
#endif
static inline double subtract_log(double a_log, double b_log)
{
return a_log + log(1 - exp(b_log - a_log));
}
static inline double sum_log(double a, double b) // log(exp(a)+exp(b))
{
if ( a==-HUGE_VAL && b==-HUGE_VAL ) return -HUGE_VAL;
if ( a>b )
return log(1 + exp(b-a)) + a;
else
return log(1 + exp(a-b)) + b;
}
static double process_trio_ACM(args_t *args, priors_t *priors, int nals, double *pl[3], int npl, double *qs[3], int *al0, int *al1)
{
assert( nals>1 && nals<=4 );
*al0 = *al1 = 0;
double sum = -HUGE_VAL, max = -HUGE_VAL;
int i, ca,cb, fa,fb, ma,mb, ci=0;
for (ca=0; ca<nals; ca++)
{
for (cb=0; cb<=ca; cb++)
{
int cals = (1<<ca)|(1<<cb);
double cpl = pl[iCHILD][ci];
int fi = 0;
for (fa=0; fa<nals; fa++)
{
for (fb=0; fb<=fa; fb++)
{
int fals = (1<<fa)|(1<<fb);
double fpl;
if ( args->with_ppl ) fpl = pl[iFATHER][fi];
else
{
fpl = 0;
for (i=0; i<nals; i++)
{
if ( fals&(1<<i) )
fpl += subtract_log(0,qs[iFATHER][i]);
else if ( cals&(1<<i) )
fpl += qs[iFATHER][i];
else if ( fa==fb )
fpl += qs[iFATHER][i];
}
}
int mi = 0;
for (ma=0; ma<nals; ma++)
{
for (mb=0; mb<=ma; mb++)
{
int mals = (1<<ma)|(1<<mb);
double mpl = 0;
if ( args->with_ppl ) mpl = pl[iMOTHER][mi];
else
{
mpl = 0;
for (i=0; i<nals; i++)
{
if ( mals&(1<<i) )
mpl += subtract_log(0,qs[iMOTHER][i]);
else if ( cals&(1<<i) )
mpl += qs[iMOTHER][i];
else if ( ma==mb )
mpl += qs[iMOTHER][i];
}
}
double val = cpl + fpl + mpl + priors->pprob[fi][mi][ci];
sum = sum_log(sum,val);
#define DEBUG 0
#if DEBUG
if(val!=-HUGE_VAL)
fprintf(stderr,"m,f,c: %d%d+%d%d=%d%d dn=%d (%d,%d,%d) mpl,fpl,cpl: %+e %+e %+e \t prior:%+e \t pval=%+e sum=%+e %c\n",
mb,ma,fb,fa,cb,ca,priors->denovo[fi][mi][ci],fi,mi,ci,mpl,fpl,cpl,priors->pprob[fi][mi][ci], val,sum,(priors->denovo[fi][mi][ci] && max < val)?'*':'-');
#endif
if ( priors->denovo[fi][mi][ci] && max < val )
{
max = val;
if ( priors->denovo_allele[fi][mi][ci] == ca )
*al0 = cb, *al1 = ca;
else
*al0 = ca, *al1 = cb;
}
mi++;
}
}
fi++;
}
}
ci++;
}
}
#if DEBUG
fprintf(stderr,"max=%e sum=%e ret=%e\n",max,sum,max-sum);
#endif
return log2phred(subtract_log(0,max-sum));
}
static double process_trio_DNG(args_t *args, priors_t *priors, int nals, double *pl[3], int npl, int *al0, int *al1)
{
assert( nals>1 && nals<=4 );
*al0 = *al1 = 0;
double sum = -HUGE_VAL, max = -HUGE_VAL;
int ca,cb, fa,fb, ma,mb, ci=0;
for (ca=0; ca<nals; ca++)
{
for (cb=0; cb<=ca; cb++)
{
int fi = 0;
for (fa=0; fa<nals; fa++)
{
for (fb=0; fb<=fa; fb++)
{
int mi = 0;
for (ma=0; ma<nals; ma++)
{
for (mb=0; mb<=ma; mb++)
{
double val;
val = pl[iCHILD][ci] + pl[iFATHER][fi] + pl[iMOTHER][mi] + priors->pprob[fi][mi][ci];
sum = sum_log(val,sum);
#if DEBUG
if(val!=-HUGE_VAL)
fprintf(stderr,"m,f,c: %d%d+%d%d=%d%d dn=%d (%d,%d,%d) mpl,fpl,cpl: %+e %+e %+e \t prior:%+e \t pval=%+e sum=%+e %c\n",
mb,ma,fb,fa,cb,ca,priors->denovo[fi][mi][ci],fi,mi,ci,pl[iMOTHER][mi],pl[iFATHER][fi],pl[iCHILD][ci],priors->pprob[fi][mi][ci], val,sum,(priors->denovo[fi][mi][ci] && max < val)?'*':'-');
#endif
if ( priors->denovo[fi][mi][ci] && max < val )
{
max = val;
if ( priors->denovo_allele[fi][mi][ci] == ca )
*al0 = cb, *al1 = ca;
else
*al0 = ca, *al1 = cb;
}
mi++;
}
}
fi++;
}
}
ci++;
}
}
#if DEBUG
fprintf(stderr,"max=%e sum=%e ret=%e\n",max,sum,max-sum);
#endif
return log2phred(subtract_log(0,max-sum));
}
static int process_trio_naive(args_t *args, priors_t *priors, int nals, int32_t gts[3], int *denovo_allele)
{
int fi = seq3[gts[iFATHER]];
int mi = seq3[gts[iMOTHER]];
int ci = seq3[gts[iCHILD]];