/** * Returns true if this Partition contains the TreeNode. * * @param node The TreeNode * @return true if the Key is in the Partition */ public boolean contains(TreeNode node) { return this.leavesMap.containsKey(node.getLabel()); }
/** * Returns the TreeNode of the Key if it is in this Partition. * If not it returns null. * * @param key The Key of the TreeNode * @return The TreeNode */ public TreeNode getNode(Object key) { return (TreeNode) leavesMap.get(((TreeNode) key).getLabel()); }
public int compare(TreeNode o1, TreeNode o2) { String l1 = o1.getLabel(); String l2 = o2.getLabel(); if (l1 == null) { TreeNode[] leaves = o1.getLeaves(); l1 = leaves[leaves.length - 1].getLabel(); } if (l2 == null) { TreeNode[] leaves = o2.getLeaves(); l2 = leaves[leaves.length - 1].getLabel(); } return l1.compareTo(l2); }
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; }
public TreePartition(TreeNode[] leafes, Collection<String> allTaxaInTree) { groupA = new HashSet<>(); for (TreeNode leaf : leafes) { groupA.add(leaf.getLabel()); } groupB = new HashSet<>(allTaxaInTree); groupB.removeAll(groupA); // union = Sets.union(groupB,groupA); hash = calculateHash(); }
private int getTaxaNumber() { Set<String> taxa = new HashSet<String>(); for (Tree tree : trees) { for (TreeNode leaf : tree.getLeaves()) { taxa.add(leaf.getLabel()); } } return taxa.size(); }
public static void removeLeafesAndPruneTree(Tree tree, Collection<String> toRemove) { Set<TreeNode> toKeep = new HashSet<TreeNode>(); for (TreeNode leaf : tree.getLeaves()) { if (!toRemove.contains(leaf.getLabel())) toKeep.add(leaf); } keepLeafesAndPruneTree(tree, toKeep); }
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; } }
protected TreeNode find(String label, Tree tree) { if (label == null) return null; for (TreeNode v : tree.vertices()) { if (v.isLeaf() && label.equals(v.getLabel())) return v; } return null; }
public static void addLeafesFromChildren(final Map<Set<String>, TreeNode> childrenSets, final TreeNode current, final Set<String> parentTaxaSet, final boolean setEdgeweightsToZero) { if (setEdgeweightsToZero) current.getEdgeToParent().setWeight(0d); if (current.isLeaf()) { parentTaxaSet.add(current.getLabel()); } else { Set<String> currentTaxaSet = new HashSet<String>(); for (TreeNode child : current.children()) { addLeafesFromChildren(childrenSets, child, currentTaxaSet, setEdgeweightsToZero); } parentTaxaSet.addAll(currentTaxaSet); childrenSets.put(currentTaxaSet, current); } }
public static void keepLeafesAndPruneTree(Tree tree, Collection<TreeNode> toKeep) { boolean deleted = true; while (deleted) { deleted = false; for (TreeNode node : tree.getLeaves()) { if (node.getLabel() == null || !toKeep.contains(node)) { tree.removeVertex(node); deleted = true; } } } pruneDegreeOneNodes(tree); }
public void addTree(Tree tree) { String name = tree.getName(); this.treeList.add(tree); for (TreeNode treeNode : tree.vertices()) { if (!addInternaleTaxa && treeNode.isLeaf()) taxa.addTaxon(treeNode.getLabel()); } String nn = taxa.translate(name); if (nn == null) { nn = "tree_" + treeList.size(); } this.trees.put(nn, tree); }
/** * Clones this node. * * @return node the new node */ public TreeNode cloneNode() { TreeNode n = new TreeNode(); n.setLabel(getLabel()); n.setIndex(getIndex()); return n; }
private static void moveUP(Edge<TreeNode> edgeForLabelMove, String label) { String newLabel = edgeForLabelMove.getSource().getLabel(); edgeForLabelMove.getSource().setLabel(label); if (newLabel != null) moveUP(edgeForLabelMove.getSource().getEdgeToParent(), newLabel); }
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; }
private static void stepUp(Tree tree, TreeNode child, TreeNode parent, String oldChildLabel) { TreeNode newParent = parent.getParent(); String newOldLabel = parent.getLabel(); double weight = child.getDistanceToParent(); tree.removeEdge(parent, child); tree.addEdge(child, parent).setWeight(weight); parent.setLabel(oldChildLabel); if (newParent != null) { stepUp(tree, parent, newParent, newOldLabel); } }