Debian Codegraph @

src/dr/oldevomodel/indel/IstvansProposal.java

raw symbols SCIP
1 /*
2 * IstvansProposal.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.oldevomodel.indel;
27
28 import java.util.HashMap;
29 import java.util.Random;
30
31
32 public class IstvansProposal {
33
34 static double cGap = -5;
35 // Gap penalty
36 static int cGapsymbol = -1;
37 // Is -1 representing gap?
38 static double cBasis = 1.5;
39
40 static final int cMaxlength = 1000;
41
42 static final int cMaxUnalignDimension = 17;
43
44 static final int eFree = 0;
45 static final int ePossible = 1;
46 static final int eEdgeUsed = 2;
47 static final int eUsed = 3;
48
49 // Static count of path count bound overruns
50 static int sBigUnalignableRegion = 0;
51 private final Random r = new Random();
52 // dynamic programming table
53 // does this need initialization before each proposal?
54 private final double[][] iDP = new double[cMaxlength][cMaxlength];
55
56 public void setGapSymbol(int gapSymbol) {
57 cGapsymbol = gapSymbol;
58 }
59
60 /**
61 * @returns the combinatorial factor associated to the alignment, i.e.
62 * the number of valid paths in the DP table.
63 */
64 int countPaths(int[][] iInputAlignment, int iStartCol, int iEndCol) {
65
66 // Initialise
67
68 int iLen = iEndCol - iStartCol + 1;
69 int iLeaves = iInputAlignment.length;
70 int iFirstNotUsed = 0; // First not-'used' alignment vector (for efficiency)
71 int iState[] = new int[ iLen ]; // Helper array, to traverse the region in the DP table corresp. to the alignment
72 IntMathVec iPos = new IntMathVec( iLeaves ); // Current position; sum of all used vectors
73 HashMap<IntMathVec, Integer> iTable = new HashMap<IntMathVec, Integer>();
74
75 IntMathVec[] iAlignment = new IntMathVec[iLen];
76 for (int i=iStartCol; i<=iEndCol; i++) {
77 iAlignment[i-iStartCol] = new IntMathVec(iLeaves);
78 for (int j=0; j<iLeaves; j++) {
79 iAlignment[i-iStartCol].iV[j] = ((iInputAlignment[j][i] == cGapsymbol) ? 0 : 1);
80 }
81 }
82
83 // Enter first count into DP table
84 iTable.put(iPos, 1);
85
86 // Array of possible vector indices, used in inner loop
87 int[] iPossibles = new int[cMaxUnalignDimension];
88
89 do {
90
91 // Find all possible vectors from current position, iPos
92 IntMathVec iMask = new IntMathVec( iLeaves );
93 int iPtr;
94 int iNumPossible = 0;
95 for (iPtr = iFirstNotUsed; iMask.zeroEntry() && iPtr<iLen; iPtr++) {
96 if (iState[ iPtr ] != eUsed) {
97 if (iMask.innerProduct( iAlignment[iPtr] ) == 0) {
98 iState[ iPtr ] = ePossible;
99 if (iNumPossible == cMaxUnalignDimension) {
100 // This gets too hairy - bail out.
101 sBigUnalignableRegion++;
102 return -1;
103 }
104 iPossibles[iNumPossible++] = iPtr;
105 }
106 iMask.add( iAlignment[iPtr] );
107 }
108 }
109
110 // Loop over all combinations of possible vectors, which define edges from
111 // iPos to another possible position, by ordinary binary counting.
112
113 IntMathVec iNewPos = new IntMathVec( iPos );
114 IntMathVec iSignature = new IntMathVec( iPos.iV.length );
115 int iPosPtr;
116 boolean iUnusedPos;
117 boolean iFoundNonZero;
118
119 do {
120
121 // Find next combination
122 iFoundNonZero = false;
123 for (iPosPtr = iNumPossible - 1; iPosPtr >= 0; --iPosPtr) {
124 int iCurPtr = iPossibles[ iPosPtr ];
125 if (iState[ iCurPtr ] == ePossible) {
126 iState[ iCurPtr ] = eEdgeUsed;
127 iNewPos.add( iAlignment[ iCurPtr ] );
128 // Compute signature vector
129 iSignature.addMultiple( iAlignment[ iCurPtr ], iPosPtr+1 );
130 // Signal: non-zero combination found, and stop
131 iFoundNonZero = true;
132 iPosPtr = 0;
133 } else {
134 // It was eEdgeUsed (i.e., digit == 1), so reset digit and continue
135 iState[ iCurPtr ] = ePossible;
136 iNewPos.subtract( iAlignment[ iCurPtr ] );
137 iSignature.addMultiple( iAlignment[ iCurPtr ], -iPosPtr-1 );
138 }
139 }
140
141 if (iFoundNonZero) {
142 //System.out.print("Reading from pos " + iPos);
143 int iLeft = iTable.get(iPos);
144 //System.out.print(" left=" + iLeft);
145 int iRight;
146 Object iRightObj = iTable.get( iNewPos );
147 if (iRightObj == null) {
148 iRight = 0;
149 iUnusedPos = true;
150 } else {
151 iRight = (Integer) iRightObj;
152 iUnusedPos = false;
153 }
154 //System.out.print(" right=" + iRight);
155 iRight += iLeft;
156 //System.out.print(" sig=" + iSignature);
157 // And store
158 //System.out.println(" Storing pos " + iNewPos);
159 // If we are storing a value at a previously unused position, make sure we use a fresh key object
160 if (iUnusedPos) {
161 iTable.put(iNewPos.clone(), iRight);
162 } else {
163 iTable.put( iNewPos, iRight);
164 }
165 }
166
167 } while (iFoundNonZero);
168
169 // Now find next entry in DP table. Use farthest unused vector
170 --iPtr;
171 while (iPtr >= 0 && iState[iPtr] != ePossible) {
172 // Undo any possible used vector that we encounter
173 if (iState[iPtr] == eUsed) {
174 iPos.subtract( iAlignment[iPtr] );
175 iState[iPtr] = eFree;
176 }
177 --iPtr;
178 }
179
180 if (iPtr == -1) {
181 // No more unused vectors, so we also fell through the edge loop above,
182 // hence iNewPos contains the final position
183 return iTable.get(iNewPos);
184 }
185
186
187 // Now use this farthest-out possible vector
188 iState[iPtr] = eUsed;
189 iPos.add( iAlignment[iPtr] );
190 if (iPtr <= iFirstNotUsed)
191 iFirstNotUsed++;
192
193 } while (true);
194
195 } // recursion
196
197
198
199 /**
200 * @returns the log of the hastings ratio
201 * I cannot guarantee it works, however :) Istvan 12/06/2003
202 * Version 2.0 It returns with a statistically correct Hastings ratio
203 */
204 public double propose(int[][] iAlignment, double[][][] iProbs, double[] iBasefreqs, int[] iParent, double[] iTau, int[][] returnedAlignment, double iP, double exponent, double gapPenalty) {
205
206 int iL1;
207 // length of the cut out alignment
208 int iPos;
209 // position of the cut out alignment (first index)
210 int iL2;
211 // length of the obtained alignment
212 int[][][] iProfile = new int[iParent.length][iParent.length][cMaxlength];
213 // profiles obtained in the proposal
214 int[][] iTempProfile = new int[iParent.length][cMaxlength];
215 // temporary profile since at the stochastic traceback we get the profile in reverse order
216 int[] iXP = new int[iParent.length], iYP = new int[iParent.length];
217 // length of a profile (X) and number of sequences involved into the profile (Y)
218 double iTempdouble1 = 0.0, iTempdouble2 = 0.0, iTempdouble3 = 0.0, iTempdouble4 = 0.0;
219 // temporal variables in the dp recursion and stochastic traceback
220 double iProposalLoglikelihood = 0.0;
221 // loglikelihood of the proposal, what else?
222 int iTempint1;
223 // temporal variables for counting gaps, iTempint1 is also used at child-sorting
224 double[][] iScoring = new double[iProbs[0].length][iProbs[0][0].length];
225 // scoring table for the dynamic programming
226 int[] iChild1 = new int[iParent.length], iChild2 = new int[iParent.length];
227 // these arrays make the work on the tree easier
228 int[] iSorter = new int[iParent.length];
229 // this array helps at sorting children so the profile of root will be the correct multiple alignment
230 boolean iTempBoole;
231 // temporary boolean variable for checking all-gap columns
232 int[][] iProfileNumber = new int[iParent.length][iAlignment.length];
233
234
235 //Testing preconditions
236 if (iP <= 0.0 || iP >= 1.0) throw new IllegalArgumentException("iP must be in range (0,1)");
237 if (exponent < 1.0) throw new IllegalArgumentException("exponent must be in range [1,+infinity)");
238 if (gapPenalty >= 0.0) throw new IllegalArgumentException("gapPenalty must be in range (-infinity,0)");
239
240 cGap = gapPenalty;
241 cBasis = exponent;
242
243
244
245 /* obtaining iChild1 and iChild2 */
246
247 for(int i = 0; i < iParent.length - 1; i++) {
248 if(iChild1[iParent[i]] == 0) {
249 iChild1[iParent[i]] = i;
250 } else {
251 iChild2[iParent[i]] = i;
252 }
253 }
254
255 // now sorting children...
256
257 for(int i = 0; i < iAlignment.length; i++)
258 iSorter[i]=i;
259 // iSorter for leaves
260 for(int i = iAlignment.length; i < iParent.length; i++) {
261 if(iSorter[iChild1[i]] > iSorter[iChild2[i]])
262 iSorter[i] = iSorter[iChild2[i]];
263 else
264 iSorter[i] = iSorter[iChild1[i]];
265 // iSorter for internal nodes, it gets the lower value
266
267 if(iSorter[iChild1[i]] > iSorter[iChild2[i]]) {
268 // iChild1[i] has the smaller iSorter value
269 // this guarantees that the root's profile contains the sequences in the proper order
270 iTempint1 = iChild1[i];
271 iChild1[i] = iChild2[i];
272 iChild2[i] = iTempint1;
273 }
274 }
275 /* cutting a part of the alignment */
276
277 // obtaining the length of the cut out part (iL1) and the first index (iPos)
278 for(iL1=1; iL1 < iAlignment[0].length && Math.random() > iP; iL1++);
279
280
281 //iL1 = 1;
282 iPos = r.nextInt(iAlignment[0].length - iL1 + 1);
283
284 //System.out.println("Window size = " + iL1);
285 // collecting the cut out sequences from the alignment
286 // they will be the first profiles
287 for(int i = 0; i < iAlignment.length; i++) {
288 iYP[i] = 1;
289 for(int j = iPos; j <iPos + iL1; j++)
290 if(iAlignment[i][j] != cGapsymbol) {
291 iProfile[i][0][iXP[i]] = iAlignment[i][j];
292 iXP[i]++;
293 }
294 // iProfileNumber
295 iProfileNumber[i][0] = i;
296 }
297
298 //Printing out the cut out sequences
299 // System.out.println("Cut out sequences:");
300 //for(int i = 0; i < iAlignment.length; i++){
301 // System.out.print(i+". ");
302 // for(int j = 0; j < iXP[i]; j++){
303 //System.out.print(iProfile[i][0][j]);
304 // }
305 // System.out.println("");
306 //}
307
308
309 /* dynamic programming algorithm for the proposal */
310
311 for(int k = iAlignment.length; k < iParent.length; k++) {
312
313 // obtaining the scoring matrix
314 for(int i = 0; i < iProbs[0].length; i++) {
315 for(int j = 0; j < iProbs[0][0].length; j++){
316 iScoring[i][j] = 0;
317 for(int l = 0; l < iProbs[0].length; l++)
318 iScoring[i][j] += iProbs[iChild1[k]][i][l] * iProbs[iChild2[k]][l][j];
319 }
320 }
321 for(int i = 0; i < iProbs[0].length; i++) {
322 for(int j = 0; j < iProbs[0][0].length; j++) {
323 // System.out.println("iScoring["+i+"]["+j+"] = "+iScoring[i][j]);
324 iScoring[i][j] = Math.log(iScoring[i][j] / iBasefreqs[j]);
325 //System.out.println("iScoring["+i+"]["+j+"] = "+iScoring[i][j]);
326 }
327 }
328
329 // initialising the table
330 iDP[0][0]=0.0;
331 for(int i = 1; i <= iXP[iChild1[k]]; i++){
332 iDP[i][0] = iDP[i - 1][0] + cGap;
333 }
334 for(int j = 1; j <= iXP[iChild2[k]]; j++) {
335 iDP[0][j] = iDP[0][j - 1] + cGap;
336 }
337
338 // main dynamic programming
339 for(int i = 1; i <= iXP[iChild1[k]]; i++)
340 for(int j = 1; j <= iXP[iChild2[k]]; j++){
341 iTempdouble1 = iDP[i - 1][j] + cGap;
342
343 iTempdouble2 = iDP[i][j - 1] + cGap;
344
345 {
346 int iCounter = 0;
347 iTempdouble3 = 0.0;
348 for(int l = 0; l < iYP[iChild1[k]]; l++)
349 for(int m = 0; m < iYP[iChild2[k]]; m++) {
350 if(iProfile[iChild1[k]][l][i - 1] != cGapsymbol && iProfile[iChild2[k]][m][j - 1] != cGapsymbol){
351 iTempdouble3 += iScoring[iProfile[iChild1[k]][l][i - 1]][iProfile[iChild2[k]][m][j - 1]];
352 iCounter++;
353 }
354 }
355 iTempdouble3 = iDP[i - 1][j - 1] + iTempdouble3/iCounter;
356 }
357
358
359 if(iTempdouble1 > iTempdouble2)
360 iDP[i][j] = iTempdouble1;
361 else
362 iDP[i][j] = iTempdouble2;
363 if(iTempdouble3 > iDP[i][j])
364 iDP[i][j] = iTempdouble3;
365 }
366
367 // Stochastic traceback, generating the new profile
368 int iI = iXP[iChild1[k]];
369 int jJ = iXP[iChild2[k]];
370 while(iI > 0 || jJ > 0) {
371 if(iI > 0){
372 iTempdouble1 = iDP[iI - 1][jJ] + cGap;
373 }
374
375
376 if(jJ > 0) {
377 iTempdouble2 = iDP[iI][jJ - 1] + cGap;
378 }
379
380 if(iI > 0 && jJ > 0) {
381 int iCounter = 0;
382 iTempdouble3 = 0.0;
383 for(int l = 0; l < iYP[iChild1[k]]; l++)
384 for(int m = 0; m < iYP[iChild2[k]]; m++) {
385 if(iProfile[iChild1[k]][l][iI - 1] != cGapsymbol && iProfile[iChild2[k]][m][jJ - 1] != cGapsymbol){
386 iTempdouble3 += iScoring[iProfile[iChild1[k]][l][iI - 1]][iProfile[iChild2[k]][m][jJ - 1]];
387 iCounter++;
388 }
389 }
390 iTempdouble3 = iDP[iI - 1][jJ - 1] + iTempdouble3/iCounter;
391 }
392
393 //System.out.println("iTempdouble1 = " + iTempdouble1);
394 //System.out.println("iTempdouble2 = " + iTempdouble2);
395 //System.out.println("iTempdouble3 = " + iTempdouble3);
396
397
398 // normalising
399 iTempdouble4 = 0.0;
400 if(iI > 0) {
401 iTempdouble4 += Math.exp(iTempdouble1 * Math.log(cBasis));
402 }
403 if(jJ > 0) {
404 iTempdouble4 += Math.exp(iTempdouble2 * Math.log(cBasis));
405 }
406 if(iI > 0 && jJ > 0) {
407 iTempdouble4 += Math.exp(iTempdouble3 * Math.log(cBasis));
408 }
409
410 //System.out.println("iTempdouble4 = " + iTempdouble4);
411
412
413 if(iI > 0)
414 iTempdouble1 = Math.exp(iTempdouble1 * Math.log(cBasis))/iTempdouble4;
415 else
416 iTempdouble1 = 0.0;
417
418 if(jJ > 0)
419 iTempdouble2 = Math.exp(iTempdouble2 * Math.log(cBasis))/iTempdouble4;
420 else
421 iTempdouble2 = 0.0;
422
423 if(iI > 0 && jJ > 0)
424 iTempdouble3 = Math.exp(iTempdouble3 * Math.log(cBasis))/iTempdouble4;
425 else
426 iTempdouble3 = 0.0;
427
428 //System.out.println("Sum: " + (iTempdouble1+iTempdouble2+iTempdouble3));
429
430
431 // stochastic step
432 iTempdouble4 = Math.random();
433 if(iTempdouble4 < iTempdouble1) {
434 // the probability is in the denominator, that's why we substract
435 iProposalLoglikelihood -= Math.log(iTempdouble1);
436 for(int l = 0; l < iYP[iChild1[k]]; l++) {
437 iTempProfile[l][iXP[k]] = iProfile[iChild1[k]][l][iI - 1];
438 }
439 for(int l = iYP[iChild1[k]]; l < iYP[iChild1[k]] + iYP[iChild2[k]]; l++) {
440 iTempProfile[l][iXP[k]] = cGapsymbol;
441 }
442 iXP[k]++;
443 iI--;
444 // if(iI == -1) { // it must almost never happen, however, if it does happen in a magical way...
445 // iXP[k]--;
446 // iI++;
447 //}
448 }
449 else if(iTempdouble4 < iTempdouble1 + iTempdouble2) {
450 // the probability is in the denominator, that's why we substract
451 iProposalLoglikelihood -= Math.log(iTempdouble2);
452 for(int l = 0; l < iYP[iChild1[k]]; l++) {
453 iTempProfile[l][iXP[k]] = cGapsymbol;
454 }
455 for(int l = iYP[iChild1[k]]; l < iYP[iChild1[k]] + iYP[iChild2[k]]; l++) {
456 iTempProfile[l][iXP[k]] = iProfile[iChild2[k]][l - iYP[iChild1[k]]][jJ - 1];
457 }
458 iXP[k]++;
459 jJ--;
460 //if(jJ == -1) { // it must almost never happen, however, if it does happen in a magical way...
461 // iXP[k]--;
462 // jJ++;
463 //}
464 }
465 else {
466 if(iTempdouble3 <= 0) {
467 System.out.println("How the fuck could it happen?");
468 }
469 // the probability is in the denominator, that's why we substract
470 iProposalLoglikelihood -= Math.log(iTempdouble3);
471 for(int l = 0; l < iYP[iChild1[k]]; l++)
472 iTempProfile[l][iXP[k]] = iProfile[iChild1[k]][l][iI - 1];
473 for(int l = iYP[iChild1[k]]; l < iYP[iChild1[k]] + iYP[iChild2[k]]; l++)
474 iTempProfile[l][iXP[k]] = iProfile[iChild2[k]][l - iYP[iChild1[k]]][jJ - 1];
475 iXP[k]++;
476 jJ--;
477 iI--;
478 // if(jJ == -1 || iI == -1) { // it must almost never happen, however, if it does happen in a magical way...
479 // iXP[k]--;
480 // iI++;
481 // jJ++;
482 //}
483
484 }
485
486
487
488 }// finished the traceback, iI, jJ
489
490
491 //System.out.println("iProposalLoglikelihood:"+iProposalLoglikelihood);
492
493 iYP[k] = iYP[iChild1[k]] + iYP[iChild2[k]];
494
495 //Making new iProfileNumbers
496 for(int i = 0; i < iYP[iChild1[k]]; i++)
497 iProfileNumber[k][i] = iProfileNumber[iChild1[k]][i];
498 for(int i = iYP[iChild1[k]]; i < iYP[iChild1[k]] + iYP[iChild2[k]]; i++)
499 iProfileNumber[k][i] = iProfileNumber[iChild2[k]][i - iYP[iChild1[k]]];
500
501 // now reversing the profile, loading from the iTempProfile into iProfile
502
503 for(int i = 0; i < iXP[k]; i++) {
504 for(int j = 0; j < iYP[k]; j++) {
505 iProfile[k][j][i] = iTempProfile[j][iXP[k] - i - 1];
506 }
507 }
508
509 } // k, index for the internal nodes, iProfile[root] is the obtained multiple alignment
510
511 iL2 = iXP[iParent.length - 1];
512
513 // putting the new sub-alignment into the new proposal
514
515 //System.out.println("iL1 = " + iL1);
516 //System.out.println("iL2 = " + iL2);
517 //System.out.println("iPos = " + iPos);
518 //System.out.println("iParent.length = " + iParent.length);
519 //System.out.println("iAlignment.length = " + iAlignment.length);
520 //System.out.println("returnedAlignment.length = " + returnedAlignment.length);
521 //System.out.println("iAlignment[0].length = " + iAlignment[0].length);
522
523
524
525 for (int i = 0; i < iAlignment.length; i++) {
526 returnedAlignment[i] = new int[iAlignment[0].length + iL2 - iL1];
527 //System.out.println("returnedAlignment[i].length = " + returnedAlignment[i].length);
528
529 }
530
531 for(int i = 0; i < iPos; i++) {
532 for(int j = 0; j < iAlignment.length; j++) {
533 returnedAlignment[j][i] = iAlignment[j][i];
534 }
535 }
536 for(int i = iPos; i < iPos + iL2; i++) {
537 for(int j = 0; j < iAlignment.length; j++) {
538 //System.out.println("i = " + i + ", j = " + j);
539 returnedAlignment[iProfileNumber[iParent.length - 1][j]][i] = iProfile[iParent.length - 1][j][i - iPos];
540 }
541 }
542
543
544 //Printing the new alignment
545 //System.out.println("Proposed alignment");
546 // for(int i = 0; i < iAlignment.length; i++){
547 // System.out.print(i+". ");
548 // for(int j = 0; j < iL2; j++){
549 //System.out.print(iProfile[iParent.length-1][i][j]);
550 // }
551 // System.out.println("");
552 //}
553
554
555
556
557 for(int i = iPos + iL1; i < iAlignment[0].length; i++) {
558 for(int j = 0; j < iAlignment.length; j++) {
559 returnedAlignment[j][i + iL2 -iL1] = iAlignment[j][i];
560 }
561 }
562
563
564 // I involve the combinatorical factor into the Hastings ratio
565 // countPaths(int[][] iInputAlignment, int iStartCol, int iEndCol)
566
567
568 int iCF = countPaths(returnedAlignment,iPos,iPos+iL2-1);
569 if(iCF == -1){
570 System.out.println("**** HEY ALEXEI! IT'S A FUCKING FUCKED ALIGNMENT! ****");
571 iProposalLoglikelihood -= Math.exp(100.0);
572 iCF = 1;
573 }
574
575
576 iProposalLoglikelihood -= Math.log((double)iCF);
577
578
579 iCF = countPaths(iAlignment,iPos,iPos+iL1-1);
580 if(iCF == -1){
581 System.out.println("**** HEY ALEXEI! IT'S A FUCKING FUCKED ALIGNMENT! ****");
582 iProposalLoglikelihood -= Math.exp(100.0);
583 iCF = 1;
584 }
585 iProposalLoglikelihood += Math.log((double)iCF);
586
587
588 /*
589 Now I'm going to calculate the Hastings ratio.
590 To do that, I must make the profile of each node
591 I copy the multiple alignment into profiles, and then delete the all-gaps columns
592 The traceback is easy: based on checking all-gap subcolumns
593 */
594
595
596 // collecting the sequences from the alignment -- now without cut out!
597 // they will be the first profiles
598 for(int i = 0; i < iAlignment.length; i++) {
599 iYP[i] = 1;
600 for(int j = iPos; j <iPos + iL1; j++)
601 iProfile[i][0][j - iPos] = iAlignment[i][j];
602 }
603
604
605 // profiles at internal nodes are obtained by merging profiles of children
606 for(int i = iAlignment.length; i < iParent.length; i++) {
607 iYP[i] = iYP[iChild1[i]] + iYP[iChild2[i]];
608 for(int j = 0; j < iL1; j++) {
609 for(int k = 0; k < iYP[iChild1[i]]; k++)
610 iProfile[i][k][j] = iProfile[iChild1[i]][k][j];
611 for(int k = iYP[iChild1[i]]; k < iYP[iChild1[i]] + iYP[iChild2[i]]; k++)
612 iProfile[i][k][j] = iProfile[iChild2[i]][k - iYP[iChild1[i]]][j];
613 }
614 }
615
616
617 // and now -- cutting out all-gap columns
618 for(int i = 0; i < iParent.length; i++){
619 // first i load it into the temporary profile and then back...
620 for(int j = 0; j < iL1; j++)
621 for(int k = 0; k < iYP[i]; k++)
622 iTempProfile[k][j] = iProfile[i][k][j];
623
624
625 iXP[i] = 0;
626 for(int j = 0; j < iL1; j++){
627 // checking for all-gap;
628 boolean notAllGaps = false;
629 for(int k = 0; k < iYP[i]; k++){
630 if (iTempProfile[k][j] != cGapsymbol) {
631 notAllGaps = true;
632 break;
633 }
634 }
635
636 if(notAllGaps) {
637 // if not all-gap then
638 for(int k = 0; k < iYP[i]; k++)
639 iProfile[i][k][iXP[i]] = iTempProfile[k][j];
640 iXP[i]++;
641 }
642 }
643
644 }
645
646
647 // profiles are ready, so now the DP
648 for(int k = iAlignment.length; k < iParent.length; k++) {
649
650
651 // obtaining the scoring matrix
652 for(int i = 0; i < iProbs[0].length; i++)
653 for(int j = 0; j < iProbs[0][0].length; j++){
654 iScoring[i][j] = 0.0;
655 for(int l = 0; l < iProbs[0].length; l++)
656 iScoring[i][j] += iProbs[iChild1[k]][i][l] * iProbs[iChild2[k]][l][j];
657 }
658 for(int i = 0; i < iProbs[0].length; i++)
659 for(int j = 0; j < iProbs[0][0].length; j++)
660 iScoring[i][j] = Math.log(iScoring[i][j] / iBasefreqs[j]);
661
662
663 // initialising the table
664 iDP[0][0]=0.0;
665 for(int i = 1; i <= iXP[iChild1[k]]; i++){
666 iDP[i][0] = iDP[i - 1][0] + cGap;
667 }
668 for(int j = 1; j <= iXP[iChild2[k]]; j++) {
669 iTempdouble2 = 0.0;
670 for(int m = 0; m < iYP[iChild2[k]]; m++)
671 if(iProfile[iChild2[k]][m][j - 1] != cGapsymbol)
672 iTempdouble2 += cGap;
673 iDP[0][j] = iDP[0][j - 1] + cGap;
674 }
675
676
677 // main dynamic programming
678 for(int i = 1; i <= iXP[iChild1[k]]; i++)
679 for(int j = 1; j <= iXP[iChild2[k]]; j++){
680 iTempdouble1 = iDP[i - 1][j] + cGap;
681
682
683 iTempdouble2 = 0.0;
684 for(int m = 0; m < iYP[iChild2[k]]; m++)
685 if(iProfile[iChild2[k]][m][j - 1] != cGapsymbol)
686 iTempdouble2 += cGap;
687 iTempdouble2 = iDP[i][j - 1] + cGap;
688
689 {
690 int iCounter = 0;
691 iTempdouble3 = 0.0;
692 for(int l = 0; l < iYP[iChild1[k]]; l++)
693 for(int m = 0; m < iYP[iChild2[k]]; m++) {
694 if(iProfile[iChild1[k]][l][i - 1] != cGapsymbol && iProfile[iChild2[k]][m][j - 1] != cGapsymbol){
695 iTempdouble3 += iScoring[iProfile[iChild1[k]][l][i - 1]][iProfile[iChild2[k]][m][j - 1]];
696 iCounter++;
697 }
698 }
699 iTempdouble3 = iDP[i - 1][j - 1] + iTempdouble3/iCounter;
700 }
701
702 if(iTempdouble1 > iTempdouble2)
703 iDP[i][j] = iTempdouble1;
704 else
705 iDP[i][j] = iTempdouble2;
706 if(iTempdouble3 > iDP[i][j])
707 iDP[i][j] = iTempdouble3;
708 }
709
710
711 // traceback, now not stochastic
712 int i = iXP[iChild1[k]];
713 int j = iXP[iChild2[k]];
714 int n = iXP[k];
715 while(n > 0) {
716
717
718 if(i > 0){
719 //System.out.println("i = " + i + "; j = " + j);
720
721 iTempdouble1 = iDP[i - 1][j] + cGap;
722 }
723
724
725
726 if(j > 0) {
727 //System.out.println("i = " + i + "; j = " + j);
728
729 iTempdouble2 = iDP[i][j - 1] + cGap;
730 }
731
732
733 if(i > 0 && j > 0) {
734 int iCounter = 0;
735 iTempdouble3 = 0.0;
736 for(int l = 0; l < iYP[iChild1[k]]; l++)
737 for(int m = 0; m < iYP[iChild2[k]]; m++) {
738 if(iProfile[iChild1[k]][l][i - 1] != cGapsymbol && iProfile[iChild2[k]][m][j - 1] != cGapsymbol){
739 iTempdouble3 += iScoring[iProfile[iChild1[k]][l][i - 1]][iProfile[iChild2[k]][m][j - 1]];
740 iCounter++;
741 }
742 }
743 iTempdouble3 = iDP[i - 1][j - 1] + iTempdouble3/iCounter;
744 }
745
746
747 // normalising
748 iTempdouble4 = 0.0;
749 if(i > 0)
750 iTempdouble4 += Math.exp(iTempdouble1 * Math.log(cBasis));
751 if(j > 0)
752 iTempdouble4 += Math.exp(iTempdouble2 * Math.log(cBasis));
753 if(i > 0 && j > 0)
754 iTempdouble4 += Math.exp(iTempdouble3 * Math.log(cBasis));
755
756
757 if(i > 0)
758 iTempdouble1 = Math.exp(iTempdouble1 * Math.log(cBasis))/iTempdouble4;
759 else
760 iTempdouble1 = 0.0;
761
762
763 if(j > 0)
764 iTempdouble2 = Math.exp(iTempdouble2 * Math.log(cBasis))/iTempdouble4;
765 else
766 iTempdouble2 = 0.0;
767
768
769 if(i > 0 && j > 0)
770 iTempdouble3 = Math.exp(iTempdouble3 * Math.log(cBasis))/iTempdouble4;
771 else
772 iTempdouble3 = 0.0;
773
774 //System.out.println("Sum in the second DP: " + (iTempdouble1+iTempdouble2+iTempdouble3));
775
776
777 // finding the proper step (all-gap subcolumns tell us the way
778 iTempBoole = true;
779 for(int l = 0; l < iYP[iChild1[k]] && iTempBoole; l++)
780 iTempBoole = (iProfile[k][l][n-1] == cGapsymbol);
781
782
783 if(iTempBoole) {
784 // all the first part is gap, so we decrease j
785 j--;
786 iProposalLoglikelihood += Math.log(iTempdouble2);
787 n--;
788 }
789 else {
790 iTempBoole = true;
791 for(int l = iYP[iChild1[k]]; l < iYP[iChild1[k]] + iYP[iChild2[k]] && iTempBoole; l++)
792 iTempBoole = (iProfile[k][l][n-1] == cGapsymbol);
793 if(iTempBoole) {
794 // all the second part is gap, so we decrease i
795 i--;
796 iProposalLoglikelihood += Math.log(iTempdouble1);
797 n--;
798 }
799 else {
800 // no all-gap subcolumn, decreasing both i and j;
801 i--;
802 j--;
803 iProposalLoglikelihood += Math.log(iTempdouble3);
804 n--;
805 }
806 }
807
808
809 } // end of traceback
810
811 //System.out.println("iProposalLogLikelihood2=" + iProposalLoglikelihood);
812 } // end of k
813
814 //System.out.println("iL1=" + iL1);
815 //System.out.println("iL2=" + iL2);
816 return iProposalLoglikelihood + (iL1 - iL2)*Math.log(1.0-iP);
817 }
818 }