| 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.substmodel; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.inference.model.Model; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.model.Variable; |
| 32 |
import dr.math.matrixAlgebra.Vector; |
| 33 |
|
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class SubstitutionEpochModel extends AbstractSubstitutionModel { |
| 41 |
|
| 42 |
public static final boolean DEBUG = false; |
| 43 |
|
| 44 |
public SubstitutionEpochModel(String name, |
| 45 |
List<SubstitutionModel> modelList, |
| 46 |
Parameter transitionTimes, |
| 47 |
DataType dataType, FrequencyModel freqModel) { |
| 48 |
|
| 49 |
super(name, dataType, freqModel); |
| 50 |
|
| 51 |
this.modelList = modelList; |
| 52 |
this.transitionTimesParameter = transitionTimes; |
| 53 |
this.transitionTimes = transitionTimesParameter.getParameterValues(); |
| 54 |
|
| 55 |
addVariable(transitionTimes); |
| 56 |
|
| 57 |
for (SubstitutionModel model : modelList) |
| 58 |
addModel(model); |
| 59 |
|
| 60 |
numberModels = modelList.size(); |
| 61 |
weight = new double[numberModels]; |
| 62 |
stateCount = dataType.getStateCount(); |
| 63 |
stepMatrix = new double[stateCount * stateCount]; |
| 64 |
productMatrix = new double[stateCount * stateCount]; |
| 65 |
resultMatrix = new double[stateCount * stateCount]; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
protected void frequenciesChanged() { |
| 70 |
} |
| 71 |
|
| 72 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 73 |
if (model == freqModel) |
| 74 |
frequenciesChanged(); |
| 75 |
else |
| 76 |
fireModelChanged(object, index); |
| 77 |
} |
| 78 |
|
| 79 |
protected void ratesChanged() { |
| 80 |
} |
| 81 |
|
| 82 |
protected void setupRelativeRates() { |
| 83 |
} |
| 84 |
|
| 85 |
public void getTransitionProbabilities(double startTime, double endTime, double distance, double[] matrix) { |
| 86 |
int matrixCount = 0; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
boolean oneMatrix = (getEpochWeights(startTime, endTime, weight) == 1); |
| 93 |
for (int m = 0; m < numberModels; m++) { |
| 94 |
if (weight[m] > 0) { |
| 95 |
SubstitutionModel model = modelList.get(m); |
| 96 |
if (matrixCount == 0) { |
| 97 |
if (oneMatrix) { |
| 98 |
model.getTransitionProbabilities(distance, matrix); |
| 99 |
break; |
| 100 |
} else |
| 101 |
model.getTransitionProbabilities(distance * weight[m], resultMatrix); |
| 102 |
matrixCount++; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
} else { |
| 108 |
model.getTransitionProbabilities(distance * weight[m], stepMatrix); |
| 109 |
|
| 110 |
int index = 0; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
for (int i = 0; i < stateCount; i++) { |
| 118 |
for (int j = 0; j < stateCount; j++) { |
| 119 |
productMatrix[index] = 0; |
| 120 |
for (int k = 0; k < stateCount; k++) { |
| 121 |
productMatrix[index] += resultMatrix[i * stateCount + k] * stepMatrix[k * stateCount + j]; |
| 122 |
} |
| 123 |
index++; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
double[] tmpMatrix = resultMatrix; |
| 131 |
resultMatrix = productMatrix; |
| 132 |
productMatrix = tmpMatrix; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
if (!oneMatrix) |
| 137 |
System.arraycopy(productMatrix, 0, matrix, 0, stateCount * stateCount); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
private int getEpochWeights(double startTime, double endTime, double[] weights) { |
| 146 |
|
| 147 |
int matrixCount = 0; |
| 148 |
final double lengthTime = endTime - startTime; |
| 149 |
final int lastTime = numberModels - 2; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if (startTime <= transitionTimes[0]) { |
| 159 |
if (endTime <= transitionTimes[0]) |
| 160 |
weights[0] = 1; |
| 161 |
else |
| 162 |
weights[0] = (transitionTimes[0] - startTime) / lengthTime; |
| 163 |
matrixCount++; |
| 164 |
} else |
| 165 |
weights[0] = 0; |
| 166 |
|
| 167 |
|
| 168 |
for (int i = 1; i <= lastTime; i++) { |
| 169 |
if (startTime <= transitionTimes[i]) { |
| 170 |
double start = Math.max(startTime, transitionTimes[i - 1]); |
| 171 |
double end = Math.min(endTime, transitionTimes[i]); |
| 172 |
weights[i] = (end - start) / lengthTime; |
| 173 |
matrixCount++; |
| 174 |
} else |
| 175 |
weights[i] = 0; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
if (lastTime >= 0) { |
| 180 |
if (endTime > transitionTimes[lastTime]) { |
| 181 |
double start = Math.max(startTime, transitionTimes[lastTime]); |
| 182 |
weights[lastTime + 1] = (endTime - start) / lengthTime; |
| 183 |
matrixCount++; |
| 184 |
} else |
| 185 |
weights[lastTime + 1] = 0; |
| 186 |
} |
| 187 |
|
| 188 |
if (DEBUG) { |
| 189 |
double totalWeight = 0; |
| 190 |
for (int i = 0; i < numberModels; i++) |
| 191 |
totalWeight += weights[i]; |
| 192 |
System.err.println("Start: " + startTime + " End: " + endTime + " Count: " + matrixCount + " Weight: " + totalWeight + " - " + new Vector(weights)); |
| 193 |
if (totalWeight > 1.001) System.exit(-1); |
| 194 |
if (totalWeight < 0.999) System.exit(-1); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
return matrixCount; |
| 202 |
} |
| 203 |
|
| 204 |
public void getTransitionProbabilities(double distance, double[] matrix) { |
| 205 |
throw new RuntimeException("Should not get here in a substitution epoch model."); |
| 206 |
} |
| 207 |
|
| 208 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 209 |
super.handleVariableChangedEvent(variable, index, type); |
| 210 |
|
| 211 |
if (variable == transitionTimesParameter) { |
| 212 |
transitionTimes = transitionTimesParameter.getParameterValues(); |
| 213 |
fireModelChanged(variable, index); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
protected void storeState() { |
| 218 |
} |
| 219 |
|
| 220 |
protected void restoreState() { |
| 221 |
} |
| 222 |
|
| 223 |
private List<SubstitutionModel> modelList; |
| 224 |
private Parameter transitionTimesParameter; |
| 225 |
private double[] transitionTimes; |
| 226 |
private double[] weight; |
| 227 |
private double[] stepMatrix; |
| 228 |
private double[] productMatrix; |
| 229 |
private double[] resultMatrix; |
| 230 |
private int numberModels; |
| 231 |
private int stateCount; |
| 232 |
|
| 233 |
} |