| 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.continuous; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.tree.TreeModel; |
| 31 |
import dr.inference.loggers.LogColumn; |
| 32 |
import dr.inference.loggers.NumberColumn; |
| 33 |
import dr.inference.model.*; |
| 34 |
import dr.xml.*; |
| 35 |
import org.w3c.dom.Document; |
| 36 |
import org.w3c.dom.Element; |
| 37 |
|
| 38 |
import java.util.ArrayList; |
| 39 |
import java.util.HashMap; |
| 40 |
import java.util.List; |
| 41 |
import java.util.logging.Logger; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class InhibitionAssayLikelihood extends AbstractModelLikelihood { |
| 50 |
|
| 51 |
public static final String TRAIT_LIKELIHOOD = "inhibitionLikelihood"; |
| 52 |
public static final String TRAIT_NAME = "traitName"; |
| 53 |
public static final String ROOT_PRIOR = "rootPrior"; |
| 54 |
public static final String MODEL = "diffusionModel"; |
| 55 |
public static final String TREE = "tree"; |
| 56 |
public static final String TRAIT_PARAMETER = "traitParameter"; |
| 57 |
public static final String SET_TRAIT = "setOutcomes"; |
| 58 |
public static final String MISSING = "missingIndicator"; |
| 59 |
public static final String CACHE_BRANCHES = "cacheBranches"; |
| 60 |
public static final String IN_REAL_TIME = "inRealTime"; |
| 61 |
public static final String PRECISION = "precision"; |
| 62 |
|
| 63 |
public InhibitionAssayLikelihood(TreeModel treeModel, |
| 64 |
|
| 65 |
|
| 66 |
MatrixParameter dataParameter, |
| 67 |
Parameter precision) { |
| 68 |
|
| 69 |
super(TRAIT_LIKELIHOOD); |
| 70 |
this.treeModel = treeModel; |
| 71 |
this.dataParameter = dataParameter; |
| 72 |
|
| 73 |
addModel(treeModel); |
| 74 |
|
| 75 |
addVariable(dataParameter); |
| 76 |
addVariable(precision); |
| 77 |
|
| 78 |
N = treeModel.getExternalNodeCount(); |
| 79 |
|
| 80 |
StringBuffer sb = new StringBuffer("Creating inhibition assay model:\n"); |
| 81 |
sb.append("\tPlease cite O'Brien and Suchard (in preparation) if you publish results using this model."); |
| 82 |
|
| 83 |
Logger.getLogger("dr.evomodel").info(sb.toString()); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 92 |
|
| 93 |
likelihoodKnown = false; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
private double getBranchMean(TreeModel tree, NodeRef node) { |
| 99 |
|
| 100 |
if (tree.isRoot(node)) { |
| 101 |
return tree.getNodeTrait(node, "mean"); |
| 102 |
} else { |
| 103 |
double rate; |
| 104 |
if (isClusterChangeOnBranchAbove(tree, node)) { |
| 105 |
rate = tree.getNodeTrait(node, "mean"); |
| 106 |
|
| 107 |
} else { |
| 108 |
rate = getBranchMean(tree, tree.getParent(node)); |
| 109 |
} |
| 110 |
return rate; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public final boolean isClusterChangeOnBranchAbove(TreeModel tree, NodeRef node) { |
| 115 |
return tree.getNodeTrait(node, "indicator") == 1; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public final boolean areNodesInSameCluster(TreeModel tree, NodeRef node1, NodeRef node2) { |
| 120 |
return clusterStart(tree, node1) == clusterStart(tree, node2); |
| 121 |
} |
| 122 |
|
| 123 |
public final NodeRef clusterStart(TreeModel tree, NodeRef node) { |
| 124 |
if (tree.isRoot(node) || isClusterChangeOnBranchAbove(tree, node)) |
| 125 |
return node; |
| 126 |
return clusterStart(tree, tree.getParent(node)); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 134 |
|
| 135 |
likelihoodKnown = false; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
protected void storeState() { |
| 146 |
storedLikelihoodKnown = likelihoodKnown; |
| 147 |
storedLogLikelihood = logLikelihood; |
| 148 |
storedTreeLength = treeLength; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
protected void restoreState() { |
| 155 |
likelihoodKnown = storedLikelihoodKnown; |
| 156 |
logLikelihood = storedLogLikelihood; |
| 157 |
treeLength = storedTreeLength; |
| 158 |
} |
| 159 |
|
| 160 |
protected void acceptState() { |
| 161 |
} |
| 162 |
|
| 163 |
public TreeModel getTreeModel() { |
| 164 |
return treeModel; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public Model getModel() { |
| 180 |
return this; |
| 181 |
} |
| 182 |
|
| 183 |
public String toString() { |
| 184 |
return getClass().getName() + "(" + getLogLikelihood() + ")"; |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
public final double getLogLikelihood() { |
| 189 |
if (!likelihoodKnown) { |
| 190 |
logLikelihood = calculateLogLikelihood(); |
| 191 |
likelihoodKnown = true; |
| 192 |
} |
| 193 |
return logLikelihood; |
| 194 |
} |
| 195 |
|
| 196 |
public void makeDirty() { |
| 197 |
likelihoodKnown = false; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public double calculateLogLikelihood() { |
| 206 |
|
| 207 |
logLikelihood = 0; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
double[] mean = new double[N]; |
| 225 |
|
| 226 |
for (int i = 0; i < N; i++) |
| 227 |
mean[i] = getBranchMean(treeModel, treeModel.getExternalNode(i)); |
| 228 |
|
| 229 |
final double[][] data = dataParameter.getParameterAsMatrix(); |
| 230 |
final boolean[][] commonCluster = determineCommonClusters(); |
| 231 |
|
| 232 |
for (int i = 0; i < N; i++) { |
| 233 |
for (int j = i; j < N; j++) { |
| 234 |
if (i != j) { |
| 235 |
if (commonCluster[i][j]) { |
| 236 |
|
| 237 |
} else { |
| 238 |
|
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
return logLikelihood; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
private boolean[][] determineCommonClusters() { |
| 250 |
|
| 251 |
boolean[][] commonCluster = new boolean[N][N]; |
| 252 |
for (int i = 0; i < N; i++) { |
| 253 |
for (int j = i; j < N; j++) { |
| 254 |
if (i != j) { |
| 255 |
NodeRef nodeI = treeModel.getExternalNode(i); |
| 256 |
NodeRef nodeJ = treeModel.getExternalNode(j); |
| 257 |
if (areNodesInSameCluster(treeModel, nodeI, nodeJ)) { |
| 258 |
commonCluster[i][j] = commonCluster[j][i] = true; |
| 259 |
} else { |
| 260 |
commonCluster[i][j] = commonCluster[j][i] = false; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
return commonCluster; |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
public double getMaxLogLikelihood() { |
| 271 |
return maxLogLikelihood; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
public int[] getRestrictedGrowthFunction() { |
| 276 |
boolean[][] commonCluster = determineCommonClusters(); |
| 277 |
int totalClusters = 1; |
| 278 |
int[] map = new int[N]; |
| 279 |
map[0] = 0; |
| 280 |
|
| 281 |
for (int i = 1; i < N; i++) { |
| 282 |
boolean notFound = true; |
| 283 |
for (int j = 0; notFound && j < i; j++) { |
| 284 |
if (commonCluster[i][j]) { |
| 285 |
notFound = false; |
| 286 |
map[i] = map[j]; |
| 287 |
} |
| 288 |
} |
| 289 |
if (notFound) { |
| 290 |
map[i] = totalClusters; |
| 291 |
totalClusters++; |
| 292 |
} |
| 293 |
} |
| 294 |
return map; |
| 295 |
} |
| 296 |
|
| 297 |
public String getClusterString() { |
| 298 |
|
| 299 |
int[] map = getRestrictedGrowthFunction(); |
| 300 |
|
| 301 |
StringBuffer sb = new StringBuffer("{"); |
| 302 |
sb.append(map[0]); |
| 303 |
for (int i = 1; i < N; i++) |
| 304 |
sb.append("," + map[i]); |
| 305 |
sb.append("}"); |
| 306 |
|
| 307 |
return sb.toString(); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public LogColumn[] getColumns() { |
| 318 |
return new LogColumn[]{ |
| 319 |
new LikelihoodColumn(getId()), |
| 320 |
|
| 321 |
new ClustersColumn(getId()) |
| 322 |
}; |
| 323 |
} |
| 324 |
|
| 325 |
private String[] attributeLabel = null; |
| 326 |
|
| 327 |
public String[] getNodeAttributeLabel() { |
| 328 |
if (attributeLabel == null) { |
| 329 |
double[] trait = treeModel.getMultivariateNodeTrait(treeModel.getRoot(), "trait"); |
| 330 |
attributeLabel = new String[trait.length]; |
| 331 |
if (trait.length == 1) |
| 332 |
attributeLabel[0] = traitName; |
| 333 |
else { |
| 334 |
for (int i = 1; i <= trait.length; i++) |
| 335 |
attributeLabel[i - 1] = traitName + i; |
| 336 |
} |
| 337 |
} |
| 338 |
return attributeLabel; |
| 339 |
} |
| 340 |
|
| 341 |
public String[] getAttributeForNode(Tree tree, NodeRef node) { |
| 342 |
double trait[] = treeModel.getMultivariateNodeTrait(node, "trait"); |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
String[] value = new String[trait.length]; |
| 352 |
for (int i = 0; i < trait.length; i++) |
| 353 |
value[i] = Double.toString(trait[i]); |
| 354 |
|
| 355 |
|
| 356 |
return value; |
| 357 |
} |
| 358 |
|
| 359 |
private class LikelihoodColumn extends NumberColumn { |
| 360 |
|
| 361 |
public LikelihoodColumn(String label) { |
| 362 |
super(label); |
| 363 |
} |
| 364 |
|
| 365 |
public double getDoubleValue() { |
| 366 |
return getLogLikelihood(); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
private class ClustersColumn extends LogColumn.Abstract { |
| 384 |
|
| 385 |
public ClustersColumn(String label) { |
| 386 |
super(label); |
| 387 |
} |
| 388 |
|
| 389 |
protected String getFormattedValue() { |
| 390 |
return getClusterString(); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public Element createElement(Document d) { |
| 399 |
throw new RuntimeException("Not implemented yet!"); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 407 |
|
| 408 |
public String getParserName() { |
| 409 |
return TRAIT_LIKELIHOOD; |
| 410 |
} |
| 411 |
|
| 412 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 418 |
|
| 419 |
|
| 420 |
MatrixParameter data = (MatrixParameter) xo.getChild(MatrixParameter.class); |
| 421 |
|
| 422 |
XMLObject cxo = xo.getChild(PRECISION); |
| 423 |
|
| 424 |
Parameter precision = (Parameter) cxo.getChild(Parameter.class); |
| 425 |
|
| 426 |
int numTips = treeModel.getExternalNodeCount(); |
| 427 |
if (numTips != data.getColumnDimension() || numTips != data.getRowDimension()) |
| 428 |
throw new XMLParseException("Dimensions of matrix '" + data.getId() + "' do not match the number of taxa in '" + treeModel.getId() + "'"); |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
List<Integer> missingIndices = null; |
| 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 |
return new InhibitionAssayLikelihood(treeModel, data, precision); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
private Parameter getTraitParameterByName(CompoundParameter traits, String name) { |
| 512 |
|
| 513 |
|
| 514 |
for (int i = 0; i < traits.getParameterCount(); i++) { |
| 515 |
Parameter found = traits.getParameter(i); |
| 516 |
|
| 517 |
if (found.getStatisticName().compareTo(name) == 0) |
| 518 |
return found; |
| 519 |
} |
| 520 |
return null; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
public String getParserDescription() { |
| 528 |
return "Provides the likelihood of a continuous trait evolving on a tree by a " + |
| 529 |
"given diffusion model."; |
| 530 |
} |
| 531 |
|
| 532 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 533 |
return rules; |
| 534 |
} |
| 535 |
|
| 536 |
private final XMLSyntaxRule[] rules = { |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
new ElementRule(TreeModel.class), |
| 541 |
new ElementRule(MatrixParameter.class), |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
new ElementRule(PRECISION, |
| 547 |
new XMLSyntaxRule[]{ |
| 548 |
new ElementRule(Parameter.class)}) |
| 549 |
|
| 550 |
|
| 551 |
}; |
| 552 |
|
| 553 |
|
| 554 |
public Class getReturnType() { |
| 555 |
return AbstractMultivariateTraitLikelihood.class; |
| 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 |
private TreeModel treeModel = null; |
| 609 |
private MatrixParameter dataParameter = null; |
| 610 |
private final Parameter precision = null; |
| 611 |
|
| 612 |
private final int N; |
| 613 |
|
| 614 |
|
| 615 |
MultivariateDiffusionModel diffusionModel = null; |
| 616 |
String traitName = null; |
| 617 |
|
| 618 |
CompoundParameter traitParameter; |
| 619 |
List<Integer> missingIndices; |
| 620 |
|
| 621 |
ArrayList dataList = new ArrayList(); |
| 622 |
|
| 623 |
private double logLikelihood; |
| 624 |
private final double maxLogLikelihood = Double.NEGATIVE_INFINITY; |
| 625 |
private double storedLogLikelihood; |
| 626 |
private boolean likelihoodKnown = false; |
| 627 |
private boolean storedLikelihoodKnown = false; |
| 628 |
|
| 629 |
|
| 630 |
private final HashMap<NodeRef, Double> cachedLikelihoods = null; |
| 631 |
|
| 632 |
private double treeLength; |
| 633 |
private double storedTreeLength; |
| 634 |
|
| 635 |
private boolean inSubstitutionTime; |
| 636 |
} |
| 637 |
|