/** * Lazy and one time computation of the level of this node. * * @return int level */ public int getLevel() { if (level == -1) { TreeNode parent = this; level = 0; while ((parent = parent.getParent()) != null) level++; } return level; }
/** * Compute a list of ALL children of a {@link TreeNode}. * ALL means all nodes, that are adjacent to this node. That are all direct children * and the parent (if there is one). * <p> * Runtime: O(d) */ public ArrayList<TreeNode> getAllChildren() { ArrayList<TreeNode> children = new ArrayList<TreeNode>(); //add all children for (TreeNode child : this.children()) { children.add(child); } /**Alternative: children.addAll(node.getChildren()); => but needs maybe O(d^2)*/ //add the parent if (this.getParent() != null) { children.add(this.getParent()); } return children; }
/** * @return siblings all siblings of this node */ public List<TreeNode> getSiblings() { TreeNode p = getParent(); if (p == null) return null; List<TreeNode> siblings = new ArrayList<TreeNode>(); for (TreeNode child : p.children()) { if (child != this) siblings.add(child); } return siblings; }
/** * Indicates, if this node is a child of the given node. * * @param node: the node, that may be parent of this node * @return true, if this node is a child of the given node, false otherwise */ public boolean isChildOf(TreeNode node) { if (this.getParent().equalsNode(node)) { return true; } else { return false; } }
/** * Returns true if the given node is sibling of this node. * * @param node the other node * @return true if the other node is sibling of this node */ public boolean isNodeSibling(TreeNode node) { TreeNode p = getParent(); if (p == null) return false; for (Edge<TreeNode> oe : p.edges()) { if (oe.getOpposit(p) == this) return true; } return false; }
/** * Returns the root node of this tree. If the root was not set explicitly, this * searches the tree and returns the first node with in-degree equal to 0. * <p> * The search is only done once and the found root node is cached, so make sure to call * that after the tree is complete (all nodes and edges inserted) or call {@link #setRoot(TreeNode)} * with null to reset the root node cache. * * @return root node of the tree */ public TreeNode getRoot() { if (root == null) { // find root, do a search for the node // with inDegree 0 for (TreeNode v : vertices()) { if (v.getParent() == null) { setRoot(v); } } } return (TreeNode) root; }
public DistanceMatrix(Tree tree) { TreeNode[] leafes = tree.getLeaves(); matrix = new double[leafes.length][leafes.length]; for (int i = 0; i < leafes.length; i++) { leafToIndex.put(leafes[i].getLabel(), i); for (int j = 0; j < leafes.length; j++) { if (i != j) { TreeNode lca = tree.findLeastCommonAncestor(leafes[i], leafes[j]); //BigDecimal weight = new BigDecimal(leafes[i].getDistanceToParent()); double weight = leafes[i].getDistanceToParent(); TreeNode parent = leafes[i].getParent(); while (parent != lca) { //weight = weight.add(new BigDecimal(parent.getDistanceToParent())); weight = weight + parent.getDistanceToParent(); parent = parent.getParent(); } //weight = weight.add(new BigDecimal(leafes[j].getDistanceToParent())); weight = weight + leafes[j].getDistanceToParent(); parent = leafes[j].getParent(); while (parent != lca) { //weight = weight.add(new BigDecimal(parent.getDistanceToParent())); weight = weight + parent.getDistanceToParent(); parent = parent.getParent(); } matrix[i][j] = weight;//.doubleValue();//weight.multiply(new BigDecimal(ACCURACY)).doubleValue(); matrix[j][i] = matrix[i][j]; } } } }
/** * This method returns the last common ancestor of two nodes of a tree. * <p> * This eventually returns null if one of the given nodes is null or the given nodes * are not in this tree. * * @param nodeA * @param nodeB * @return the LCA of nodeA and nodeB or null if no LCA exists */ public TreeNode findLeastCommonAncestor(TreeNode nodeA, TreeNode nodeB) { if (nodeA == null || nodeB == null) return null; if (nodeA.getGraph() != this || nodeB.getGraph() != this) return null; /* set nodeA and nodeB to equal level */ if (nodeA.getLevel() < nodeB.getLevel()) { while (nodeA.getLevel() != nodeB.getLevel()) { nodeB = (TreeNode) nodeB.getParent(); } } else { while (nodeA.getLevel() != nodeB.getLevel()) { nodeA = (TreeNode) nodeA.getParent(); } } /* set both nodes to their parent, until they are equal */ while (nodeA != nodeB) { nodeA = (TreeNode) nodeA.getParent(); nodeB = (TreeNode) nodeB.getParent(); } return (nodeA); }
/** * @return previous sibling of this node */ public TreeNode getPreviousSibling() { TreeNode p = getParent(); if (p == null) return null; TreeNode prev = null; for (Edge<TreeNode> oe : p.edges()) { if (oe.getOpposit(p) == this) { return prev; } prev = oe.getOpposit(p); } return null; }
/** * @return next sibling of this node */ public TreeNode getNextSibling() { TreeNode p = getParent(); if (p == null) return null; boolean returnNext = false; for (Edge<TreeNode> oe : p.edges()) { if (returnNext) return oe.getOpposit(p); if (oe.getOpposit(p) == this) { returnNext = true; } } return null; }
/** * remove the taxon with the given label and prune the tree * * @param merge merge label * @param taxa the taxa * @param trees pruned trees */ protected void removeAndPrune(String merge, List<String> taxa, List<Tree> trees) { for (Tree tree : trees) { for (TreeNode node : tree.vertices()) { if (node.getLabel() != null && taxa.contains(node.getLabel()) && !node.getLabel().equals(merge)) { // remove TreeNode parent = node.getParent(); tree.removeVertex(node); } } TreeUtils.pruneDegreeOneNodes(tree, sumEdgeWeightsWhenPruning, USE_PARENT_WEIGHT); } }
TreeNode p = node.getParent(); if (!p.equals(tree.getRoot())) { Bipartition parentPartition = lcaToLabels.get(p);
/** * Computes the number of the given child for this node. * This number is 0 {@literal <=} childNum {@literal <} 'number of children', if the child is a real child of this node. * It is childNum = 'number of children', if the given child is parent of this node. * * @param child: the child, for that its number has to be computed * @return the child number for the given node */ public int getChildNumber(TreeNode child) { if (this.getParent().equalsNode(child)) { return childCount(); } else if (child.isChildOf(this)) { int num = 0; for (TreeNode nodesChild : this.children()) { if (!nodesChild.equalsNode(child)) { num++; } else { break; } } return num; } else { throw new RuntimeException("Given child is no child of this node!"); } }
private static void connectParent(Tree tree, TreeNode newRoot, TreeNode parent, double weight) { TreeNode next = parent.getParent(); double nextWeight = 1; if (next != null) { nextWeight = tree.getEdge(next, parent).getWeight(); tree.removeEdge(next, parent); } tree.addEdge(newRoot, parent).setWeight(weight); if (next != null) { connectParent(tree, parent, next, nextWeight); } }
private static void stepUp(Tree tree, TreeNode child, TreeNode parent, String oldChildLabel) { TreeNode newParent = parent.getParent(); String newOldLabel = parent.getLabel(); double weight = child.getDistanceToParent(); tree.removeEdge(parent, child); tree.addEdge(child, parent).setWeight(weight); parent.setLabel(oldChildLabel); if (newParent != null) { stepUp(tree, parent, newParent, newOldLabel); } }
public static boolean moveFakeRoot(Tree tree, TreeNode newRoot) { if (tree.containsVertex(newRoot) && newRoot.isInnerNode()) { TreeNode oldRoot = tree.getRoot(); TreeNode parent = newRoot.getParent(); if (parent != null) { String label = newRoot.getLabel(); newRoot.setLabel(null); stepUp(tree, newRoot, parent, label); tree.setRoot(newRoot); }// if not node is already root } return false; }
public Edge getDistanceEdge(TreeNode source, TreeNode target) { int intersection = 0; double s = 0; for (TreeNode treeNodeSource : source.getPartition() .getLeavesArray()) { for (TreeNode treeNodeTarget : target.getPartition() .getLeavesArray()) { if (treeNodeSource.getLabel().equals( treeNodeTarget.getLabel())) { // if (treeNodeSource.equals(treeNodeTarget)){ intersection++; } } } // //System.out.println("getDistance Methode likelihoods: intersection --> "+intersection); TreeNode getRootHelp = source; while (getRootHelp.getParent() != null) { getRootHelp = getRootHelp.getParent(); } int allLeaves = getRootHelp.getLeaves().length; s = BCNWithLikelihoods.computeValueWithDouble(allLeaves, source .getPartition().getSize(), target.getPartition().getSize(), intersection); return new Edge(source, target, s); }
private void hangIn(TreeNode n, HashMap<String, MultiNode> labeledNodes, HashMap<TreeNode, MultiNode> all, EdgeType type) { MultiNode m = null; if (n.getLabel() != null) { m = labeledNodes.get(n.getLabel()); } if (m == null) { m = new MultiNode(); all.put(n, m); if (n.getLabel() != null) { m.setLabel(n.getLabel()); labeledNodes.put(n.getLabel(), m); } addVertex(m); } if (n.getEdgeToParent() != null) { MultiNode p = all.get(n.getParent()); addEdge(p, m, type).setWeight(n.getEdgeToParent().getWeight()); } for (TreeNode c : n.children()) { hangIn(c, labeledNodes, all, type); } }
/** * Reroot tree with the new root placed at the incomming edge of the given outgroup * * @param tree the tree * @param outgroup the outgroup taxon */ public static void rerootToOutgroup(Tree tree, TreeNode outgroup, double edgeLengthToOutgroup) { TreeNode parent = outgroup.getParent(); if (parent == null) return; TreeNode newRoot = new TreeNode(); // add outgroup to new root tree.addVertex(newRoot); double oldWeight = tree.getEdge(parent, outgroup).getWeight(); tree.removeEdge(parent, outgroup); final Edge<TreeNode> edge = tree.addEdge(newRoot, outgroup); edge.setWeight(edgeLengthToOutgroup); // connect parent connectParent(tree, newRoot, parent, oldWeight - edgeLengthToOutgroup); tree.setRoot(newRoot); pruneDegreeOneNodes(tree); }
TreeNode parent = node.getParent(); double weight_parent = node.getDistanceToParent(); double weight_child = child.getDistanceToParent();