private boolean isSubtree() { return insertionPoint.isInnerNode(); }
/** * Traverses the tree rooted at this node and counts all inner-vertices/clades . * * @return clades under this node */ public int cladeCount(){ int i = 0; for (TreeNode node : depthFirstIterator()) { if (node.isInnerNode()) i++; } return i; }
private void reApplyLabels(List<Tree> trees, Map<TreeNode, String> labels) { for (Tree tree : trees) { for (TreeNode node : tree.vertices()) { if (node.isInnerNode()) { node.setLabel(labels.get(node)); } } } }
public void init(List<Tree> sourceTrees) { nameToObject = new HashMap<>(); ppSet = new HashSet<>(); for (Tree tree : sourceTrees) { Set<TreeNode> taxa = new HashSet<>(Arrays.asList(tree.getLeaves())); for (TreeNode treeNode : tree.vertices()) { if (treeNode.isInnerNode() && !treeNode.equals(tree.getRoot())) { ppSet.add(new PPCharacter(treeNode, new HashSet<>(taxa))); } } } }
private static Set<TreePartition> getPartitions(Tree tree, Set<String> allTaxa){ Set<TreePartition> partitions = new HashSet<TreePartition>(); for (TreeNode current : tree.vertices()) { if (current.isInnerNode()) { // if (!current.equals(calculated.getRoot())) { TreeNode[] leafes = current.getLeaves(); if ((allTaxa.size() - leafes.length) > 1) partitions.add(new TreePartition(leafes, allTaxa)); // } } } return partitions; }
private Map<TreeNode, Set<Object>> getNodeToLeafMap(Tree tree) { Map<TreeNode, Set<Object>> map = new HashMap<>(tree.getNumTaxa()); for (TreeNode node : tree.vertices()) { if (node.isInnerNode()) { Set<Object> leafSet = new HashSet<>(); for (TreeNode taxon : node.getLeaves()) { leafSet.add(nameToObject.get(taxon.getLabel())); } map.put(node, leafSet); } } return map; }
/** * 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 Map<TreeNode, String> removeAndMapInnerLabels(List<Tree> trees) { Map<TreeNode, String> labels = new HashMap(); for (Tree tree : trees) { for (TreeNode treeNode : tree.vertices()) { if (treeNode.isInnerNode()) { String label = treeNode.getLabel(); treeNode.setLabel(null); if (label != null) { labels.put(treeNode, label); } } } } return labels; }
public static boolean containsInnerLabels(Tree tree) { for (TreeNode node : tree.vertices()) { if (node.isInnerNode()) { if (!((node.getLabel() == null) || (node.getLabel().equals(TreeNodeProperties.PROPERTY_LABEL)))) { return true; } } } return false; }
private static List<TreeNode> checkForBootstrapErrors(Tree tree) { List<TreeNode> errorNodes = new LinkedList<>(); for (TreeNode node : tree.getRoot().depthFirstIterator()) { if (!node.equals(tree.getRoot()) && node.isInnerNode()) { String label = node.getLabel(); if (node.getLabel() != null) { try { int bs = Integer.valueOf(node.getLabel()); } catch (NumberFormatException e) { e.printStackTrace(); errorNodes.add(node); } } else { errorNodes.add(node); } } } if (errorNodes.isEmpty()) return null; return errorNodes; }
public static List<WeightedTreePartitions> getBiparts(Tree sourceTree){ List<WeightedTreePartitions> biparts = new LinkedList<>(); final TreeNode r = sourceTree.getRoot(); Set<String> sourceLeafes = getLeafLabels(r); for (TreeNode treeNode : sourceTree.getRoot().depthFirstIterator()) { if (treeNode != r && treeNode.isInnerNode()) { biparts.add(new WeightedTreePartitions(getLeafLabels(treeNode), sourceLeafes)); } } return biparts; }
/** * Clones the tree and removes al inner vertices on the tree. Due to cloning * the given tree is not modified * * @param sourceTree the source tree * @return clone clone of the soruce treee without any innter vertex labels */ public static Tree deleteInnerLabels(Tree sourceTree) { Tree tree = sourceTree.cloneTree(); for (TreeNode node : tree.vertices()) { if (node.isInnerNode()) { node.setLabel(null); } } return tree; }
/** * Returns true if all trees are binary. * * @param trees the trees * @return True, if trees are binary */ public static boolean isBinary(Tree[] trees) { for (Tree tree : trees) { for (TreeNode node : tree.getRoot().depthFirstIterator()) { if (node.isInnerNode() && node.childCount() > 2) { return false; } } } return true; }
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 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; }
/** * Removes al inner vertices on the tree and sets branch length to 1 * the given tree is modified * * @param tree the source tree */ public static void cleanTree(Tree tree) { for (TreeNode node : tree.getRoot().depthFirstIterator()) { if (!node.equals(tree.getRoot())) node.getEdgeToParent().setWeight(1d); if (node.isInnerNode()) node.setLabel(null); } }
public static void reorderingBootstrapLabelsForRooting(Edge<TreeNode> edgeToReroot) { TreeNode outGroup = edgeToReroot.getTarget(); String startingLabel; //reordering inner labels for bootstrap Values if (outGroup.isInnerNode()) { startingLabel = outGroup.getLabel(); } else { startingLabel = edgeToReroot.getSource().getLabel(); } if (startingLabel != null) { moveUP(edgeToReroot, startingLabel); } }
public static Map<Set<String>, TreeNode> getChildrenMap(final Tree tree, final boolean setEdgeweightsToZero) { Map<Set<String>, TreeNode> childrenSets = new HashMap<Set<String>, TreeNode>(tree.vertexCount()); for (TreeNode node : tree.vertices()) { if (node.isInnerNode()) { if (node != tree.getRoot()) { if (setEdgeweightsToZero) node.getEdgeToParent().setWeight(0d); childrenSets.put(getLeafLabels(node), node); } } else { if (setEdgeweightsToZero) node.getEdgeToParent().setWeight(0d); } } return childrenSets; }
public static void addCladewiseSplitFit(List<Tree> sourceTrees, FlipCutWeights.Weights weighting, Collection<Tree> trees) { final SimpleCosts comp; if (weighting == FlipCutWeights.Weights.UNIT_COST) { comp = new UnitCostComputer(sourceTrees,null); } else { comp = new WeightCostComputer(sourceTrees,weighting); } List<WeightedTreePartitions> biparts = new LinkedList<>(); for (Tree sourceTree : sourceTrees) { final TreeNode r = sourceTree.getRoot(); Set<String> sourceLeafes = getLeafLabels(r); for (TreeNode treeNode : sourceTree.getRoot().depthFirstIterator()) { if (treeNode != r && treeNode.isInnerNode()) { biparts.add(new WeightedTreePartitions(getLeafLabels(treeNode), sourceLeafes, comp.getEdgeWeight(treeNode))); } } } for (Tree tree : trees) { addCladewiseSplitFit(biparts, tree); } } }
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; }