private static Set<Integer> buildFitchUnion(TreeNode parent, Map<TreeNode, Set<Integer>> valueSets) { Set<Integer> union = new HashSet<Integer>(2); int plus = 0; int minus = 0; for (TreeNode child : parent.getChildren()) { Set<Integer> valueSet = valueSets.get(child); if (valueSet.contains(-1)) minus++; if (valueSet.contains(1)) plus++; } if (plus >= minus) { union.add(1); } if (minus >= plus) union.add(-1); return union; }
/** * Inserts node to specific position. Shifts any subsequent nodes to the right. * * @param tree the tree * @param i the position */ private static void insertNode(Tree tree, TreeNode parent, TreeNode node, int i) { if (i < 0) { throw new IndexOutOfBoundsException("Index is smaller than 0"); } ArrayList<TreeNode> children = parent.getChildren(); children.add(i, node); tree.addVertex(node); for (TreeNode child : parent.getChildren()) { tree.removeEdge(parent, child); } for (TreeNode child : children) { tree.addEdge(parent, child); } }
private static void removePolytomies(Tree tree, Comparator<TreeNode> comp) { tree.getRoot().depthFirstIterator().forEach(node -> { int toRemove = node.childCount() - 2; if (toRemove > 0) { ArrayList<TreeNode> children = node.getChildren(); sortChildren(children, comp); for (int i = 0; i < toRemove; i++) { TreeNode remove = children.get(i); tree.removeSubtree(remove); } } }); }
if (node != null) { Set<N> toInsert = new HashSet(node.childCount()); for (TreeNode child : node.getChildren()) { if (child.isInnerNode()) { N n = scaffoldCharacterMapping.inverse().get(child);
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));
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)); for (TreeNode node : tree.vertices()) { if (node.isInnerNode()) { List<TreeNode> children = node.getChildren();
private Tree addCompatibleCladesToBackboneTree(Tree backboneTree, Collection<Bipartition> partitionsToInsert) { for (Bipartition partition : partitionsToInsert) { Set<TreeNode> leafes = new HashSet<>(); for (int i = partition.partition.nextSetBit(0); i >= 0; i = partition.partition.nextSetBit(i + 1)) { leafes.add(indexToLeaf1.get(i)); } TreeNode lca = backboneTree.findLeastCommonAncestor(new ArrayList<>(leafes)); TreeNode nuLca = new TreeNode(); backboneTree.addVertex(nuLca); for (TreeNode child : lca.getChildren()) { TreeNode leaf; if (child.isInnerNode()) { leaf = child.depthFirstIterator().iterator().next(); } else { leaf = child; } if (leafes.contains(leaf)) { backboneTree.removeEdge(lca, child); backboneTree.addEdge(nuLca, child); } } backboneTree.addEdge(lca, nuLca); } return backboneTree; }
for (TreeNode child : prunedModel.getRoot().getChildren()) { rootEdge.add(TreeUtils.getLeafLabels(child));
for (TreeNode child : prunedModel.getRoot().getChildren()) { rootEdges.add(TreeUtils.getLeafLabels(child));
Set<Set<Object>> partitionsAgainst = new HashSet<>(); Map<Set<Object>, Map<Set<Object>, Set<Set<Object>>>> supported = new HashMap<>(partitions.size()); for (TreeNode treeNode : parentToCheck.getChildren()) { Set<Object> s; Map<Set<Object>, Set<Set<Object>>> pairwiseSupport = new HashMap<>(); supported.put(s, pairwiseSupport); for (TreeNode part : treeNode.getChildren()) { Set<Object> p; if (part.isInnerNode()) {
ArrayList<TreeNode> kids = parent.getChildren(); ArrayList<TreeNode> sortme = new ArrayList<TreeNode>(); ArrayList<TreeNode> neworder = new ArrayList<TreeNode>();
intersection.add(1); intersection.add(-1); for (TreeNode child : parent.getChildren()) { intersection.retainAll(valueSets.get(child));
for (TreeNode child : cladeToCheck.getChildren()) { if (child.isInnerNode()) { partition.put(new HashSet<Object>(leafMap.get(child)), child);
int i = j + 1; TreeNode e1 = current.getChildren().get(j); TreeNode e2 = current.getChildren().get(i); queue.addAll(current.getChildren()); for (TreeNode treeNode : tree.getRoot().depthFirstIterator()) { if (treeNode.isInnerNode()) { while (treeNode.getChildren().size() > 2) { List<TreeNode> list = treeNode.getChildren(); Collections.shuffle(list); while (treeNode.getChildren().size() > 2) { e1 = treeNode.getChildren().get(0); e2 = treeNode.getChildren().get(1); } else { e1 = treeNode.getChildren().get(treeNode.getChildren().size() - 2); e2 = treeNode.getChildren().get(treeNode.getChildren().size() - 1);
for (TreeNode child : node.getChildren()) { t1.removeEdge(node, child); t1.addEdge(parent, child);
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; }
for (TreeNode child : node.getChildren()) { t1.removeEdge(node, child); t1.addEdge(parent, child); t1.addVertex(nuLca); for (TreeNode child : lca.getChildren()) { TreeNode leaf; if (child.isInnerNode()) {
List<TreeNode> nodes = ancestor.getChildren();