/** * Traverses the tree rooted at this node and counts all vertices. * * @return vertices under this node */ public int vertexCount(){ int i = 0; for (TreeNode node : depthFirstIterator()) { i++; } return i; }
public void removeEdge(Edge<TreeNode> e) { if (incompingEdge != null && e == incompingEdge) { incompingEdge = null; level = -1; } else { edges.remove(e); /* Fix #145 - we have to reset the level of all nodes below this one */ for (TreeNode node : this.depthFirstIterator()) { node.level = -1; } } }
/** * 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; }
/** * @param t a tree * @return map mapping between the TreeNodes of a tree (including the * leaves) and indices */ public Map<Integer, TreeNode> getIndizes(Tree t) { Map<Integer, TreeNode> map = new HashMap<Integer, TreeNode>(); int i = 0; for (TreeNode treeNode : t.getRoot().depthFirstIterator()) { if (Thread.interrupted()) return null; map.put(i, treeNode); i++; } return map; }
private void getLeaves(Tree model) { TreeNode root2 = (TreeNode) model.getRoot(); if (!sym) whichRoot += 1; leaves = new ArrayList<TreeNode>(); for (TreeNode dummy : root2.depthFirstIterator()) { if (dummy.isLeaf()) leaves.add(dummy); } }
private List<String> labels(TreeNode n) { List<String> l = new ArrayList<String>(); for (TreeNode node : n.depthFirstIterator()) { if (node.isLeaf()) { String label = node.getLabel(); if (label != null) { l.add(label); } } } return l; } }
/** * method fills a vector with the nodes of the compare tree * * @param tree */ public static Vector<TreeNode> getInternalNodes(Tree tree) { TreeNode root = (TreeNode) tree.getRoot(); Vector<TreeNode> internalNodes = new Vector<TreeNode>(); for (TreeNode dummy : root.depthFirstIterator()) { if (!dummy.isLeaf()) { internalNodes.add(dummy); } } return internalNodes; }
/** * Returns the leaves under this node in depths first traversal order. * * @return leaves under this node * @see #depthFirstIterator() */ public TreeNode[] getLeaves() { if (isLeaf()) return new TreeNode[]{this}; List<TreeNode> l = new ArrayList<TreeNode>(); for (TreeNode c : depthFirstIterator()) { if (c.isLeaf()) { l.add(c); } } TreeNode[] n = new TreeNode[l.size()]; l.toArray(n); return n; }
/** * method fills a vector with the nodes of the compare tree * * @param compareTree */ private void getInternalNodes(Tree compareTree) { this.compareTree = compareTree; TreeNode root = (TreeNode) compareTree.getRoot(); internalNodes = new ArrayList<TreeNode>(); for (TreeNode dummy : root.depthFirstIterator()) { if (!dummy.isLeaf()) { internalNodes.add(dummy); } } }
/** * The set of labels of all leafs below the input tree node * * @param treeNode the input node * @return The Set of leaf labels */ public static Set<String> getLeafLabels(TreeNode treeNode) { Set<String> leafSet = new HashSet<String>(); if (treeNode.isLeaf()) { leafSet.add(treeNode.getLabel()); } else { for (TreeNode node : treeNode.depthFirstIterator()) { if (node.isLeaf()) leafSet.add(node.getLabel()); } } return leafSet; }
private void buildNormTrees() { normalizedTrees = new ArrayList<Tree>(trees.size()); for (int i = 0; i < factors.length; i++) { double factor = factors[i]; Tree clone = trees.get(i).cloneTree(); if (factor != 1d) { Set<Edge> edges = new HashSet<Edge>(clone.edgeCount()); for (TreeNode node : clone.getRoot().depthFirstIterator()) { edges.addAll(node.getAllEdges()); } for (Edge edge : edges) { edge.setWeight(edge.getWeight() * factor); } } normalizedTrees.add(clone); } }
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; }
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); } } }); }
/** * Add an edge to this node. */ @SuppressWarnings("unchecked") public void addEdge(Edge e) { if (e.getSource() == this) { if (getEdge(e.getTarget()) == null) { edges.add(e); /* Fix #145 - we have to reset the level of all nodes below this one */ for (TreeNode node : this.depthFirstIterator()) { node.level = -1; } } } else { incompingEdge = e; level = -1; } }
/** * Creates and returns the {@link Partition} of this node. Partitions are not * cached and recomputed for each call. * * @return partition of this node */ public Partition getPartition() { Partition partition = new Partition(); for (TreeNode n : depthFirstIterator()) { if (n.isLeaf()) { partition.add(n); } } return partition; }
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; }
/** * 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; }
/** * 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); } }
THashSet<String> getLeafLabels(Tree tree1) { THashSet<String> taxonSet = new THashSet<>(tree1.vertexCount()); for (TreeNode taxon : tree1.getRoot().depthFirstIterator()) { if (taxon.isLeaf()) { taxonSet.add(taxon.getLabel()); } } return taxonSet; }
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); } } }