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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 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 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
|
/*
* File Node.java
*
* Copyright (C) 2010 Remco Bouckaert remco@cs.auckland.ac.nz
*
* This file is part of BEAST2.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership and licensing.
*
* BEAST is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* BEAST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BEAST; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
package beast.base.evolution.tree;
import java.util.*;
import beast.base.core.BEASTObject;
import beast.base.core.Description;
import beast.base.util.HeapSort;
@Description("Nodes in building beast.tree data structure.")
public class Node extends BEASTObject {
/**
* label nr of node, used mostly when this is a leaf.
*/
protected int labelNr;
/**
* height of this node.
*/
protected double height = Double.MAX_VALUE;
/**
* Arbitrarily labeled metadata on this node. Not currently implemented as part of state!
*/
protected Map<String, Object> metaData = new TreeMap<>();
/**
* Length metadata for the edge above this node. Not currently implemented as part of state!
*/
protected Map<String, Object> lengthMetaData = new TreeMap<>();
/**
* list of children of this node *
* Don't use m_left and m_right directly
* Use getChildCount() and getChild(x) or getChildren() instead
*/
protected List<Node> children = new ArrayList<>();
/**
* parent node in the beast.tree, null if root *
*/
protected Node parent = null;
/**
* status of this node after an operation is performed on the state *
*/
protected int isDirty = Tree.IS_CLEAN;
/**
* meta-data contained in square brackets in Newick *
*/
public String metaDataString, lengthMetaDataString;
/**
* The Tree that this node is a part of.
* This allows e.g. access to the State containing the Tree *
*/
protected Tree m_tree;
public Node() {
}
public Node(final String id) {
setID(id);
initAndValidate();
}
public void setTree(Tree tree) {
m_tree = tree;
}
public Tree getTree() {
return m_tree;
}
@Override
public void initAndValidate() {
// do nothing
}
/**
* @return number uniquely identifying the node in the tree.
* This is a number between 0 and the total number of nodes in the tree
* Leaf nodes are number 0 to #leaf nodes -1
* Internal nodes are numbered #leaf nodes up to #nodes-1
* The root node is always numbered #nodes-1
*/
public int getNr() {
return labelNr;
}
public void setNr(final int labelIndex) {
labelNr = labelIndex;
}
public double getHeight() {
return height;
}
public double getDate() {
return m_tree.getDate(height);
}
public void setHeight(final double height) {
startEditing();
this.height = height;
isDirty |= Tree.IS_DIRTY;
if (!isLeaf()) {
getLeft().isDirty |= Tree.IS_DIRTY;
if (getRight() != null) {
getRight().isDirty |= Tree.IS_DIRTY;
}
}
}
/**
* Set the node height in operators for data augmentation likelihood.
* It only changes this node to be dirty, not any of child nodes.
* @param height new node height
*/
public void setHeightDA(final double height) {
startEditing();
this.height = height;
isDirty |= Tree.IS_DIRTY;
}
/**
* @return length of branch between this node and its parent in the beast.tree
*/
public final double getLength() {
if (isRoot()) {
return 0;
} else {
return getParent().getHeight() - getHeight();
}
}
/**
* methods for accessing the dirtiness state of the Node.
* A Node is Tree.IS_DIRTY if its value (like height) has changed
* A Node Tree.IS_if FILTHY if its parent or child has changed.
* Otherwise the node is Tree.IS_CLEAN *
*/
public int isDirty() {
return isDirty;
}
public void makeDirty(final int dirty) {
isDirty |= dirty;
}
public void makeAllDirty(final int dirty) {
isDirty = dirty;
if (!isLeaf()) {
getLeft().makeAllDirty(dirty);
if (getRight() != null) {
getRight().makeAllDirty(dirty);
}
}
}
/**
* @return parent node, or null if this is root *
*/
public Node getParent() {
return parent;
}
/**
* Calls setParent(parent, true)
*
* @param parent the new parent to be set, must be called from within an operator.
*/
public void setParent(final Node parent) {
setParent(parent, true);
}
/**
* Sets the parent of this node
*
* @param parent the node to become parent
* @param inOperator if true, then startEditing() is called and setting the parent will make tree "filthy"
*/
public void setParent(final Node parent, final boolean inOperator) {
if (inOperator) startEditing();
if (this.parent != parent) {
this.parent = parent;
if (inOperator) isDirty = Tree.IS_FILTHY;
}
}
/**
* Sets the parent of this node. No overhead, no side effects like setting dirty flags etc.
*
* @param parent the node to become parent
*/
void setParentImmediate(final Node parent) {
this.parent = parent;
}
/**
* @return unmodifiable list of children of this node
*/
public List<Node> getChildren() {
return Collections.unmodifiableList(children);
}
/**
* @return modifiable list of children of this node
*/
public List<Node> getChildrenMutable() {
return children;
}
/**
* get all child node under this node, if this node is leaf then list.size() = 0.
* This returns all child nodes including this node.
*
* @return all child nodes including this node
* @deprecated issue 703: name is confusing, replaced by
* {@link #getAllChildNodesAndSelf() getAllChildNodesAndSelf}
*/
@Deprecated
public List<Node> getAllChildNodes() {
return getAllChildNodesAndSelf();
}
// recursive
/**
* @deprecated issue 703: name is confusing, replaced by
* {@link #getAllChildNodesAndSelf(List<Node>) getAllChildNodesAndSelf}
*/
@Deprecated
public void getAllChildNodes(final List<Node> childNodes) {
getAllChildNodesAndSelf(childNodes);
}
/**
* get all child node under this node, if this node is leaf then list.size() = 0.
* This returns all child nodes including this node.
*
* @return all child nodes including this node
*/
public List<Node> getAllChildNodesAndSelf() {
final List<Node> childNodes = new ArrayList<>();
if (!this.isLeaf()) getAllChildNodesAndSelf(childNodes);
return childNodes;
}
// recursive
public void getAllChildNodesAndSelf(final List<Node> childNodes) {
childNodes.add(this);
for (Node child : children)
child.getAllChildNodesAndSelf(childNodes);
}
/**
* get all leaf node under this node, if this node is leaf then list.size() = 0.
*
* @return
*/
public List<Node> getAllLeafNodes() {
final List<Node> leafNodes = new ArrayList<>();
if (!this.isLeaf()) getAllLeafNodes(leafNodes);
return leafNodes;
}
// recursive
public void getAllLeafNodes(final List<Node> leafNodes) {
if (this.isLeaf()) {
leafNodes.add(this);
}
for (Node child : children)
child.getAllLeafNodes(leafNodes);
}
/**
* @return true if current node is root node *
*/
public boolean isRoot() {
return parent == null;
}
/**
* @return true if current node is a leaf node *
*/
public boolean isLeaf() {
return children.size() == 0;
//return getLeft() == null && getRight() == null;
}
public void removeChild(final Node child) {
startEditing();
children.remove(child);
}
/**
* Removes all children from this node.
*
* @param inOperator if true then startEditing() is called. For operator uses, called removeAllChildren(true), otherwise
* use set to false.
*/
public void removeAllChildren(final boolean inOperator) {
if (inOperator) startEditing();
children.clear();
}
public void addChild(final Node child) {
child.setParent(this);
children.add(child);
}
/**
* @return count number of nodes in beast.tree, starting with current node *
*/
public int getNodeCount() {
int nodes = 1;
for (final Node child : children) {
nodes += child.getNodeCount();
}
return nodes;
}
public int getLeafNodeCount() {
if (isLeaf()) {
return 1;
}
int nodes = 0;
for (final Node child : children) {
nodes += child.getLeafNodeCount();
}
return nodes;
}
public int getInternalNodeCount() {
if (isLeaf()) {
return 0;
}
int nodes = 1;
for (final Node child : children) {
nodes += child.getInternalNodeCount();
}
return nodes;
}
/**
* @return beast.tree in Newick format, with length and meta data
* information. Unlike toNewick(), here Nodes are numbered, instead of
* using the node labels.
* If there are internal nodes with non-null IDs then their numbers are also printed.
* Also, all internal nodes are labelled if printInternalNodeNumbers
* is set true. This is useful for example when storing a State to file
* so that it can be restored.
*/
public String toShortNewick(final boolean printInternalNodeNumbers) {
final StringBuilder buf = new StringBuilder();
if (!isLeaf()) {
buf.append("(");
boolean isFirst = true;
for (Node child : getChildren()) {
if (isFirst)
isFirst = false;
else
buf.append(",");
buf.append(child.toShortNewick(printInternalNodeNumbers));
}
buf.append(")");
}
if (isLeaf() || getID() != null || printInternalNodeNumbers) {
buf.append(getNr());
}
buf.append(getNewickMetaData());
buf.append(":").append(getNewickLengthMetaData()).append(getLength());
return buf.toString();
}
/**
* prints newick string where it orders by highest leaf number
* in a clade. Print node numbers (m_iLabel) incremented by 1
* for leaves and internal nodes with non-null IDs.
*/
String toSortedNewick(final int[] maxNodeInClade) {
return toSortedNewick(maxNodeInClade, false);
}
public String toSortedNewick(int[] maxNodeInClade, boolean printMetaData) {
StringBuilder buf = new StringBuilder();
if (!isLeaf()) {
if (getChildCount() <= 2) {
// Computationally cheap method for special case of <=2 children
buf.append("(");
String child1 = getChild(0).toSortedNewick(maxNodeInClade, printMetaData);
int child1Index = maxNodeInClade[0];
if (getChildCount() > 1) {
String child2 = getChild(1).toSortedNewick(maxNodeInClade, printMetaData);
int child2Index = maxNodeInClade[0];
if (child1Index > child2Index) {
buf.append(child2);
buf.append(",");
buf.append(child1);
} else {
buf.append(child1);
buf.append(",");
buf.append(child2);
maxNodeInClade[0] = child1Index;
}
} else {
buf.append(child1);
}
buf.append(")");
if (getID() != null) {
buf.append(labelNr+1);
}
} else {
// General method for >2 children
String[] childStrings = new String[getChildCount()];
int[] maxNodeNrs = new int[getChildCount()];
Integer[] indices = new Integer[getChildCount()];
for (int i = 0; i < getChildCount(); i++) {
childStrings[i] = getChild(i).toSortedNewick(maxNodeInClade, printMetaData);
maxNodeNrs[i] = maxNodeInClade[0];
indices[i] = i;
}
Arrays.sort(indices, (i1, i2) -> {
if (maxNodeNrs[i1] < maxNodeNrs[i2])
return -1;
if (maxNodeNrs[i1] > maxNodeNrs[i2])
return 1;
return 0;
});
maxNodeInClade[0] = maxNodeNrs[maxNodeNrs.length - 1];
buf.append("(");
for (int i = 0; i < indices.length; i++) {
if (i > 0)
buf.append(",");
buf.append(childStrings[indices[i]]);
}
buf.append(")");
if (getID() != null) {
buf.append(labelNr + 1);
}
}
} else {
maxNodeInClade[0] = labelNr;
buf.append(labelNr + 1);
}
if (printMetaData) {
buf.append(getNewickMetaData());
}
buf.append(":");
if (printMetaData)
buf.append(getNewickLengthMetaData());
buf.append(getLength());
return buf.toString();
}
@Deprecated
public String toNewick(final List<String> labels) {
throw new UnsupportedOperationException("Please use toNewick(). Labels will come from node.getId() or node.getNr().");
}
/**
*
* @param onlyTopology if true, only print topology
* @return
*/
public String toNewick(boolean onlyTopology) {
final StringBuilder buf = new StringBuilder();
if (!isLeaf()) {
buf.append("(");
boolean isFirst = true;
for (Node child : getChildren()) {
if (isFirst)
isFirst = false;
else
buf.append(",");
buf.append(child.toNewick(onlyTopology));
}
buf.append(")");
if (getID() != null)
buf.append(getID());
} else {
if (getID() != null)
buf.append(getID());
else
buf.append(labelNr);
}
if (!onlyTopology) {
buf.append(getNewickMetaData());
buf.append(":").append(getNewickLengthMetaData()).append(getLength());
}
return buf.toString();
}
/**
* @return beast.tree in Newick format with taxon labels for labelled tip nodes
* and labeled (having non-null ID) internal nodes.
* If a tip node doesn't have an ID (taxon label) then node number (m_iLabel) is printed.
*/
public String toNewick() {
return toNewick(false);
}
public String getNewickMetaData() {
if (metaDataString != null)
return "[&" + metaDataString + ']';
else
return "";
}
public String getNewickLengthMetaData() {
if (lengthMetaDataString != null)
return "[&" + lengthMetaDataString + "]";
else
return "";
}
/**
* @param labels
* @return beast.tree in long Newick format, with all length and meta data
* information, but with leafs labelled with their names
*/
public String toString(final List<String> labels) {
final StringBuilder buf = new StringBuilder();
if (isLeaf()) {
buf.append(labels.get(labelNr));
} else {
buf.append("(");
boolean isFirst = true;
for (Node child : getChildren()) {
if (isFirst)
isFirst = false;
else
buf.append(",");
buf.append(child.toString(labels));
}
buf.append(")");
}
if (isLeaf())
buf.append(labels.get(labelNr));
if (metaDataString != null) {
buf.append('[');
buf.append(metaDataString);
buf.append(']');
}
buf.append(":");
if (lengthMetaDataString != null) {
buf.append('[');
buf.append(lengthMetaDataString);
buf.append(']');
}
buf.append(getLength());
return buf.toString();
}
@Override
public String toString() {
return toShortNewick(true);
}
/**
* sorts nodes in children according to lowest numbered label in subtree
*
* @return
*/
public int sort() {
if (isLeaf()) {
return labelNr;
}
final int childCount = getChildCount();
if (childCount == 1) return getChild(0).sort();
final List<Integer> lowest = new ArrayList<>();
final int[] indices = new int[childCount];
// relies on this being a copy of children list
final List<Node> children = new ArrayList<>(getChildren());
for (final Node child : children) {
lowest.add(child.sort());
}
HeapSort.sort(lowest, indices);
for (int i = 0; i < childCount; i++) {
setChild(i, children.get(indices[i]));
}
return lowest.get(indices[0]);
} // sort
/**
* during parsing, leaf nodes are numbered 0...m_nNrOfLabels-1
* but internal nodes are left to zero. After labeling internal
* nodes, m_iLabel uniquely identifies a node in a beast.tree.
*
* @param labelIndex
* @return
*/
public int labelInternalNodes(int labelIndex) {
if (isLeaf()) {
return labelIndex;
} else {
labelIndex = getLeft().labelInternalNodes(labelIndex);
if (getRight() != null) {
labelIndex = getRight().labelInternalNodes(labelIndex);
}
labelNr = labelIndex++;
}
return labelIndex;
} // labelInternalNodes
/**
* @return (deep) copy of node
*/
public Node copy() {
final Node node = new Node();
node.height = height;
node.labelNr = labelNr;
node.metaDataString = metaDataString;
node.lengthMetaDataString = lengthMetaDataString;
node.metaData = new TreeMap<>(metaData);
node.lengthMetaData = new TreeMap<>(lengthMetaData);
node.parent = null;
node.setID(getID());
for (final Node child : getChildren()) {
node.addChild(child.copy());
}
return node;
} // copy
/**
* assign values to a tree in array representation *
*/
public void assignTo(final Node[] nodes) {
final Node node = nodes[getNr()];
node.height = height;
node.labelNr = labelNr;
node.metaDataString = metaDataString;
node.lengthMetaDataString = lengthMetaDataString;
node.metaData = new TreeMap<>(metaData);
node.lengthMetaData = new TreeMap<>(lengthMetaData);
node.parent = null;
node.setID(getID());
if (getLeft() != null) {
node.setLeft(nodes[getLeft().getNr()]);
getLeft().assignTo(nodes);
node.getLeft().parent = node;
if (getRight() != null) {
node.setRight(nodes[getRight().getNr()]);
getRight().assignTo(nodes);
node.getRight().parent = node;
}
}
}
/**
* assign values from a tree in array representation *
*/
public void assignFrom(final Node[] nodes, final Node node) {
height = node.height;
labelNr = node.labelNr;
metaDataString = node.metaDataString;
lengthMetaDataString = node.lengthMetaDataString;
metaData = new TreeMap<>(node.metaData);
lengthMetaData = new TreeMap<>(node.lengthMetaData);
parent = null;
setID(node.getID());
if (node.getLeft() != null) {
setLeft(nodes[node.getLeft().getNr()]);
getLeft().assignFrom(nodes, node.getLeft());
getLeft().parent = this;
if (node.getRight() != null) {
setRight(nodes[node.getRight().getNr()]);
getRight().assignFrom(nodes, node.getRight());
getRight().parent = this;
}
}
}
/**
* set meta-data according to pattern.
* Only heights are recognised, but derived classes could deal with
* richer meta data patterns.
*/
public void setMetaData(final String pattern, final Object value) {
startEditing();
if (pattern.equals(TraitSet.DATE_TRAIT) ||
pattern.equals(TraitSet.AGE_TRAIT) ||
pattern.equals(TraitSet.DATE_FORWARD_TRAIT) ||
pattern.equals(TraitSet.DATE_BACKWARD_TRAIT)) {
height = (Double) value;
isDirty |= Tree.IS_DIRTY;
} else {
metaData.put(pattern, value);
}
}
/**
* Removes metadata from the node for the given key.
*/
public void removeMetaData(final String key) {
metaData.remove(key);
}
/**
* Add edge length metadata with given key and value.
*
* @param key key for metadata
* @param value value of metadata for this edge length
*/
public void setLengthMetaData(String key, Object value) {
startEditing();
lengthMetaData.put(key, value);
}
/**
* Retrieve metadata with key matching pattern. If pattern
* happens to match either "date", "date-forward" or "date-backward",
* return the node age instead.
*
* @param pattern key to retrieve.
* @return metadata object or null of key not found.
*/
public Object getMetaData(final String pattern) {
if (pattern.equals(TraitSet.DATE_TRAIT) ||
pattern.equals(TraitSet.AGE_TRAIT) ||
pattern.equals(TraitSet.DATE_FORWARD_TRAIT) ||
pattern.equals(TraitSet.DATE_BACKWARD_TRAIT)) {
return height;
} else
return metaData.get(pattern);
}
public Object getLengthMetaData(String key) {
return lengthMetaData.get(key);
}
public Set<String> getMetaDataNames() {
return metaData.keySet();
}
public Set<String> getLengthMetaDataNames() {
return lengthMetaData.keySet();
}
/**
* scale height of this node and all its descendants
*
* @param scale scale factor
* @return degrees of freedom scaled (used for HR calculations)
*/
public int scale(final double scale) {
startEditing();
int dof = 0;
isDirty |= Tree.IS_DIRTY;
if (!isLeaf() && !isFake()) {
height *= scale;
if (isRoot() || parent.getHeight() != getHeight())
dof += 1;
}
if (!isLeaf()) {
dof += getLeft().scale(scale);
if (getRight() != null) {
dof += getRight().scale(scale);
}
if (height < getLeft().height || height < getRight().height) {
throw new IllegalArgumentException("Scale gives negative branch length");
}
}
return dof;
}
// /**
// * Used for sampled ancestor trees
// * Scales this node and all its descendants (either all descendants, or only non-sampled descendants)
// *
// * @param scale the scalar to multiply each scaled node age by
// * @param scaleSNodes true if sampled nodes should be scaled as well as internal nodes, false if only non-sampled
// * internal nodes should be scaled.
// */
// public void scale(double scale, boolean scaleSNodes) {
// startEditing();
// isDirty |= Tree.IS_DIRTY;
// if (scaleSNodes || (!isLeaf() && !isFake())) {
// height *= scale;
// }
// if (!isLeaf()) {
// (getLeft()).scale(scale, scaleSNodes);
// if (getRight() != null) {
// (getRight()).scale(scale, scaleSNodes);
// }
// if (height < getLeft().height || height < getRight().height) {
// throw new IllegalArgumentException("Scale gives negative branch length");
// }
// }
// }
protected void startEditing() {
if (m_tree != null && m_tree.getState() != null) {
m_tree.startEditing(null);
}
}
/**
* @return the number of children of this node.
*/
public int getChildCount() {
return children.size();
}
/**
* This method returns the i'th child, numbering starting from 0.
* getChild(0) returns the same node as getLeft()
* getChild(1) returns the same node as getRight()
*
* This method is unprotected and will throw an ArrayOutOfBoundsException if provided an index larger than getChildCount() - 1, or smaller than 0.
*
* @return the i'th child of this node.
*/
public Node getChild(final int childIndex) {
return children.get(childIndex);
}
/**
* This sets the i'th child of this node. Will pad out the children with null's if getChildCount() <= childIndex.
*/
public void setChild(final int childIndex, final Node node) {
while (children.size() <= childIndex) {
children.add(null);
}
children.set(childIndex, node);
}
/**
* This sets the zero'th (left in binary trees) child of this node.
*
* @param leftChild new left child
* trees should not be assumed to be binary. One child and more than two are both valid in some models.
*/
public void setLeft(final Node leftChild) {
if (children.size() == 0) {
children.add(leftChild);
} else {
children.set(0, leftChild);
}
}
/**
* This method returns the zero'th child (called left child in binary trees).
* Will return null if there are no children.
*
* @return left child (zero'th child), or null if this node has no children.
* trees should not be assumed to be binary. One child and more than two are both valid in some models.
*/
public Node getLeft() {
if (children.size() == 0) {
return null;
}
return children.get(0);
}
/**
* This sets the second child (index 1, called right child in binary trees). If this node had no children then the left
* child will be set to null after this call.
*
* @param rightChild new right child
* trees should not be assumed to be binary. One child and more than two are both valid in some models.
*/
public void setRight(final Node rightChild) {
switch (children.size()) {
case 0:
children.add(null);
case 1:
children.add(rightChild);
break;
default:
children.set(1, rightChild);
break;
}
}
/**
* This method returns the second child (index 1, called right child in binary trees).
* Will return null if there are no children or only one child.
*
* @return right child (child 1), or null if this node has no children, or only one child.
* trees should not be assumed to be binary. One child and more than two are both valid in some models.
*/
public Node getRight() {
if (children.size() <= 1) {
return null;
}
return children.get(1);
}
public static Node connect(final Node left, final Node right, final double h) {
final Node n = new Node();
n.setHeight(h);
n.setLeft(left);
n.setRight(right);
left.parent = n;
right.parent = n;
return n;
}
/**
* @return true if this leaf actually represents a direct ancestor
* (i.e. is on the end of a zero-length branch)
*/
public boolean isDirectAncestor() {
return (isLeaf() && !isRoot() && this.getParent().getHeight() == this.getHeight());
}
/**
* @return true if this is a "fake" internal node (i.e. one of its children is a direct ancestor)
*/
public boolean isFake() {
if (this.isLeaf())
return false;
return ((this.getLeft()).isDirectAncestor() || (this.getRight() != null && (this.getRight()).isDirectAncestor()));
}
/**
* Retrieve the (technically leaf) node whose ID matches the ID of the taxon
* associated with this sampled ancestor.
*
* @return node corresponding to the sampled ancestor
*/
public Node getDirectAncestorChild() {
if (!this.isFake()) {
return null;
}
if (this.getLeft().isDirectAncestor()) {
return this.getLeft();
}
return this.getRight();
}
/**
* Retrieve the true child node of this sampled ancestor.
*
* @return true child node
*/
public Node getNonDirectAncestorChild(){
if (!this.isFake()) {
return null;
}
if ((this.getLeft()).isDirectAncestor()){
return getRight();
}
if ((this.getRight()).isDirectAncestor()){
return getLeft();
}
return null;
}
public Node getFakeChild(){
if ((this.getLeft()).isFake()){
return getLeft();
}
if ((this.getRight()).isFake()){
return getRight();
}
return null;
}
} // class Node
|