-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorfHMM_EM.py
More file actions
1303 lines (920 loc) · 46.8 KB
/
orfHMM_EM.py
File metadata and controls
1303 lines (920 loc) · 46.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# import modules
from math import inf
from math import log
import numpy as np
from scipy.stats import nbinom
from collections import defaultdict
from scipy.optimize import minimize
import random
# In[ ]:
# # log func
# In[ ]:
def log_func(number):
'''
convert to log form
number: scalar
output: scalar
'''
if number > 0:
return log(number)
else:
return -inf
# In[ ]:
# # log-sum-exp
# In[ ]:
def logSumExp(x):
'''
HMM Notes p17
log sum(exp(x1)+exp(x2)+...)
x: list
result: scalar
'''
m = max(x)
if m == -inf: # infinity
return -inf
else:
minus_m = np.array(x) - m
result = m + log(sum(np.exp(minus_m)))
return result
# In[ ]:
# examples
# x = list(range(-100,-50))
# log(sum(np.exp(x)))
# logSumExp(x)
# In[ ]:
# # log likelihood f(x)
# In[ ]:
def lnNB(x, alpha, beta, E):
'''
Notes p2
x: scalar. Realization from NB distribution
alpha, beta: scalar. Parameters from Gamma distribution
E: scalar. Normalization factor for n-th sequence
result: scalar. Log likelihood f(x)
'''
r = alpha
p = beta / (E+beta)
result = nbinom.logpmf(x, r, p) # x follows NB(alpha, beta/(E+beta))
return result
# In[ ]:
# examples
#x = 50
#alpha = 1
#beta = 0.1
#E = 10
#lnNB(x, alpha, beta, E)
# In[ ]:
# # Forward
# In[ ]:
def start_codon_false(i, prob_current, prob_next, observed_data, alpha_list, beta_list, E, model1):
'''
Calculate forward algorithm given the next codon won't start
i: integer. Current i-th element inside this RNA sequence
prob_current: a list of 21 or 10 probabilites. Indicates current forward algorithm
prob_next: a list of 21 or 10 probabilites. Indicates next forward algorithm, updating this value
observed_data: a list. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: scalar. Normalization factor for this specific sequences
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: prob_next
'''
if model1 == True:
# state 1 to state 1
temp = prob_current[0] + log(1) + lnNB(observed_data[i+1], alpha_list[0], beta_list[0], E)
prob_next[0] = temp
# state 10 and state 11 to state 11
log_10_11 = prob_current[9] + log(1)
log_11_11 = prob_current[10] + log(1)
temp = [log_10_11, log_11_11]
prob_next[10] = logSumExp(temp) + lnNB(observed_data[i+1], alpha_list[10], beta_list[10], E)
elif model1 == False:
# state 1 and 10 to state 1
log_1_1 = prob_current[0] + log_func(1)
log_10_1 = prob_current[9] + log_func(1)
temp = [log_1_1, log_10_1]
prob_next[0] = logSumExp(temp) + lnNB(observed_data[i+1], alpha_list[0], beta_list[0], E)
return prob_next
# In[ ]:
def start_codon_true(i, prob_current, prob_next, trans, next_codon, observed_data, alpha_list, beta_list, E, model1):
'''
Calculate forward algorithm given the next codon is one of the start codons
i: integer. Current i-th element inside this RNA sequence
prob_current: a list of 21 or 10 probabilites. Indicates current forward algorithm
prob_next: a list of 21 or 10probabilites. Indicates next forward algorithm, updating this value
trans: a dictionary that key is the start codon (string), value is a list of scalars (three transition probability)
next_codon: string. Indicates the specific start codon
observed_data: a list. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: scalar. Normalization factor for this specific sequences
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: prob_next
'''
if model1 == True:
# state 1 to state 1
log_1_1 = log_func(1 - trans[next_codon][0] - trans[next_codon][1])
temp = prob_current[0] + log_1_1 + lnNB(observed_data[i+1], alpha_list[0], beta_list[0], E)
prob_next[0] = temp
# state 1 to state 2
log_1_2 = log_func(trans[next_codon][0])
temp = prob_current[0] + log_1_2 + lnNB(observed_data[i+1], alpha_list[1], beta_list[1], E)
prob_next[1] = temp
# state 1 and state 11 to state 12
log_1_12 = prob_current[0] + log_func(trans[next_codon][1])
log_11_12 = prob_current[10] + log_func(trans[next_codon][2])
temp = [log_1_12, log_11_12]
prob_next[11] = logSumExp(temp) + lnNB(observed_data[i+1], alpha_list[11], beta_list[11], E)
# state 10 and state 11 to state 11
log_10_11 = prob_current[9] + log(1)
log_11_11 = prob_current[10] + log_func(1 - trans[next_codon][2])
temp = [log_10_11, log_11_11]
prob_next[10] = logSumExp(temp) + lnNB(observed_data[i+1], alpha_list[10], beta_list[10], E)
elif model1 == False:
# state 1 and 10 to state 1
log_1_1 = prob_current[0] + log_func(1 - trans[next_codon])
log_10_1 = prob_current[9] + log_func(1)
temp = [log_1_1, log_10_1]
prob_next[0] = logSumExp(temp) + lnNB(observed_data[i+1], alpha_list[0], beta_list[0], E)
# state 1 to state 2
log_1_2 = log_func(trans[next_codon])
temp = prob_current[0] + log_1_2 + lnNB(observed_data[i+1], alpha_list[1], beta_list[1], E)
prob_next[1] = temp
return prob_next
# In[ ]:
def stop_codon_false(i, prob_current, prob_next, observed_data, alpha_list, beta_list, E, model1):
'''
Calculate forward algorithm given the next codon won't stop
i: integer. Current i-th element inside this RNA sequence
prob_current: a list of 21 or 10 probabilites. Indicates current forward algorithm
prob_next: a list of 21 or 10 probabilites. Indicates next forward algorithm, updating this value
observed_data: a list. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: scalar. Normalization factor for this specific sequences
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: prob_next
'''
if model1 == True:
# state 4 and state 7 to state 5
log_4_5 = prob_current[3] + log(1)
log_7_5 = prob_current[6] + log(1)
temp = [log_4_5, log_7_5]
prob_next[4] = logSumExp(temp) + lnNB(observed_data[i+1], alpha_list[4], beta_list[4], E)
# state 14 and state 17 to state 15
log_14_15 = prob_current[13] + log(1)
log_17_15 = prob_current[16] + log(1)
temp = [log_14_15, log_17_15]
prob_next[14] = logSumExp(temp) + lnNB(observed_data[i+1], alpha_list[14], beta_list[14], E)
elif model1 == False:
# state 4 and state 7 to state 5
log_4_5 = prob_current[3] + log(1)
log_7_5 = prob_current[6] + log(1)
temp = [log_4_5, log_7_5]
prob_next[4] = logSumExp(temp) + lnNB(observed_data[i+1], alpha_list[4], beta_list[4], E)
return prob_next
# In[ ]:
def stop_codon_true(i, prob_current, prob_next, observed_data, alpha_list, beta_list, E, model1):
'''
Calculate forward algorithm given the next codon is one of the stop codons
i: integer. Current i-th element inside this RNA sequence
prob_current: a list of 21 or 10 probabilites. Indicates current forward algorithm
prob_next: a list of 21 or 10 probabilites. Indicates next forward algorithm, updating this value
observed_data: a list. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: scalar. Normalization factor for this specific sequences
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: prob_next
'''
if model1 == True:
# state 4 to state 5
prob_next[4] = prob_current[3] + log(1) + lnNB(observed_data[i+1], alpha_list[4], beta_list[4], E)
# state 7 to state 8
prob_next[7] = prob_current[6] + log(1) + lnNB(observed_data[i+1], alpha_list[7], beta_list[7], E)
# state 14 to 15
prob_next[14] = prob_current[13] + log(1) + lnNB(observed_data[i+1], alpha_list[14], beta_list[14], E)
# state 17 to state 18
prob_next[17] = prob_current[16] + log(1) + lnNB(observed_data[i+1], alpha_list[17], beta_list[17], E)
elif model1 == False:
# state 4 to state 5
prob_next[4] = prob_current[3] + log(1) + lnNB(observed_data[i+1], alpha_list[4], beta_list[4], E)
# state 7 to state 8
prob_next[7] = prob_current[6] + log(1) + lnNB(observed_data[i+1], alpha_list[7], beta_list[7], E)
return prob_next
# In[ ]:
def forward_algorithm(RNA_data, observed_data, alpha_list, beta_list, E, trans, stop_codon_list, num_sequence, model1):
'''
Compute and store the forward algorithm for a given sequence
RNA_data: a list of lists. Each inner list indicates a single RNA sequence and this list contains letters 'A', 'C', 'U', 'G'
observed_data: a list of lists. Each inner list indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: a list of scalars. Normalization factor for all sequences
trans: a dictionary that key is the start codon (string), value is a list of scalars (three transition probability)
stop_codon_list: a list of stop codons (string)
num_sequence: scalar indicates n-th sequence
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: matrix stores the forward algorithm for single RNA sequence
'''
# Find E, observed_data, RNA_data according to the order of sequences
E = E[num_sequence - 1]
observed_data = observed_data[num_sequence - 1]
RNA_data = RNA_data[num_sequence - 1]
sequence_length = len(RNA_data) # length of data
if model1 == True:
output = np.zeros((sequence_length, 21)) # initialize output matrix
prob_current = np.ones(21) * (-inf) # initialize probability, 21 different states
elif model1 == False:
output = np.zeros((sequence_length, 10))
prob_current = np.ones(10) * (-inf) # initialize probability, 10 different states
prob_current[0] = lnNB(observed_data[0], alpha_list[0], beta_list[0], E)
output[0] = prob_current
for i in range(sequence_length - 1):
start_codon = False
stop_codon = False
next_codon = ""
if model1 == True:
prob_next = np.ones(21) * (-inf)
elif model1 == False:
prob_next = np.ones(10) * (-inf)
# Check start and stop codon
if i + 3 < sequence_length:
next_codon = RNA_data[i+1] + RNA_data[i+2] + RNA_data[i+3]
if next_codon in trans.keys():
start_codon = True
if next_codon in stop_codon_list:
stop_codon = True
# start codon is false
if start_codon == False:
prob_next = start_codon_false(i, prob_current, prob_next, observed_data, alpha_list, beta_list, E, model1)
# start codon is ture
elif start_codon == True:
prob_next = start_codon_true(i, prob_current, prob_next, trans, next_codon, observed_data,
alpha_list, beta_list, E, model1)
# stop codon is ture
if stop_codon == True:
prob_next = stop_codon_true(i, prob_current, prob_next, observed_data, alpha_list, beta_list, E, model1)
# stop codon is false
elif stop_codon == False:
prob_next = stop_codon_false(i, prob_current, prob_next, observed_data, alpha_list, beta_list, E, model1)
# transter to next state with probability 1
if model1 == True:
sure_to_transit = [1, 2, 4, 5, 7, 8, 11, 12, 14, 15, 17, 18]
elif model1 == False:
sure_to_transit = [1, 2, 4, 5, 7, 8]
for k in sure_to_transit:
temp = prob_current[k] + log(1) + lnNB(observed_data[i+1], alpha_list[k+1], beta_list[k+1], E)
prob_next[k+1] = temp
if model1 == True:
# state 20 and state 21 to state 21
log_20_21 = prob_current[19] + log(1)
log_21_21 = prob_current[20] + log(1)
temp = [log_20_21, log_21_21]
prob_next[20] = logSumExp(temp) + lnNB(observed_data[i+1], alpha_list[20], beta_list[20], E)
prob_current = prob_next
output[i+1] = prob_current
return output
# In[ ]:
def forward_matrix(RNA_data, observed_data, alpha_list, beta_list, E, trans, stop_codon_list, model1):
'''
Compute forward matrix by combining result from each single sequence
RNA_data: a list of lists. Each inner list indicates a single RNA sequence and this list contains letters 'A', 'C', 'U', 'G'
observed_data: a list of lists. Each inner list indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: a list of scalars. Normalization factor for all sequences
trans: a dictionary that key is the start codon (string), value is a list of scalars (three transition probability)
stop_codon_list: a list of stop codons (string)
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: matrix stores the forward algorithm for multiple RNA sequences
'''
output = []
for n in range(1, len(observed_data)+1):
output.append(forward_algorithm(RNA_data, observed_data, alpha_list, beta_list, E, trans, stop_codon_list, n, model1))
return output
# In[ ]:
# forward = forward_matrix(RNA_data, observed_data, alpha_list, beta_list, E, trans, stop_codon_list)
# # Backward
# In[ ]:
def start_codon_false_back(i, prob_current_back, prob_previous_back, observed_data, alpha_list, beta_list, E, model1):
'''
Calculate backward algorithm given the next codon won't start
i: integer. Current i-th element inside this RNA sequence
prob_current_back: a list of 21 or 10 probabilites. Indicates current backward algorithm
prob_previous_back: a list of 21 or 10 probabilites. Indicates previous backward algorithm, updating this value
observed_data: a list. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: scalar. Normalization factor for this specific sequences
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: prob_previous_back
'''
# state 1 to state 1
temp = prob_current_back[0] + log(1) + lnNB(observed_data[i+1], alpha_list[0], beta_list[0], E)
prob_previous_back[0] = temp
if model1 == True:
# state 11 to state 11
temp = prob_current_back[10] + log(1) + lnNB(observed_data[i+1], alpha_list[10], beta_list[10], E)
prob_previous_back[10] = temp
return prob_previous_back
# In[ ]:
def start_codon_true_back(i, prob_current_back, prob_previous_back, trans, next_codon, observed_data, alpha_list, beta_list, E, model1):
'''
Calculate backward algorithm given the next codon is one of the start codons
i: integer. Current i-th element inside this RNA sequence
prob_current_back: a list of 21 or 10 probabilites. Indicates current backward algorithm
prob_previous_back: a list of 21 or 10 probabilites. Indicates previous backward algorithm, updating this value
trans: a dictionary that key is the start codon (string), value is a list of scalars (three transition probability)
next_codon: string. Indicates the specific start codon
observed_data: a list. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: scalar. Normalization factor for this specific sequences
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: prob_previous_back
'''
if model1 == True:
# state 1 to state 1
temp = log_func(1 - trans[next_codon][0] - trans[next_codon][1])
log_1_1 = prob_current_back[0] + temp + lnNB(observed_data[i+1], alpha_list[0], beta_list[0], E)
# state 1 to state 2
temp = log_func(trans[next_codon][0])
log_1_2 = prob_current_back[1] + temp + lnNB(observed_data[i+1], alpha_list[1], beta_list[1], E)
# state 1 to state 12
temp = log_func(trans[next_codon][1])
log_1_12 = prob_current_back[11] + temp + lnNB(observed_data[i+1], alpha_list[11], beta_list[11], E)
temp = [log_1_1, log_1_2, log_1_12]
prob_previous_back[0] = logSumExp(temp)
# state 11 to state 11, 12
temp = log_func(1 - trans[next_codon][2])
log_11_11 = prob_current_back[10] + temp + lnNB(observed_data[i+1], alpha_list[10], beta_list[10], E)
temp = log_func(trans[next_codon][2])
log_11_12 = prob_current_back[11] + temp + lnNB(observed_data[i+1], alpha_list[11], beta_list[11], E)
temp = [log_11_11, log_11_12]
prob_previous_back[10] = logSumExp(temp)
elif model1 == False:
# state 1 to state 1, 2
temp = log_func(1 - trans[next_codon])
log_1_1 = prob_current_back[0] + temp + lnNB(observed_data[i+1], alpha_list[0], beta_list[0], E)
temp = log_func(trans[next_codon])
log_1_2 = prob_current_back[1] + temp + lnNB(observed_data[i+1], alpha_list[1], beta_list[1], E)
temp = [log_1_1, log_1_2]
prob_previous_back[0] = logSumExp(temp)
return prob_previous_back
# In[ ]:
def stop_codon_false_back(i, prob_current_back, prob_previous_back, observed_data, alpha_list, beta_list, E, model1):
'''
Calculate backward algorithm given the next codon won't stop
i: integer. Current i-th element inside this RNA sequence
prob_current_back: a list of 21 or 10 probabilites. Indicates current backward algorithm
prob_previous_back: a list of 21 or 10 probabilites. Indicates previous backward algorithm, updating this value
observed_data: a list. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: scalar. Normalization factor for this specific sequences
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: prob_previous_back
'''
# state 7 to state 5
prob_previous_back[6] = prob_current_back[4] + log(1) + lnNB(observed_data[i+1], alpha_list[4], beta_list[4], E)
if model1 == True:
# state 17 to state 15
prob_previous_back[16] = prob_current_back[14] + log(1) + lnNB(observed_data[i+1], alpha_list[14], beta_list[14], E)
return prob_previous_back
# In[ ]:
def stop_codon_true_back(i, prob_current_back, prob_previous_back, observed_data, alpha_list, beta_list, E, model1):
'''
Calculate backward algorithm given the next codon is stop codon
i: integer. Current i-th element inside this RNA sequence
prob_current_back: a list of 21 or 10 probabilites. Indicates current backward algorithm
prob_previous_back: a list of 21 or 10 probabilites. Indicates previous backward algorithm, updating this value
observed_data: a list. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: scalar. Normalization factor for this specific sequences
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: prob_previous_back
'''
# state 7 to state 8
prob_previous_back[6] = prob_current_back[7] + log(1) + lnNB(observed_data[i+1], alpha_list[7], beta_list[7], E)
if model1 == True:
# state 17 to state 18
prob_previous_back[16] = prob_current_back[17] + log(1) + lnNB(observed_data[i+1], alpha_list[17], beta_list[17], E)
return prob_previous_back
# In[ ]:
def backward_algorithm(RNA_data, observed_data, alpha_list, beta_list, E, trans, stop_codon_list, num_sequence, model1):
'''
Compute and store the backward algorithm for a given sequence
RNA_data: a list of lists. Each inner list indicates a single RNA sequence and this list contains letters 'A', 'C', 'U', 'G'
observed_data: a list of lists. Each inner list indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: a list of scalars. Normalization factor for all sequences
trans: a dictionary that key is the start codon (string), value is a list of scalars (three transition probability)
stop_codon_list: a list of stop codons (string)
num_sequence: scalar indicates n-th sequence
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: matrix stores the backward algorithm for single RNA sequence
'''
# Find E, observed_data, RNA_data according the order of sequences
E = E[num_sequence - 1]
observed_data = observed_data[num_sequence - 1]
RNA_data = RNA_data[num_sequence - 1]
sequence_length = len(RNA_data) # length of data
if model1 == True:
prob_current_back = list(np.zeros(21)) # initialize probability, 21 different states
output = np.zeros((sequence_length, 21)) # initialize matrix
elif model1 == False:
prob_current_back = list(np.zeros(10)) # initialize probability, 10 different states
output = np.zeros((sequence_length, 10))
output[sequence_length - 1] = prob_current_back
iteration_list = np.arange(0, sequence_length-1, 1).tolist()
iteration_list.reverse()
for i in iteration_list:
start_codon = False
stop_codon = False
if model1 == True:
prob_previous_back = list(np.ones(21) * (-inf))
elif model1 == False:
prob_previous_back = list(np.ones(10) * (-inf))
next_codon = ""
# Check start and stop codon
if i + 3 < sequence_length:
next_codon = RNA_data[i+1] + RNA_data[i+2] + RNA_data[i+3]
if next_codon in trans.keys():
start_codon = True
if next_codon in stop_codon_list:
stop_codon = True
# start codon is false
if start_codon == False:
prob_previous_back = start_codon_false_back(i, prob_current_back, prob_previous_back, observed_data,
alpha_list, beta_list, E, model1)
# start codon is true
elif start_codon == True:
prob_previous_back = start_codon_true_back(i, prob_current_back, prob_previous_back, trans,
next_codon, observed_data, alpha_list, beta_list, E, model1)
# stop codon is true
if stop_codon == True:
prob_previous_back = stop_codon_true_back(i, prob_current_back, prob_previous_back, observed_data,
alpha_list, beta_list, E, model1)
# stop codon is false
elif stop_codon == False:
prob_previous_back = stop_codon_false_back(i, prob_current_back, prob_previous_back, observed_data,
alpha_list, beta_list, E, model1)
# traster with probability 1
if model1 == True:
sure_to_transit = [2, 3, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15, 16, 18, 19, 20]
elif model1 == False:
sure_to_transit = [2, 3, 4, 5, 6, 8, 9]
for k in sure_to_transit:
temp = prob_current_back[k] + log(1) + lnNB(observed_data[i+1], alpha_list[k], beta_list[k], E)
prob_previous_back[k-1] = temp
if model1 == True:
# state 21 to state 21
prob_previous_back[20] = prob_current_back[20] + log(1) + lnNB(observed_data[i+1], alpha_list[20], beta_list[20], E)
elif model1 == False:
# state 10 to state 1
prob_previous_back[9] = prob_current_back[0] + log(1) + lnNB(observed_data[i+1], alpha_list[0], beta_list[0], E)
prob_current_back = prob_previous_back
output[i] = prob_current_back
return output
# In[ ]:
def backward_matrix(RNA_data, observed_data, alpha_list, beta_list, E, trans, stop_codon_list, model1):
'''
Compute backward matrix by combining result from each single sequence
RNA_data: a list of lists. Each inner list indicates a single RNA sequence and this list contains letters 'A', 'C', 'U', 'G'
observed_data: a list of lists. Each inner list indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: a list of scalars. Normalization factor for all sequences
trans: a dictionary that key is the start codon (string), value is a list of scalars (three transition probability)
stop_codon_list: a list of stop codons (string)
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: matrix stores the backward algorithm for multiple RNA sequences
'''
output = []
for n in range(1, len(observed_data)+1):
output.append(backward_algorithm(RNA_data, observed_data, alpha_list, beta_list, E, trans,
stop_codon_list, n, model1))
return output
# In[ ]:
#backward = backward_matrix(RNA_data, observed_data, alpha_list, beta_list, E, trans, stop_codon_list)
# In[ ]:
# # Incomplete log likelihood (simple) with backward (test accuracy)
# In[ ]:
def incomplete_log_likelihood(backward, observed_data, alpha_list, beta_list, E):
'''
Notes p3
Compute the incomplete log likelihood with backward algorithm
backward: a list of matrix. Stores backward algorithm output
observed_data: a list of lists. Each inner list indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: a list of scalars. Normalization factor for all sequences
output: scalar. Indicates incomplete log likelihood
'''
# initialize parameters
temp_list = []
# incomplete log likelihood
for n in range(1, len(observed_data)+1):
B1_1 = backward[n-1][0][0]
nb = lnNB(observed_data[n-1][0], alpha_list[0], beta_list[0], E[n-1])
temp_list.append(B1_1 + nb)
output = logSumExp(temp_list)
return output
# In[ ]:
#incomplete_log_likelihood(backward, observed_data, alpha_list, beta_list, E)
# In[ ]:
# # Incomplete log likelihood (simple) with forward (actual use)
# In[ ]:
def incomplete_log_likelihood(forward, observed_data, alpha_list, beta_list, E):
'''
Notes p3
Compute the incomplete log likelihood with forward algorithm
backward: a list of matrix. Stores backward algorithm output
observed_data: a list of lists. Each inner list indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: a list of scalars. Normalization factor for all sequences
output: scalar. Indicates incomplete log likelihood
'''
# initialize parameters
output_list = []
# incomplete log likelihood
for n in range(1, len(observed_data)+1):
last_position = len(observed_data[n-1]) - 1
output_list.append(logSumExp(forward[n-1][last_position]))
return logSumExp(output_list)
# In[ ]:
#incomplete_log_likelihood(forward, observed_data, alpha_list, beta_list, E)
# In[ ]:
# # Compute L(simple)
# In[ ]:
def compute_L(forward, backward, model1):
'''
Notes p4
Compute one state probability L
forward: a list of matrix. Compute and store by forward algorithm
backward: a list of matrix. Compute and store by backward algorithm
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: a list of matrix
'''
output = []
# n sequences
for n in range(1, len(forward)+1):
if model1 == True:
sequence = np.zeros((len(forward[n-1]), 21))
elif model1 == False:
sequence = np.zeros((len(forward[n-1]), 10))
for t in range(1, len(forward[n-1])+1):
temp = logSumExp((forward[n-1] + backward[n-1])[t-1]) # denominator
sequence[t-1] = forward[n-1][t-1] + backward[n-1][t-1] - temp
output.append(sequence)
return output
# In[ ]:
#L = compute_L(forward, backward)
# # Compute H
# In[ ]:
def transprob_comp(curr_state, next_state, codon_specific, trans, model1):
'''
Compute transition probability given the current state and next state with specific codon
curr_state: int. Indicates the current state (1-21 or 1-10)
next_state: int. Indicates the next state (1-21 or 1-10 )
codon_specific: string. Indicates the specific codon
trans: dictionary
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: scalar
'''
if model1 == True:
if curr_state == 1:
if next_state == 1:
return log_func(1 - trans[codon_specific][0] - trans[codon_specific][1])
elif next_state == 2:
return log_func(trans[codon_specific][0])
elif next_state == 12:
return log_func(trans[codon_specific][1])
if curr_state == 11:
if next_state == 11:
return log_func(1 - trans[codon_specific][2])
elif next_state == 12:
return log_func(trans[codon_specific][2])
if model1 == False:
if curr_state == 1:
if next_state == 1:
return log_func(1 - trans[codon_specific])
elif next_state == 2:
return log_func(trans[codon_specific])
# In[ ]:
def compute_H_nume(forward, backward, curr_state, next_state, codon_specific, observed_data, alpha_list, beta_list, num_sequence, t, trans, E, model1):
'''
Notes p4
Compute numerator for H function
forward: a list of matrix
backward: a list of matrix
curr_state: int. Indicates the current state (1-21 or 1-10)
next_state: int. Indicates the next state (1-21 or 1-10)
codon_specific: string. Indicates the specific codon
observed_data: a list of lists. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
num_sequence: int. Indicates which RNA sequence
t: int. Indicates the position inside RNA sequence
trans: dictionary
E: list. Normalization factor
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: scalar
'''
transprob = transprob_comp(curr_state, next_state, codon_specific, trans, model1)
numerator = forward[num_sequence - 1][t - 1][curr_state - 1] + transprob + lnNB(observed_data[num_sequence - 1][t], alpha_list[next_state - 1], beta_list[next_state - 1], E[num_sequence - 1]) + backward[num_sequence - 1][t][next_state - 1]
return numerator
# In[ ]:
def compute_H_deno(forward, backward, curr_state, codon_specific, observed_data, alpha_list, beta_list, num_sequence, t, trans, E, model1):
'''
Notes p4
Compute denominator for H function according to current state
forward: a list of matrix
backward: a list of matrix
curr_state: int. Indicates the current state (1-21)
next_state: int. Indicates the next state (1-21)
codon_specific: string. Indicates the specific codon
observed_data: a list. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
num_sequence: int. Indicates which RNA sequence
t: int. Indicates the position inside RNA sequence
trans: dictionary
E: list. Normalization factor
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: scalar
'''
output_list = []
# case state1 to state1, 2, 12
if curr_state == 1:
if model1 == True:
temp = [1, 2, 12]
elif model1 == False:
temp = [1, 2]
for next_state in temp:
output_list.append(compute_H_nume(forward, backward, curr_state, next_state, codon_specific,
observed_data, alpha_list, beta_list, num_sequence, t, trans,
E, model1))
# case state11 to state11, 12
elif curr_state == 11:
for next_state in [11, 12]:
output_list.append(compute_H_nume(forward, backward, curr_state, next_state, codon_specific,
observed_data, alpha_list, beta_list, num_sequence, t, trans,
E, model1))
return logSumExp(output_list)
# In[ ]:
def compute_H_codon(forward, backward, curr_state, next_state, codon_specific, RNA_data, observed_data, alpha_list, beta_list, E, trans, t, num_sequence, model1):
'''
Notes p4
Compute codon specific H function
forward: a list of matrix
backward: a list of matrix
curr_state: int. Indicates the current state (1-21 or 1-10)
next_state: int. Indicates the next state (1-21 or 1-10)
codon_specific: string. Indicates the specific codon
RNA_data: list of lists
observed_data: a list of lists. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 or 10 states (NB parameter)
beta_list: a list of beta values for 21 or 10 states (NB parameter)
E: a list of scalars
trans: dictionary
t: int. Indicates the position inside RNA sequence
num_sequence: int. Indicates which RNA sequence
model1: boolean (True/False). Identify it's model1 (21-states: True) or model2 (10-states: False)
output: scalar
'''
# initialize parameters
output = -inf
numerator_list = []
# check codon
if t + 3 <= len(RNA_data[num_sequence - 1]):
next_codon = RNA_data[num_sequence - 1][t] + RNA_data[num_sequence - 1][t+1] + RNA_data[num_sequence - 1][t+2]
if codon_specific == next_codon:
numerator = compute_H_nume(forward, backward, curr_state, next_state, codon_specific, observed_data,
alpha_list, beta_list, num_sequence, t, trans, E, model1)
if numerator == -inf:
return -inf
denominator = compute_H_deno(forward, backward, curr_state, codon_specific, observed_data,
alpha_list, beta_list, num_sequence, t, trans, E, model1)
output = numerator - denominator
return output
# # store probabilities
# In[ ]:
def store_info(forward, backward, RNA_data, observed_data, alpha_list, beta_list, E, trans, stop_codon_list, ci, model1):
'''
Store codon specific H function
forward: a list of matrix
backward: a list of matrix
RNA_data: list of lists
observed_data: a list of lists. Indicates the height of a sequence and this list contains scalars
alpha_list: a list of alpha values for 21 states (NB parameter)
beta_list: a list of beta values for 21 states (NB parameter)
E: a list of scalars
trans: dictionary
stop_codon_list: list of stop codons (strings)
ci: string. Given codon
model1: boolean (True/False). Identify it's model1 (21-states) or model2 (10-states)
output: a list of scalars
'''
temp = []
n11_list = []
n12_list = []
n112_list = []
n1111_list = []
n1112_list = []
for n in range(1, len(observed_data) + 1):
for t in range(1, len(observed_data[n-1]) - 1):
n11_list.append(compute_H_codon(forward, backward, 1, 1, ci, RNA_data, observed_data,