forked from kuangmeng/DLPAlign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProbabilisticModel.h
1566 lines (1365 loc) · 61 KB
/
ProbabilisticModel.h
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
/////////////////////////////////////////////////////////////////
// ProbabilisticModel.h
//
// Routines for (1) local pair-HMM posterior probability computations
// (2) double affine pair-HMM posterior probability computations
// (2) chained anchoring
// (3) maximum weight trace alignment
/////////////////////////////////////////////////////////////////
#ifndef PROBABILISTICMODEL_H
#define PROBABILISTICMODEL_H
#include <list>
#include <cmath>
#include <cstdio>
#include "SafeVector.h"
#include "ScoreType.h"
#include "SparseMatrix.h"
#include "MultiSequence.h"
using namespace std;
const int NumMatchStates = 1;
const int NumInsertStates = 2; // for double affine pair-HMM
const int NumMatrixTypes = NumMatchStates + NumInsertStates * 2;
/////////////////////////////////////////////////////////////////
// ProbabilisticModel
//
// Class for storing the parameters of a probabilistic model and
// performing different computations based on those parameters.
// In particular, this class handles the computation of
// posterior probabilities that may be used in alignment.
/////////////////////////////////////////////////////////////////
class ProbabilisticModel
{
float initialDistribution[NumMatrixTypes]; // holds the initial probabilities for each state
float transProb[NumMatrixTypes][NumMatrixTypes]; // holds all state-to-state transition probabilities for double affine pair-HMM
float matchProb[256][256]; // emission probabilities for match states
float insProb[256][NumMatrixTypes]; // emission probabilities for insert states
float local_transProb[3][3]; // holds central state-to-state transition probabilities for local pair-HMM
float random_transProb[2]; // holds flanking state-to-state transition probabilities for local pair-HMM
public:
/////////////////////////////////////////////////////////////////
// ProbabilisticModel::ProbabilisticModel()
//
// Constructor. Builds a new probabilistic model using the
// given parameters.
/////////////////////////////////////////////////////////////////
ProbabilisticModel (const VF &initDistribMat, const VF &gapOpen, const VF &gapExtend,
const VVF &emitPairs, const VF &emitSingle)
{
/**********************************
double affine pair-HMM:
initDistribMat[0,1,3,4]: initialization of starting states
gapOpen[0,2]: long and short open gap probabilities
gapExtend[0,2]:long and short extend gap probabilities
local pair-HMM:
initDistribMat[2]: leave from a flanking state probability
gapOpen[1]: central open gap probability
gapExtend[1]:central extend gap probability
***********************************/
//double affine pair-HMM
// build transition matrix
VVF transMat (NumMatrixTypes, VF (NumMatrixTypes, 0.0f));
transMat[0][0] = 1;
for (int i = 0; i < NumInsertStates; i++)
{
transMat[0][2*i+1] = gapOpen[2*i];
transMat[0][2*i+2] = gapOpen[2*i];
transMat[0][0] -= (gapOpen[2*i] + gapOpen[2*i]);
assert (transMat[0][0] > 0);
transMat[2*i+1][2*i+1] = gapExtend[2*i];
transMat[2*i+2][2*i+2] = gapExtend[2*i];
transMat[2*i+1][2*i+2] = 0;
transMat[2*i+2][2*i+1] = 0;
transMat[2*i+1][0] = 1 - gapExtend[2*i];
transMat[2*i+2][0] = 1 - gapExtend[2*i];
}
// create initial and transition probability matrices
for (int i = 0; i < NumMatrixTypes; i++)
{
initialDistribution[i] = LOG (initDistribMat[i]);
for (int j = 0; j < NumMatrixTypes; j++)
transProb[i][j] = LOG (transMat[i][j]);
}
//due to local model parameters' initilization
//need to correct initialDistribution[2]
initialDistribution[2] = LOG (initDistribMat[1]);
// create insertion and match probability matrices
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < NumMatrixTypes; j++)
insProb[i][j] = LOG (emitSingle[i]);
for (int j = 0; j < 256; j++)
matchProb[i][j] = LOG (emitPairs[i][j]);
}
//local pair-HMM
// build transition matrix
VVF ltransMat (3, VF (3, 0.0f));
ltransMat[0][0] = 1;
ltransMat[0][1] = gapOpen[1];
ltransMat[0][2] = gapOpen[1];
ltransMat[0][0] -= (gapOpen[1] + gapOpen[1]);
assert (ltransMat[0][0] > 0);
ltransMat[1][1] = gapExtend[1];
ltransMat[2][2] = gapExtend[1];
ltransMat[1][2] = 0;
ltransMat[2][1] = 0;
ltransMat[1][0] = 1 - gapExtend[1];
ltransMat[2][0] = 1 - gapExtend[1];
// create initial and transition probability matrices
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
local_transProb[i][j] = LOG (ltransMat[i][j]);
}
// create initial and transition probability matrices
random_transProb[0] = LOG (initDistribMat[2]);//probability to leave from a randam state
random_transProb[1] = LOG (1-initDistribMat[2]);//probability to stay in a randam state
}
/////////////////////////////////////////////////////////////////
// ProbabilisticModel::ComputeForwardMatrix()
//
// Computes a set of forward probability matrices for aligning
// seq1 and seq2.
//
// For efficiency reasons, a single-dimensional floating-point
// array is used here, with the following indexing scheme:
//
// forward[i + NumMatrixTypes * (j * (seq2Length+1) + k)]
// refers to the probability of aligning through j characters
// of the first sequence, k characters of the second sequence,
// and ending in state i.
// flag: 1 probcons, 0 local
/////////////////////////////////////////////////////////////////
VF *ComputeForwardMatrix (Sequence *seq1, Sequence *seq2, bool flag=true) const
{
assert (seq1);
assert (seq2);
const int seq1Length = seq1->GetLength();
const int seq2Length = seq2->GetLength();
// retrieve the points to the beginning of each sequence
SafeVector<char>::iterator iter1 = seq1->GetDataPtr();
SafeVector<char>::iterator iter2 = seq2->GetDataPtr();
// create matrix
VF *forwardPtr;
if(flag) forwardPtr = new VF (NumMatrixTypes * (seq1Length+1) * (seq2Length+1), LOG_ZERO);
else forwardPtr = new VF (3 * (seq1Length+1) * (seq2Length+1), LOG_ZERO);
assert (forwardPtr);
VF &forward = *forwardPtr;
// initialization condition
if(flag)
{
forward[0 + NumMatrixTypes * (1 * (seq2Length+1) + 1)] =
initialDistribution[0] + matchProb[(unsigned char) iter1[1]][(unsigned char) iter2[1]];
for (int k = 0; k < NumInsertStates; k++)
{
forward[2*k+1 + NumMatrixTypes * (1 * (seq2Length+1) + 0)] =
initialDistribution[2*k+1] + insProb[(unsigned char) iter1[1]][k];
forward[2*k+2 + NumMatrixTypes * (0 * (seq2Length+1) + 1)] =
initialDistribution[2*k+2] + insProb[(unsigned char) iter2[1]][k];
}
}
// remember offset for each index combination
int ij = 0;
int i1j = -seq2Length - 1;
int ij1 = -1;
int i1j1 = -seq2Length - 2;
if(flag)
{
ij *= NumMatrixTypes;
i1j *= NumMatrixTypes;
ij1 *= NumMatrixTypes;
i1j1 *= NumMatrixTypes;
}
else
{
ij *= 3;
i1j *= 3;
ij1 *= 3;
i1j1 *= 3;
}
// compute forward scores
for (int i = 0; i <= seq1Length; i++)
{
unsigned char c1 = (i == 0) ? '~' : (unsigned char) iter1[i];
for (int j = 0; j <= seq2Length; j++)
{
unsigned char c2 = (j == 0) ? '~' : (unsigned char) iter2[j];
//local
if(i == 1 && j == 1 && !flag) forward[0 + ij] =
matchProb[c1][c2] - insProb[c1][0] - insProb[c2][0] - 2*random_transProb[1];
if (i > 1 || j > 1)
{
if (i > 0 && j > 0)
{
if(flag)
{
forward[0 + ij] = forward[0 + i1j1] + transProb[0][0];
for (int k = 1; k < NumMatrixTypes; k++)
LOG_PLUS_EQUALS (forward[0 + ij], forward[k + i1j1] + transProb[k][0]);
forward[0 + ij] += matchProb[c1][c2];
}
//local
else
{
forward[0 + ij] = matchProb[c1][c2] - insProb[c1][0] - insProb[c2][0] - 2*random_transProb[1];
for (int k = 0; k < 3; k++)
LOG_PLUS_EQUALS (forward[0 + ij], matchProb[c1][c2] - insProb[c1][0] - insProb[c2][0] +
forward[k + i1j1] + local_transProb[k][0] - 2*random_transProb[1]);
}
}
if (i > 0)
{
if(flag)
{
for (int k = 0; k < NumInsertStates; k++)
forward[2*k+1 + ij] = insProb[c1][k] +
LOG_ADD (forward[0 + i1j] + transProb[0][2*k+1],
forward[2*k+1 + i1j] + transProb[2*k+1][2*k+1]);
}
//local
else
{
forward[1 + ij] = LOG_ADD (forward[0 + i1j] + local_transProb[0][1] - random_transProb[1],
forward[1 + i1j] + local_transProb[1][1] - random_transProb[1]);
}
}
if (j > 0)
{
if(flag)
{
for (int k = 0; k < NumInsertStates; k++)
forward[2*k+2 + ij] = insProb[c2][k] +
LOG_ADD (forward[0 + ij1] + transProb[0][2*k+2],
forward[2*k+2 + ij1] + transProb[2*k+2][2*k+2]);
}
//local
else
{
forward[2 + ij] = LOG_ADD (forward[0 + ij1] + local_transProb[0][2] - random_transProb[1],
forward[2 + ij1] + local_transProb[2][2] - random_transProb[1]);
}
}
}
if(flag)
{
ij += NumMatrixTypes;
i1j += NumMatrixTypes;
ij1 += NumMatrixTypes;
i1j1 += NumMatrixTypes;
}
else
{
ij += 3;
i1j += 3;
ij1 += 3;
i1j1 += 3;
}
}
}
return forwardPtr;
}
/////////////////////////////////////////////////////////////////
// ProbabilisticModel::ComputeBackwardMatrix()
//
// Computes a set of backward probability matrices for aligning
// seq1 and seq2.
//
// For efficiency reasons, a single-dimensional floating-point
// array is used here, with the following indexing scheme:
//
// backward[i + NumMatrixTypes * (j * (seq2Length+1) + k)]
// refers to the probability of starting in state i and
// aligning from character j+1 to the end of the first
// sequence and from character k+1 to the end of the second
// sequence.
/////////////////////////////////////////////////////////////////
VF *ComputeBackwardMatrix (Sequence *seq1, Sequence *seq2, bool flag=true) const
{
assert (seq1);
assert (seq2);
const int seq1Length = seq1->GetLength();
const int seq2Length = seq2->GetLength();
SafeVector<char>::iterator iter1 = seq1->GetDataPtr();
SafeVector<char>::iterator iter2 = seq2->GetDataPtr();
// create matrix
VF *backwardPtr;
if(flag) backwardPtr = new VF (NumMatrixTypes * (seq1Length+1) * (seq2Length+1), LOG_ZERO);
else backwardPtr = new VF (3 * (seq1Length+1) * (seq2Length+1), LOG_ZERO);
assert (backwardPtr);
VF &backward = *backwardPtr;
// initialization condition
if(flag)
{
for (int k = 0; k < NumMatrixTypes; k++)
backward[NumMatrixTypes * ((seq1Length+1) * (seq2Length+1) - 1) + k] = initialDistribution[k];
}
// remember offset for each index combination
int ij = (seq1Length+1) * (seq2Length+1) - 1;
int i1j = ij + seq2Length + 1;
int ij1 = ij + 1;
int i1j1 = ij + seq2Length + 2;
if(flag)
{
ij *= NumMatrixTypes;
i1j *= NumMatrixTypes;
ij1 *= NumMatrixTypes;
i1j1 *= NumMatrixTypes;
}
else
{
ij *= 3;
i1j *= 3;
ij1 *= 3;
i1j1 *= 3;
}
// compute backward scores
for (int i = seq1Length; i >= 0; i--)
{
unsigned char c1 = (i == seq1Length) ? '~' : (unsigned char) iter1[i+1];
for (int j = seq2Length; j >= 0; j--)
{
unsigned char c2 = (j == seq2Length) ? '~' : (unsigned char) iter2[j+1];
if(!flag) backward[0 + ij] = LOG_ONE;//local
if (i < seq1Length && j < seq2Length)
{
if(flag)
{
const float ProbXY = backward[0 + i1j1] + matchProb[c1][c2];
for (int k = 0; k < NumMatrixTypes; k++)
LOG_PLUS_EQUALS (backward[k + ij], ProbXY + transProb[k][0]);
}
//local
else
{
const float ProbXY = backward[0 + i1j1] + matchProb[c1][c2] - insProb[c1][0] - insProb[c2][0];
for (int k = 0; k < 3; k++)
LOG_PLUS_EQUALS (backward[k + ij], ProbXY + local_transProb[k][0] - 2*random_transProb[1] );
}
}
if (i < seq1Length)
{
if(flag)
{
for (int k = 0; k < NumInsertStates; k++)
{
LOG_PLUS_EQUALS (backward[0 + ij], backward[2*k+1 + i1j] + insProb[c1][k] + transProb[0][2*k+1]);
LOG_PLUS_EQUALS (backward[2*k+1 + ij], backward[2*k+1 + i1j] + insProb[c1][k] + transProb[2*k+1][2*k+1]);
}
}
//local
else
{
LOG_PLUS_EQUALS (backward[0 + ij], backward[1 + i1j] + local_transProb[0][1] - random_transProb[1]);
LOG_PLUS_EQUALS (backward[1 + ij], backward[1 + i1j] + local_transProb[1][1] - random_transProb[1]);
}
}
if (j < seq2Length)
{
if(flag)
{
for (int k = 0; k < NumInsertStates; k++)
{
LOG_PLUS_EQUALS (backward[0 + ij], backward[2*k+2 + ij1] + insProb[c2][k] + transProb[0][2*k+2]);
LOG_PLUS_EQUALS (backward[2*k+2 + ij], backward[2*k+2 + ij1] + insProb[c2][k] + transProb[2*k+2][2*k+2]);
}
}
//local
else
{
LOG_PLUS_EQUALS (backward[0 + ij], backward[2 + ij1] + local_transProb[0][2] - random_transProb[1]);
LOG_PLUS_EQUALS (backward[2 + ij], backward[2 + ij1] + local_transProb[2][2] - random_transProb[1]);
}
}
if(flag)
{
ij -= NumMatrixTypes;
i1j -= NumMatrixTypes;
ij1 -= NumMatrixTypes;
i1j1 -= NumMatrixTypes;
}
else
{
ij -= 3;
i1j -= 3;
ij1 -= 3;
i1j1 -= 3;
}
}
}
return backwardPtr;
}
/////////////////////////////////////////////////////////////////
// ProbabilisticModel::ComputeTotalProbability()
//
// Computes the total probability of an alignment given
// the forward and backward matrices.
// flag: 1 probcons, 0 local
/////////////////////////////////////////////////////////////////
float ComputeTotalProbability (Sequence *seq1, Sequence *seq2,
const VF &forward, const VF &backward, bool flag=true) const
{
// compute total probability
float totalForwardProb = LOG_ZERO;
float totalBackwardProb = LOG_ZERO;
const int seq1Length = seq1->GetLength();
const int seq2Length = seq2->GetLength();
if(flag)
{
for (int k = 0; k < NumMatrixTypes; k++)
{
LOG_PLUS_EQUALS (totalForwardProb,
forward[k + NumMatrixTypes * ((seq1Length+1) * (seq2Length+1) - 1)] +
backward[k + NumMatrixTypes * ((seq1Length+1) * (seq2Length+1) - 1)]);
}
totalBackwardProb =
forward[0 + NumMatrixTypes * (1 * (seq2Length+1) + 1)] +
backward[0 + NumMatrixTypes * (1 * (seq2Length+1) + 1)];
for (int k = 0; k < NumInsertStates; k++)
{
LOG_PLUS_EQUALS (totalBackwardProb,
forward[2*k+1 + NumMatrixTypes * (1 * (seq2Length+1) + 0)] +
backward[2*k+1 + NumMatrixTypes * (1 * (seq2Length+1) + 0)]);
LOG_PLUS_EQUALS (totalBackwardProb,
forward[2*k+2 + NumMatrixTypes * (0 * (seq2Length+1) + 1)] +
backward[2*k+2 + NumMatrixTypes * (0 * (seq2Length+1) + 1)]);
}
}
else
{
SafeVector<char>::iterator iter1 = seq1->GetDataPtr();
SafeVector<char>::iterator iter2 = seq2->GetDataPtr();
int ij = 0;
for (int i = 0; i <= seq1Length; i++)
{
unsigned char c1 = (i == 0) ? '~' : (unsigned char) iter1[i];
for (int j = 0; j <= seq2Length; j++)
{
unsigned char c2 = (j == 0) ? '~' : (unsigned char) iter2[j];
if(i>0&&j>0)
{
LOG_PLUS_EQUALS (totalForwardProb,forward[ij]);
LOG_PLUS_EQUALS (totalBackwardProb,backward[ij] + matchProb[c1][c2]
- insProb[c1][0] - insProb[c2][0] - 2*random_transProb[1]);
}
ij += 3;
}
}
}
return (totalForwardProb + totalBackwardProb) / 2;
}
/////////////////////////////////////////////////////////////////
// ProbabilisticModel::ComputePosteriorMatrix()
//
// Computes the posterior probability matrix based on
// the forward and backward matrices.
// flag: 1 probcons, 0 local
/////////////////////////////////////////////////////////////////
VF *ComputePosteriorMatrix (Sequence *seq1, Sequence *seq2,
const VF &forward, const VF &backward, bool flag=true) const
{
assert (seq1);
assert (seq2);
const int seq1Length = seq1->GetLength();
const int seq2Length = seq2->GetLength();
float totalProb = ComputeTotalProbability (seq1, seq2,forward, backward, flag);
// compute posterior matrices
VF *posteriorPtr = new VF((seq1Length+1) * (seq2Length+1));
assert (posteriorPtr);
VF &posterior = *posteriorPtr;
int ij = 0;
VF::iterator ptr = posterior.begin();
for (int i = 0; i <= seq1Length; i++)
{
for (int j = 0; j <= seq2Length; j++)
{
*(ptr++) = EXP (min (LOG_ONE, forward[ij] + backward[ij] - totalProb));
if(flag) ij += NumMatrixTypes;
else ij += 3;
}
}
posterior[0] = 0;
return posteriorPtr;
}
/*
/////////////////////////////////////////////////////////////////
// ProbabilisticModel::ComputeExpectedCounts()
//
// Computes the expected counts for the various transitions.
/////////////////////////////////////////////////////////////////
VVF *ComputeExpectedCounts () const {
assert (seq1);
assert (seq2);
const int seq1Length = seq1->GetLength();
const int seq2Length = seq2->GetLength();
SafeVector<char>::iterator iter1 = seq1->GetDataPtr();
SafeVector<char>::iterator iter2 = seq2->GetDataPtr();
// compute total probability
float totalProb = ComputeTotalProbability (seq1Length, seq2Length,
forward, backward);
// initialize expected counts
VVF *countsPtr = new VVF(NumMatrixTypes + 1, VF(NumMatrixTypes, LOG_ZERO)); assert (countsPtr);
VVF &counts = *countsPtr;
// remember offset for each index combination
int ij = 0;
int i1j = -seq2Length - 1;
int ij1 = -1;
int i1j1 = -seq2Length - 2;
ij *= NumMatrixTypes;
i1j *= NumMatrixTypes;
ij1 *= NumMatrixTypes;
i1j1 *= NumMatrixTypes;
// compute expected counts
for (int i = 0; i <= seq1Length; i++){
unsigned char c1 = (i == 0) ? '~' : (unsigned char) iter1[i];
for (int j = 0; j <= seq2Length; j++){
unsigned char c2 = (j == 0) ? '~' : (unsigned char) iter2[j];
if (i > 0 && j > 0){
for (int k = 0; k < NumMatrixTypes; k++)
LOG_PLUS_EQUALS (counts[k][0],
forward[k + i1j1] + transProb[k][0] +
matchProb[c1][c2] + backward[0 + ij]);
}
if (i > 0){
for (int k = 0; k < NumInsertStates; k++){
LOG_PLUS_EQUALS (counts[0][2*k+1],
forward[0 + i1j] + transProb[0][2*k+1] +
insProb[c1][k] + backward[2*k+1 + ij]);
LOG_PLUS_EQUALS (counts[2*k+1][2*k+1],
forward[2*k+1 + i1j] + transProb[2*k+1][2*k+1] +
insProb[c1][k] + backward[2*k+1 + ij]);
}
}
if (j > 0){
for (int k = 0; k < NumInsertStates; k++){
LOG_PLUS_EQUALS (counts[0][2*k+2],
forward[0 + ij1] + transProb[0][2*k+2] +
insProb[c2][k] + backward[2*k+2 + ij]);
LOG_PLUS_EQUALS (counts[2*k+2][2*k+2],
forward[2*k+2 + ij1] + transProb[2*k+2][2*k+2] +
insProb[c2][k] + backward[2*k+2 + ij]);
}
}
ij += NumMatrixTypes;
i1j += NumMatrixTypes;
ij1 += NumMatrixTypes;
i1j1 += NumMatrixTypes;
}
}
// scale all expected counts appropriately
for (int i = 0; i < NumMatrixTypes; i++)
for (int j = 0; j < NumMatrixTypes; j++)
counts[i][j] -= totalProb;
}
*/
/////////////////////////////////////////////////////////////////
// ProbabilisticModel::ComputeNewParameters()
//
// Computes a new parameter set based on the expected counts
// given.
/////////////////////////////////////////////////////////////////
void ComputeNewParameters (Sequence *seq1, Sequence *seq2,
const VF &forward, const VF &backward,
VF &initDistribMat, VF &gapOpen,
VF &gapExtend, VVF &emitPairs, VF &emitSingle, bool enableTrainEmissions) const
{
assert (seq1);
assert (seq2);
const int seq1Length = seq1->GetLength();
const int seq2Length = seq2->GetLength();
SafeVector<char>::iterator iter1 = seq1->GetDataPtr();
SafeVector<char>::iterator iter2 = seq2->GetDataPtr();
// compute total probability
float totalProb = ComputeTotalProbability (seq1, seq2,
forward, backward);
// initialize expected counts
VVF transCounts (NumMatrixTypes, VF (NumMatrixTypes, LOG_ZERO));
VF initCounts (NumMatrixTypes, LOG_ZERO);
VVF pairCounts (256, VF (256, LOG_ZERO));
VF singleCounts (256, LOG_ZERO);
// remember offset for each index combination
int ij = 0;
int i1j = -seq2Length - 1;
int ij1 = -1;
int i1j1 = -seq2Length - 2;
ij *= NumMatrixTypes;
i1j *= NumMatrixTypes;
ij1 *= NumMatrixTypes;
i1j1 *= NumMatrixTypes;
// compute initial distribution posteriors
initCounts[0] = LOG_ADD (forward[0 + NumMatrixTypes * (1 * (seq2Length+1) + 1)] +
backward[0 + NumMatrixTypes * (1 * (seq2Length+1) + 1)],
forward[0 + NumMatrixTypes * ((seq1Length+1) * (seq2Length+1) - 1)] +
backward[0 + NumMatrixTypes * ((seq1Length+1) * (seq2Length+1) - 1)]);
for (int k = 0; k < NumInsertStates; k++)
{
initCounts[2*k+1] = LOG_ADD (forward[2*k+1 + NumMatrixTypes * (1 * (seq2Length+1) + 0)] +
backward[2*k+1 + NumMatrixTypes * (1 * (seq2Length+1) + 0)],
forward[2*k+1 + NumMatrixTypes * ((seq1Length+1) * (seq2Length+1) - 1)] +
backward[2*k+1 + NumMatrixTypes * ((seq1Length+1) * (seq2Length+1) - 1)]);
initCounts[2*k+2] = LOG_ADD (forward[2*k+2 + NumMatrixTypes * (0 * (seq2Length+1) + 1)] +
backward[2*k+2 + NumMatrixTypes * (0 * (seq2Length+1) + 1)],
forward[2*k+2 + NumMatrixTypes * ((seq1Length+1) * (seq2Length+1) - 1)] +
backward[2*k+2 + NumMatrixTypes * ((seq1Length+1) * (seq2Length+1) - 1)]);
}
// compute expected counts
for (int i = 0; i <= seq1Length; i++)
{
unsigned char c1 = (i == 0) ? '~' : (unsigned char) toupper(iter1[i]);
for (int j = 0; j <= seq2Length; j++)
{
unsigned char c2 = (j == 0) ? '~' : (unsigned char) toupper(iter2[j]);
if (i > 0 && j > 0)
{
if (enableTrainEmissions && i == 1 && j == 1)
{
LOG_PLUS_EQUALS (pairCounts[c1][c2],
initialDistribution[0] + matchProb[c1][c2] + backward[0 + ij]);
LOG_PLUS_EQUALS (pairCounts[c2][c1],
initialDistribution[0] + matchProb[c2][c1] + backward[0 + ij]);
}
for (int k = 0; k < NumMatrixTypes; k++)
{
LOG_PLUS_EQUALS (transCounts[k][0],
forward[k + i1j1] + transProb[k][0] +
matchProb[c1][c2] + backward[0 + ij]);
if (enableTrainEmissions && (i != 1 || j != 1))
{
LOG_PLUS_EQUALS (pairCounts[c1][c2],
forward[k + i1j1] + transProb[k][0] +
matchProb[c1][c2] + backward[0 + ij]);
LOG_PLUS_EQUALS (pairCounts[c2][c1],
forward[k + i1j1] + transProb[k][0] +
matchProb[c2][c1] + backward[0 + ij]);
}
}
}
if (i > 0)
{
for (int k = 0; k < NumInsertStates; k++)
{
LOG_PLUS_EQUALS (transCounts[0][2*k+1],
forward[0 + i1j] + transProb[0][2*k+1] +
insProb[c1][k] + backward[2*k+1 + ij]);
LOG_PLUS_EQUALS (transCounts[2*k+1][2*k+1],
forward[2*k+1 + i1j] + transProb[2*k+1][2*k+1] +
insProb[c1][k] + backward[2*k+1 + ij]);
if (enableTrainEmissions)
{
if (i == 1 && j == 0)
{
LOG_PLUS_EQUALS (singleCounts[c1],
initialDistribution[2*k+1] + insProb[c1][k] + backward[2*k+1 + ij]);
}
else
{
LOG_PLUS_EQUALS (singleCounts[c1],
forward[0 + i1j] + transProb[0][2*k+1] +
insProb[c1][k] + backward[2*k+1 + ij]);
LOG_PLUS_EQUALS (singleCounts[c1],
forward[2*k+1 + i1j] + transProb[2*k+1][2*k+1] +
insProb[c1][k] + backward[2*k+1 + ij]);
}
}
}
}
if (j > 0)
{
for (int k = 0; k < NumInsertStates; k++)
{
LOG_PLUS_EQUALS (transCounts[0][2*k+2],
forward[0 + ij1] + transProb[0][2*k+2] +
insProb[c2][k] + backward[2*k+2 + ij]);
LOG_PLUS_EQUALS (transCounts[2*k+2][2*k+2],
forward[2*k+2 + ij1] + transProb[2*k+2][2*k+2] +
insProb[c2][k] + backward[2*k+2 + ij]);
if (enableTrainEmissions)
{
if (i == 0 && j == 1)
{
LOG_PLUS_EQUALS (singleCounts[c2],
initialDistribution[2*k+2] + insProb[c2][k] + backward[2*k+2 + ij]);
}
else
{
LOG_PLUS_EQUALS (singleCounts[c2],
forward[0 + ij1] + transProb[0][2*k+2] +
insProb[c2][k] + backward[2*k+2 + ij]);
LOG_PLUS_EQUALS (singleCounts[c2],
forward[2*k+2 + ij1] + transProb[2*k+2][2*k+2] +
insProb[c2][k] + backward[2*k+2 + ij]);
}
}
}
}
ij += NumMatrixTypes;
i1j += NumMatrixTypes;
ij1 += NumMatrixTypes;
i1j1 += NumMatrixTypes;
}
}
// scale all expected counts appropriately
for (int i = 0; i < NumMatrixTypes; i++)
{
initCounts[i] -= totalProb;
for (int j = 0; j < NumMatrixTypes; j++)
transCounts[i][j] -= totalProb;
}
if (enableTrainEmissions)
{
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 256; j++)
pairCounts[i][j] -= totalProb;
singleCounts[i] -= totalProb;
}
}
// compute new initial distribution
float totalInitDistribCounts = 0;
for (int i = 0; i < NumMatrixTypes; i++)
totalInitDistribCounts += exp (initCounts[i]); // should be 2
initDistribMat[0] = min (1.0f, max (0.0f, (float) exp (initCounts[0]) / totalInitDistribCounts));
for (int k = 0; k < NumInsertStates; k++)
{
float val = (exp (initCounts[2*k+1]) + exp (initCounts[2*k+2])) / 2;
initDistribMat[2*k+1] = initDistribMat[2*k+2] = min (1.0f, max (0.0f, val / totalInitDistribCounts));
}
// compute total counts for match state
float inMatchStateCounts = 0;
for (int i = 0; i < NumMatrixTypes; i++)
inMatchStateCounts += exp (transCounts[0][i]);
for (int i = 0; i < NumInsertStates; i++)
{
// compute total counts for gap state
float inGapStateCounts =
exp (transCounts[2*i+1][0]) +
exp (transCounts[2*i+1][2*i+1]) +
exp (transCounts[2*i+2][0]) +
exp (transCounts[2*i+2][2*i+2]);
gapOpen[2*i] = gapOpen[2*i+1] =
(exp (transCounts[0][2*i+1]) +
exp (transCounts[0][2*i+2])) /
(2 * inMatchStateCounts);
gapExtend[2*i] = gapExtend[2*i+1] =
(exp (transCounts[2*i+1][2*i+1]) +
exp (transCounts[2*i+2][2*i+2])) /
inGapStateCounts;
}
if (enableTrainEmissions)
{
float totalPairCounts = 0;
float totalSingleCounts = 0;
for (int i = 0; i < 256; i++)
{
for (int j = 0; j <= i; j++)
totalPairCounts += exp (pairCounts[j][i]);
totalSingleCounts += exp (singleCounts[i]);
}
for (int i = 0; i < 256; i++) if (!islower ((char) i))
{
int li = (int)((unsigned char) tolower ((char) i));
for (int j = 0; j <= i; j++) if (!islower ((char) j))
{
int lj = (int)((unsigned char) tolower ((char) j));
emitPairs[i][j] = emitPairs[i][lj] = emitPairs[li][j] = emitPairs[li][lj] =
emitPairs[j][i] = emitPairs[j][li] = emitPairs[lj][i] = emitPairs[lj][li] = exp(pairCounts[j][i]) / totalPairCounts;
}
emitSingle[i] = emitSingle[li] = exp(singleCounts[i]) / totalSingleCounts;
}
}
}
/////////////////////////////////////////////////////////////////
// ProbabilisticModel::ComputeAlignment()
//
// Computes an alignment based on the given posterior matrix.
// This is done by finding the maximum summing path (or
// maximum weight trace) through the posterior matrix. The
// final alignment is returned as a pair consisting of:
// (1) a string (e.g., XXXBBXXXBBBBBBYYYYBBB) where X's and
// denote insertions in one of the two sequences and
// B's denote that both sequences are present (i.e.
// matches).
// (2) a float indicating the sum achieved
/////////////////////////////////////////////////////////////////
pair<SafeVector<char> *, float> ComputeAlignment (int seq1Length, int seq2Length,
const VF &posterior) const
{
float *twoRows = new float[(seq2Length+1)*2];
assert (twoRows);
float *oldRow = twoRows;
float *newRow = twoRows + seq2Length + 1;
char *tracebackMatrix = new char[(seq1Length+1)*(seq2Length+1)];
assert (tracebackMatrix);
char *tracebackPtr = tracebackMatrix;
VF::const_iterator posteriorPtr = posterior.begin() + seq2Length + 1;
// initialization
for (int i = 0; i <= seq2Length; i++)
{
oldRow[i] = 0;
*(tracebackPtr++) = 'L';
}
// fill in matrix
for (int i = 1; i <= seq1Length; i++)
{
// initialize left column
newRow[0] = 0;
posteriorPtr++;
*(tracebackPtr++) = 'U';
// fill in rest of row
for (int j = 1; j <= seq2Length; j++)
{
ChooseBestOfThree (*(posteriorPtr++) + oldRow[j-1], newRow[j-1], oldRow[j],
'D', 'L', 'U', &newRow[j], tracebackPtr++);
}
// swap rows
float *temp = oldRow;
oldRow = newRow;
newRow = temp;
}
// store best score
float total = oldRow[seq2Length];
delete [] twoRows;
// compute traceback
SafeVector<char> *alignment = new SafeVector<char>;
assert (alignment);
int r = seq1Length, c = seq2Length;
while (r != 0 || c != 0)
{
char ch = tracebackMatrix[r*(seq2Length+1) + c];
switch (ch)
{
case 'L':
c--;
alignment->push_back ('Y');
break;
case 'U':
r--;
alignment->push_back ('X');
break;
case 'D':
c--;
r--;
alignment->push_back ('B');
break;
default:
assert (false);
}
}
delete [] tracebackMatrix;
reverse (alignment->begin(), alignment->end());
return make_pair(alignment, total);
}
/////////////////////////////////////////////////////////////////
// ProbabilisticModel::ComputeAlignmentWithGapPenalties()
//
// Similar to ComputeAlignment() except with gap penalties.
/////////////////////////////////////////////////////////////////
pair<SafeVector<char> *, float> ComputeAlignmentWithGapPenalties (MultiSequence *align1,
MultiSequence *align2,
const VF &posterior, int numSeqs1,
int numSeqs2,
float gapOpenPenalty,
float gapContinuePenalty) const
{
int seq1Length = align1->GetSequence(0)->GetLength();
int seq2Length = align2->GetSequence(0)->GetLength();
SafeVector<SafeVector<char>::iterator > dataPtrs1 (align1->GetNumSequences());
SafeVector<SafeVector<char>::iterator > dataPtrs2 (align2->GetNumSequences());
// grab character data
for (int i = 0; i < align1->GetNumSequences(); i++)
dataPtrs1[i] = align1->GetSequence(i)->GetDataPtr();
for (int i = 0; i < align2->GetNumSequences(); i++)
dataPtrs2[i] = align2->GetSequence(i)->GetDataPtr();
// the number of active sequences at any given column is defined to be the
// number of non-gap characters in that column; the number of gap opens at
// any given column is defined to be the number of gap characters in that
// column where the previous character in the respective sequence was not
// a gap
SafeVector<int> numActive1 (seq1Length+1), numGapOpens1 (seq1Length+1);
SafeVector<int> numActive2 (seq2Length+1), numGapOpens2 (seq2Length+1);
// compute number of active sequences and gap opens for each group
for (int i = 0; i < align1->GetNumSequences(); i++)
{