| 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.arg; |
| 27 |
|
| 28 |
import dr.evolution.tree.MutableTreeListener; |
| 29 |
import dr.evolution.tree.NodeRef; |
| 30 |
import dr.evolution.tree.Tree; |
| 31 |
import dr.evolution.tree.TreeUtils; |
| 32 |
import dr.evolution.util.MutableTaxonListListener; |
| 33 |
import dr.evolution.util.Taxon; |
| 34 |
import dr.evomodel.arg.ARGModel.Node; |
| 35 |
import dr.util.Attributable; |
| 36 |
|
| 37 |
import java.util.*; |
| 38 |
|
| 39 |
public class ARGTree implements Tree { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected Taxon[] taxaList; |
| 44 |
protected int taxaCount; |
| 45 |
|
| 46 |
private final Node initialRoot; |
| 47 |
|
| 48 |
public ARGModel argModel; |
| 49 |
|
| 50 |
|
| 51 |
private final Map<NodeRef,NodeRef> mapARGNodesToTreeNodes; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private int partition = -9; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public boolean wasRootTrimmed() { |
| 81 |
return (root != initialRoot); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
public String toGraphString() { |
| 86 |
StringBuffer sb = new StringBuffer(); |
| 87 |
for (Node node : nodes) { |
| 88 |
sb.append(node.number); |
| 89 |
if (node.leftParent != null) |
| 90 |
sb.append(" " + node.leftParent.number); |
| 91 |
else |
| 92 |
sb.append(" 0"); |
| 93 |
if (node.rightParent != null) |
| 94 |
sb.append(" " + node.rightParent.number); |
| 95 |
else |
| 96 |
sb.append(" 0"); |
| 97 |
if (node.leftChild != null) |
| 98 |
sb.append(" " + node.leftChild.number); |
| 99 |
else |
| 100 |
sb.append(" 0"); |
| 101 |
if (node.rightChild != null) |
| 102 |
sb.append(" " + node.rightChild.number); |
| 103 |
else |
| 104 |
sb.append(" 0"); |
| 105 |
if (node.taxon != null) |
| 106 |
sb.append(" " + node.taxon.toString()); |
| 107 |
sb.append("\n"); |
| 108 |
} |
| 109 |
sb.append("Root = " + ((Node) getRoot()).number + "\n"); |
| 110 |
return new String(sb); |
| 111 |
} |
| 112 |
|
| 113 |
public ARGTree(ARGModel arg, int partition) { |
| 114 |
this.argModel = arg; |
| 115 |
mapARGNodesToTreeNodes = new HashMap<NodeRef,NodeRef>(arg.getNodeCount()); |
| 116 |
|
| 117 |
this.partition = partition; |
| 118 |
ARGModel.Node node = arg.new Node(((Node) arg.getRoot()), partition); |
| 119 |
initialRoot = node; |
| 120 |
|
| 121 |
int j = arg.externalNodeCount; |
| 122 |
node.stripOutDeadEnds(); |
| 123 |
root = node.stripOutSingleChildNodes(node); |
| 124 |
node = root; |
| 125 |
nodeCount = 2 * j - 1; |
| 126 |
externalNodeCount = j; |
| 127 |
internalNodeCount = j - 1; |
| 128 |
nodes = new Node[nodeCount]; |
| 129 |
|
| 130 |
do { |
| 131 |
node = (Node) TreeUtils.postorderSuccessor(this, node); |
| 132 |
if (node.isExternal()) { |
| 133 |
|
| 134 |
nodes[node.number] = node; |
| 135 |
mapARGNodesToTreeNodes.put(node.mirrorNode,node); |
| 136 |
} else { |
| 137 |
|
| 138 |
|
| 139 |
nodes[j] = node; |
| 140 |
node.number = j; |
| 141 |
j++; |
| 142 |
mapARGNodesToTreeNodes.put(node.mirrorNode, node); |
| 143 |
} |
| 144 |
} while (node != root); |
| 145 |
hasRates = false; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
public Map<NodeRef,NodeRef> getMapping() { |
| 150 |
return mapARGNodesToTreeNodes; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public boolean checkForNullRights(Node node) { |
| 163 |
return node.checkForNullRights(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public final Type getUnits() { |
| 175 |
return units; |
| 176 |
} |
| 177 |
|
| 178 |
public void setUnits(Type units) { |
| 179 |
this.units = units; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public final int getNodeCount() { |
| 193 |
return nodeCount; |
| 194 |
} |
| 195 |
|
| 196 |
public final boolean hasNodeHeights() { |
| 197 |
return true; |
| 198 |
} |
| 199 |
|
| 200 |
public final double getNodeHeight(NodeRef node) { |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
return ((Node) node).getHeight(); |
| 206 |
} |
| 207 |
|
| 208 |
public final double getNodeHeightUpper(NodeRef node) { |
| 209 |
return ((Node) node).heightParameter.getBounds().getUpperLimit(0); |
| 210 |
} |
| 211 |
|
| 212 |
public final double getNodeHeightLower(NodeRef node) { |
| 213 |
return ((Node) node).heightParameter.getBounds().getLowerLimit(0); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public final double getNodeRate(NodeRef node) { |
| 222 |
|
| 223 |
|
| 224 |
if (!hasRates) { |
| 225 |
return 1.0; |
| 226 |
} |
| 227 |
|
| 228 |
return ((Node) node).getRate(partition); |
| 229 |
} |
| 230 |
|
| 231 |
public Object getNodeAttribute(NodeRef node, String name) { |
| 232 |
throw new UnsupportedOperationException("TreeModel does not use NodeAttributes"); |
| 233 |
} |
| 234 |
|
| 235 |
public Iterator getNodeAttributeNames(NodeRef node) { |
| 236 |
throw new UnsupportedOperationException("TreeModel does not use NodeAttributes"); |
| 237 |
} |
| 238 |
|
| 239 |
public double getNodeTrait(NodeRef node) { |
| 240 |
if (!hasTraits) throw new IllegalArgumentException("Trait parameters have not been created"); |
| 241 |
return ((Node) node).getTrait(); |
| 242 |
} |
| 243 |
|
| 244 |
public final Taxon getNodeTaxon(NodeRef node) { |
| 245 |
return ((Node) node).taxon; |
| 246 |
} |
| 247 |
|
| 248 |
public final boolean isExternal(NodeRef node) { |
| 249 |
return ((Node) node).isExternal(); |
| 250 |
} |
| 251 |
|
| 252 |
public final boolean isRoot(NodeRef node) { |
| 253 |
return (node == root); |
| 254 |
} |
| 255 |
|
| 256 |
public final int getChildCount(NodeRef node) { |
| 257 |
|
| 258 |
return ((Node) node).getChildCount(); |
| 259 |
} |
| 260 |
|
| 261 |
public final NodeRef getChild(NodeRef node, int i) { |
| 262 |
return ((Node) node).getChild(i); |
| 263 |
} |
| 264 |
|
| 265 |
public final NodeRef getParent(NodeRef node) { |
| 266 |
|
| 267 |
return ((Node) node).leftParent; |
| 268 |
} |
| 269 |
|
| 270 |
public final boolean hasBranchLengths() { |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
public final double getBranchLength(NodeRef node) { |
| 275 |
NodeRef parent = getParent(node); |
| 276 |
if (parent == null) { |
| 277 |
return 0.0; |
| 278 |
} |
| 279 |
|
| 280 |
return getNodeHeight(parent) - getNodeHeight(node); |
| 281 |
} |
| 282 |
|
| 283 |
public final NodeRef getExternalNode(int i) { |
| 284 |
return nodes[i]; |
| 285 |
} |
| 286 |
|
| 287 |
public final NodeRef getInternalNode(int i) { |
| 288 |
return nodes[i + externalNodeCount]; |
| 289 |
} |
| 290 |
|
| 291 |
public final NodeRef getNode(int i) { |
| 292 |
return nodes[i]; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public final int getExternalNodeCount() { |
| 299 |
return externalNodeCount; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public final int getInternalNodeCount() { |
| 306 |
return internalNodeCount; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public final NodeRef getRoot() { |
| 313 |
return root; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public int getTaxonCount() { |
| 324 |
return getExternalNodeCount(); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public Taxon getTaxon(int taxonIndex) { |
| 340 |
return ((Node) getExternalNode(taxonIndex)).taxon; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public String getTaxonId(int taxonIndex) { |
| 349 |
Taxon taxon = getTaxon(taxonIndex); |
| 350 |
if (taxon != null) { |
| 351 |
return taxon.getId(); |
| 352 |
} else { |
| 353 |
return null; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public int getTaxonIndex(String id) { |
| 361 |
for (int i = 0, n = getTaxonCount(); i < n; i++) { |
| 362 |
if (getTaxonId(i).equals(id)) return i; |
| 363 |
} |
| 364 |
return -1; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public int getTaxonIndex(Taxon taxon) { |
| 371 |
for (int i = 0, n = getTaxonCount(); i < n; i++) { |
| 372 |
if (getTaxon(i) == taxon) return i; |
| 373 |
} |
| 374 |
return -1; |
| 375 |
} |
| 376 |
|
| 377 |
public List<Taxon> asList() { |
| 378 |
List<Taxon> taxa = new ArrayList<Taxon>(); |
| 379 |
for (int i = 0, n = getTaxonCount(); i < n; i++) { |
| 380 |
taxa.add(getTaxon(i)); |
| 381 |
} |
| 382 |
return taxa; |
| 383 |
} |
| 384 |
|
| 385 |
public Iterator<Taxon> iterator() { |
| 386 |
return new Iterator<Taxon>() { |
| 387 |
private int index = -1; |
| 388 |
|
| 389 |
public boolean hasNext() { |
| 390 |
return index < getTaxonCount() - 1; |
| 391 |
} |
| 392 |
|
| 393 |
public Taxon next() { |
| 394 |
index ++; |
| 395 |
return getTaxon(index); |
| 396 |
} |
| 397 |
|
| 398 |
public void remove() { } |
| 399 |
}; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public final Object getTaxonAttribute(int taxonIndex, String name) { |
| 410 |
Taxon taxon = getTaxon(taxonIndex); |
| 411 |
if (taxon != null) { |
| 412 |
return taxon.getAttribute(name); |
| 413 |
} |
| 414 |
return null; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public int addTaxon(Taxon taxon) { |
| 422 |
throw new IllegalArgumentException("Cannot add taxon to a TreeModel"); |
| 423 |
} |
| 424 |
|
| 425 |
public boolean removeTaxon(Taxon taxon) { |
| 426 |
throw new IllegalArgumentException("Cannot add taxon to a TreeModel"); |
| 427 |
} |
| 428 |
|
| 429 |
public void setTaxonId(int taxonIndex, String id) { |
| 430 |
throw new IllegalArgumentException("Cannot set taxon id in a TreeModel"); |
| 431 |
} |
| 432 |
|
| 433 |
public void setTaxonAttribute(int taxonIndex, String name, Object value) { |
| 434 |
throw new IllegalArgumentException("Cannot set taxon attribute in a TreeModel"); |
| 435 |
} |
| 436 |
|
| 437 |
public void addMutableTreeListener(MutableTreeListener listener) { |
| 438 |
} |
| 439 |
|
| 440 |
public void addMutableTaxonListListener(MutableTaxonListListener listener) { |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
protected String id = null; |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
public String getId() { |
| 453 |
return id; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
public void setId(String id) { |
| 460 |
this.id = id; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
private Attributable.AttributeHelper treeAttributes = null; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
public void setAttribute(String name, Object value) { |
| 476 |
if (treeAttributes == null) |
| 477 |
treeAttributes = new Attributable.AttributeHelper(); |
| 478 |
treeAttributes.setAttribute(name, value); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public Object getAttribute(String name) { |
| 486 |
if (treeAttributes == null) |
| 487 |
return null; |
| 488 |
else |
| 489 |
return treeAttributes.getAttribute(name); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
public Iterator<String> getAttributeNames() { |
| 496 |
if (treeAttributes == null) |
| 497 |
return null; |
| 498 |
else |
| 499 |
return treeAttributes.getAttributeNames(); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public final String getNewick() { |
| 506 |
return TreeUtils.newick(this); |
| 507 |
} |
| 508 |
|
| 509 |
public final String getUniqueNewick(){ |
| 510 |
return TreeUtils.uniqueNewick(this,this.getRoot()); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
public String toString() { |
| 517 |
return getNewick(); |
| 518 |
} |
| 519 |
|
| 520 |
public Tree getCopy() { |
| 521 |
throw new UnsupportedOperationException("please don't call this function"); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
protected Node root = null; |
| 534 |
protected int storedRootNumber; |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
protected Node[] nodes = null; |
| 540 |
protected Node[] storedNodes = null; |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
protected int nodeCount; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
protected int externalNodeCount; |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
protected int internalNodeCount; |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
private Type units; |
| 565 |
|
| 566 |
protected boolean inEdit = false; |
| 567 |
|
| 568 |
private final boolean hasRates; |
| 569 |
private final boolean hasTraits = false; |
| 570 |
|
| 571 |
|
| 572 |
} |