private static void setLabel(TreeNode n, StringBuffer label, StringBuffer nhxB) { if (nhxB.length() > 0) { String nhx = nhxB.substring(6); String[] tags = nhx.split(":"); HashMap<String, String> extLabel = new HashMap<String, String>(); for (int k = 0; k < tags.length; k++) { String[] nameValue = tags[k].split("="); extLabel.put(nameValue[0], nameValue[1]); } createTaxonInfo(n, extLabel); } String l = label.toString(); l = l.trim(); if (l.length() > 0) { n.setLabel(l); } }
/** * If not done allready, this creates a new TreeNode * representing this bipartiton. This is used to create * the consensus tree in the NConsensus Algorithm * * @param tree the Tree for the node * @return TreeNode the node */ public TreeNode getNode(Tree tree, double count) { if(node == null){ node = new TreeNode(); tree.addVertex(node); if(label != null) node.setLabel(label); else if (count >= 0d) node.setLabel(Double.toString(count)); } return node; }
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)); } } } }
/** * replaces all '_' with a space * * @param tree the tree */ public static void cleanLabels(Tree tree) { for (TreeNode n : tree.vertices()) { String label = n.getLabel(); if (label != null && label.length() > 0) { label = label.trim(); label = label.replaceAll("_", " "); n.setLabel(label); } } }
/** * 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 Tree addCladewiseSplitFit(List<WeightedTreePartitions> biparts, Tree tree) { Map<Set<String>, TreeNode> labelsToClade = getChildrenMap(tree); Set<String> all = getLeafLabels(tree); Map<WeightedTreePartitions, AtomicDouble> partitionsToConflicts = new HashMap<>(labelsToClade.size()); for (Set<String> leafesInClade : labelsToClade.keySet()) { partitionsToConflicts.put(new WeightedTreePartitions(leafesInClade, all), new AtomicDouble(0)); } double numberOfChars = 0; for (WeightedTreePartitions sourcePartition : biparts) { numberOfChars += sourcePartition.weight; for (Map.Entry<WeightedTreePartitions, AtomicDouble> e : partitionsToConflicts.entrySet()) { if (e.getKey().isIncompatible(sourcePartition)) { e.getValue().addAndGet(e.getKey().weight); } } } for (Map.Entry<WeightedTreePartitions, AtomicDouble> entry : partitionsToConflicts.entrySet()) { TreeNode first = labelsToClade.remove(entry.getKey().getGroupA()); TreeNode second = labelsToClade.remove(entry.getKey().getGroupB()); String l = String.valueOf(1 - (entry.getValue().doubleValue() / numberOfChars)); if (first != null) first.setLabel(l); if (second != null) second.setLabel(l); } return tree; }
/** * 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; }
/** * Clones this node. * * @return node the new node */ public TreeNode cloneNode() { TreeNode n = new TreeNode(); n.setLabel(getLabel()); n.setIndex(getIndex()); return n; }
/** * 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); } }
/** * Creates a copy of the given tree and renames all vertices according to the translation table. * If strict pattersn is set, all source names are translated to valid nexus taxon names. * * @param tree the source tree * @param translation the translation table * @param strictPatterns use only valid nexus taxon names * @return tree translated tree */ public static Tree renameNodes(Tree tree, Map<String, String> translation, boolean strictPatterns) { Tree t = tree.cloneTree(); for (TreeNode node : t.vertices()) { if (node.getLabel() != null) { String newName = null; if (strictPatterns) newName = translation.get(NexusTaxaBlock.translate(node.getLabel())); else { newName = translation.get(node.getLabel()); } if (newName != null) { node.setLabel(newName); } } } return t; }
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); }
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; }
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); } }
newNode.setLabel(n.getLabel());
root.setLabel(null); tree.addVertex(old); tree.addEdge(root, old);