| 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.evolution.tree; |
| 27 |
|
| 28 |
import dr.evolution.util.MutableTaxonListListener; |
| 29 |
import dr.evolution.util.Taxon; |
| 30 |
import dr.util.Attributable; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.Iterator; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Map; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class FlexibleTree implements MutableTree { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public FlexibleTree() { |
| 50 |
root = null; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public FlexibleTree(Tree tree) { |
| 57 |
this(tree, false); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public FlexibleTree(Tree tree, boolean copyAttributes) { |
| 64 |
|
| 65 |
setUnits(tree.getUnits()); |
| 66 |
|
| 67 |
root = new FlexibleNode(tree, tree.getRoot(), copyAttributes); |
| 68 |
|
| 69 |
nodeCount = tree.getNodeCount(); |
| 70 |
internalNodeCount = tree.getInternalNodeCount(); |
| 71 |
externalNodeCount = tree.getExternalNodeCount(); |
| 72 |
|
| 73 |
|
| 74 |
nodes = new FlexibleNode[nodeCount]; |
| 75 |
|
| 76 |
FlexibleNode node = root; |
| 77 |
do { |
| 78 |
node = (FlexibleNode) TreeUtils.postorderSuccessor(this, node); |
| 79 |
if ((node.getNumber() >= externalNodeCount && node.isExternal()) || |
| 80 |
(node.getNumber() < externalNodeCount && !node.isExternal())) { |
| 81 |
throw new RuntimeException("Error cloning tree: node numbers are incompatible"); |
| 82 |
} |
| 83 |
nodes[node.getNumber()] = node; |
| 84 |
} while (node != root); |
| 85 |
|
| 86 |
heightsKnown = tree.hasNodeHeights(); |
| 87 |
lengthsKnown = tree.hasBranchLengths(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public FlexibleTree(FlexibleNode root) { |
| 94 |
this(root, true, true, null); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public FlexibleTree(FlexibleNode root, boolean heightsKnown, boolean lengthsKnown) { |
| 101 |
|
| 102 |
this(root, heightsKnown, lengthsKnown, null); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public FlexibleTree(FlexibleNode root, Map<Taxon, Integer> taxonNumberMap) { |
| 109 |
this(root, true, true, taxonNumberMap); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public FlexibleTree(FlexibleNode root, boolean heightsKnown, boolean lengthsKnown, Map<Taxon, Integer> taxonNumberMap) { |
| 116 |
|
| 117 |
adoptNodes(root, taxonNumberMap); |
| 118 |
|
| 119 |
this.heightsKnown = heightsKnown; |
| 120 |
this.lengthsKnown = lengthsKnown; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public Tree getCopy() { |
| 128 |
return new FlexibleTree(this); |
| 129 |
} |
| 130 |
|
| 131 |
public void adoptTreeModelOrdering() { |
| 132 |
resolveTree(); |
| 133 |
int i = 0; |
| 134 |
int j = externalNodeCount; |
| 135 |
|
| 136 |
FlexibleNode node = (FlexibleNode) getRoot(); |
| 137 |
|
| 138 |
do { |
| 139 |
node = (FlexibleNode) TreeUtils.postorderSuccessor(this, node); |
| 140 |
|
| 141 |
if (node.isExternal()) { |
| 142 |
node.setNumber(i); |
| 143 |
nodes[i] = node; |
| 144 |
i++; |
| 145 |
} else { |
| 146 |
node.setNumber(j); |
| 147 |
nodes[j] = node; |
| 148 |
j++; |
| 149 |
} |
| 150 |
} while (node != root); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
protected void adoptNodes(FlexibleNode node, Map<Taxon, Integer> taxonNumberMap) { |
| 158 |
|
| 159 |
if (inEdit) throw new RuntimeException("Mustn't be in an edit transaction to call this method!"); |
| 160 |
|
| 161 |
internalNodeCount = 0; |
| 162 |
externalNodeCount = 0; |
| 163 |
|
| 164 |
root = node; |
| 165 |
|
| 166 |
do { |
| 167 |
node = (FlexibleNode) TreeUtils.postorderSuccessor(this, node); |
| 168 |
if (node.isExternal()) { |
| 169 |
externalNodeCount++; |
| 170 |
} else |
| 171 |
internalNodeCount++; |
| 172 |
|
| 173 |
} while (node != root); |
| 174 |
|
| 175 |
nodeCount = internalNodeCount + externalNodeCount; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
nodes = new FlexibleNode[nodeCount]; |
| 180 |
|
| 181 |
node = root; |
| 182 |
int i = 0; |
| 183 |
int j = externalNodeCount; |
| 184 |
|
| 185 |
do { |
| 186 |
node = (FlexibleNode) TreeUtils.postorderSuccessor(this, node); |
| 187 |
|
| 188 |
if (node.isExternal()) { |
| 189 |
if (taxonNumberMap != null && taxonNumberMap.size() > 0) { |
| 190 |
i = taxonNumberMap.get(node.getTaxon()); |
| 191 |
} |
| 192 |
|
| 193 |
node.setNumber(i); |
| 194 |
|
| 195 |
nodes[i] = node; |
| 196 |
|
| 197 |
if (taxonNumberMap == null || taxonNumberMap.size() == 0) { |
| 198 |
i++; |
| 199 |
} |
| 200 |
} else { |
| 201 |
node.setNumber(j); |
| 202 |
|
| 203 |
nodes[j] = node; |
| 204 |
j++; |
| 205 |
} |
| 206 |
} while (node != root); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public final Type getUnits() { |
| 213 |
return units; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public final void setUnits(Type units) { |
| 220 |
this.units = units; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public int getNodeCount() { |
| 228 |
return nodeCount; |
| 229 |
} |
| 230 |
|
| 231 |
public boolean hasNodeHeights() { |
| 232 |
return heightsKnown; |
| 233 |
} |
| 234 |
|
| 235 |
public double getNodeHeight(NodeRef node) { |
| 236 |
if (!heightsKnown) { |
| 237 |
calculateNodeHeights(); |
| 238 |
} |
| 239 |
return ((FlexibleNode) node).getHeight(); |
| 240 |
} |
| 241 |
|
| 242 |
public boolean hasBranchLengths() { |
| 243 |
return lengthsKnown; |
| 244 |
} |
| 245 |
|
| 246 |
public double getBranchLength(NodeRef node) { |
| 247 |
if (!lengthsKnown) { |
| 248 |
calculateBranchLengths(); |
| 249 |
} |
| 250 |
return ((FlexibleNode) node).getLength(); |
| 251 |
} |
| 252 |
|
| 253 |
public double getNodeRate(NodeRef node) { |
| 254 |
|
| 255 |
Object rateAttr = getNodeAttribute(node, "rate"); |
| 256 |
|
| 257 |
if (rateAttr != null) { |
| 258 |
if (rateAttr instanceof Number) return (Double) rateAttr; |
| 259 |
if (rateAttr instanceof String) return Double.parseDouble((String) rateAttr); |
| 260 |
} |
| 261 |
|
| 262 |
return ((FlexibleNode) node).getRate(); |
| 263 |
} |
| 264 |
|
| 265 |
public Taxon getNodeTaxon(NodeRef node) { |
| 266 |
return ((FlexibleNode) node).getTaxon(); |
| 267 |
} |
| 268 |
|
| 269 |
public void setNodeTaxon(NodeRef node, Taxon taxon) { |
| 270 |
((FlexibleNode) node).setTaxon(taxon); |
| 271 |
} |
| 272 |
|
| 273 |
public int getChildCount(NodeRef node) { |
| 274 |
return ((FlexibleNode) node).getChildCount(); |
| 275 |
} |
| 276 |
|
| 277 |
public boolean isExternal(NodeRef node) { |
| 278 |
return ((FlexibleNode) node).getChildCount() == 0; |
| 279 |
} |
| 280 |
|
| 281 |
public boolean isRoot(NodeRef node) { |
| 282 |
return (node == root); |
| 283 |
} |
| 284 |
|
| 285 |
public NodeRef getChild(NodeRef node, int i) { |
| 286 |
return ((FlexibleNode) node).getChild(i); |
| 287 |
} |
| 288 |
|
| 289 |
public NodeRef getParent(NodeRef node) { |
| 290 |
return ((FlexibleNode) node).getParent(); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
public final NodeRef getExternalNode(int i) { |
| 295 |
return nodes[i]; |
| 296 |
} |
| 297 |
|
| 298 |
public final NodeRef getInternalNode(int i) { |
| 299 |
return nodes[i + externalNodeCount]; |
| 300 |
} |
| 301 |
|
| 302 |
public final NodeRef getNode(int i) { |
| 303 |
return nodes[i]; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public final int getExternalNodeCount() { |
| 310 |
return externalNodeCount; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public final int getInternalNodeCount() { |
| 317 |
return internalNodeCount; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public final NodeRef getRoot() { |
| 324 |
return root; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public final void setRoot(NodeRef r) { |
| 331 |
|
| 332 |
if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!"); |
| 333 |
|
| 334 |
if (!(r instanceof FlexibleNode)) { |
| 335 |
throw new IllegalArgumentException(); |
| 336 |
} |
| 337 |
root = (FlexibleNode) r; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public final double getRootHeight() { |
| 345 |
return getNodeHeight(root); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public final void setRootHeight(double height) { |
| 352 |
setNodeHeight(root, height); |
| 353 |
|
| 354 |
fireTreeChanged(); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
public void addChild(NodeRef p, NodeRef c) { |
| 359 |
if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!"); |
| 360 |
FlexibleNode parent = (FlexibleNode) p; |
| 361 |
FlexibleNode child = (FlexibleNode) c; |
| 362 |
if (parent.hasChild(child)) throw new IllegalArgumentException("Child already existists in parent"); |
| 363 |
|
| 364 |
parent.addChild(child); |
| 365 |
} |
| 366 |
|
| 367 |
public void removeChild(NodeRef p, NodeRef c) { |
| 368 |
|
| 369 |
if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!"); |
| 370 |
|
| 371 |
FlexibleNode parent = (FlexibleNode) p; |
| 372 |
FlexibleNode child = (FlexibleNode) c; |
| 373 |
|
| 374 |
for (int i = 0; i < parent.getChildCount(); i++) { |
| 375 |
if (parent.getChild(i) == child) { |
| 376 |
parent.removeChild(i); |
| 377 |
return; |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
public void replaceChild(NodeRef node, NodeRef child, NodeRef newChild) { |
| 383 |
throw new RuntimeException("Unimplemented"); |
| 384 |
} |
| 385 |
|
| 386 |
public boolean beginTreeEdit() { |
| 387 |
boolean r = inEdit; |
| 388 |
inEdit = true; |
| 389 |
return r; |
| 390 |
} |
| 391 |
|
| 392 |
public void endTreeEdit() { |
| 393 |
inEdit = false; |
| 394 |
|
| 395 |
fireTreeChanged(); |
| 396 |
} |
| 397 |
|
| 398 |
public void setNodeHeight(NodeRef n, double height) { |
| 399 |
if (!heightsKnown) { |
| 400 |
calculateNodeHeights(); |
| 401 |
} |
| 402 |
FlexibleNode node = (FlexibleNode) n; |
| 403 |
node.setHeight(height); |
| 404 |
|
| 405 |
lengthsKnown = false; |
| 406 |
|
| 407 |
fireTreeChanged(); |
| 408 |
} |
| 409 |
|
| 410 |
public void setBranchLength(NodeRef n, double length) { |
| 411 |
if (!lengthsKnown) { |
| 412 |
calculateBranchLengths(); |
| 413 |
} |
| 414 |
|
| 415 |
FlexibleNode node = (FlexibleNode) n; |
| 416 |
node.setLength(length); |
| 417 |
|
| 418 |
heightsKnown = false; |
| 419 |
|
| 420 |
fireTreeChanged(); |
| 421 |
} |
| 422 |
|
| 423 |
public void setHeightsKnown(boolean heightsKnown) { |
| 424 |
this.heightsKnown = heightsKnown; |
| 425 |
} |
| 426 |
|
| 427 |
public void setLengthsKnown(boolean lengthsKnown) { |
| 428 |
this.lengthsKnown = lengthsKnown; |
| 429 |
} |
| 430 |
|
| 431 |
public void setNodeRate(NodeRef n, double rate) { |
| 432 |
FlexibleNode node = (FlexibleNode) n; |
| 433 |
node.setRate(rate); |
| 434 |
|
| 435 |
fireTreeChanged(); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
protected void calculateNodeHeights() { |
| 442 |
|
| 443 |
if (!lengthsKnown) { |
| 444 |
throw new IllegalArgumentException("Branch lengths not known"); |
| 445 |
} |
| 446 |
|
| 447 |
nodeLengthsToHeights((FlexibleNode) getRoot(), 0.0); |
| 448 |
|
| 449 |
double maxHeight = 0.0; |
| 450 |
FlexibleNode node; |
| 451 |
for (int i = 0; i < getExternalNodeCount(); i++) { |
| 452 |
node = (FlexibleNode) getExternalNode(i); |
| 453 |
if (node.getHeight() > maxHeight) { |
| 454 |
maxHeight = node.getHeight(); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
for (int i = 0; i < getNodeCount(); i++) { |
| 459 |
node = (FlexibleNode) getNode(i); |
| 460 |
node.setHeight(maxHeight - node.getHeight()); |
| 461 |
} |
| 462 |
|
| 463 |
heightsKnown = true; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
private void nodeLengthsToHeights(FlexibleNode node, double height) { |
| 471 |
|
| 472 |
double newHeight = height; |
| 473 |
|
| 474 |
if (node.getLength() > 0.0) { |
| 475 |
newHeight += node.getLength(); |
| 476 |
} |
| 477 |
|
| 478 |
node.setHeight(newHeight); |
| 479 |
|
| 480 |
for (int i = 0; i < node.getChildCount(); i++) { |
| 481 |
nodeLengthsToHeights(node.getChild(i), newHeight); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
protected void calculateBranchLengths() { |
| 489 |
|
| 490 |
nodeHeightsToLengths((FlexibleNode) getRoot(), getRootHeight()); |
| 491 |
|
| 492 |
lengthsKnown = true; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
private void nodeHeightsToLengths(FlexibleNode node, double height) { |
| 499 |
|
| 500 |
node.setLength(height - node.getHeight()); |
| 501 |
|
| 502 |
for (int i = 0; i < node.getChildCount(); i++) { |
| 503 |
nodeHeightsToLengths(node.getChild(i), node.getHeight()); |
| 504 |
} |
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public void changeRoot(NodeRef node, double height) { |
| 512 |
FlexibleNode node1 = (FlexibleNode) node; |
| 513 |
FlexibleNode parent = node1.getParent(); |
| 514 |
|
| 515 |
double l1 = height - getNodeHeight(node); |
| 516 |
if (l1 < 0.0) { |
| 517 |
throw new IllegalArgumentException("New root height less than the node's height"); |
| 518 |
} |
| 519 |
|
| 520 |
double l2 = getNodeHeight(parent) - height; |
| 521 |
if (l2 < 0.0) { |
| 522 |
throw new IllegalArgumentException("New root height above the node's parent's height"); |
| 523 |
} |
| 524 |
|
| 525 |
changeRoot(node, l1, l2); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public void changeRoot(NodeRef node, double l1, double l2) { |
| 532 |
|
| 533 |
FlexibleNode node1 = (FlexibleNode) node; |
| 534 |
FlexibleNode parent = node1.getParent(); |
| 535 |
if (parent == null || parent == root) { |
| 536 |
|
| 537 |
return; |
| 538 |
} |
| 539 |
|
| 540 |
beginTreeEdit(); |
| 541 |
|
| 542 |
if (!lengthsKnown) { |
| 543 |
calculateBranchLengths(); |
| 544 |
} |
| 545 |
|
| 546 |
FlexibleNode parent2 = parent.getParent(); |
| 547 |
|
| 548 |
swapParentNode(parent, parent2, null); |
| 549 |
|
| 550 |
|
| 551 |
parent.removeChild(node1); |
| 552 |
root.addChild(node1); |
| 553 |
root.addChild(parent); |
| 554 |
|
| 555 |
node1.setLength(l1); |
| 556 |
parent.setLength(l2); |
| 557 |
|
| 558 |
heightsKnown = false; |
| 559 |
String t = toString(); |
| 560 |
|
| 561 |
endTreeEdit(); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
private void swapParentNode(FlexibleNode node, FlexibleNode parent, FlexibleNode child) { |
| 568 |
|
| 569 |
if (parent != null) { |
| 570 |
FlexibleNode parent2 = parent.getParent(); |
| 571 |
|
| 572 |
swapParentNode(parent, parent2, node); |
| 573 |
|
| 574 |
if (child != null) { |
| 575 |
node.removeChild(child); |
| 576 |
child.addChild(node); |
| 577 |
node.setLength(child.getLength()); |
| 578 |
} |
| 579 |
|
| 580 |
} else { |
| 581 |
|
| 582 |
node.removeChild(child); |
| 583 |
if (node.getChildCount() > 1) { |
| 584 |
throw new IllegalArgumentException("Trees must be binary"); |
| 585 |
} |
| 586 |
|
| 587 |
FlexibleNode tmp = node.getChild(0); |
| 588 |
node.removeChild(tmp); |
| 589 |
child.addChild(tmp); |
| 590 |
tmp.setLength(tmp.getLength() + child.getLength()); |
| 591 |
} |
| 592 |
|
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
public void resolveTree() { |
| 599 |
|
| 600 |
for (int i = 0; i < getInternalNodeCount(); i++) { |
| 601 |
|
| 602 |
FlexibleNode node = ((FlexibleNode) getInternalNode(i)); |
| 603 |
|
| 604 |
if (node.getChildCount() > 2) { |
| 605 |
resolveNode(node); |
| 606 |
} else if (node.getChildCount() == 1) { |
| 607 |
FlexibleNode parent = node.getParent(); |
| 608 |
if (parent != null) { |
| 609 |
|
| 610 |
FlexibleNode child = node.getChild(0); |
| 611 |
child.setParent(parent); |
| 612 |
parent.removeChild(node); |
| 613 |
parent.addChild(child); |
| 614 |
} else { |
| 615 |
|
| 616 |
root = node; |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
} |
| 621 |
|
| 622 |
adoptNodes(root, null); |
| 623 |
|
| 624 |
fireTreeChanged(); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
private void resolveNode(FlexibleNode node) { |
| 631 |
|
| 632 |
while (node.getChildCount() > 2) { |
| 633 |
FlexibleNode node0 = node.getChild(0); |
| 634 |
FlexibleNode node1 = node.getChild(1); |
| 635 |
|
| 636 |
node.removeChild(node0); |
| 637 |
node.removeChild(node1); |
| 638 |
|
| 639 |
FlexibleNode node2 = node.getShallowCopy(); |
| 640 |
node2.addChild(node0); |
| 641 |
node2.addChild(node1); |
| 642 |
node2.setLength(0.0); |
| 643 |
|
| 644 |
node.addChild(node2); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
public void setNodeAttribute(NodeRef node, String name, Object value) { |
| 656 |
((FlexibleNode) node).setAttribute(name, value); |
| 657 |
|
| 658 |
fireTreeChanged(); |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
public Object getNodeAttribute(NodeRef node, String name) { |
| 667 |
return ((FlexibleNode) node).getAttribute(name); |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
public Iterator getNodeAttributeNames(NodeRef node) { |
| 674 |
return ((FlexibleNode) node).getAttributeNames(); |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
public int getTaxonCount() { |
| 685 |
return getExternalNodeCount(); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
public Taxon getTaxon(int taxonIndex) { |
| 692 |
return ((FlexibleNode) getExternalNode(taxonIndex)).getTaxon(); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
public String getTaxonId(int taxonIndex) { |
| 700 |
Taxon taxon = getTaxon(taxonIndex); |
| 701 |
if (taxon != null) |
| 702 |
return taxon.getId(); |
| 703 |
else |
| 704 |
return ((FlexibleNode) getExternalNode(taxonIndex)).getId(); |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
public int getTaxonIndex(String id) { |
| 711 |
for (int i = 0, n = getTaxonCount(); i < n; i++) { |
| 712 |
if (getTaxonId(i).equals(id)) return i; |
| 713 |
} |
| 714 |
return -1; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
public int getTaxonIndex(Taxon taxon) { |
| 721 |
for (int i = 0, n = getTaxonCount(); i < n; i++) { |
| 722 |
if (getTaxon(i) == taxon) return i; |
| 723 |
} |
| 724 |
return -1; |
| 725 |
} |
| 726 |
|
| 727 |
public List<Taxon> asList() { |
| 728 |
List<Taxon> taxa = new ArrayList<Taxon>(); |
| 729 |
for (int i = 0, n = getTaxonCount(); i < n; i++) { |
| 730 |
taxa.add(getTaxon(i)); |
| 731 |
} |
| 732 |
return taxa; |
| 733 |
} |
| 734 |
|
| 735 |
public Iterator<Taxon> iterator() { |
| 736 |
return new Iterator<Taxon>() { |
| 737 |
private int index = -1; |
| 738 |
|
| 739 |
public boolean hasNext() { |
| 740 |
return index < getTaxonCount() - 1; |
| 741 |
} |
| 742 |
|
| 743 |
public Taxon next() { |
| 744 |
index++; |
| 745 |
return getTaxon(index); |
| 746 |
} |
| 747 |
|
| 748 |
public void remove() { } |
| 749 |
}; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
public Object getTaxonAttribute(int taxonIndex, String name) { |
| 760 |
Taxon taxon = getTaxon(taxonIndex); |
| 761 |
if (taxon != null) |
| 762 |
return taxon.getAttribute(name); |
| 763 |
else |
| 764 |
return ((FlexibleNode) getExternalNode(taxonIndex)).getAttribute(name); |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
public int addTaxon(Taxon taxon) { |
| 772 |
throw new IllegalArgumentException("Cannot add taxon to a MutableTree"); |
| 773 |
} |
| 774 |
|
| 775 |
public boolean removeTaxon(Taxon taxon) { |
| 776 |
throw new IllegalArgumentException("Cannot add taxon to a MutableTree"); |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
public void setTaxonId(int taxonIndex, String id) { |
| 784 |
Taxon taxon = getTaxon(taxonIndex); |
| 785 |
if (taxon != null) |
| 786 |
taxon.setId(id); |
| 787 |
else |
| 788 |
((FlexibleNode) getExternalNode(taxonIndex)).setId(id); |
| 789 |
|
| 790 |
fireTreeChanged(); |
| 791 |
fireTaxaChanged(); |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
public void setTaxonAttribute(int taxonIndex, String name, Object value) { |
| 803 |
Taxon taxon = getTaxon(taxonIndex); |
| 804 |
if (taxon != null) |
| 805 |
taxon.setAttribute(name, value); |
| 806 |
else |
| 807 |
((FlexibleNode) getExternalNode(taxonIndex)).setAttribute(name, value); |
| 808 |
|
| 809 |
fireTreeChanged(); |
| 810 |
fireTaxaChanged(); |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
protected String id = null; |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
public String getId() { |
| 823 |
return id; |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
public void setId(String id) { |
| 830 |
this.id = id; |
| 831 |
|
| 832 |
fireTreeChanged(); |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
private Attributable.AttributeHelper attributes = null; |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
public void setAttribute(String name, Object value) { |
| 848 |
if (attributes == null) |
| 849 |
attributes = new Attributable.AttributeHelper(); |
| 850 |
attributes.setAttribute(name, value); |
| 851 |
|
| 852 |
fireTreeChanged(); |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
public Object getAttribute(String name) { |
| 860 |
if (attributes == null) |
| 861 |
return null; |
| 862 |
else |
| 863 |
return attributes.getAttribute(name); |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
public Iterator<String> getAttributeNames() { |
| 870 |
if (attributes == null) |
| 871 |
return null; |
| 872 |
else |
| 873 |
return attributes.getAttributeNames(); |
| 874 |
} |
| 875 |
|
| 876 |
public void addMutableTreeListener(MutableTreeListener listener) { |
| 877 |
mutableTreeListeners.add(listener); |
| 878 |
} |
| 879 |
|
| 880 |
private void fireTreeChanged() { |
| 881 |
for (MutableTreeListener mutableTreeListener : mutableTreeListeners) { |
| 882 |
(mutableTreeListener).treeChanged(this); |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
private final ArrayList<MutableTreeListener> mutableTreeListeners = new ArrayList<MutableTreeListener>(); |
| 887 |
|
| 888 |
public void addMutableTaxonListListener(MutableTaxonListListener listener) { |
| 889 |
mutableTaxonListListeners.add(listener); |
| 890 |
} |
| 891 |
|
| 892 |
private void fireTaxaChanged() { |
| 893 |
for (MutableTaxonListListener mutableTaxonListListener : mutableTaxonListListeners) { |
| 894 |
(mutableTaxonListListener).taxaChanged(this); |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
private final ArrayList<MutableTaxonListListener> mutableTaxonListListeners = new ArrayList<MutableTaxonListListener>(); |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
public String toString() { |
| 904 |
return TreeUtils.newick(this); |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
public boolean equals(Object obj) { |
| 911 |
if (obj == null) { |
| 912 |
return false; |
| 913 |
} |
| 914 |
|
| 915 |
if (!(obj instanceof Tree)) { |
| 916 |
throw new IllegalArgumentException("FlexibleTree.equals can only compare instances of Tree"); |
| 917 |
} |
| 918 |
return TreeUtils.equal(this, (Tree) obj); |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
FlexibleNode root; |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
FlexibleNode[] nodes = null; |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
int nodeCount; |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
int externalNodeCount; |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
int internalNodeCount; |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
private Type units = Type.SUBSTITUTIONS; |
| 954 |
|
| 955 |
boolean inEdit = false; |
| 956 |
|
| 957 |
boolean heightsKnown = false; |
| 958 |
boolean lengthsKnown = false; |
| 959 |
} |