| 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 |
package dr.oldevomodel.indel; |
| 27 |
|
| 28 |
|
| 29 |
import dr.evolution.alignment.Alignment; |
| 30 |
import dr.evolution.datatype.DataType; |
| 31 |
import dr.evolution.tree.NodeRef; |
| 32 |
import dr.evolution.tree.Tree; |
| 33 |
import dr.oldevomodel.substmodel.SubstitutionModel; |
| 34 |
import dr.math.BFloat; |
| 35 |
|
| 36 |
import java.util.HashMap; |
| 37 |
|
| 38 |
public class HomologyRecursion { |
| 39 |
|
| 40 |
|
| 41 |
IntMathVec[] iAlignment; |
| 42 |
|
| 43 |
|
| 44 |
int[][] iSequences; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private int[] iParent; |
| 52 |
|
| 53 |
|
| 54 |
private double[] iTau; |
| 55 |
|
| 56 |
|
| 57 |
double iLambda; |
| 58 |
double iMu; |
| 59 |
|
| 60 |
|
| 61 |
int iNumNucs; |
| 62 |
|
| 63 |
|
| 64 |
double[][][] iTrans; |
| 65 |
|
| 66 |
|
| 67 |
double[] iEquil; |
| 68 |
|
| 69 |
|
| 70 |
private double iH[], iN[], iB[], iE[]; |
| 71 |
|
| 72 |
|
| 73 |
private double iInitial; |
| 74 |
|
| 75 |
|
| 76 |
private NativeTreeLikelihood iNativeMethod; |
| 77 |
|
| 78 |
|
| 79 |
static private final int eFree = 0, ePossible = 1, eEdgeUsed = 2, eUsed = 3; |
| 80 |
|
| 81 |
|
| 82 |
static private final int cMaxUnalignDimension = 10; |
| 83 |
|
| 84 |
|
| 85 |
static private final double MIN_EDGE_LENGTH = 1e-3; |
| 86 |
|
| 87 |
|
| 88 |
static int sBigUnalignableRegion = 0; |
| 89 |
|
| 90 |
String PrintDouble(double[] d) { |
| 91 |
String str = ""; |
| 92 |
for(double aD : d) { |
| 93 |
str = str + aD + " "; |
| 94 |
} |
| 95 |
return str; |
| 96 |
} |
| 97 |
|
| 98 |
void checkConsistency() { |
| 99 |
|
| 100 |
int iCols = iAlignment.length; |
| 101 |
int iRows = iAlignment[0].iV.length; |
| 102 |
String iErr = ""; |
| 103 |
IntMathVec iPos = new IntMathVec( iRows ); |
| 104 |
for (int i=0; i<iCols; i++) { |
| 105 |
for (int j=0; j<iRows; j++) { |
| 106 |
if (iAlignment[i].iV[j] != 0 && iAlignment[i].iV[j] != 1) { |
| 107 |
iErr = "Non-0/1 emissions."; |
| 108 |
} |
| 109 |
} |
| 110 |
iPos.add( iAlignment[i] ); |
| 111 |
} |
| 112 |
for (int j=0; j<iRows; j++) { |
| 113 |
if (iSequences[j].length != iPos.iV[j]) { |
| 114 |
iErr = "Bad sequences length"; |
| 115 |
} |
| 116 |
for (int i=0; i<iPos.iV[j]; i++) { |
| 117 |
if ((iSequences[j][i]<0)||(iSequences[j][i]>=iNumNucs)) { |
| 118 |
iErr = "Nucleotide codes in iSequences not in range, " + iSequences[j][i] + "not in [0,"+(iNumNucs-1)+"]"; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
int[] iChildren = new int[ iParent.length ]; |
| 123 |
for (int i=0; i<iParent.length-1; i++) |
| 124 |
iChildren[ iParent[i] ]++; |
| 125 |
for (int i=0; i<iRows; i++) |
| 126 |
if (iChildren[i] != 0) { |
| 127 |
iErr = "Num tips does not correspond to num sequences, or bad tree"; |
| 128 |
} |
| 129 |
for (int i=iRows; i<iParent.length; i++) |
| 130 |
if (iChildren[i] != 2) { |
| 131 |
iErr = "Bad tree - not binary? Or too many tips?"; |
| 132 |
} |
| 133 |
if (iParent[iParent.length-1] != -1) { |
| 134 |
iErr = "Bad tree - root not in final position, or not labeled -1"; |
| 135 |
} |
| 136 |
for (int i=0; i<iParent.length-1; i++) { |
| 137 |
if (iTau[i] < MIN_EDGE_LENGTH) { |
| 138 |
iErr = ""; |
| 139 |
iTau[i] = MIN_EDGE_LENGTH; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
if ( !iErr.equals("") ) |
| 148 |
System.out.println(iErr); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public HomologyRecursion() { |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
public void init(Tree tree, Alignment alignment, SubstitutionModel substModel, |
| 158 |
double mutationRate, double lengthDistr, double deathRate) { |
| 159 |
|
| 160 |
|
| 161 |
initTree(tree, mutationRate); |
| 162 |
|
| 163 |
int[] treeIndex = new int[tree.getTaxonCount()]; |
| 164 |
for (int i =0; i < treeIndex.length; i++) { |
| 165 |
treeIndex[i] = tree.getTaxonIndex(alignment.getTaxonId(i)); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
initAlignment(alignment, treeIndex); |
| 171 |
|
| 172 |
|
| 173 |
initSequences(alignment, treeIndex); |
| 174 |
|
| 175 |
|
| 176 |
initSubstitutionModel(substModel); |
| 177 |
|
| 178 |
|
| 179 |
iLambda = deathRate*lengthDistr; |
| 180 |
iMu = deathRate; |
| 181 |
|
| 182 |
|
| 183 |
DataType dataType = substModel.getDataType(); |
| 184 |
iNumNucs = dataType.getStateCount(); |
| 185 |
|
| 186 |
|
| 187 |
initTKF91(); |
| 188 |
|
| 189 |
|
| 190 |
checkConsistency(); |
| 191 |
|
| 192 |
|
| 193 |
iNativeMethod = new NativeTreeLikelihood(); |
| 194 |
iNativeMethod.init(iNumNucs, cMaxUnalignDimension, iParent, iEquil, iTrans, iSequences, iN, iH, iE, iB); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
private void initTree(Tree tree, double mutationRate) { |
| 199 |
iParent = new int[tree.getNodeCount()]; |
| 200 |
iTau = new double[tree.getNodeCount()-1]; |
| 201 |
populate(tree, tree.getRoot(), new int[] {tree.getExternalNodeCount()}, mutationRate); |
| 202 |
iParent[tree.getNodeCount()-1] = -1; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private void initSubstitutionModel(SubstitutionModel model) { |
| 210 |
|
| 211 |
DataType dataType = model.getDataType(); |
| 212 |
int stateCount = dataType.getStateCount(); |
| 213 |
|
| 214 |
iTrans = new double[iTau.length][stateCount][stateCount]; |
| 215 |
|
| 216 |
double[] transProb = new double[stateCount*stateCount]; |
| 217 |
int count; |
| 218 |
for (int i =0; i < iTau.length; i++) { |
| 219 |
model.getTransitionProbabilities(iTau[i], transProb); |
| 220 |
count = 0; |
| 221 |
for (int j = 0; j < stateCount; j++) { |
| 222 |
for (int k = 0; k < stateCount; k++) { |
| 223 |
iTrans[i][j][k] = transProb[count]; |
| 224 |
count += 1; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
iEquil = new double[stateCount]; |
| 231 |
for (int k = 0; k < stateCount; k++) { |
| 232 |
iEquil[k] = model.getFrequencyModel().getFrequency(k); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
private void initAlignment(Alignment alignment, int[] treeIndex) { |
| 240 |
|
| 241 |
int numSeqs = alignment.getSequenceCount(); |
| 242 |
int numSites = alignment.getSiteCount(); |
| 243 |
DataType dataType = alignment.getDataType(); |
| 244 |
int numStates = dataType.getStateCount(); |
| 245 |
|
| 246 |
iAlignment = new IntMathVec[numSites]; |
| 247 |
|
| 248 |
int[] column = new int[numSeqs]; |
| 249 |
for (int i =0; i < numSites; i++) { |
| 250 |
for (int j = 0; j < numSeqs; j++) { |
| 251 |
column[treeIndex[j]] = ((alignment.getState(j, i) >= numStates) ? 0 : 1); |
| 252 |
} |
| 253 |
iAlignment[i] = new IntMathVec(column); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
private void initSequences(Alignment alignment, int[] treeIndex) { |
| 261 |
|
| 262 |
int numSeqs = alignment.getSequenceCount(); |
| 263 |
DataType dataType = alignment.getDataType(); |
| 264 |
int numStates = dataType.getStateCount(); |
| 265 |
|
| 266 |
iSequences = new int[numSeqs][]; |
| 267 |
|
| 268 |
for (int i = 0; i < numSeqs; i++) { |
| 269 |
|
| 270 |
int seqLength = 0; |
| 271 |
for (int j =0; j < alignment.getSiteCount(); j++) { |
| 272 |
int state = alignment.getState(i, j); |
| 273 |
if (state>=0 && state<numStates) { seqLength += 1;} |
| 274 |
|
| 275 |
} |
| 276 |
iSequences[treeIndex[i]] = new int[seqLength]; |
| 277 |
int count = 0; |
| 278 |
for (int j =0; j < alignment.getSiteCount(); j++) { |
| 279 |
int state = alignment.getState(i, j); |
| 280 |
if (state>=0 && state<numStates) { |
| 281 |
iSequences[treeIndex[i]][count] = state; |
| 282 |
count += 1; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
private int populate(Tree tree, NodeRef node, int[] current, double mutationRate) { |
| 293 |
|
| 294 |
int nodeNumber = node.getNumber(); |
| 295 |
|
| 296 |
|
| 297 |
if (tree.isExternal(node)) { |
| 298 |
iTau[nodeNumber] = |
| 299 |
(tree.getNodeHeight(tree.getParent(node)) - tree.getNodeHeight(node)) * mutationRate; |
| 300 |
return nodeNumber; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
int[] childNumbers = new int[tree.getChildCount(node)]; |
| 305 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 306 |
childNumbers[i] = populate(tree, tree.getChild(node, i), current, mutationRate); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
nodeNumber = current[0]; |
| 311 |
|
| 312 |
if (!tree.isRoot(node)) { |
| 313 |
|
| 314 |
iTau[nodeNumber] = |
| 315 |
(tree.getNodeHeight(tree.getParent(node)) - tree.getNodeHeight(node)) * mutationRate; |
| 316 |
} |
| 317 |
|
| 318 |
current[0] += 1; |
| 319 |
|
| 320 |
|
| 321 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 322 |
iParent[childNumbers[i]] = nodeNumber; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
return nodeNumber; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public void initTKF91() { |
| 335 |
|
| 336 |
int iNumNodes = iParent.length; |
| 337 |
double iBeta[] = new double[iNumNodes]; |
| 338 |
iB = new double[iNumNodes]; |
| 339 |
iE = new double[iNumNodes]; |
| 340 |
iH = new double[iNumNodes]; |
| 341 |
iN = new double[iNumNodes]; |
| 342 |
iInitial = 1.0; |
| 343 |
for (int i=0; i<iNumNodes; i++) { |
| 344 |
if (i==iNumNodes-1) { |
| 345 |
|
| 346 |
iBeta[ i ] = 1.0/iMu; |
| 347 |
iH[i] = 0.0; |
| 348 |
} else { |
| 349 |
|
| 350 |
iBeta[ i ] = Math.exp((iLambda-iMu)*iTau[i]); |
| 351 |
iBeta[ i ] = (1.0-iBeta[i])/(iMu-iLambda*iBeta[i]); |
| 352 |
iH[i] = Math.exp(-iMu*iTau[i])*(1.0-iLambda*iBeta[i]); |
| 353 |
} |
| 354 |
iB[i] = iLambda*iBeta[i]; |
| 355 |
iE[i] = iMu*iBeta[i]; |
| 356 |
iN[i] = (1.0-iMu*iBeta[i])*(1.0-iB[i]) - iH[i]; |
| 357 |
iInitial *= (1.0 - iB[i]); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
private double treeRecursion(IntMathVec iSignature, IntMathVec iPos) { |
| 367 |
|
| 368 |
|
| 369 |
if (iNativeMethod.isAvailable()) { |
| 370 |
return iNativeMethod.treeRecursion(iSignature, iPos); |
| 371 |
} |
| 372 |
|
| 373 |
int iLeaves = iSignature.iV.length; |
| 374 |
int iNumNodes = iParent.length; |
| 375 |
int[] iHom = new int[ iNumNodes ]; |
| 376 |
int[] iHomNum = new int[ iNumNodes ]; |
| 377 |
int[] iHomMultiplicity = new int[ cMaxUnalignDimension+1 ]; |
| 378 |
int[] iChild1 = new int[ iNumNodes ], iChild2 = new int[ iNumNodes ]; |
| 379 |
double[][] iFh = new double[ iNumNodes ][], iFi = new double[ iNumNodes ][]; |
| 380 |
|
| 381 |
|
| 382 |
for (int i=0; i<iNumNodes; i++) { |
| 383 |
iFh[i] = new double[ iNumNucs ]; |
| 384 |
iFi[i] = new double[ iNumNucs + 1]; |
| 385 |
} |
| 386 |
for (int i=0; i<iLeaves; i++) { |
| 387 |
iHomMultiplicity[ iSignature.iV[ i ] ]++; |
| 388 |
iHom[ i ] = iSignature.iV[ i ]; |
| 389 |
if (iSignature.iV[i] == 0) |
| 390 |
iHomNum[ i ] = 0; |
| 391 |
else |
| 392 |
iHomNum[ i ] = 1; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
boolean iClashingHomology = false; |
| 398 |
for (int i=0; i<iNumNodes-1; i++) { |
| 399 |
if ((iHomNum[i] == iHomMultiplicity[ iHom[i] ]) || (iHom[i] == 0)) { |
| 400 |
|
| 401 |
} else { |
| 402 |
if (iHom[ iParent[ i ]] == 0 || iHom[ iParent[ i ]] == iHom[i]) { |
| 403 |
|
| 404 |
iHom[iParent[i]] = iHom[i]; |
| 405 |
|
| 406 |
iHomNum[iParent[i]] += iHomNum[i]; |
| 407 |
} else { |
| 408 |
|
| 409 |
iClashingHomology = true; |
| 410 |
} |
| 411 |
} |
| 412 |
if (iChild1[ iParent[ i ] ] == 0) |
| 413 |
iChild1[ iParent[ i ] ] = i; |
| 414 |
else |
| 415 |
iChild2[ iParent[ i ] ] = i; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
if (iClashingHomology) { |
| 420 |
return 0.0; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
for (int i=0; i<iLeaves; i++) { |
| 425 |
if (iSignature.iV[i] == 0) { |
| 426 |
|
| 427 |
iFi[i][iNumNucs] = 1.0; |
| 428 |
} else { |
| 429 |
|
| 430 |
iFh[i][ iSequences[i][iPos.iV[i]] ] = 1.0; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
for (int i=iLeaves; i<iNumNodes; i++) { |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
if ((iHom[i] != 0) && (iHom[i] == iHom[iChild1[i]]) && (iHom[i] == iHom[iChild2[i]])) { |
| 448 |
|
| 449 |
|
| 450 |
for (int j=0; j<iNumNucs; j++) { |
| 451 |
double iL = 0.0; |
| 452 |
double iR = 0.0; |
| 453 |
for (int k=0; k<iNumNucs; k++) { |
| 454 |
iL += iFh[iChild1[i]][k] * iH[iChild1[i]] * iTrans[iChild1[i]][j][k]; |
| 455 |
iR += iFh[iChild2[i]][k] * iH[iChild2[i]] * iTrans[iChild2[i]][j][k]; |
| 456 |
} |
| 457 |
iFh[i][j] = iL*iR; |
| 458 |
} |
| 459 |
|
| 460 |
} else if (iHom[i] != 0) { |
| 461 |
|
| 462 |
|
| 463 |
int iChildH, iChildI; |
| 464 |
if (iHom[i] == iHom[iChild1[i]]) { |
| 465 |
iChildH = iChild1[i]; |
| 466 |
iChildI = iChild2[i]; |
| 467 |
} else { |
| 468 |
iChildH = iChild2[i]; |
| 469 |
iChildI = iChild1[i]; |
| 470 |
} |
| 471 |
|
| 472 |
for (int j=0; j<iNumNucs; j++) { |
| 473 |
double iL = 0.0; |
| 474 |
double iR = iE[iChildI] * iFi[iChildI][iNumNucs]; |
| 475 |
for (int k=0; k<iNumNucs; k++) { |
| 476 |
|
| 477 |
iL += iFh[iChildH][k] * iH[iChildH] * iTrans[iChildH][j][k]; |
| 478 |
iR += |
| 479 |
(iFh[iChildI][k] + iFi[iChildI][k]) * (iN[iChildI] - iE[iChildI]*iB[iChildI]) * iEquil[k] + |
| 480 |
iFi[iChildI][k] * iH[iChildI] * iTrans[iChildI][j][k]; |
| 481 |
|
| 482 |
} |
| 483 |
|
| 484 |
iFh[i][j] = iL*iR; |
| 485 |
|
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
} else { |
| 490 |
|
| 491 |
int iC1 = iChild1[i]; |
| 492 |
int iC2 = iChild2[i]; |
| 493 |
|
| 494 |
|
| 495 |
for (int j=0; j<iNumNucs; j++) { |
| 496 |
double iL1 = 0.0, iL2 = 0.0; |
| 497 |
double iR1 = iE[iC1] * iFi[iC1][iNumNucs]; |
| 498 |
double iR2 = iE[iC2] * iFi[iC2][iNumNucs]; |
| 499 |
|
| 500 |
|
| 501 |
for (int k=0; k<iNumNucs; k++) { |
| 502 |
iL1 += iFh[iC1][k] * iH[iC1] * iTrans[iC1][j][k]; |
| 503 |
iL2 += iFh[iC2][k] * iH[iC2] * iTrans[iC2][j][k]; |
| 504 |
iR1 += |
| 505 |
(iFh[iC1][k] + iFi[iC1][k]) * (iN[iC1] - iE[iC1]*iB[iC1]) * iEquil[k] + |
| 506 |
iFi[iC1][k] * iH[iC1] * iTrans[iC1][j][k]; |
| 507 |
iR2 += |
| 508 |
(iFh[iC2][k] + iFi[iC2][k]) * (iN[iC2] - iE[iC2]*iB[iC2]) * iEquil[k] + |
| 509 |
iFi[iC2][k] * iH[iC2] * iTrans[iC2][j][k]; |
| 510 |
} |
| 511 |
iFh[i][j] = iL1*iR2 + iL2*iR1; |
| 512 |
iFi[i][j] = iR1*iR2; |
| 513 |
} |
| 514 |
|
| 515 |
double iL = iFi[iC1][iNumNucs]; |
| 516 |
double iR = iFi[iC2][iNumNucs]; |
| 517 |
for (int j=0; j<iNumNucs; j++) { |
| 518 |
|
| 519 |
iL -= iB[iC1] * (iFi[iC1][j] + iFh[iC1][j]) * iEquil[j]; |
| 520 |
iR -= iB[iC2] * (iFi[iC2][j] + iFh[iC2][j]) * iEquil[j]; |
| 521 |
|
| 522 |
} |
| 523 |
iFi[i][iNumNucs] = iL*iR; |
| 524 |
} |
| 525 |
|
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
int iRoot = iNumNodes-1; |
| 531 |
double iResult = iFi[iRoot][iNumNucs]; |
| 532 |
for (int i=0; i<iNumNucs; i++) |
| 533 |
iResult -= (iFi[iRoot][i] + iFh[iRoot][i]) * iB[iRoot] * iEquil[i]; |
| 534 |
|
| 535 |
return iResult; |
| 536 |
|
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
public double recursion() { |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
int iLen = iAlignment.length; |
| 549 |
int iLeaves = iAlignment[0].iV.length; |
| 550 |
int iFirstNotUsed = 0; |
| 551 |
int iState[] = new int[ iLen ]; |
| 552 |
IntMathVec iPos = new IntMathVec( iLeaves ); |
| 553 |
HashMap<IntMathVec, BFloat> iTable = new HashMap<IntMathVec, BFloat>(); |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
double iNullEmissionFac = treeRecursion( iPos, iPos ); |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
iTable.put( iPos, new BFloat(iInitial / iNullEmissionFac) ); |
| 562 |
|
| 563 |
|
| 564 |
int[] iPossibles = new int[cMaxUnalignDimension]; |
| 565 |
|
| 566 |
do { |
| 567 |
|
| 568 |
|
| 569 |
IntMathVec iMask = new IntMathVec( iLeaves ); |
| 570 |
int iPtr; |
| 571 |
int iNumPossible = 0; |
| 572 |
for (iPtr = iFirstNotUsed; iMask.zeroEntry() && iPtr<iLen; iPtr++) { |
| 573 |
if (iState[ iPtr ] != eUsed) { |
| 574 |
if (iMask.innerProduct( iAlignment[iPtr] ) == 0) { |
| 575 |
iState[ iPtr ] = ePossible; |
| 576 |
|
| 577 |
if (iNumPossible == cMaxUnalignDimension) { |
| 578 |
|
| 579 |
sBigUnalignableRegion++; |
| 580 |
System.err.println("We bailed out cause it was hairy: iNumPossible=" + iNumPossible); |
| 581 |
return Double.NEGATIVE_INFINITY; |
| 582 |
} |
| 583 |
iPossibles[iNumPossible++] = iPtr; |
| 584 |
} |
| 585 |
iMask.add( iAlignment[iPtr] ); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
IntMathVec iNewPos = new IntMathVec( iPos ); |
| 593 |
IntMathVec iSignature = new IntMathVec( iPos.iV.length ); |
| 594 |
int iPosPtr; |
| 595 |
boolean iUnusedPos; |
| 596 |
boolean iFoundNonZero; |
| 597 |
|
| 598 |
do { |
| 599 |
|
| 600 |
|
| 601 |
iFoundNonZero = false; |
| 602 |
for (iPosPtr = iNumPossible - 1; iPosPtr >= 0; --iPosPtr) { |
| 603 |
int iCurPtr = iPossibles[ iPosPtr ]; |
| 604 |
if (iState[ iCurPtr ] == ePossible) { |
| 605 |
iState[ iCurPtr ] = eEdgeUsed; |
| 606 |
iNewPos.add( iAlignment[ iCurPtr ] ); |
| 607 |
|
| 608 |
iSignature.addMultiple( iAlignment[ iCurPtr ], iPosPtr+1 ); |
| 609 |
|
| 610 |
iFoundNonZero = true; |
| 611 |
iPosPtr = 0; |
| 612 |
} else { |
| 613 |
|
| 614 |
iState[ iCurPtr ] = ePossible; |
| 615 |
iNewPos.subtract( iAlignment[ iCurPtr ] ); |
| 616 |
iSignature.addMultiple( iAlignment[ iCurPtr ], -iPosPtr-1 ); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
if (iFoundNonZero) { |
| 621 |
|
| 622 |
BFloat iLeft = (BFloat)(iTable.get( iPos )).clone(); |
| 623 |
|
| 624 |
BFloat iRightObj = iTable.get( iNewPos ); |
| 625 |
BFloat iRight; |
| 626 |
if (iRightObj == null) { |
| 627 |
iUnusedPos = true; |
| 628 |
iRight = new BFloat(0); |
| 629 |
} else { |
| 630 |
iRight = iRightObj; |
| 631 |
iUnusedPos = false; |
| 632 |
} |
| 633 |
double iTransFac = (-treeRecursion( iSignature, iPos )) / iNullEmissionFac; |
| 634 |
|
| 635 |
iLeft.multiply( iTransFac ); |
| 636 |
iRight.add( iLeft ); |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
if (iUnusedPos) { |
| 642 |
iTable.put( iNewPos.clone(), iRight ); |
| 643 |
} else { |
| 644 |
iTable.put( iNewPos, iRight ); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
} while (iFoundNonZero); |
| 649 |
|
| 650 |
|
| 651 |
--iPtr; |
| 652 |
while (iPtr >= 0 && iState[iPtr] != ePossible) { |
| 653 |
|
| 654 |
if (iState[iPtr] == eUsed) { |
| 655 |
iPos.subtract( iAlignment[iPtr] ); |
| 656 |
iState[iPtr] = eFree; |
| 657 |
} |
| 658 |
--iPtr; |
| 659 |
} |
| 660 |
|
| 661 |
if (iPtr == -1) { |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
return (iTable.get( iNewPos )).log(); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
iState[iPtr] = eUsed; |
| 670 |
iPos.add( iAlignment[iPtr] ); |
| 671 |
if (iPtr <= iFirstNotUsed) |
| 672 |
iFirstNotUsed++; |
| 673 |
|
| 674 |
} while (true); |
| 675 |
|
| 676 |
} |
| 677 |
} |
| 678 |
|