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
|
package beagle;
public class GeneralBeagleImpl implements Beagle {
public static final boolean DEBUG = false;
public static final boolean SCALING = true; //this was true XJ
// These settings are chosen for single precision computation.
// The single precision exponents go from -126 to 127 (2 ^ x)
public static final int SCALING_FACTOR_COUNT = 254; // -126, 127
public static final int SCALING_FACTOR_OFFSET = 126; // the zero point
private static final int SCALING_EXPONENT_THRESHOLD = 2;
protected final int tipCount;
protected final int partialsBufferCount;
protected final int compactBufferCount;
protected final int stateCount;
protected final int patternCount;
protected final int eigenBufferCount;
protected final int matrixBufferCount;
protected final int categoryCount;
protected int partialsSize;
protected int matrixSize;
protected double[][] cMatrices;
protected double[][] eigenValues;
protected double[][] stateFrequencies;
protected double[] categoryRates;
protected double[][] categoryWeights;
protected double[] patternWeights;
protected double[][] partials;
protected int[][] scalingFactorCounts;
protected int[][] tipStates;
protected double[][] matrices;
double[] tmpPartials;
protected double[] scalingFactors;
protected double[] logScalingFactors;
/**
* Constructor
*
* @param stateCount number of states
*/
public GeneralBeagleImpl(final int tipCount,
final int partialsBufferCount,
final int compactBufferCount,
final int stateCount,
final int patternCount,
final int eigenBufferCount,
final int matrixBufferCount,
final int categoryCount,
final int scaleBufferCount) {
this.tipCount = tipCount;
this.partialsBufferCount = partialsBufferCount;
this.compactBufferCount = compactBufferCount;
this.stateCount = stateCount;
this.patternCount = patternCount;
this.eigenBufferCount = eigenBufferCount;
this.matrixBufferCount = matrixBufferCount;
this.categoryCount = categoryCount;
// Logger.getLogger("beagle").info("Constructing double-precision Java BEAGLE implementation.");
if (patternCount < 1) {
throw new IllegalArgumentException("Pattern count must be at least 1");
}
if (categoryCount < 1) {
throw new IllegalArgumentException("Category count must be at least 1");
}
cMatrices = new double[eigenBufferCount][stateCount * stateCount * stateCount];
eigenValues = new double[eigenBufferCount][stateCount];
stateFrequencies = new double[eigenBufferCount][stateCount];
categoryWeights = new double[eigenBufferCount][categoryCount];
categoryRates = new double[categoryCount];
partialsSize = patternCount * stateCount * categoryCount;
patternWeights = new double[patternCount];
tipStates = new int[compactBufferCount][];
partials = new double[partialsBufferCount][];
for (int i = 0; i < partialsBufferCount; i++) {
partials[i] = new double[partialsSize];
}
if (SCALING) {
// create the scaling factor accumulation counts.
// These mirror each partials buffer
scalingFactorCounts = new int[partialsBufferCount][];
for (int i = 0; i < partialsBufferCount; i++) {
scalingFactorCounts[i] = new int[SCALING_FACTOR_COUNT];
}
// Create the scaling factor look up tables. These
// could be statics I guess.
scalingFactors = new double[SCALING_FACTOR_COUNT];
logScalingFactors = new double[SCALING_FACTOR_COUNT];
int exponent = -126;
for (int i = 0; i < SCALING_FACTOR_COUNT; i++) {
scalingFactors[i] = Math.pow(2.0, exponent);
logScalingFactors[i] = Math.log(scalingFactors[i]);
exponent ++;
}
}
tmpPartials = new double[patternCount * stateCount];
matrixSize = stateCount * stateCount;
matrices = new double[matrixBufferCount][categoryCount * matrixSize];
}
public void finalize() throws Throwable {
super.finalize();
}
public void setPatternWeights(final double[] patternWeights) {
System.arraycopy(patternWeights, 0, this.patternWeights, 0, this.patternWeights.length);
}
@Override
public void setPatternPartitions(int partitionCount, int[] patternPartitions) {
throw new UnsupportedOperationException("Not implemented yet");
}
/**
* Sets partials for a tip - these are numbered from 0 and remain
* constant throughout the run.
*
* @param tipIndex the tip index
* @param states an array of patternCount state indices
*/
public void setTipStates(int tipIndex, int[] states) {
assert(tipIndex >= 0 && tipIndex < tipCount);
if (this.tipStates[tipIndex] == null) {
tipStates[tipIndex] = new int[patternCount];
}
int k = 0;
for (int state : states) {
this.tipStates[tipIndex][k] = (state < stateCount ? state : stateCount);
k++;
}
}
public void getTipStates(int tipIndex, int[] states) {
assert(tipIndex >= 0 && tipIndex < tipCount);
if (this.tipStates[tipIndex] == null) {
throw new RuntimeException("Unset tip states");
}
System.arraycopy(this.tipStates[tipIndex], 0, states, 0, states.length);
}
public void setTipPartials(int tipIndex, double[] inPartials) {
assert(tipIndex >= 0 && tipIndex < tipCount);
if (this.partials[tipIndex] == null) {
this.partials[tipIndex] = new double[partialsSize];
}
int k = 0;
for (int i = 0; i < categoryCount; i++) {
System.arraycopy(inPartials, 0, this.partials[tipIndex], k, inPartials.length);
k += inPartials.length;
}
}
public void setRootPrePartials(int[] inbufferIndices, int[] instateFrequenciesIndices, int count){
if(count == 1){
int stateFrequenciesIndex = instateFrequenciesIndices[0];
int bufferIndex = inbufferIndices[0];
if (this.partials[bufferIndex] == null){
this.partials[bufferIndex] = new double[partialsSize];
}
int k = 0;
for(int i = 0; i < categoryCount; i++){
for(int j = 0; j < patternCount; j++){
System.arraycopy(this.stateFrequencies[stateFrequenciesIndex], 0, this.partials[bufferIndex], k, stateCount);
k += stateCount;
}
}
}
}
public void setPartials(final int bufferIndex, final double[] partials) {
assert(this.partials[bufferIndex] != null);
System.arraycopy(partials, 0, this.partials[bufferIndex], 0, partialsSize);
}
public void getPartials(final int bufferIndex, final int scaleIndex, final double[] partials) {
System.arraycopy(this.partials[bufferIndex], 0, partials, 0, partialsSize);
}
@Override
public void getLogScaleFactors(int scaleIndex, double[] outFactors) {
throw new UnsupportedOperationException("Not implemented. Email Marc Suchard if required (offer coauthorship for enhanced service)");
}
public void getScaleFactors(final int scaleIndex, final double[] partials) {
throw new RuntimeException("Not yet implemented.");
}
public void setEigenDecomposition(int eigenIndex, double[] eigenVectors, double[] inverseEigenValues, double[] eigenValues) {
int l =0;
for (int i = 0; i < stateCount; i++) {
for (int j = 0; j < stateCount; j++) {
for (int k = 0; k < stateCount; k++) {
cMatrices[eigenIndex][l] = eigenVectors[(i * stateCount) + k] * inverseEigenValues[(k * stateCount) + j];
l++;
}
}
}
System.arraycopy(eigenValues, 0, this.eigenValues[eigenIndex], 0, eigenValues.length);
}
public void setStateFrequencies(final int stateFrequenciesIndex, final double[] stateFrequencies) {
System.arraycopy(stateFrequencies, 0, this.stateFrequencies[stateFrequenciesIndex], 0, stateCount);
}
public void setCategoryWeights(final int categoryWeightsIndex, final double[] categoryWeights) {
System.arraycopy(categoryWeights, 0, this.categoryWeights[categoryWeightsIndex], 0, categoryCount);
}
public void setCategoryRates(double[] categoryRates) {
System.arraycopy(categoryRates, 0, this.categoryRates, 0, this.categoryRates.length);
}
public void setCategoryRatesWithIndex(final int categoryRatesIndex, double[] categoryRates) {
throw new UnsupportedOperationException("setCategoryRatesWithIndex not implemented in GeneralBeagleImpl");
}
public void setTransitionMatrix(final int matrixIndex, final double[] inMatrix, final double paddedValue) {
System.arraycopy(inMatrix, 0, this.matrices[matrixIndex], 0, this.matrixSize);
}
public void setDifferentialMatrix(final int matrixIndex, final double[] inMatrix) {
setTransitionMatrix(matrixIndex, inMatrix, 0.0);
}
public void getTransitionMatrix(final int matrixIndex, final double[] outMatrix) {
System.arraycopy(this.matrices[matrixIndex],0,outMatrix,0,outMatrix.length);
}
// /////////////////////////
// ---TODO: Epoch model---//
// /////////////////////////
public void convolveTransitionMatrices(
final int[] firstIndices,
final int[] secondIndices,
final int[] resultIndices,
int matrixCount) {
for (int u = 0; u < matrixCount; u++) {
int firstIndex = firstIndices[u];
int secondIndex = secondIndices[u];
int resultIndex = resultIndices[u];
if (DEBUG) {
System.err.println("Convolving matrix " + firstIndex + " with matrix " + secondIndex + " into " + resultIndex);
}
for (int l = 0; l < categoryCount; l++) {
for (int i = 0; i < stateCount; i++) {
for (int j = 0; j < stateCount; j++) {
for (int k = 0; k < stateCount; k++) {
// sum +=
}//END: k loop
}// END: j loop
}// END: i loop
}// /END: l loop
}// END: u loop
}// END: convolveTransitionMatrices
public void addTransitionMatrices(
final int[] firstIndices,
final int[] secondIndices,
final int[] resultIndices,
int matrixCount) {
throw new RuntimeException("Not yet implemented");
}
public void transposeTransitionMatrices(final int[] inIndices,
final int[] outIndices,
int matrixCount) {
throw new RuntimeException("Not yet implemented");
}
public void updateTransitionMatrices(final int eigenIndex,
final int[] probabilityIndices,
final int[] firstDerivativeIndices,
final int[] secondDervativeIndices,
final double[] edgeLengths,
final int count) {
for (int u = 0; u < count; u++) {
int matrixIndex = probabilityIndices[u];
if (DEBUG) System.err.println("Updating matrix for node " + matrixIndex);
double[] tmp = new double[stateCount];
int n = 0;
for (int l = 0; l < categoryCount; l++) {
// if (DEBUG) System.err.println("1: Rate "+l+" = "+categoryRates[l]);
for (int i = 0; i < stateCount; i++) {
tmp[i] = Math.exp(eigenValues[eigenIndex][i] * edgeLengths[u] * categoryRates[l]);
}
// if (DEBUG) System.err.println(new dr.math.matrixAlgebra.Vector(tmp));
// if (DEBUG) System.exit(-1);
int m = 0;
for (int i = 0; i < stateCount; i++) {
for (int j = 0; j < stateCount; j++) {
double sum = 0.0;
for (int k = 0; k < stateCount; k++) {
sum += cMatrices[eigenIndex][m] * tmp[k];
m++;
}
// if (DEBUG) System.err.println("1: matrices[][]["+n+"] = "+sum);
if (sum > 0)
matrices[matrixIndex][n] = sum;
else
matrices[matrixIndex][n] = 0; // TODO Decision: set to -sum (as BEAST does)
n++;
}
}
// if (DEBUG) System.err.println(new dr.math.matrixAlgebra.Vector(matrices[currentMatricesIndices[nodeIndex]][nodeIndex]));
// if (DEBUG) System.exit(0);
}
}
}
public void updateTransitionMatricesWithMultipleModels(
final int[] eigenIndices,
final int[] categoryRateIndices,
final int[] probabilityIndices,
final int[] firstDerivativeIndices,
final int[] secondDervativeIndices,
final double[] edgeLengths,
final int count) {
throw new UnsupportedOperationException("updateTransitionMatricesWithMultipleModels not implemented in GeneralBeagleImpl");
}
/**
* Operations list is a list of 7-tuple integer indices, with one 7-tuple per operation.
* Format of 7-tuple operation: {destinationPartials,
* destinationScaleWrite,
* destinationScaleRead,
* child1Partials,
* child1TransitionMatrix,
* child2Partials,
* child2TransitionMatrix}
*
*/
public void updatePartials(final int[] operations, final int operationCount, final int cumulativeScaleIndex) {
int x = 0;
for (int op = 0; op < operationCount; op++) {
int bufferIndex3 = operations[x];
int bufferIndex1 = operations[x + 3];
int matrixIndex1 = operations[x + 4];
int bufferIndex2 = operations[x + 5];
int matrixIndex2 = operations[x + 6];
x += Beagle.OPERATION_TUPLE_SIZE;
int exponent = 0;
if (compactBufferCount == 0) {
exponent = updatePartialsPartials(bufferIndex1, matrixIndex1, bufferIndex2, matrixIndex2, bufferIndex3);
} else {
if (bufferIndex1 < tipCount && tipStates[bufferIndex1] != null) {
if (bufferIndex2 < tipCount && tipStates[bufferIndex2] != null) {
exponent = updateStatesStates(bufferIndex1, matrixIndex1, bufferIndex2, matrixIndex2, bufferIndex3);
} else {
exponent = updateStatesPartials(bufferIndex1, matrixIndex1, bufferIndex2, matrixIndex2, bufferIndex3);
}
} else {
if (bufferIndex2 < tipCount && tipStates[bufferIndex2] != null) {
exponent = updateStatesPartials(bufferIndex2, matrixIndex2, bufferIndex1, matrixIndex1, bufferIndex3);
} else {
exponent = updatePartialsPartials(bufferIndex1, matrixIndex1, bufferIndex2, matrixIndex2, bufferIndex3);
}
}
}
if (SCALING) {
if (exponent > SCALING_EXPONENT_THRESHOLD) {
rescalePartials(bufferIndex3);
}
}
}
}
/**
* Operations list is a list of 7-tuple integer indices, with one 7-tuple per operation.
* Format of 7-tuple operation: {destinationPartials,
* destinationScaleWrite,
* destinationScaleRead,
* pre-order partials of the parent node,
* Ptr matrices of the current node,
* post-order partials of the sibling node,
* Ptr matrices of the sibling node}
*
*/
public void updatePrePartials(final int[] operations, final int operationCount, final int cumulativeScaleIndex) {
int x = 0;
for (int op = 0; op < operationCount; op++) {
int bufferIndex3 = operations[x];
int bufferIndex1 = operations[x + 3];
int matrixIndex1 = operations[x + 4];
int bufferIndex2 = operations[x + 5];
int matrixIndex2 = operations[x + 6];
x += Beagle.OPERATION_TUPLE_SIZE;
int exponent = 0;
exponent = updatePrePartialsPartials(bufferIndex1, matrixIndex1, bufferIndex2, matrixIndex2, bufferIndex3);
if (SCALING) {
if (exponent > SCALING_EXPONENT_THRESHOLD) {
rescalePartials(bufferIndex3);
}
}
}
}
public void updatePrePartialsByPartition(final int[] operations, final int operationCount) {
throw new BeagleException("updatePrePartialsByPartition", -1);
}
@Override
public void calculateEdgeDifferentials(final int[] postBufferIndices,
final int[] preBufferIndices,
final int[] derivativeMatrixIndices,
final int[] categoryWeightsIndices,
int count,
double[] outDerivatives,
double[] outSumDerivatives,
double[] outSumSquaredDerivatives) {
throw new RuntimeException("Not yet implemented");
}
@Override
public void calculateCrossProductDifferentials(final int[] postBufferIndices,
final int[] preBufferIndices,
final int[] categoryRateIndices,
final int[] categoryWeightsIndices,
final double[] edgeLengths,
int count,
double[] outSumDerivatives,
double[] outSumSquaredDerivatives) {
throw new RuntimeException("Not yet implemented");
}
@Override
public void calculateEdgeDerivative(int[] postBufferIndices,
int[] preBufferIndices,
int rootBufferIndex,
int[] firstDerivativeIndices,
int[] secondDerivativeIndices,
int categoryWeightsIndex,
int categoryRatesIndex,
int stateFrequenciesIndex,
int[] cumulativeScaleIndices,
int count,
double[] outFirstDerivative,
double[] outDiagonalSecondDerivative) {
throw new RuntimeException("Not yet implemented");
}
/**
* Operations list is a list of 9-tuple integer indices, with one 7-tuple per operation.
* Format of 9-tuple operation: {destinationPartials,
* destinationScaleWrite,
* destinationScaleRead,
* child1Partials,
* child1TransitionMatrix,
* child2Partials,
* child2TransitionMatrix,
* partition,
* cumulativeScaleIndex}
*
*/
public void updatePartialsByPartition(final int[] operations, final int operationCount) {
throw new UnsupportedOperationException("updatePartialsByPartition not implemented in GeneralBeagleImpl");
}
private void rescalePartials(final int bufferIndex) {
double[] partials = this.partials[bufferIndex];
int[] counts = scalingFactorCounts[bufferIndex];
if (DEBUG) {
System.err.println("rescaling buffer "+ bufferIndex);
}
int u = 0;
for (int l = 0; l < categoryCount; l++) {
for (int k = 0; k < patternCount; k++) {
double maxValue = partials[u];
for (int i = 1; i < stateCount; i++) {
if (partials[u + i] > maxValue) {
maxValue = partials[u + i];
}
}
// find the exponent for the largest value
int exponent = Math.getExponent(maxValue);
if (exponent != 0) {
// invert and offset it to get the index of the appropriate scale factor
int index = SCALING_FACTOR_OFFSET - exponent;
double scalingFactor = scalingFactors[index];
// increment the count of how many times this factor has been used
counts[index] += patternWeights[k];
if (DEBUG) {
System.err.println("exponent "+ exponent + ", index " + index + ", factor " + scalingFactor);
}
// do the rescaling
for (int i = 0; i < stateCount; i++) {
partials[u] *= scalingFactor;
u++;
}
}
}
}
}
//placeholder for potential implementation
private void rescalePartialsByPartition(final int bufferIndex) {
throw new UnsupportedOperationException("rescalePartialsByPartition not implemented in GeneralBeagleImpl");
}
public void setCPUThreadCount(int threadCount) {
throw new UnsupportedOperationException("setCPUThreadCount not implemented in GeneralBeagleImpl");
}
public void accumulateScaleFactors(int[] scaleIndices, int count, int outScaleIndex) {
// throw new UnsupportedOperationException("accumulateScaleFactors not implemented in GeneralBeagleImpl");
}
public void removeScaleFactors(int[] scaleIndices, int count, int cumulativeScaleIndex) {
// throw new UnsupportedOperationException("accumulateScaleFactors not implemented in GeneralBeagleImpl");
}
public void copyScaleFactors(int destScalingIndex, int srcScalingIndex) {
// throw new UnsupportedOperationException("accumulateScaleFactors not implemented in GeneralBeagleImpl");
}
public void resetScaleFactors(int cumulativeScaleIndex) {
// throw new UnsupportedOperationException("accumulateScaleFactors not implemented in GeneralBeagleImpl");
}
public void accumulateScaleFactorsByPartition(int[] scaleIndices, int count, int outScaleIndex, int partitionIndex) {
// throw new UnsupportedOperationException("accumulateScaleFactorsByPartition not implemented in GeneralBeagleImpl");
}
public void removeScaleFactorsByPartition(int[] scaleIndices, int count, int cumulativeScaleIndex, int partitionIndex) {
// throw new UnsupportedOperationException("accumulateScaleFactorsByPartition not implemented in GeneralBeagleImpl");
}
public void resetScaleFactorsByPartition(int cumulativeScaleIndex, int partitionIndex) {
// throw new UnsupportedOperationException("accumulateScaleFactorsByPartition not implemented in GeneralBeagleImpl");
}
/**
* Calculates partial likelihoods at a node when both children have states.
* @returns the larges absolute exponent
*/
protected int updateStatesStates(int bufferIndex1, int matrixIndex1, int bufferIndex2, int matrixIndex2, int bufferIndex3)
{
double[] matrices1 = matrices[matrixIndex1];
double[] matrices2 = matrices[matrixIndex2];
int[] states1 = tipStates[bufferIndex1];
int[] states2 = tipStates[bufferIndex2];
double[] partials3 = partials[bufferIndex3];
if (SCALING) {
// zero the scaling factor counts for this node (only tips below)
int[] counts3 = scalingFactorCounts[bufferIndex3];
for (int i = 0; i < counts3.length; i++) {
counts3[i] = 0;
}
}
int v = 0;
for (int l = 0; l < categoryCount; l++) {
for (int k = 0; k < patternCount; k++) {
int state1 = states1[k];
int state2 = states2[k];
int w = l * matrixSize;
if (state1 < stateCount && state2 < stateCount) {
for (int i = 0; i < stateCount; i++) {
partials3[v] = matrices1[w + state1] * matrices2[w + state2];
v++;
w += stateCount;
}
} else if (state1 < stateCount) {
// child 2 has a gap or unknown state so treat it as unknown
for (int i = 0; i < stateCount; i++) {
partials3[v] = matrices1[w + state1];
v++;
w += stateCount;
}
} else if (state2 < stateCount) {
// child 2 has a gap or unknown state so treat it as unknown
for (int i = 0; i < stateCount; i++) {
partials3[v] = matrices2[w + state2];
v++;
w += stateCount;
}
} else {
// both children have a gap or unknown state so set partials to 1
for (int j = 0; j < stateCount; j++) {
partials3[v] = 1.0;
v++;
}
}
}
}
return 0; // don't bother checking exponents for cherries
}
/**
* Calculates partial likelihoods at a node when one child has states and one has partials.
*/
protected int updateStatesPartials(int bufferIndex1, int matrixIndex1, int bufferIndex2, int matrixIndex2, int bufferIndex3)
{
double[] matrices1 = matrices[matrixIndex1];
double[] matrices2 = matrices[matrixIndex2];
int[] states1 = tipStates[bufferIndex1];
double[] partials2 = partials[bufferIndex2];
double[] partials3 = partials[bufferIndex3];
double sum, tmp;
int u = 0;
int v = 0;
int exponent = 0;
if (SCALING) {
int[] counts2 = scalingFactorCounts[bufferIndex2];
int[] counts3 = scalingFactorCounts[bufferIndex3];
// only one internal node below so just copy those scaling factor counts
System.arraycopy(counts2, 0, counts3, 0, counts2.length);
}
for (int l = 0; l < categoryCount; l++) {
for (int k = 0; k < patternCount; k++) {
int state1 = states1[k];
int w = l * matrixSize;
if (state1 < stateCount) {
for (int i = 0; i < stateCount; i++) {
tmp = matrices1[w + state1];
sum = 0.0;
for (int j = 0; j < stateCount; j++) {
sum += matrices2[w] * partials2[v + j];
w++;
}
partials3[u] = tmp * sum;
if (SCALING) {
// this is to find the largest absolute exponent
exponent |= Math.abs(Math.getExponent(partials3[u]));
}
u++;
}
v += stateCount;
} else {
// Child 1 has a gap or unknown state so don't use it
for (int i = 0; i < stateCount; i++) {
sum = 0.0;
for (int j = 0; j < stateCount; j++) {
sum += matrices2[w] * partials2[v + j];
w++;
}
partials3[u] = sum;
if (SCALING) {
exponent |= Math.abs(Math.getExponent(partials3[u]));
}
u++;
}
v += stateCount;
}
}
}
return exponent;
}
protected int updatePartialsPartials(int bufferIndex1, int matrixIndex1, int bufferIndex2, int matrixIndex2, int bufferIndex3)
{
double[] matrices1 = matrices[matrixIndex1];
double[] matrices2 = matrices[matrixIndex2];
double[] partials1 = partials[bufferIndex1];
double[] partials2 = partials[bufferIndex2];
double[] partials3 = partials[bufferIndex3];
double sum1, sum2;
int exponent = 0;
if (SCALING) {
int[] counts1 = scalingFactorCounts[bufferIndex1];
int[] counts2 = scalingFactorCounts[bufferIndex2];
int[] counts3 = scalingFactorCounts[bufferIndex3];
for (int i = 0; i < counts1.length; i++) {
// The scale factor counts is the sum of the two nodes below
counts3[i] = counts1[i] + counts2[i];
}
}
int u = 0;
int v = 0;
for (int l = 0; l < categoryCount; l++) {
for (int k = 0; k < patternCount; k++) {
int w = l * matrixSize;
for (int i = 0; i < stateCount; i++) {
sum1 = sum2 = 0.0;
for (int j = 0; j < stateCount; j++) {
sum1 += matrices1[w] * partials1[v + j];
sum2 += matrices2[w] * partials2[v + j];
w++;
}
partials3[u] = sum1 * sum2;
if (SCALING) {
// this is to find the largest absolute exponent
exponent |= Math.abs(Math.getExponent(partials3[u]));
}
u++;
}
v += stateCount;
}
if (DEBUG) {
// System.err.println("1:PP node = "+nodeIndex3);
// for(int p=0; p<partials3.length; p++) {
// System.err.println("1:PP\t"+partials3[p]);
// }
// System.err.println("node = "+nodeIndex3);
// System.err.println(new dr.math.matrixAlgebra.Vector(partials3));
// System.err.println(new dr.math.matrixAlgebra.Vector(scalingFactors[currentPartialsIndices[nodeIndex3]][nodeIndex3]));
//System.exit(-1);
}
}
return exponent;
}
protected int updatePrePartialsPartials(int bufferIndex1, int matrixIndex1, int bufferIndex2, int matrixIndex2, int bufferIndex3)
{
double[] matrices1 = matrices[matrixIndex1]; // Ptr matrices of the current node
double[] matrices2 = matrices[matrixIndex2]; // Ptr matrices of the sibling node
double[] partials1 = partials[bufferIndex1]; // pre-order partials of the parent node
double[] partials2 = partials[bufferIndex2]; // post-order partials of the sibling node
double[] partials3 = partials[bufferIndex3]; //destPartials
double sum2, MjPj;
int exponent = 0;
if (SCALING) {
int[] counts1 = scalingFactorCounts[bufferIndex1];
int[] counts2 = scalingFactorCounts[bufferIndex2];
int[] counts3 = scalingFactorCounts[bufferIndex3];
for (int i = 0; i < counts1.length; i++) {
// The scale factor counts is the sum of the two nodes below
counts3[i] = counts1[i] + counts2[i];
}
}
int u = 0;
int v = 0;
for (int l = 0; l < categoryCount; l++) {
for (int k = 0; k < patternCount; k++) {
int w = l * matrixSize;
for (int i = 0; i < stateCount; i++) {
sum2 = 0.0;
for (int j = 0; j < stateCount; j++) {
sum2 += matrices2[w] * partials2[v + j];
w++;
}
// sum2 = M_j P_j
// now 2nd loop
MjPj = sum2 * partials1[v + i];
w = l * matrixSize;
for (int j = 0; j < stateCount; j++){
partials3[v + j] += matrices1[w] * MjPj;
w++;
}
// if (SCALING) {
// // this is to find the largest absolute exponent
// exponent |= Math.abs(Math.getExponent(partials3[u]));
// }
u++;
}
v += stateCount;
}
if (DEBUG) {
// System.err.println("1:PP node = "+nodeIndex3);
// for(int p=0; p<partials3.length; p++) {
// System.err.println("1:PP\t"+partials3[p]);
// }
// System.err.println("node = "+nodeIndex3);
// System.err.println(new dr.math.matrixAlgebra.Vector(partials3));
// System.err.println(new dr.math.matrixAlgebra.Vector(scalingFactors[currentPartialsIndices[nodeIndex3]][nodeIndex3]));
//System.exit(-1);
}
}
return exponent;
}
public void calculateRootLogLikelihoods(final int[] bufferIndices, final int[] categoryWeightsIndices, final int[] stateFrequenciesIndices, final int[] cumulativeScaleIndices, final int count, final double[] outSumLogLikelihood) {
assert(count == 1); // @todo implement integration across multiple subtrees
double[] rootPartials = partials[bufferIndices[0]];
int u = 0;
int v = 0;
for (int k = 0; k < patternCount; k++) {
for (int i = 0; i < stateCount; i++) {
tmpPartials[u] = rootPartials[v] * categoryWeights[categoryWeightsIndices[0]][0];
u++;
v++;
}
}
for (int l = 1; l < categoryCount; l++) {
u = 0;
for (int k = 0; k < patternCount; k++) {
for (int i = 0; i < stateCount; i++) {
tmpPartials[u] += rootPartials[v] * categoryWeights[categoryWeightsIndices[0]][l];
u++;
v++;
}
}
}
u = 0;
outSumLogLikelihood[0] = 0.0;
for (int k = 0; k < patternCount; k++) {
double sum = 0.0;
for (int i = 0; i < stateCount; i++) {
sum += stateFrequencies[stateFrequenciesIndices[0]][i] * tmpPartials[u];
u++;
}
outSumLogLikelihood[0] += Math.log(sum) * patternWeights[k];
}
if (SCALING) {
int[] rootCounts = scalingFactorCounts[bufferIndices[0]];
for (int i = 0; i < SCALING_FACTOR_COUNT; i++) {
// we multiplied the scaling factors in so now subtract the logs
outSumLogLikelihood[0] -= (logScalingFactors[i] * rootCounts[i]);
}
}
}
public void calculateRootLogLikelihoodsByPartition(final int[] bufferIndices, final int[] categoryWeightsIndices, final int[] stateFrequenciesIndices, final int[] cumulativeScaleIndices, final int[] partitionIndices, final int partitionCount, final int count, final double[] outSumLogLikelihoodByPartition, final double[] outSumLogLikelihood) {
throw new UnsupportedOperationException("calculateRootLogLikelihoodsByPartition not implemented in GeneralBeagleImpl");
}
public void calculateEdgeLogLikelihoods(final int[] parentBufferIndices, final int[] childBufferIndices, final int[] probabilityIndices, final int[] firstDerivativeIndices, final int[] secondDerivativeIndices, final int[] categoryWeightsIndices, final int[] stateFrequenciesIndices, final int[] cumulativeScaleIndices, final int count, final double[] outSumLogLikelihood, final double[] outSumFirstDerivative, final double[] outSumSecondDerivative) {
throw new UnsupportedOperationException("calculateEdgeLogLikelihoods not implemented in GeneralBeagleImpl");
}
public void getSiteLogLikelihoods(final double[] outLogLikelihoods) {
throw new UnsupportedOperationException("getSiteLogLikelihoods not implemented in GeneralBeagleImpl");
}
public InstanceDetails getDetails() {
InstanceDetails details = new InstanceDetails();
details.setResourceNumber(0);
details.setFlags(BeagleFlag.PRECISION_DOUBLE.getMask());
return details;
}
}
|