| 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.substmodel; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.evomodel.siteratemodel.SiteRateModel; |
| 30 |
import dr.inference.loggers.LogColumn; |
| 31 |
import dr.inference.loggers.Loggable; |
| 32 |
import dr.inference.loggers.NumberColumn; |
| 33 |
import dr.inference.model.Model; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.inference.model.Variable; |
| 36 |
import dr.math.matrixAlgebra.Matrix; |
| 37 |
import dr.math.matrixAlgebra.Vector; |
| 38 |
import dr.util.Citable; |
| 39 |
import dr.util.Citation; |
| 40 |
import dr.util.CommonCitations; |
| 41 |
|
| 42 |
import java.util.ArrayList; |
| 43 |
import java.util.Arrays; |
| 44 |
import java.util.Collections; |
| 45 |
import java.util.List; |
| 46 |
import java.util.logging.Logger; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class MarkovModulatedSubstitutionModel extends ComplexSubstitutionModel implements Citable, Loggable { |
| 52 |
|
| 53 |
private List<SubstitutionModel> baseModels; |
| 54 |
private final int numBaseModel; |
| 55 |
private final int baseStateCount; |
| 56 |
|
| 57 |
private final Parameter switchingRates; |
| 58 |
|
| 59 |
private static final boolean IGNORE_RATES = false; |
| 60 |
private static final boolean DEBUG = false; |
| 61 |
private static final boolean NEW_STORE_RESTORE = true; |
| 62 |
|
| 63 |
private final double[] baseMatrix; |
| 64 |
private Parameter rateScalar; |
| 65 |
|
| 66 |
private boolean birthDeathModel; |
| 67 |
private boolean geometricRates; |
| 68 |
|
| 69 |
private final SiteRateModel gammaRateModel; |
| 70 |
|
| 71 |
private EigenDecomposition storedEigenDecomposition; |
| 72 |
private boolean storedUpdateMatrix; |
| 73 |
|
| 74 |
public MarkovModulatedSubstitutionModel(String name, |
| 75 |
List<SubstitutionModel> baseModels, |
| 76 |
Parameter switchingRates, |
| 77 |
DataType dataType, |
| 78 |
EigenSystem eigenSystem) { |
| 79 |
this(name, baseModels, switchingRates, dataType, eigenSystem, null, false, null); |
| 80 |
} |
| 81 |
|
| 82 |
public MarkovModulatedSubstitutionModel(String name, |
| 83 |
List<SubstitutionModel> baseModels, |
| 84 |
Parameter switchingRates, |
| 85 |
DataType dataType, |
| 86 |
EigenSystem eigenSystem, |
| 87 |
Parameter rateScalar, |
| 88 |
boolean geometricRates, |
| 89 |
SiteRateModel gammaRateModel) { |
| 90 |
|
| 91 |
super(name, dataType, null, null); |
| 92 |
|
| 93 |
this.baseModels = baseModels; |
| 94 |
numBaseModel = baseModels.size(); |
| 95 |
|
| 96 |
if (numBaseModel == 0) { |
| 97 |
throw new RuntimeException("May not construct MarkovModulatedSubstitutionModel with 0 base models"); |
| 98 |
} |
| 99 |
|
| 100 |
this.switchingRates = switchingRates; |
| 101 |
addVariable(switchingRates); |
| 102 |
|
| 103 |
if (switchingRates.getDimension() != 2 * (numBaseModel - 1) |
| 104 |
&& switchingRates.getDimension() != numBaseModel * (numBaseModel - 1) |
| 105 |
) { |
| 106 |
throw new RuntimeException("Wrong switching rate dimensions"); |
| 107 |
} |
| 108 |
|
| 109 |
List<FrequencyModel> freqModels = new ArrayList<FrequencyModel>(); |
| 110 |
int stateSizes = 0; |
| 111 |
|
| 112 |
baseStateCount = baseModels.get(0).getFrequencyModel().getFrequencyCount(); |
| 113 |
baseMatrix = new double[baseStateCount * baseStateCount]; |
| 114 |
|
| 115 |
for (int i = 0; i < numBaseModel; i++) { |
| 116 |
addModel(baseModels.get(i)); |
| 117 |
freqModels.add(baseModels.get(i).getFrequencyModel()); |
| 118 |
addModel(baseModels.get(i).getFrequencyModel()); |
| 119 |
DataType thisDataType = baseModels.get(i).getDataType(); |
| 120 |
stateSizes += thisDataType.getStateCount(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
freqModel = new MarkovModulatedFrequencyModel("mm", freqModels, switchingRates); |
| 125 |
addModel(freqModel); |
| 126 |
|
| 127 |
if (stateCount != stateSizes) { |
| 128 |
throw new RuntimeException("Incompatible state counts in " + getModelName() + " (currently: " + stateCount + "). Models add up to " + stateSizes + "."); |
| 129 |
} |
| 130 |
|
| 131 |
birthDeathModel = true; |
| 132 |
this.geometricRates = geometricRates; |
| 133 |
|
| 134 |
if (numBaseModel > 1) { |
| 135 |
if (switchingRates.getDimension() != 2 * (numBaseModel - 1)) { |
| 136 |
birthDeathModel = false; |
| 137 |
|
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (gammaRateModel != null) { |
| 142 |
addModel(gammaRateModel); |
| 143 |
|
| 144 |
if (gammaRateModel.getCategoryCount() != numBaseModel && numBaseModel % gammaRateModel.getCategoryCount() != 0) { |
| 145 |
throw new RuntimeException("Wrong discretized gamma dimension"); |
| 146 |
} |
| 147 |
} |
| 148 |
this.gammaRateModel = gammaRateModel; |
| 149 |
|
| 150 |
if (rateScalar != null) { |
| 151 |
addVariable(rateScalar); |
| 152 |
|
| 153 |
if (rateScalar.getDimension() != 1 && rateScalar.getDimension() != numBaseModel) { |
| 154 |
throw new RuntimeException("Wrong rate scalar dimensions"); |
| 155 |
} |
| 156 |
} |
| 157 |
this.rateScalar = rateScalar; |
| 158 |
|
| 159 |
setDoNormalization(false); |
| 160 |
|
| 161 |
updateMatrix = true; |
| 162 |
|
| 163 |
Logger.getLogger("dr.app.beagle").info("\tConstructing a Markov-modulated Markov chain substitution model with " + stateCount + " states; please cite:\n" |
| 164 |
+ Citable.Utils.getCitationString(this)); |
| 165 |
} |
| 166 |
|
| 167 |
public int getNumBaseModel() { |
| 168 |
return numBaseModel; |
| 169 |
} |
| 170 |
|
| 171 |
public double getModelRateScalar(int model) { |
| 172 |
if (gammaRateModel != null) { |
| 173 |
model = model % gammaRateModel.getCategoryCount(); |
| 174 |
if (DEBUG) { |
| 175 |
System.err.println("M" + model + " = " + gammaRateModel.getRateForCategory(model)); |
| 176 |
} |
| 177 |
return gammaRateModel.getRateForCategory(model); |
| 178 |
} |
| 179 |
if (rateScalar == null) { |
| 180 |
return 1.0; |
| 181 |
} else { |
| 182 |
if (rateScalar.getDimension() == 1) { |
| 183 |
return rateScalar.getParameterValue(0); |
| 184 |
} else { |
| 185 |
return rateScalar.getParameterValue(model); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
protected void storeState() { |
| 191 |
if (DEBUG) { |
| 192 |
System.err.println("MMSM.sS"); |
| 193 |
} |
| 194 |
|
| 195 |
if (NEW_STORE_RESTORE) { |
| 196 |
if (eigenDecomposition != null) { |
| 197 |
storedEigenDecomposition = eigenDecomposition.copy(); |
| 198 |
} |
| 199 |
storedUpdateMatrix = updateMatrix; |
| 200 |
} else { |
| 201 |
super.storeState(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
protected void restoreState() { |
| 206 |
if (DEBUG) { |
| 207 |
System.err.println("MMSM.rS"); |
| 208 |
} |
| 209 |
|
| 210 |
if (NEW_STORE_RESTORE) { |
| 211 |
EigenDecomposition tmp = storedEigenDecomposition; |
| 212 |
storedEigenDecomposition = eigenDecomposition; |
| 213 |
eigenDecomposition = tmp; |
| 214 |
|
| 215 |
updateMatrix = storedUpdateMatrix; |
| 216 |
} else { |
| 217 |
super.restoreState(); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
protected void setupQMatrix(double[] rates, double[] pi, double[][] matrix) { |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
for (int i = 0; i < matrix.length; ++i) { |
| 226 |
Arrays.fill(matrix[i], 0.0); |
| 227 |
} |
| 228 |
|
| 229 |
for (int m = 0; m < numBaseModel; ++m) { |
| 230 |
final int offset = m * baseStateCount; |
| 231 |
baseModels.get(m).getInfinitesimalMatrix(baseMatrix); |
| 232 |
if (DEBUG) { |
| 233 |
System.err.println("m " + m + " : " + new dr.math.matrixAlgebra.Vector(baseMatrix)); |
| 234 |
} |
| 235 |
final double rateScalar = getModelRateScalar(m); |
| 236 |
int k = 0; |
| 237 |
for (int i = 0; i < baseStateCount; i++) { |
| 238 |
for (int j = 0; j < baseStateCount; j++) { |
| 239 |
matrix[offset + i][offset + j] = rateScalar * baseMatrix[k]; |
| 240 |
k++; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
if (!IGNORE_RATES && numBaseModel > 1) { |
| 247 |
double[] swRates = switchingRates.getParameterValues(); |
| 248 |
if (DEBUG) { |
| 249 |
System.err.print("Switching rates: "); |
| 250 |
for (int i = 0; i < swRates.length; i++) { |
| 251 |
System.err.print(swRates[i] + " "); |
| 252 |
} |
| 253 |
System.err.println(); |
| 254 |
} |
| 255 |
double totalRate = 0.0; |
| 256 |
for (double rate : swRates) { |
| 257 |
totalRate += rate; |
| 258 |
} |
| 259 |
int sw = 0; |
| 260 |
for (int g = 0; g < numBaseModel; ++g) { |
| 261 |
for (int h = 0; h < numBaseModel; ++h) { |
| 262 |
boolean valid = birthDeathModel ? Math.abs(g - h) == 1 : g != h; |
| 263 |
if (valid) { |
| 264 |
double rate = swRates[sw]; |
| 265 |
if (geometricRates) { |
| 266 |
rate *= getModelRateScalar(numBaseModel - h - 1) |
| 267 |
/ totalRate; |
| 268 |
} |
| 269 |
for (int i = 0; i < baseStateCount; ++i) { |
| 270 |
matrix[g * baseStateCount + i][h * baseStateCount + i] = rate; |
| 271 |
} |
| 272 |
sw++; |
| 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 |
public EigenDecomposition getEigenDecomposition() { |
| 298 |
if (DEBUG) { |
| 299 |
System.err.println("MMSM.getED"); |
| 300 |
} |
| 301 |
EigenDecomposition ed = super.getEigenDecomposition(); |
| 302 |
|
| 303 |
if (DEBUG) { |
| 304 |
double[][] q = getQCopy(); |
| 305 |
System.err.println(new Matrix(q)); |
| 306 |
System.err.println(""); |
| 307 |
System.err.println(new dr.math.matrixAlgebra.Vector(ed.getEigenValues())); |
| 308 |
System.err.println(""); |
| 309 |
double[] tp = new double[q.length * q.length]; |
| 310 |
|
| 311 |
getTransitionProbabilities(1.0, tp, ed); |
| 312 |
System.err.println(new Vector(tp)); |
| 313 |
|
| 314 |
double[] infinitesimal = new double[q.length * q.length]; |
| 315 |
getInfinitesimalMatrix(infinitesimal); |
| 316 |
double[][] matrixForm = new double[q.length][q.length]; |
| 317 |
int i = 0,j = 0; |
| 318 |
for (int k = 0; k < infinitesimal.length; k++) { |
| 319 |
matrixForm[i][j] = infinitesimal[k]; |
| 320 |
j++; |
| 321 |
if ((k+1) % q.length == 0) { |
| 322 |
i++; |
| 323 |
j = 0; |
| 324 |
} |
| 325 |
} |
| 326 |
System.err.println("Infinitesimal Q matrix:\n" + new Matrix(matrixForm)); |
| 327 |
|
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
return ed; |
| 332 |
} |
| 333 |
|
| 334 |
@Override |
| 335 |
public Citation.Category getCategory() { |
| 336 |
return Citation.Category.SUBSTITUTION_MODELS; |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public String getDescription() { |
| 341 |
return "Markov modulated substitution model"; |
| 342 |
} |
| 343 |
|
| 344 |
public List<Citation> getCitations() { |
| 345 |
return Collections.singletonList(CommonCitations.SUCHARD_2012); |
| 346 |
} |
| 347 |
|
| 348 |
@Override |
| 349 |
protected void frequenciesChanged() { |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
@Override |
| 354 |
protected void ratesChanged() { |
| 355 |
updateMatrix = true; |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
protected void setupRelativeRates(double[] rates) { |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 364 |
if (DEBUG) { |
| 365 |
System.err.println("MMSM.hMCE"); |
| 366 |
} |
| 367 |
|
| 368 |
updateMatrix = true; |
| 369 |
|
| 370 |
|
| 371 |
fireModelChanged(); |
| 372 |
} |
| 373 |
|
| 374 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 375 |
|
| 376 |
if (variable == switchingRates || variable == rateScalar) { |
| 377 |
|
| 378 |
updateMatrix = true; |
| 379 |
|
| 380 |
fireModelChanged(); |
| 381 |
} |
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
public LogColumn[] getColumns() { |
| 386 |
|
| 387 |
List<LogColumn> columns = new ArrayList<LogColumn>(); |
| 388 |
for (LogColumn parentColumn : super.getColumns()) { |
| 389 |
columns.add(parentColumn); |
| 390 |
} |
| 391 |
|
| 392 |
for (int i = 0; i < numBaseModel; ++i) { |
| 393 |
String label = "rateScalar." + i; |
| 394 |
columns.add(new RateColumn(label, i)); |
| 395 |
} |
| 396 |
|
| 397 |
return columns.toArray(new LogColumn[0]); |
| 398 |
} |
| 399 |
|
| 400 |
private class RateColumn extends NumberColumn { |
| 401 |
|
| 402 |
private final int index; |
| 403 |
|
| 404 |
public RateColumn(String label, int index) { |
| 405 |
super(label); |
| 406 |
this.index = index; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
@Override |
| 413 |
public double getDoubleValue() { |
| 414 |
return getModelRateScalar(index); |
| 415 |
} |
| 416 |
} |
| 417 |
} |