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; }
/** * 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; }
public static boolean treeEquals(Tree t1, Tree t2) { if (t1.equals(t2)) return true; if (t1.vertexCount() != t2.vertexCount()) return false; Set<String> l1 = TreeUtils.getLeafLabels(t1.getRoot()); Set<String> l2 = TreeUtils.getLeafLabels(t2.getRoot()); if (!l1.equals(l2)) return false; double[] rates = FN_FP_RateComputer.calculateRates(t1, t2, false); if (rates[2] == 0d && rates[3] == 0d) { return true; } return false; }
/** * 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()); }
/** * Create an array that contains the pairwise least common ancestor nodes for all leaves in the given tree. * The array is indexed by the indices implied by {@link Tree#getLeaves()} * * @param tree the tree * @return map the lca map */ public static TreeNode[][] lcaMap(Tree tree) { final TreeNode[] leaves = tree.getLeaves(); TreeNode[][] map = new TreeNode[leaves.length][leaves.length]; for (int i = 0; i < leaves.length; i++) { TreeNode n1 = leaves[i]; map[i][i] = n1; for (int j = i; j < leaves.length; j++) { TreeNode n2 = leaves[j]; final TreeNode lca = tree.findLeastCommonAncestor(n1, n2); map[i][j] = lca; map[j][i] = lca; } } return map; }
private double caclulateTreeResolution(Tree tree) { return TreeUtils.calculateTreeResolution(tree.getNumTaxa(), tree.vertexCount()); }
Tree t1 = trees.get(0).cloneTree(); //because t1 gets modified Tree t2 = trees.get(1); allLeafes = new TObjectIntHashMap<>(t1.vertexCount(), Constants.DEFAULT_LOAD_FACTOR, -2); Map<TreeNode, Bipartition> lcaToLabels2 = new HashMap<>(t2.vertexCount()); Set<Bipartition> patitionsToProof2 = new HashSet<>(t1.vertexCount()); for (TreeNode node : t2.getRoot().depthFirstIterator()) { if (!node.equals(t2.getRoot())) { Bipartition nodePartition = lcaToLabels2.get(node); if (!node.getParent().equals(t2.getRoot())) { TreeNode p = node.getParent(); Map<TreeNode, Bipartition> lcaToLabels1 = new HashMap<>(t2.vertexCount()); Set<Bipartition> compatiblePartitions1 = new HashSet<>(t1.vertexCount()); Set<Bipartition> patitionsToProof1 = new HashSet<>(t1.vertexCount()); for (TreeNode node : t1.getRoot().depthFirstIterator()) { if (!node.equals(t1.getRoot())) { Bipartition nodePartition = lcaToLabels1.get(node); if (!node.getParent().equals(t1.getRoot())) { TreeNode p = node.getParent(); Set<Bipartition> patitionsToRemove2 = new HashSet<>(t1.vertexCount()); Iterator<Bipartition> i1 = patitionsToProof1.iterator(); while (i1.hasNext()) {
for (TreeNode treeNode : tree.getLeaves()) { if (treeNode.getLabel() != null && treeNode.getLabel().length() > 0) { nodes.put(treeNode.getLabel(), treeNode); TreeNode lca = tree.findLeastCommonAncestor(lcaNodes.toArray(new TreeNode[lcaNodes.size()])); TreeNode parent = lca2added.get(singleTaxon.lca.toString()); if (lca == null || lca == tree.getRoot()) { tree.addVertex(newRoot); tree.addEdge(newRoot, tree.getRoot()); tree.setRoot(newRoot); parent = newRoot; } else { tree.addVertex(parent); TreeNode lcaParent = lca.getParent(); tree.removeEdge(lcaParent, lca); tree.addEdge(lcaParent, parent); tree.addEdge(parent, lca); tree.addVertex(newNode); tree.addEdge(parent, newNode); } else { Tree subtree = Newick.getTreeFromString(singleTaxon.subtree); if (subtree.vertexCount() == 2) { TreeNode node = subtree.getRoot().getChildAt(0);
Tree t1 = trees.get(0).cloneTree(); //because t1 gets modified Tree t2 = trees.get(1); allLeafes = new TObjectIntHashMap<>(t1.vertexCount(), Constants.DEFAULT_LOAD_FACTOR, -2); numOfTaxa = -1; indexToLeaf1 = new TIntObjectHashMap<>(); Map<TreeNode, Bipartition> lcaToLabels1 = new HashMap<>(t1.vertexCount()); Set<Bipartition> partitions1 = new HashSet<>(t1.vertexCount()); createPartitionsAndLCAMap(t1, partitions1, lcaToLabels1); Map<TreeNode, Bipartition> lcaToLabels2 = new HashMap<>(t2.vertexCount()); Set<Bipartition> partitions2 = new HashSet<>(t2.vertexCount()); createPartitionsAndLCAMap(t2, partitions2, lcaToLabels2); partitionsToRemove1.retainAll(incompatiblePartitions); Iterator<TreeNode> t1It = t1.vertices().iterator(); while (t1It.hasNext()) { TreeNode node = t1It.next(); if (node.isInnerNode() && !node.equals(t1.getRoot())) { if (partitionsToRemove1.contains(lcaToLabels1.get(node))) { t1.removeEdge(node, child); t1.addEdge(parent, child); t1.removeVertex(node);
if (tree.vertexCount() == 1) { TreeNode oldRoot = tree.getRoot(); TreeNode root = new TreeNode(); tree.addVertex(root); tree.addEdge(root, oldRoot); tree.setRoot(root); tree.addVertex(nn); tree.addEdge(root, nn); tree.removeVertex(n); TreeNode np = new TreeNode(); tree.addVertex(np); tree.addEdge(p, np); tree.addVertex(nn); tree.addEdge(np, nn); if (tree.vertexCount() == 0) { TreeNode root = new TreeNode(); tree.addVertex(root); tree.setRoot(root); for (String s : merged) { TreeNode nn = new TreeNode(s); tree.addVertex(nn); tree.addEdge(root, nn); } else if (tree.vertexCount() == 1) {
TreeNode currentRoot = tree.getRoot(); TreeNode source = edge.getSource(); TreeNode target = edge.getTarget(); tree.removeVertex(currentRoot); tree.addVertex(currentRoot); tree.removeEdge(target.getParent(), target); tree.addEdge(currentRoot, target).setWeight(w); tree.removeEdge(source.getParent(), source); tree.addEdge(currentRoot, source).setWeight(sourceWeight); tree.addEdge(last, node).setWeight(path.get(node)); last = node; w = otherRootChildren.get(rchild); if (rchild.getEdgeToParent() != null) { tree.removeEdge(rchild.getParent(), rchild); tree.addEdge(rootchild, rchild).setWeight(w); tree.setRoot(currentRoot);
ntaxa = 0; for (int i = 0; i < input.length; i++) { Iterator iter = input[i].vertices().iterator(); while (iter.hasNext()) { TreeNode n = (TreeNode) iter.next(); nvert += input[i].vertexCount(); postOrder((TreeNode) input[i].getRoot()); status = ((i + 1) * 0.4) / input.length; LOGGER.finer("...counting in tree " + (i + 1)); preOrder((TreeNode) input[i].getRoot(), null); status = 0.4 + (((i + 1) * 0.4) / input.length); LOGGER.finer("...checking tree " + (i + 1)); result = new Tree(); if (count >= majorityThreshold) { TreeNode n = node.getNode(result, ((double) count / (double) this.input.length) * 100d); result.addVertex(n); addedNodes.add(node); if (node.getParentPartition() != null) { TreeNode n1 = node.getParentPartition().getNode(result); result.addEdge(n1, n); if (finalCheck((TreeNode) result.getRoot()) == null) { LOGGER.fine("Failed ... recomputing tree");
protected static Tree getTree(Reader r) { NewickStreamTokenizer st = new NewickStreamTokenizer(r); Tree t = new Tree(); StringBuffer label = new StringBuffer(); StringBuffer nhx = new StringBuffer(); TreeNode n = new TreeNode(); TreeNode x = new TreeNode(); t.addVertex(x); t.addVertex(n); t.addEdge(x, n); t.setRoot(x); TreeNode c = null; case '(': c = new TreeNode(); t.addVertex(c); t.addEdge(n, c); nhx = new StringBuffer(); c = new TreeNode(); t.addVertex(c); t.addEdge(n.getParent(), c); if (((TreeNode) t.getRoot()).childCount() == 1) { TreeNode child = ((TreeNode) t.getRoot()).getChildAt(0); t.removeVertex(t.getRoot()); t.setRoot(child);
for (TreeNode leaf : t.getLeaves()) { labelToNode.put(leaf.getLabel(), leaf); siblingLeaves.add(labelToNode.get(leaf)); TreeNode lca = t.findLeastCommonAncestor(siblingLeaves); TreeNode lcaParent = lca.getParent(); if (lcaParent != null) { //check if we have to insert a new root TreeNode nuLcaParent = new TreeNode(); t.addVertex(nuLcaParent); t.addEdge(nuLcaParent, lca); t.addEdge(lcaParent, nuLcaParent); t.removeEdge(lcaParent, lca); lca = nuLcaParent; t.addVertex(nuLcaParent); t.addEdge(nuLcaParent, lca); t.setRoot(nuLcaParent); lca = nuLcaParent; t.addVertex(nuNode); t.addEdge(lca, nuNode); t.addVertex(node); if (node.isLeaf()) { labelToNode.put(node.getLabel(), node);
Set<String> labels = new HashSet<String>(); for (TreeNode n : trees[0].vertices()) { if (n.getLabel() != null) { if (n.isLeaf()) for (TreeNode n : trees[i].vertices()) { if (n.isLeaf()) tree_labels.add(n.getLabel()); Tree nt = new Tree(); nt.setName(t.getName()); TreeNode root = (TreeNode) t.getRoot(); pruneTraverse(nt, root, null, labels); if (nt.vertexCount() > 0 && ((TreeNode) nt.getRoot()).childCount() > 0) { newTrees.add(nt);
List<Tree> bestRootedInputTrees = null; int totalNumberOfErrors = Integer.MAX_VALUE; int numTaxa = unrootedModel.getNumTaxa(); for (TreeNode node : unrootedModel.vertices()) { if (!node.equals(unrootedModel.getRoot())) { if (clade.size() > 2 && (numTaxa - clade.size()) > 2) { Tree rootedModel = unrootedModel.cloneTree(); List<TreeNode> nodes = new LinkedList<>(); for (String s : clade) { nodes.add(rootedModel.getVertex(s)); Edge<TreeNode> edgeToReroot = rootedModel.findLeastCommonAncestor(nodes).getEdgeToParent(); TreeUtils.reorderingBootstrapLabelsForRooting(edgeToReroot); TreeUtils.rerootToOutgroup(rootedModel, edgeToReroot.getTarget()); Tree[] forRooting = TreeUtils.cloneAndPruneTrees(new Tree[]{tree, rootedModel}); if (forRooting[1].getRoot().childCount() == 2) { sumOfErrorScores += findMostCompatibleRoot(forRooting[0], forRooting[1]); } else {
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; }
/** * 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); }
queue.add(tree.getRoot()); tree.removeEdge(current, e1); tree.removeEdge(current, e2); tree.addEdge(newNode, e1); tree.addEdge(newNode, e2); j++; for (TreeNode treeNode : tree.getRoot().depthFirstIterator()) { if (treeNode.isInnerNode()) { while (treeNode.getChildren().size() > 2) { tree.addVertex(newNode); tree.removeEdge(treeNode, e1); tree.removeEdge(treeNode, e2); tree.addEdge(treeNode, newNode); tree.addEdge(newNode, e1); tree.addEdge(newNode, e2); for (TreeNode treeNode : tree.getRoot().depthFirstIterator()) { if (treeNode.isInnerNode()) { while (treeNode.getChildren().size() > 2) { tree.addVertex(newNode); tree.removeEdge(treeNode, e1);