/** * The number of labels of all leafs in the input trees * * @param trees tree to get leafs from * @return number of leafs in this list of trees */ public static int getLeafCount(List<Tree> trees) { return getLeafLabels(trees).size(); }
/** * 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 list of leaves contained in all trees * * @param trees the trees * @return sharedLeaves the leaves common to all trees */ public static List<String> commonLeaves(Collection<Tree> trees) { return commonLeaves(trees.toArray(new Tree[trees.size()])); }
public static Tree rerootTreesByModelTree(Tree inputTree, Tree model) { List<Tree> bothTrees = new ArrayList<Tree>(Arrays.asList(TreeUtils.cloneAndPruneTrees(new Tree[]{inputTree, model}))); Tree tree = bothTrees.get(0); Tree prunedModel = bothTrees.get(1); rootEdges.add(TreeUtils.getLeafLabels(child)); Set<String> allLabels = TreeUtils.getLeafLabels(tree.getRoot()); boolean newRootFound = false; Set<String> p1; if (node.isInnerNode()) { p1 = TreeUtils.getLeafLabels(node); } else { p1 = new HashSet<String>(1); TreeUtils.reorderingBootstrapLabelsForRooting(node.getEdgeToParent()); TreeUtils.rerootToOutgroup(tree, node);
private static int findMostCompatibleRoot(Tree tree, Tree prunedModel) { List<Set<String>> rootEdges = new ArrayList<>(prunedModel.getRoot().childCount()); //should be 2 otherwise method will not work for (TreeNode child : prunedModel.getRoot().getChildren()) { rootEdges.add(TreeUtils.getLeafLabels(child)); Set<String> allLabels = TreeUtils.getLeafLabels(tree.getRoot()); p1 = TreeUtils.getLeafLabels(node); } else { p1 = new HashSet<>(1); TreeUtils.reorderingBootstrapLabelsForRooting(bestRoot.getEdgeToParent()); TreeUtils.rerootToOutgroup(tree, bestRoot);
if (!sibling.equals(node)) { numOfSiblings++; siblingLeaves.addAll(TreeUtils.getLeafLabels(sibling)); TreeUtils.pruneDegreeOneNodes(t, false, false); } else { for (TreeNode child : node.children()) {
public static Tree addCladewiseSplitFit(List<WeightedTreePartitions> biparts, Tree tree) { Map<Set<String>, TreeNode> labelsToClade = getChildrenMap(tree); Set<String> all = getLeafLabels(tree); Map<WeightedTreePartitions, AtomicDouble> partitionsToConflicts = new HashMap<>(labelsToClade.size()); for (Set<String> leafesInClade : labelsToClade.keySet()) { partitionsToConflicts.put(new WeightedTreePartitions(leafesInClade, all), new AtomicDouble(0)); } double numberOfChars = 0; for (WeightedTreePartitions sourcePartition : biparts) { numberOfChars += sourcePartition.weight; for (Map.Entry<WeightedTreePartitions, AtomicDouble> e : partitionsToConflicts.entrySet()) { if (e.getKey().isIncompatible(sourcePartition)) { e.getValue().addAndGet(e.getKey().weight); } } } for (Map.Entry<WeightedTreePartitions, AtomicDouble> entry : partitionsToConflicts.entrySet()) { TreeNode first = labelsToClade.remove(entry.getKey().getGroupA()); TreeNode second = labelsToClade.remove(entry.getKey().getGroupB()); String l = String.valueOf(1 - (entry.getValue().doubleValue() / numberOfChars)); if (first != null) first.setLabel(l); if (second != null) second.setLabel(l); } return tree; }
public static double claculateMinumCharaterDeletionCosts(Tree tree, List<Tree> sourceTrees) { Set<TreeNode> deletedNodes = new HashSet<TreeNode>(); for (Tree sourceTree : sourceTrees) { for (TreeNode treeNode : sourceTree.vertices()) { if (treeNode.isInnerNode() && treeNode != sourceTree.getRoot()) { Set<String> sourceLeafLabels = getLeafLabels(treeNode); Set<TreeNode> superLeafes = getLeafesFromLabels(sourceLeafLabels, tree); TreeNode lca = tree.findLeastCommonAncestor(new ArrayList<TreeNode>(superLeafes)); Set<String> superLeafLabels = getLeafLabels(lca); if (!sourceLeafLabels.equals(superLeafLabels)) { deletedNodes.add(treeNode); } } } } double costs = 0; for (TreeNode deletedNode : deletedNodes) { costs += deletedNode.getDistanceToParent(); } return costs; }
double weightTOChild = edgeToReroot.getWeight() - weightFromParent; reorderingBootstrapLabelsForRooting(edgeToReroot); rerootToOutgroup(tree, edgeToReroot.getTarget(), weightTOChild);
private static int findMostCompatibleFakeRoot(Tree tree, Tree prunedModel) { List<Set<String>> rootEdges = new ArrayList<>(prunedModel.getRoot().childCount()); //should be >2 otherwise use the real root method for (TreeNode child : prunedModel.getRoot().getChildren()) { rootEdges.add(TreeUtils.getLeafLabels(child)); Set<String> allLabels = TreeUtils.getLeafLabels(tree.getRoot()); Set<String> clade = TreeUtils.getLeafLabels(child); if (splitsCopy.contains(clade)) splitsCopy.remove(clade); clade.removeAll(TreeUtils.getLeafLabels(node)); if (splitsCopy.contains(clade)) splitsCopy.remove(clade); TreeUtils.moveFakeRoot(tree, bestRoot);
Set<String> characterTaxa = getLeafLabels(character); Tree sourceTree = (Tree) character.getGraph(); TreeNode[] sourceTreeTaxa = sourceTree.getLeaves(); valueSets.put(parent, buildFitchUnion(parent, valueSets)); } else { valueSets.put(parent, intersection);
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); pruneDegreeOneNodes(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); }
/** * Deletes the specified node from the tree.The child of this node becomes child * of its parent. * If the node has more than one child, nothing have to be done. * * @param node: the node, that has to be deleted * @param tree: the tree, from that the tree has to be deleted */ public static void pruneInnerNode(TreeNode node, Tree tree) { pruneInnerNode(node, tree, true, false); }
/** * Remove the list of nodes from the tree and prune the tree afterwards. * * @param toRemove leaves to remove * @param tree the tree */ private void removeSubtreeFromTree(List<TreeNode> toRemove, Tree tree) { removeSubtreeFromTree(toRemove, tree, false); }
addingEnabled = false; for (Tree treeTopology : topologies) { mergedSets.put(getChildrenMap(treeTopology, true), treeTopology); Map<Set<String>, TreeNode> treeAsMap = getChildrenMap(tree); treeSetToNode.put(treeAsMap, tree); Set<Set<String>> treeAsSet = treeAsMap.keySet(); Tree mergedTree = mergedSets.get(mergedAsMap); if (minOccurence <= (toAddAsMap.size() + 1)) { mergeTrees(mergedAsMap, mergedTree, toAddAsMap, treeSetToNode); tressToReturn.add(mergedTree); mergeTrees(mergedAsMap, mergedTree, toAddAsMap, treeSetToNode);
/** * Clones all trees and removes all innter vertex labels from the clones. The given source trees * are not manipulated * * @param sourceTrees the source trees * @return clones the clones without any innter vertices */ public static Tree[] deleteInnerLabels(Tree[] sourceTrees) { Tree[] trees = new Tree[sourceTrees.length]; for (int i = 0; i < sourceTrees.length; i++) { trees[i] = deleteInnerLabels(sourceTrees[i]); } return trees; }
public static Map<Set<String>, TreeNode> getChildrenMap(final Tree tree) { return getChildrenMap(tree, false); }
public static void rerootToOutgroup(Tree tree, TreeNode outgroup) { rerootToOutgroup(tree, outgroup, (outgroup.getDistanceToParent() / 2d)); }
Set<String> clade = TreeUtils.getLeafLabels(node); if (clade.size() > 2 && (numTaxa - clade.size()) > 2) { TreeUtils.reorderingBootstrapLabelsForRooting(edgeToReroot); TreeUtils.rerootToOutgroup(rootedModel, edgeToReroot.getTarget()); Tree[] forRooting = TreeUtils.cloneAndPruneTrees(new Tree[]{tree, rootedModel});