/** * 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 static Tree deleteRootNode(Tree sourceTree, boolean clone) { if (clone) sourceTree = sourceTree.cloneTree(); TreeNode r = sourceTree.getRoot(); if (r.childCount() == 2) { List<TreeNode> children = r.getChildren(); sourceTree.removeVertex(r); TreeNode c1 = children.get(0); TreeNode c2 = children.get(1); if (c1.isInnerNode()) { sourceTree.addEdge(c1, c2); sourceTree.setRoot(c1); } else if (c2.isInnerNode()) { sourceTree.addEdge(c2, c1); sourceTree.setRoot(c2); } else { System.out.println("Could not unroot tree. Tree seems not to be a tree!"); return null; } } return sourceTree; }
public Tree buildTree() { TIntObjectMap<TreeNode> nodeMap = new TIntObjectHashMap<>(); Tree tree = new Tree(); Iterator<Edge> it = supertreeEdges.iterator(); while (it.hasNext()) { Edge edge = it.next(); nodeMap.put(edge.treeNode, new TreeNode(edge.treeNodeLabel)); tree.addVertex(nodeMap.get(edge.treeNode)); if (edge.parentNode == 0) { tree.setRoot(nodeMap.get(edge.treeNode)); it.remove(); } } for (Edge edge : supertreeEdges) { tree.addEdge(nodeMap.get(edge.parentNode), nodeMap.get(edge.treeNode)); } tree.setName(String.valueOf(currentscore)); return tree; }
t.addVertex(nuLcaParent); t.addEdge(nuLcaParent, lca); t.setRoot(nuLcaParent); lca = nuLcaParent;
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; }
tree.addVertex(newRoot); tree.addEdge(newRoot, tree.getRoot()); tree.setRoot(newRoot); parent = newRoot; } else {
/** * Create a new {@link Tree} that represents the subtree of this tree rooted at the given * node. The returned tree is a new tree that contains clones of the {@link TreeNode}s of this tree. * The node copies are created using {@link TreeNode#cloneNode()} and are * new instances, so you will not have object equality ( n != n.cloneNode() ). Node equality using * {@link TreeNode#equals(Object)} will work at least for labeled nodes (because the labels are compared), but * for performance reasons we do not do deep checks. * <p> * If this tree does not contain the given node, null is returned. * * @param n the root node of the subtree * @return subtree rooted at the given node or null */ public Tree getSubtree(TreeNode n) { if (n == null) throw new NullPointerException(); if (nodes.get(n.getIndex()) != n) { return null; } Tree r = new Tree(); r.setName(getName()); TreeNode root = n.cloneNode(); root.setIndex(-1); // reset index r.addVertex(root); r.setRoot(root); hangIn(root, n, r); return r; }
TreeNode cRoot = new TreeNode(); result.addVertex(cRoot); result.setRoot(cRoot);
/** * 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); }
tree.setRoot(currentRoot);