protected double parseTreeWeightFromLabel(Tree tree) { String rootLabel = tree.getRoot().getLabel(); if (rootLabel != null) { try { return Double.valueOf(rootLabel); } catch (NumberFormatException e) { return 1d; } } return 1d; }
/** * 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()); }
public static String getStringFromTree(Tree t, boolean addSemicolon, boolean writeWeights) { StringBuffer treeString = new StringBuffer(); TreeNode root = t.getRoot(); appendNode(root, treeString, writeWeights); return treeString.toString() + (addSemicolon ? ";" : ""); }
/** * The set of labels of all leafs in the input tree * * @param tree tree to get leafs from * @return The Set of leaf labels */ public static Set<String> getLeafLabels(Tree tree) { return getLeafLabels(tree.getRoot()); }
/** * Returns an array of all leaves. * <p> * This uses {@link TreeNode#getLeaves()} on * the root of the tree. Be sure the tree is completely initialized, because * we need a root node and {@link TreeNode#isLeaf()} is used to distinguish * between leaves and inner nodes, and this only works on a properly initialized tree. * * @return treeNodes array of all leaves in this tree * @since 0.8.2 */ public TreeNode[] getLeaves() { return getRoot().getLeaves(); }
public static void sortTree(Tree tree) { sortTree(tree, tree.getRoot()); }
public static void sortTree(Tree tree, Comparator<TreeNode> comparator) { sortTree(tree, tree.getRoot(), comparator); }
/** * @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); } }
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))); } } } }
/** * 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; }
private TreeNode searchForNode(Tree model, TreeNode node) { TreeNode found = ((TreeNode) model.getRoot()).getPartition().getNode(node); return found; }
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; }
private static Map<Set<String>, TreeNode> getTreeAsLeafMap(final Tree tree, final boolean setEdgeweightsToZero) { Map<Set<String>, TreeNode> childrenSets = new HashMap<Set<String>, TreeNode>(tree.vertexCount()); for (TreeNode child : tree.getRoot().children()) { Set<String> childTaxaSet = new HashSet<String>(); addLeafesFromChildren(childrenSets, child, childTaxaSet, setEdgeweightsToZero); } return childrenSets; }
private static void mergeTreeIntoTopology(LeafMappedTree topology, Map<Set<String>, TreeNode> treeToMergeAsLeafMap, Tree treeToMerge) { Map<Set<String>, TreeNode> toplogyAsLeafMap = topology.treeAsLeafMap; for (Map.Entry<Set<String>, TreeNode> entry : toplogyAsLeafMap.entrySet()) { TreeNode topologyNode = entry.getValue(); TreeNode treeToMergeNode = treeToMergeAsLeafMap.get(entry.getKey()); Edge topologyEdgeToParent = topologyNode.getEdgeToParent(); topologyEdgeToParent.setWeight(topologyEdgeToParent.getWeight() + treeToMergeNode.getEdgeToParent().getWeight()); checkForChilds(topologyNode, treeToMergeNode); } //check for root children checkForChilds(topology.tree.getRoot(), treeToMerge.getRoot()); }
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; }
/** * 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); } }
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); } } }); }
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 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; }