-
Notifications
You must be signed in to change notification settings - Fork 1
/
novoCallerBAM.py
812 lines (619 loc) · 19.1 KB
/
novoCallerBAM.py
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
import pysam
import numpy as np
import sys
import os
def GT_ordering_alternate(ALT_count):
combos=(ALT_count+1)*(ALT_count+2)/2
ordering=np.empty([combos,2])
count=0
for a1 in range(0,ALT_count+1):
for a2 in range(a1,ALT_count+1):
ordering[count,0]=a1
ordering[count,1]=a2
count=count+1
return ordering
def row_gen(GT1,GT2,alt_count,mut_rate):
N=alt_count
combos=(N+1)*(N+2)/2
row=np.zeros(combos)
count=0
for a1 in range(N+1):
for a2 in range(N+1):
for a3 in range(N+1):
for a4 in range(N+1):
P=1.0
if a1==GT1[0]:
P=P*(1-mut_rate)
else:
P=P*mut_rate/N
if a2==GT1[1]:
P=P*(1-mut_rate)
else:
P=P*mut_rate/N
if a3==GT2[0]:
P=P*(1-mut_rate)
else:
P=P*mut_rate/N
if a4==GT2[1]:
P=P*(1-mut_rate)
else:
P=P*mut_rate/N
count+=1
for b1 in [a1,a2]:
for b2 in [a3,a4]:
gt_work=np.sort([b1,b2])
index=(2*N+3-gt_work[0])*gt_work[0]/2+gt_work[1]-gt_work[0]
row[index]=row[index]+0.25*P
return row
def table_gen(alt_count,mut_rate):
N=alt_count
II_prev=-1
combos=(N+1)*(N+2)/2
table=np.zeros([combos**2,combos])
for a1 in range(N+1):
for a2 in range(a1,N+1):
for a3 in range(N+1):
for a4 in range(a3,N+1):
GT1=[a1,a2]
GT2=[a3,a4]
I1=(2*N+3-GT1[0])*GT1[0]/2+GT1[1]-GT1[0]
I2=(2*N+3-GT2[0])*GT2[0]/2+GT2[1]-GT2[0]
II=I1*combos+I2
if II<=II_prev:
print "error in II calc!!!"
print II_prev,II
row=row_gen(GT1,GT2,alt_count,mut_rate)
table[II,:]=row
return table
def GT_likelihood_wrt_allele_calc(ALT_count):
ordering=GT_ordering_alternate(ALT_count)
combos=(ALT_count+1)*(ALT_count+2)/2
table=np.zeros([combos,ALT_count+1])*1.
for i in range(combos):
a1=ordering[i,0]
a2=ordering[i,1]
table[i,a1]=table[i,a1]+0.5
table[i,a2]=table[i,a2]+0.5
return table
def chr_calc(str_val):
if str_val[0:3]=="chr":
str_val=str_val[3:len(str_val)]
chrom=-1
rec={"M":0,"X":23,"Y":24}
try:
chrom=rec[str_val]
except:
try:
chrom=int(str_val)
except:
chrom=25
return chrom
def get_sam_file_list(list_of_filenames):
buff=open(list_of_filenames)
line=buff.readline()
samfile_list=[]
while len(line)>1:
split_line=line.split()
filename=split_line[0]
samfile = pysam.AlignmentFile(filename, "rb" )
samfile_list.append(samfile)
line=buff.readline()
return samfile_list
def get_ADs(samfile,chrom,position_actual,REF,MQ_thresh,BQ_thresh):
position=position_actual-1
ADf=np.array([0.,0])
ADr=np.array([0.,0])
if chrom==0:
CC="M"
if chrom>0 and chrom<=22:
CC=str(chrom)
if chrom==23:
CC="X"
if chrom==24:
CC="Y"
SP=samfile.pileup("chr"+CC, position, position+1)
try:
if chrom==0:
CC="M"
if chrom>0 and chrom<=22:
CC=str(chrom)
if chrom==23:
CC="X"
if chrom==24:
CC="Y"
SP=samfile.pileup("chr"+CC, position, position+1)
except ValueError:
if chrom==0:
CC="MT"
if chrom>0 and chrom<=22:
CC=str(chrom)
if chrom==23:
CC="X"
if chrom==24:
CC="Y"
SP=samfile.pileup(CC, position, position+1)
for pileupcolumn in SP:
if(pileupcolumn.pos==position):
for pileupread in pileupcolumn.pileups:
if not pileupread.is_del and not pileupread.is_refskip :
MQ=pileupread.alignment.mapping_quality
BQ=pileupread.alignment.query_qualities[pileupread.query_position]
if MQ>=MQ_thresh and BQ>=BQ_thresh:
if pileupread.alignment.query_sequence[pileupread.query_position].upper()==REF.upper():
if pileupread.alignment.is_reverse:
ADr[0]=ADr[0]+1
else:
ADf[0]=ADf[0]+1
else:
if pileupread.alignment.is_reverse:
ADr[1]=ADr[1]+1
else:
ADf[1]=ADf[1]+1
return ADf,ADr
def get_ADs_deletion(samfile,chrom,position_actual,REF_0,MQ_thresh,BQ_thresh):
REF=REF_0
position=position_actual-1
ADf=np.array([0.,0])
ADr=np.array([0.,0])
try:
if chrom==0:
CC="M"
if chrom>0 and chrom<=22:
CC=str(chrom)
if chrom==23:
CC="X"
if chrom==24:
CC="Y"
SP=samfile.pileup("chr"+CC, position, position+1)
except ValueError:
if chrom==0:
CC="MT"
if chrom>0 and chrom<=22:
CC=str(chrom)
if chrom==23:
CC="X"
if chrom==24:
CC="Y"
SP=samfile.pileup(CC, position, position+1)
for pileupcolumn in SP:
if(pileupcolumn.pos==position):
for pileupread in pileupcolumn.pileups:
if not pileupread.is_del and not pileupread.is_refskip:
MQ=pileupread.alignment.mapping_quality
BQ=pileupread.alignment.query_qualities[pileupread.query_position]
if MQ>=MQ_thresh and BQ>=BQ_thresh:
indel_val=pileupread.indel
if pileupread.alignment.query_sequence[pileupread.query_position].upper()==REF.upper() and indel_val>=0:
if pileupread.alignment.is_reverse:
ADr[0]=ADr[0]+1
else:
ADf[0]=ADf[0]+1
else:
if pileupread.alignment.query_sequence[pileupread.query_position].upper()==REF.upper() and indel_val<0:
if pileupread.alignment.is_reverse:
ADr[1]=ADr[1]+1
else:
ADf[1]=ADf[1]+1
return ADf,ADr
def get_ADs_insertion(samfile,chrom,position_actual,REF_0,MQ_thresh,BQ_thresh):
REF=REF_0
position=position_actual-1
ADf=np.array([0.,0])
ADr=np.array([0.,0])
try:
if chrom==0:
CC="M"
if chrom>0 and chrom<=22:
CC=str(chrom)
if chrom==23:
CC="X"
if chrom==24:
CC="Y"
SP=samfile.pileup("chr"+CC, position, position+1)
except ValueError:
if chrom==0:
CC="MT"
if chrom>0 and chrom<=22:
CC=str(chrom)
if chrom==23:
CC="X"
if chrom==24:
CC="Y"
SP=samfile.pileup(CC, position, position+1)
for pileupcolumn in SP:
if(pileupcolumn.pos==position):
for pileupread in pileupcolumn.pileups:
if not pileupread.is_del and not pileupread.is_refskip:
MQ=pileupread.alignment.mapping_quality
BQ=pileupread.alignment.query_qualities[pileupread.query_position]
if MQ>=MQ_thresh and BQ>=BQ_thresh:
indel_val=pileupread.indel
if pileupread.alignment.query_sequence[pileupread.query_position].upper()==REF.upper() and indel_val<=0:
if pileupread.alignment.is_reverse:
ADr[0]=ADr[0]+1
else:
ADf[0]=ADf[0]+1
else:
if pileupread.alignment.query_sequence[pileupread.query_position].upper()==REF.upper() and indel_val>0:
if pileupread.alignment.is_reverse:
ADr[1]=ADr[1]+1
else:
ADf[1]=ADf[1]+1
return ADf,ADr
def get_ADs_combined(samfile,chrom,position_actual,REF,ALT,MQ_thresh,BQ_thresh):
split_ALT=ALT.split(",")
if len(split_ALT)>1:
ADf,ADr = get_ADs(samfile,chrom,position_actual,REF[0],MQ_thresh,BQ_thresh)
return ADf,ADr
if len(REF)>1:
ADf,ADr = get_ADs_deletion(samfile,chrom,position_actual,REF[0],MQ_thresh,BQ_thresh)
return ADf,ADr
if len(ALT)>1:
ADf,ADr = get_ADs_insertion(samfile,chrom,position_actual,REF[0],MQ_thresh,BQ_thresh)
return ADf,ADr
ADf,ADr = get_ADs(samfile,chrom,position_actual,REF[0],MQ_thresh,BQ_thresh)
return ADf,ADr
def get_all_ADs_combined(unrelated_samfiles,chrom,position_actual,REF,ALT,MQ_thresh,BQ_thresh):
ADfs=[]
ADrs=[]
for samfile in unrelated_samfiles:
ADf,ADr = get_ADs_combined(samfile,chrom,position_actual,REF,ALT,MQ_thresh,BQ_thresh)
ADfs.append(ADf)
ADrs.append(ADr)
ADfs=np.array(ADfs)
ADrs=np.array(ADrs)
return ADfs,ADrs
def M1_L_calc_aux(rho,k):
ALT_count=1
M1_L_k=np.zeros(ALT_count+1)
default=np.log((1.-rho)/ALT_count)
M1_L_k=M1_L_k+default
M1_L_k[k]=np.log(rho)
return M1_L_k
def M1_L_calc(AD,rho):
ALT_count=1
if AD.size-1 != ALT_count:
print "ERROR in M1_L_calc"
sys.exit()
M1_L=[]
for k in range(ALT_count+1):
M1_L_k=M1_L_calc_aux(rho,k)
M1_L.append(M1_L_k)
return M1_L
def M2_L_calc_aux(M1_L_k,GT_likelihood_wrt_allele_L):
ALT_count=1
if M1_L_k.size-1 != ALT_count:
print "ERROR in M2_L_calc_aux"
sys.exit()
combos=(ALT_count+1)*(ALT_count+2)/2
temp_table=GT_likelihood_wrt_allele_L+np.tile(M1_L_k.reshape([1,ALT_count+1]),[combos,1])
M2_L_k=np.zeros(combos)
for i in range(combos):
row=temp_table[i,:]
row_max=np.max(row)
row=row-row_max
M2_L_k[i]=np.log(np.sum(np.exp(row)))+row_max
return M2_L_k
def M2_L_calc(M1_L,GT_likelihood_wrt_allele_L):
ALT_count=1
if (M1_L[0]).size-1 != ALT_count:
print "ERROR in M2_L_calc"
sys.exit()
M2_L=[]
for k in range(ALT_count+1):
M1_L_k=M1_L[k]
M2_L_k=M2_L_calc_aux(M1_L_k,GT_likelihood_wrt_allele_L)
M2_L.append(M2_L_k)
return M2_L
def GT_marg_L_calc(M2_L_f,M2_L_r,ADf,ADr,prior_L):
GT_marg_L=prior_L
ALT_count=1
if ADf.size-1 != ALT_count or ADr.size-1 != ALT_count:
print "ERROR in GT_marg_L_calc"
sys.exit()
for k in range(ALT_count+1):
M2_L_k=M2_L_f[k]
GT_marg_L=GT_marg_L+ADf[k]*M2_L_k
for k in range(ALT_count+1):
M2_L_k=M2_L_r[k]
GT_marg_L=GT_marg_L+ADr[k]*M2_L_k
return GT_marg_L
def M3_L_calc_aux(GT_marg_L,M2_L_k):
M3_L_k=GT_marg_L-M2_L_k
return M3_L_k
def M3_L_calc(GT_marg_L,M2_L):
ALT_count=1
if len(M2_L)-1 != ALT_count:
print "ERROR in M3_L_calc"
sys.exit()
M3_L=[]
for k in range(ALT_count+1):
M2_L_k=M2_L[k]
M3_L_k=M3_L_calc_aux(GT_marg_L,M2_L_k)
M3_L.append(M3_L_k)
return M3_L
def M4_L_calc_aux(M3_L_k,GT_likelihood_wrt_allele_L):
ALT_count=1
if (GT_likelihood_wrt_allele_L.shape)[1]-1 != 1:
print "ERROR in M4_L_calc_aux"
sys.exit()
combos=(ALT_count+1)*(ALT_count+2)/2
temp_table=GT_likelihood_wrt_allele_L+np.tile(M3_L_k.reshape([combos,1]),[1,ALT_count+1])
M4_L_k=np.zeros(ALT_count+1)
for i in range(ALT_count+1):
column=temp_table[:,i]
column_max=np.max(column)
column=column-column_max
M4_L_k[i]=np.log(np.sum(np.exp(column)))+column_max
return M4_L_k
def M4_L_calc(M3_L,GT_likelihood_wrt_allele_L):
ALT_count=1
if (GT_likelihood_wrt_allele_L.shape)[1]-1 != ALT_count:
print "ERROR in M4_L_calc"
sys.exit()
M4_L=[]
for k in range(ALT_count+1):
M3_L_k=M3_L[k]
M4_L_k=M4_L_calc_aux(M3_L_k,GT_likelihood_wrt_allele_L)
M4_L.append(M4_L_k)
return M4_L
def A_marg_L_calc(M1_L,M4_L):
ALT_count=1
if len(M1_L)-1 != ALT_count:
print "ERROR in A_marg_L_calc"
sys.exit()
A_marg_L=[]
for k in range(ALT_count+1):
M1_L_k=M1_L[k]
M4_L_k=M4_L[k]
A_marg_L_k=M1_L_k+M4_L_k
A_marg_L.append(A_marg_L_k)
return A_marg_L
def T_term_calc_for_rho(A_marg_L,AD):
if len(A_marg_L)!=AD.size:
print "ERROR in T_term_calc"
sys.exit()
ALT_count=AD.size-1
T1_term=0.
T2_term=0.
for k in range(ALT_count+1):
A_marg_L_k=A_marg_L[k]
A_marg_temp=np.exp(A_marg_L_k-np.max(A_marg_L_k))
A_marg=A_marg_temp/np.sum(A_marg_temp)
T1_term=T1_term+A_marg[k]*AD[k]
T2_term=T2_term+(1.-A_marg[k])*AD[k]
return T1_term,T2_term
def GT_marg_L_to_GT_marg(GT_marg_L):
M=np.max(GT_marg_L)
GT_marg_L=GT_marg_L-M
GT_marg=np.exp(GT_marg_L)
S=np.sum(GT_marg)
GT_marg=GT_marg/S
joint_probty_term=np.log(S)+M
return GT_marg,joint_probty_term
def EM_step(ADf_list,ADr_list,rho_f_old,rho_r_old,prior_L_old,GT_likelihood_wrt_allele_L,a,b,D_original,allele_freq):
D=np.zeros(3)
D[0]=D_original[0]
D[1]=D_original[1]
D[2]=D_original[2]
if allele_freq<=0.:
AF=0.
else:
AF=allele_freq
f0=(1.-AF)**2.
f2=AF**2.
f1=1.-f0-f2
D=np.array([f0,f1,f2])*1000.+2.
T1_f=a-1.
T2_f=b-1.
T1_r=a-1.
T2_r=b-1.
T_for_prior=D-1.
joint_probty= (a-1.)*np.log(rho_f_old)+(b-1.)*np.log(1.-rho_f_old)
joint_probty=joint_probty+(a-1.)*np.log(rho_r_old)+(b-1.)*np.log(1.-rho_r_old)
for i in range(3):
joint_probty=joint_probty+(D[i]-1)*prior_L_old[i]
if len(ADf_list)!=len(ADr_list):
print "ERROR1 in EM_step"
sys.exit()
for i in range(len(ADf_list)):
ADf=ADf_list[i]
ADr=ADr_list[i]
M1_L_f = M1_L_calc(ADf,rho_f_old)
M1_L_r = M1_L_calc(ADr,rho_r_old)
M2_L_f = M2_L_calc(M1_L_f,GT_likelihood_wrt_allele_L)
M2_L_r = M2_L_calc(M1_L_r,GT_likelihood_wrt_allele_L)
GT_marg_L = GT_marg_L_calc(M2_L_f,M2_L_r,ADf,ADr,prior_L_old)
M3_L_f = M3_L_calc(GT_marg_L,M2_L_f)
M3_L_r = M3_L_calc(GT_marg_L,M2_L_r)
M4_L_f = M4_L_calc(M3_L_f,GT_likelihood_wrt_allele_L)
M4_L_r = M4_L_calc(M3_L_r,GT_likelihood_wrt_allele_L)
A_marg_L_f = A_marg_L_calc(M1_L_f,M4_L_f)
A_marg_L_r = A_marg_L_calc(M1_L_r,M4_L_r)
T1_term_f,T2_term_f = T_term_calc_for_rho(A_marg_L_f,ADf)
T1_term_r,T2_term_r = T_term_calc_for_rho(A_marg_L_r,ADr)
T1_f = T1_f + T1_term_f
T2_f = T2_f + T2_term_f
T1_r = T1_r + T1_term_r
T2_r = T2_r + T2_term_r
GT_marg,joint_probty_term = GT_marg_L_to_GT_marg(GT_marg_L)
joint_probty = joint_probty + joint_probty_term
T_for_prior = T_for_prior + GT_marg
rho_f_new=1./(1.+T2_f/T1_f)
rho_r_new=1./(1.+T2_r/T1_r)
prior_new=T_for_prior/np.sum(T_for_prior)
prior_L_new=np.log(prior_new)
return rho_f_new,rho_r_new,prior_L_new,joint_probty
def EM_full(ADfs,ADrs,rho_f_old,rho_r_old,prior_L_old,GT_likelihood_wrt_allele_L,a,b,D,allele_freq):
joint_probty_s=[]
joint_probty_new=np.nan
for i in range(3):
joint_probty_old=joint_probty_new
rho_f_new,rho_r_new,prior_L_new,joint_probty_new = EM_step(ADfs,ADrs,rho_f_old,rho_r_old,prior_L_old,GT_likelihood_wrt_allele_L,a,b,D,allele_freq)
rho_f_old=rho_f_new
rho_r_old=rho_r_new
prior_L_old=prior_L_new
joint_probty_s.append(joint_probty_new)
while np.abs(joint_probty_old-joint_probty_new)>10**-7:
joint_probty_old=joint_probty_new
rho_f_new,rho_r_new,prior_L_new,joint_probty_new = EM_step(ADfs,ADrs,rho_f_old,rho_r_old,prior_L_old,GT_likelihood_wrt_allele_L,a,b,D,allele_freq)
rho_f_old=rho_f_new
rho_r_old=rho_r_new
prior_L_old=prior_L_new
joint_probty_s.append(joint_probty_new)
return rho_f_new,rho_r_new,prior_L_new,joint_probty_s
def GTL_L_calc(ADf,ADr,rho_f,rho_r,GT_likelihood_wrt_allele_L):
M1_L_f = M1_L_calc(ADf,rho_f)
M1_L_r = M1_L_calc(ADr,rho_r)
M2_L_f = M2_L_calc(M1_L_f,GT_likelihood_wrt_allele_L)
M2_L_r = M2_L_calc(M1_L_r,GT_likelihood_wrt_allele_L)
prior_L = np.zeros(3)
GTL_L = GT_marg_L_calc(M2_L_f,M2_L_r,ADf,ADr,prior_L)
GTL_L=GTL_L-np.max(GTL_L)
return GTL_L
def posterior_probty_calc_exact(prior_L,table_L,C_GL_L,M_GL_L,D_GL_L):
combos=3
work_column=np.empty(combos**2)
for I1 in range(combos):
for I2 in range(combos):
II=I1*combos+I2
work_column[II]=prior_L[I1]+prior_L[I2]+M_GL_L[I1]+D_GL_L[I2]
work_table=table_L+np.tile(C_GL_L,[combos**2,1])+np.tile(np.reshape(work_column,[combos**2,1]),[1,combos])
work_table=work_table-np.max(work_table)
work_table=np.exp(work_table)
work_table=work_table/np.sum(work_table)
PP=np.max(np.array([work_table[0][1],work_table[0][2]]))
return PP,work_table
def denovo_P_calc(ADfs,ADrs,rho_f,rho_r,GT_likelihood_wrt_allele_L,table_L,prior_L):
M_GL_L=GTL_L_calc(ADfs[0],ADrs[0],rho_f,rho_r,GT_likelihood_wrt_allele_L)
D_GL_L=GTL_L_calc(ADfs[1],ADrs[1],rho_f,rho_r,GT_likelihood_wrt_allele_L)
C_GL_L=GTL_L_calc(ADfs[2],ADrs[2],rho_f,rho_r,GT_likelihood_wrt_allele_L)
PP,work_table = posterior_probty_calc_exact(prior_L,table_L,C_GL_L,M_GL_L,D_GL_L)
return PP,work_table
def PP_calc(trio_samfiles,unrelated_samfiles,chrom,pos,REF,ALT,allele_freq,MQ_thresh,BQ_thresh):
ADfs,ADrs = get_all_ADs_combined(unrelated_samfiles,chrom,pos,REF,ALT,MQ_thresh,BQ_thresh)
ADfs_U=ADfs
ADrs_U=ADrs
rho_f_old=0.8
rho_r_old=0.8
prior_old=np.array([1./3,1./3,1./3])
prior_old=prior_old/np.sum(prior_old)
prior_L_old=np.log(prior_old)
GT_likelihood_wrt_allele = GT_likelihood_wrt_allele_calc(1)
GT_likelihood_wrt_allele_L=np.log(GT_likelihood_wrt_allele)
a=2.
b=2.
D=np.array([2.,2,2])
rho_f_new,rho_r_new,prior_L_new,joint_probty_s = EM_full(ADfs,ADrs,rho_f_old,rho_r_old,prior_L_old,GT_likelihood_wrt_allele_L,a,b,D,allele_freq)
AF_unrel=0.
for i in range(ADfs.shape[0]):
temp1=GTL_L_calc(ADfs[i],ADrs[i],rho_f_new,rho_r_new,GT_likelihood_wrt_allele_L)
temp=temp1+prior_L_new
temp=temp-np.max(temp)
temp=np.exp(temp)
temp=temp/np.sum(temp)
AF_unrel=AF_unrel+temp[1]+temp[2]*2.
AF_unrel=AF_unrel/2./ADfs.shape[0]
ADfs,ADrs = get_all_ADs_combined(trio_samfiles,chrom,pos,REF,ALT,MQ_thresh,BQ_thresh)
table=table_gen(1,1e-8)
table_L=np.log(table)
PP,work_table = denovo_P_calc(ADfs,ADrs,rho_f_new,rho_r_new,GT_likelihood_wrt_allele_L,table_L,prior_L_new)
return PP,ADfs,ADrs,ADfs_U,ADrs_U,rho_f_new,rho_r_new,prior_L_new,AF_unrel
def cmp_entry(E1,E2):
if E1[0]>E2[0]:
return -1
elif E1[0]==E2[0]:
return 0
else:
return 1
def ALT_read_checker_in_parents(ADfs,ADrs):
if(len(ADfs)!=3 or len(ADrs)!=3):
print "ERROR in ALT_read_checker_in_parents"
sys.exit()
summ=ADfs[0][1]+ADfs[1][1]+ADrs[0][1]+ADrs[1][1]
if summ>3:
return False
else:
return True
def runner(outfilename,initial_filename,unrelated_filename,trio_filename):
outbuff_sorted_simple=open(outfilename,'w')
buff=open(initial_filename,'r')
line=buff.readline()
record=[]
MQ_thresh=-100.
BQ_thresh=-100.
count=0
unrelated_samfiles=get_sam_file_list(unrelated_filename)
trio_samfiles=get_sam_file_list(trio_filename)
while line:
count=count+1
print count,
sys.stdout.flush()
split_line=line.split()
chrom=chr_calc(split_line[0])
pos=int(split_line[1])
REF=split_line[3]
ALT=split_line[4]
INFO=split_line[7]
split_INFO=INFO.split(";")
for SS in split_INFO:
temp="ExAC_AF_computed="
if SS[0:len(temp)]==temp:
allele_freq=float(SS[len(temp):])
temp="MDC="
if SS[0:len(temp)]==temp:
MDC=SS[len(temp):]
temp="CSQ_gene="
if SS[0:len(temp)]==temp:
CSQ_gene=SS[len(temp):]
PP,ADfs,ADrs,ADfs_U,ADrs_U,rho_f_new,rho_r_new,prior_L_new,AF_unrel = PP_calc(trio_samfiles,unrelated_samfiles,chrom,pos,REF,ALT,allele_freq,MQ_thresh,BQ_thresh)
if AF_unrel<0.01 and ALT_read_checker_in_parents(ADfs,ADrs):
rec_single=[PP,line,MDC,ADfs,ADrs,ADfs_U,ADrs_U,allele_freq,rho_f_new,rho_r_new,prior_L_new,AF_unrel,CSQ_gene]
record.append(rec_single)
line=buff.readline()
print
record.sort(cmp_entry)
count=1
for rec in record:
PP=rec[0]
line=rec[1]
MDC=rec[2]
ADfs=rec[3]
ADrs=rec[4]
ADfs_U=rec[5]
ADrs_U=rec[6]
allele_freq=rec[7]
rho_f_new=rec[8]
rho_r_new=rec[9]
prior_L_new=rec[10]
AF_unrel=rec[11]
CSQ_gene=rec[12]
split_line=line.split()
outbuff_sorted_simple.write(str(count)+")\t"+split_line[0]+"\t"+split_line[1]+"\t"+split_line[3]+"\t"+split_line[4]+"\tAF="+str(allele_freq)+"\t")
outbuff_sorted_simple.write("rhos= "+str(rho_f_new)+","+str(rho_r_new)+"\t")
outbuff_sorted_simple.write(("prior=%r" %np.exp(prior_L_new))+"\tPP="+str(PP)+"\t")
outbuff_sorted_simple.write(("AF_unrel=%r" %AF_unrel)+"\t")
outbuff_sorted_simple.write(("CSQ_gene=%r" %CSQ_gene)+"\n")
outbuff_sorted_simple.write("trio:\n")
for i in range(len(ADfs)):
outbuff_sorted_simple.write("%r\t%r\n" %(ADfs[i],ADrs[i]))
outbuff_sorted_simple.write("unrelated:\n")
for i in range(len(ADfs_U)):
outbuff_sorted_simple.write("%r\t%r\n" %(ADfs_U[i],ADrs_U[i]))
count=count+1
if __name__=="__main__":
argv=sys.argv
for i in range(1,len(argv)):
if argv[i]=="-O":
outfilename=argv[i+1]
if argv[i]=="-I":
initial_filename=argv[i+1]
if argv[i]=="-U":
unrelated_filename=argv[i+1]
if argv[i]=="-T":
trio_filename=argv[i+1]
print "outfilename=",outfilename
print "initial_filename=",initial_filename
print "unrelated_filename=",unrelated_filename
print "trio_filename=",trio_filename
runner(outfilename,initial_filename,unrelated_filename,trio_filename)