| 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.treelikelihood; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 31 |
import dr.oldevomodel.sitemodel.SiteModel; |
| 32 |
import dr.oldevomodel.substmodel.SubstitutionModel; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.model.Variable; |
| 35 |
import dr.math.matrixAlgebra.Vector; |
| 36 |
|
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@Deprecated |
| 43 |
public class EpochTreeBranchSubstitutionModel extends TreeBranchSubstitutionModel { |
| 44 |
|
| 45 |
public static final boolean DEBUG = false; |
| 46 |
|
| 47 |
public EpochTreeBranchSubstitutionModel(String name, |
| 48 |
SiteModel siteModel, List<SubstitutionModel> substModelList, BranchRateModel branchModel, |
| 49 |
Parameter transitionTimes) { |
| 50 |
super(name, siteModel, null, branchModel); |
| 51 |
this.modelList = substModelList; |
| 52 |
|
| 53 |
this.transitionTimesParameter = transitionTimes; |
| 54 |
this.transitionTimes = transitionTimesParameter.getParameterValues(); |
| 55 |
|
| 56 |
addVariable(transitionTimes); |
| 57 |
|
| 58 |
for (SubstitutionModel model : modelList) |
| 59 |
addModel(model); |
| 60 |
|
| 61 |
numberModels = modelList.size(); |
| 62 |
weight = new double[numberModels]; |
| 63 |
stateCount = modelList.get(0).getDataType().getStateCount(); |
| 64 |
stepMatrix = new double[stateCount * stateCount]; |
| 65 |
productMatrix = new double[stateCount * stateCount]; |
| 66 |
resultMatrix = new double[stateCount * stateCount]; |
| 67 |
} |
| 68 |
|
| 69 |
public void getTransitionProbabilities(Tree tree, NodeRef node, int rateCategory, double[] matrix) { |
| 70 |
|
| 71 |
NodeRef parent = tree.getParent(node); |
| 72 |
|
| 73 |
final double branchRate = branchModel.getBranchRate(tree, node); |
| 74 |
|
| 75 |
|
| 76 |
final double startTime = tree.getNodeHeight(parent); |
| 77 |
final double endTime = tree.getNodeHeight(node); |
| 78 |
final double branchTime = branchRate * (startTime - endTime); |
| 79 |
|
| 80 |
if (branchTime < 0.0) { |
| 81 |
throw new RuntimeException("Negative branch length: " + branchTime); |
| 82 |
} |
| 83 |
|
| 84 |
double distance = siteModel.getRateForCategory(rateCategory) * branchTime; |
| 85 |
|
| 86 |
int matrixCount = 0; |
| 87 |
boolean oneMatrix = (getEpochWeights(startTime, endTime, weight) == 1); |
| 88 |
for (int m = 0; m < numberModels; m++) { |
| 89 |
if (weight[m] > 0) { |
| 90 |
SubstitutionModel model = modelList.get(m); |
| 91 |
if (matrixCount == 0) { |
| 92 |
if (oneMatrix) { |
| 93 |
model.getTransitionProbabilities(distance, matrix); |
| 94 |
break; |
| 95 |
} else |
| 96 |
model.getTransitionProbabilities(distance * weight[m], resultMatrix); |
| 97 |
matrixCount++; |
| 98 |
} else { |
| 99 |
model.getTransitionProbabilities(distance * weight[m], stepMatrix); |
| 100 |
|
| 101 |
int index = 0; |
| 102 |
for (int i = 0; i < stateCount; i++) { |
| 103 |
for (int j = 0; j < stateCount; j++) { |
| 104 |
productMatrix[index] = 0; |
| 105 |
for (int k = 0; k < stateCount; k++) { |
| 106 |
productMatrix[index] += resultMatrix[i * stateCount + k] * stepMatrix[k * stateCount + j]; |
| 107 |
} |
| 108 |
index++; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
double[] tmpMatrix = resultMatrix; |
| 113 |
resultMatrix = productMatrix; |
| 114 |
productMatrix = tmpMatrix; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
if (!oneMatrix) |
| 119 |
System.arraycopy(resultMatrix, 0, matrix, 0, stateCount * stateCount); |
| 120 |
} |
| 121 |
|
| 122 |
private int getEpochWeights(double startTime, double endTime, double[] weights) { |
| 123 |
|
| 124 |
int matrixCount = 0; |
| 125 |
final double lengthTime = endTime - startTime; |
| 126 |
final int lastTime = numberModels - 2; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if (startTime <= transitionTimes[0]) { |
| 134 |
if (endTime <= transitionTimes[0]) |
| 135 |
weights[0] = 1; |
| 136 |
else |
| 137 |
weights[0] = (transitionTimes[0] - startTime) / lengthTime; |
| 138 |
matrixCount++; |
| 139 |
} else |
| 140 |
weights[0] = 0; |
| 141 |
|
| 142 |
|
| 143 |
for (int i = 1; i <= lastTime; i++) { |
| 144 |
if (startTime <= transitionTimes[i]) { |
| 145 |
double start = Math.max(startTime, transitionTimes[i - 1]); |
| 146 |
double end = Math.min(endTime, transitionTimes[i]); |
| 147 |
weights[i] = (end - start) / lengthTime; |
| 148 |
matrixCount++; |
| 149 |
} else |
| 150 |
weights[i] = 0; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
if (lastTime >= 0) { |
| 155 |
if (endTime > transitionTimes[lastTime]) { |
| 156 |
double start = Math.max(startTime, transitionTimes[lastTime]); |
| 157 |
weights[lastTime + 1] = (endTime - start) / lengthTime; |
| 158 |
matrixCount++; |
| 159 |
} else |
| 160 |
weights[lastTime + 1] = 0; |
| 161 |
} |
| 162 |
|
| 163 |
if (DEBUG) { |
| 164 |
double totalWeight = 0; |
| 165 |
for (int i = 0; i < numberModels; i++) |
| 166 |
totalWeight += weights[i]; |
| 167 |
System.err.println("Start: " + startTime + " End: " + endTime + " Count: " + matrixCount + " Weight: " + totalWeight + " - " + new Vector(weights)); |
| 168 |
if (totalWeight > 1.001) System.exit(-1); |
| 169 |
if (totalWeight < 0.999) System.exit(-1); |
| 170 |
} |
| 171 |
|
| 172 |
return matrixCount; |
| 173 |
} |
| 174 |
|
| 175 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 176 |
super.handleVariableChangedEvent(variable, index, type); |
| 177 |
|
| 178 |
if (variable == transitionTimesParameter) { |
| 179 |
transitionTimes = transitionTimesParameter.getParameterValues(); |
| 180 |
fireModelChanged(variable, index); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
private List<SubstitutionModel> modelList; |
| 185 |
private Parameter transitionTimesParameter; |
| 186 |
private double[] transitionTimes; |
| 187 |
private double[] weight; |
| 188 |
private double[] stepMatrix; |
| 189 |
private double[] productMatrix; |
| 190 |
private double[] resultMatrix; |
| 191 |
private int numberModels; |
| 192 |
private int stateCount; |
| 193 |
|
| 194 |
} |
| 195 |
|