| 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.evomodel.treedatalikelihood; |
| 27 |
|
| 28 |
import beagle.Beagle; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.branchmodel.BranchModel; |
| 31 |
import dr.evomodel.substmodel.EigenDecomposition; |
| 32 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 33 |
import dr.util.Timer; |
| 34 |
|
| 35 |
import java.io.Serializable; |
| 36 |
import java.util.ArrayDeque; |
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.Deque; |
| 39 |
import java.util.List; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public final class HomogenousSubstitutionModelDelegate implements EvolutionaryProcessDelegate, Serializable { |
| 47 |
|
| 48 |
private final SubstitutionModel substitutionModel; |
| 49 |
|
| 50 |
private final int eigenCount = 1; |
| 51 |
private final int nodeCount; |
| 52 |
|
| 53 |
private final BufferIndexHelper eigenBufferHelper; |
| 54 |
private final BufferIndexHelper matrixBufferHelper; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public HomogenousSubstitutionModelDelegate(Tree tree, BranchModel branchModel) { |
| 63 |
this(tree, branchModel, 0); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public HomogenousSubstitutionModelDelegate(Tree tree, BranchModel branchModel, int partitionNumber) { |
| 74 |
|
| 75 |
assert(branchModel.getSubstitutionModels().size() == 1) : "this delegate should only be used with simple branch models"; |
| 76 |
|
| 77 |
this.substitutionModel = branchModel.getRootSubstitutionModel(); |
| 78 |
|
| 79 |
|
| 80 |
nodeCount = tree.getNodeCount(); |
| 81 |
|
| 82 |
|
| 83 |
eigenBufferHelper = new BufferIndexHelper(eigenCount, 0, partitionNumber); |
| 84 |
|
| 85 |
|
| 86 |
matrixBufferHelper = new BufferIndexHelper(nodeCount, 0, partitionNumber); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public boolean canReturnComplexDiagonalization() { |
| 92 |
return substitutionModel.canReturnComplexDiagonalization(); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public int getEigenBufferCount() { |
| 97 |
return eigenBufferHelper.getBufferCount(); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public int getMatrixBufferCount() { |
| 102 |
return matrixBufferHelper.getBufferCount(); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public int getSubstitutionModelCount() { |
| 107 |
return 1; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public SubstitutionModel getSubstitutionModel(int index) { |
| 112 |
assert(index == 0); |
| 113 |
return substitutionModel; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public int getEigenIndex(int bufferIndex) { |
| 118 |
return eigenBufferHelper.getOffsetIndex(bufferIndex); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public int getMatrixIndex(int branchIndex) { |
| 123 |
return matrixBufferHelper.getOffsetIndex(branchIndex); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public double[] getRootStateFrequencies() { |
| 128 |
return substitutionModel.getFrequencyModel().getFrequencies(); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void updateSubstitutionModels(Beagle beagle, boolean flip) { |
| 133 |
if (flip) { |
| 134 |
eigenBufferHelper.flipOffset(0); |
| 135 |
} |
| 136 |
EigenDecomposition ed = substitutionModel.getEigenDecomposition(); |
| 137 |
|
| 138 |
beagle.setEigenDecomposition( |
| 139 |
eigenBufferHelper.getOffsetIndex(0), |
| 140 |
ed.getEigenVectors(), |
| 141 |
ed.getInverseEigenVectors(), |
| 142 |
ed.getEigenValues()); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public void updateTransitionMatrices(Beagle beagle, int[] branchIndices, double[] edgeLengths, int updateCount, boolean flip) { |
| 147 |
|
| 148 |
int[] probabilityIndices = new int[updateCount]; |
| 149 |
|
| 150 |
for (int i = 0; i < updateCount; i++) { |
| 151 |
if (flip) { |
| 152 |
matrixBufferHelper.flipOffset(branchIndices[i]); |
| 153 |
} |
| 154 |
probabilityIndices[i] = matrixBufferHelper.getOffsetIndex(branchIndices[i]); |
| 155 |
} |
| 156 |
|
| 157 |
beagle.updateTransitionMatrices(eigenBufferHelper.getOffsetIndex(0), |
| 158 |
probabilityIndices, |
| 159 |
null, |
| 160 |
null, |
| 161 |
edgeLengths, |
| 162 |
updateCount); |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void flipTransitionMatrices(int[] branchIndices, int updateCount) { |
| 168 |
for (int i = 0; i < updateCount; i++) { |
| 169 |
matrixBufferHelper.flipOffset(branchIndices[i]); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
@Override |
| 175 |
public void storeState() { |
| 176 |
eigenBufferHelper.storeState(); |
| 177 |
matrixBufferHelper.storeState(); |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
public void restoreState() { |
| 182 |
eigenBufferHelper.restoreState(); |
| 183 |
matrixBufferHelper.restoreState(); |
| 184 |
} |
| 185 |
|
| 186 |
} |