/** * Adds a node to the tree and refreshes its vertex. this is useful to add vertices that were in another tree before. */ public int addVertex(TreeNode n, final boolean refreshIndex) { if (refreshIndex) n.setIndex(-1); return addVertex(n); }
/** * 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; }
/** * Inserts node to specific position. Shifts any subsequent nodes to the right. * * @param tree the tree * @param i the position */ private static void insertNode(Tree tree, TreeNode parent, TreeNode node, int i) { if (i < 0) { throw new IndexOutOfBoundsException("Index is smaller than 0"); } ArrayList<TreeNode> children = parent.getChildren(); children.add(i, node); tree.addVertex(node); for (TreeNode child : parent.getChildren()) { tree.removeEdge(parent, child); } for (TreeNode child : children) { tree.addEdge(parent, child); } }
private Tree addCompatibleCladesToBackboneTree(Tree backboneTree, Collection<Bipartition> partitionsToInsert) { for (Bipartition partition : partitionsToInsert) { Set<TreeNode> leafes = new HashSet<>(); for (int i = partition.partition.nextSetBit(0); i >= 0; i = partition.partition.nextSetBit(i + 1)) { leafes.add(indexToLeaf1.get(i)); } TreeNode lca = backboneTree.findLeastCommonAncestor(new ArrayList<>(leafes)); TreeNode nuLca = new TreeNode(); backboneTree.addVertex(nuLca); for (TreeNode child : lca.getChildren()) { TreeNode leaf; if (child.isInnerNode()) { leaf = child.depthFirstIterator().iterator().next(); } else { leaf = child; } if (leafes.contains(leaf)) { backboneTree.removeEdge(lca, child); backboneTree.addEdge(nuLca, child); } } backboneTree.addEdge(lca, nuLca); } return backboneTree; }
if (lcaParent != null) { //check if we have to insert a new root TreeNode nuLcaParent = new TreeNode(); t.addVertex(nuLcaParent); t.addEdge(nuLcaParent, lca); t.addEdge(lcaParent, nuLcaParent); t.addVertex(nuLcaParent); t.addEdge(nuLcaParent, lca); t.setRoot(nuLcaParent); t.addVertex(nuNode); t.addEdge(lca, nuNode); t.addVertex(node); if (node.isLeaf()) { labelToNode.put(node.getLabel(), node);
public Tree buildTree() { TIntObjectMap<TreeNode> nodeMap = new TIntObjectHashMap<>(); Tree tree = new Tree(); Iterator<Edge> it = supertreeEdges.iterator(); while (it.hasNext()) { Edge edge = it.next(); nodeMap.put(edge.treeNode, new TreeNode(edge.treeNodeLabel)); tree.addVertex(nodeMap.get(edge.treeNode)); if (edge.parentNode == 0) { tree.setRoot(nodeMap.get(edge.treeNode)); it.remove(); } } for (Edge edge : supertreeEdges) { tree.addEdge(nodeMap.get(edge.parentNode), nodeMap.get(edge.treeNode)); } tree.setName(String.valueOf(currentscore)); return tree; }
if (count >= majorityThreshold) { TreeNode n = node.getNode(result, ((double) count / (double) this.input.length) * 100d); result.addVertex(n); addedNodes.add(node);
/** * Add subtree to existing tree, recursive iteration over the subtree to add * * @param tree the target tree * @param parent the parent node * @param newNode the current node to be added to the parent * @return node added node */ private TreeNode addSubtree(Tree tree, TreeNode parent, TreeNode newNode) { TreeNode nn = newNode.cloneNode(); nn.setIndex(-1); tree.addVertex(nn); tree.addEdge(parent, nn); for (TreeNode child : newNode.children()) { addSubtree(tree, nn, child); } return nn; }
result.addVertex(newNode); result.addEdge(node, newNode); result.addVertex(newNode); result.addEdge(node, newNode); Iterator it = p[i].getLeavesIterator(); result.addVertex(l); result.addEdge(newNode, l); result.addVertex(newNode); result.addEdge(node, newNode); stepDownAndHangIn(newNode, getCommonAncestor(p[i], trees));
TreeNode oldRoot = tree.getRoot(); TreeNode root = new TreeNode(); tree.addVertex(root); tree.addEdge(root, oldRoot); tree.setRoot(root); if (!s.equals(node)) { TreeNode nn = new TreeNode(s); tree.addVertex(nn); tree.addEdge(root, nn); tree.addVertex(np); tree.addEdge(p, np); tree.addVertex(nn); tree.addEdge(np, nn); tree.addVertex(root); tree.setRoot(root); for (String s : merged) { TreeNode nn = new TreeNode(s); tree.addVertex(nn); tree.addEdge(root, nn); TreeNode old = new TreeNode(root.getLabel()); root.setLabel(null); tree.addVertex(old); tree.addEdge(root, old);
@SuppressWarnings("unchecked") private void hangIn(TreeNode newParent, TreeNode oldParent, Tree tree) { for (TreeNode n : oldParent.children()) { TreeNode newNode = n.cloneNode(); newNode.setIndex(-1); tree.addVertex(newNode); Edge edge = tree.addEdge(newParent, newNode); edge.setWeight(n.getDistanceToParent()); hangIn(newNode, n, tree); } }
/** * Create a new {@link Tree} that represents the subtree of this tree rooted at the given * node. The returned tree is a new tree that contains clones of the {@link TreeNode}s of this tree. * The node copies are created using {@link TreeNode#cloneNode()} and are * new instances, so you will not have object equality ( n != n.cloneNode() ). Node equality using * {@link TreeNode#equals(Object)} will work at least for labeled nodes (because the labels are compared), but * for performance reasons we do not do deep checks. * <p> * If this tree does not contain the given node, null is returned. * * @param n the root node of the subtree * @return subtree rooted at the given node or null */ public Tree getSubtree(TreeNode n) { if (n == null) throw new NullPointerException(); if (nodes.get(n.getIndex()) != n) { return null; } Tree r = new Tree(); r.setName(getName()); TreeNode root = n.cloneNode(); root.setIndex(-1); // reset index r.addVertex(root); r.setRoot(root); hangIn(root, n, r); return r; }
result.addVertex(cRoot); result.setRoot(cRoot);
tree.addVertex(currentRoot);
/** * Reroot tree with the new root placed at the incomming edge of the given outgroup * * @param tree the tree * @param outgroup the outgroup taxon */ public static void rerootToOutgroup(Tree tree, TreeNode outgroup, double edgeLengthToOutgroup) { TreeNode parent = outgroup.getParent(); if (parent == null) return; TreeNode newRoot = new TreeNode(); // add outgroup to new root tree.addVertex(newRoot); double oldWeight = tree.getEdge(parent, outgroup).getWeight(); tree.removeEdge(parent, outgroup); final Edge<TreeNode> edge = tree.addEdge(newRoot, outgroup); edge.setWeight(edgeLengthToOutgroup); // connect parent connectParent(tree, newRoot, parent, oldWeight - edgeLengthToOutgroup); tree.setRoot(newRoot); pruneDegreeOneNodes(tree); }