private TreeNode searchForNode(Tree model, TreeNode node) { TreeNode found = ((TreeNode) model.getRoot()).getPartition().getNode(node); return found; }
/** * The set of labels of all leafs in the input tree * * @param tree tree to get leafs from * @return The Set of leaf labels */ public static Set<String> getLeafLabels(Tree tree) { return getLeafLabels(tree.getRoot()); }
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; }
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); } }
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); } }
public static void keepLeafesAndPruneTree(Tree tree, Collection<TreeNode> toKeep) { boolean deleted = true; while (deleted) { deleted = false; for (TreeNode node : tree.getLeaves()) { if (node.getLabel() == null || !toKeep.contains(node)) { tree.removeVertex(node); deleted = true; } } } pruneDegreeOneNodes(tree); }
/** * Sets all labels of nodes in the tree to s * * @param s the string the labels are set to */ public void setAllLabels(String s) { for (TreeNode v : vertices()) { if (v.isInnerNode()) { v.setLabel(s); } } }
private double caclulateTreeResolution(Tree tree) { return TreeUtils.calculateTreeResolution(tree.getNumTaxa(), tree.vertexCount()); }
/** * Returns an array of all leaves. * <p> * This uses {@link TreeNode#getLeaves()} on * the root of the tree. Be sure the tree is completely initialized, because * we need a root node and {@link TreeNode#isLeaf()} is used to distinguish * between leaves and inner nodes, and this only works on a properly initialized tree. * * @return treeNodes array of all leaves in this tree * @since 0.8.2 */ public TreeNode[] getLeaves() { return getRoot().getLeaves(); }
/** * Clones the complete tree. This calls {@link #getSubtree(TreeNode)} using the root * node of this tree. * * @return tree clone of this tree */ public Tree cloneTree() { return getSubtree(getRoot()); }
public static void pruneDegreeOneNodes(Tree tree, boolean sumEdgeWeight, boolean useParentWeight) { for (int i = 0; i < tree.getMaxIndex(); i++) { TreeNode n = tree.getVertex(i); if (n == null) { continue; } //prune the node, if it is not null pruneInnerNode(n, tree, sumEdgeWeight, useParentWeight); } }
/** * Adds an edge from n1 to n2. This {@link EdgeType} is ignored as rooted * trees use directed edges. */ public Edge<TreeNode> addEdge(TreeNode n1, TreeNode n2, EdgeType type) { return addEdge(n1, n2); }
/** * Iterates over the tree and removes all inner vertices with outdegree == 1. * The resulting tree is at least binary. * * @param tree the tree */ public static void pruneDegreeOneNodes(Tree tree) { pruneDegreeOneNodes(tree, true, false); }
/** * Returns the {@link Edge} connecting n1 to n2 or null. */ public Edge<TreeNode> getEdge(TreeNode n1, TreeNode n2) { if (n1 == null || n2 == null) return null; return n1.getEdge(n2); }
/** * Returns true if here is an edge connecting n1 and n2 (directed from n1 to n2). * The {@link Graph} interface froces an {@link EdgeType} here, but it is ignored by * the tree implementation. */ public boolean containsEdge(TreeNode n1, TreeNode n2, EdgeType type) { return containsEdge(n1, n2); }
/** * Applies translation using strict patterns * * @param tree the tree * @param translation the translation table * @return tree translated tree */ public static Tree renameNodes(Tree tree, Map<String, String> translation) { return renameNodes(tree, translation, true); }
/** * Delegates to {@link #getEdge(TreeNode, TreeNode)} */ public Edge<TreeNode> getEdge(TreeNode n1, TreeNode n2, EdgeType type) { return getEdge(n1, n2); }
/** * Returns true if there exists an edge from n1 to n2. */ public boolean containsEdge(TreeNode n1, TreeNode n2) { if (n1 == null || n2 == null) return false; return n1.getEdge(n2) != null; }