/** * This method returns the LCA of a list of nodes. * * @param nodes of nodes * @return least common ancestor */ public TreeNode findLeastCommonAncestor(TreeNode... nodes) { List<TreeNode> na = new ArrayList<TreeNode>(); for (TreeNode node : nodes) { na.add(node); } return findLeastCommonAncestor(na); }
/** * This method returns the LCA of a list of nodes. * * @param nodes of nodes * @return least common ancestor */ public TreeNode findLeastCommonAncestor(List<TreeNode> nodes) { if (nodes == null) return null; if (nodes.size() == 0) return null; if (nodes.size() == 1) return nodes.get(0); TreeNode retVal = findLeastCommonAncestor(nodes.get(0), nodes.get(1)); for (int i = 2; i < nodes.size(); i++) { retVal = findLeastCommonAncestor(retVal, nodes.get(i)); } return (retVal); }
/** * Create an array that contains the pairwise least common ancestor nodes for all leaves in the given tree. * The array is indexed by the indices implied by {@link Tree#getLeaves()} * * @param tree the tree * @return map the lca map */ public static TreeNode[][] lcaMap(Tree tree) { final TreeNode[] leaves = tree.getLeaves(); TreeNode[][] map = new TreeNode[leaves.length][leaves.length]; for (int i = 0; i < leaves.length; i++) { TreeNode n1 = leaves[i]; map[i][i] = n1; for (int j = i; j < leaves.length; j++) { TreeNode n2 = leaves[j]; final TreeNode lca = tree.findLeastCommonAncestor(n1, n2); map[i][j] = lca; map[j][i] = lca; } } return map; }
TreeNode treeNode = tree.findLeastCommonAncestor(aNode, bNode);
public static double claculateMinumCharaterDeletionCosts(Tree tree, List<Tree> sourceTrees) { Set<TreeNode> deletedNodes = new HashSet<TreeNode>(); for (Tree sourceTree : sourceTrees) { for (TreeNode treeNode : sourceTree.vertices()) { if (treeNode.isInnerNode() && treeNode != sourceTree.getRoot()) { Set<String> sourceLeafLabels = getLeafLabels(treeNode); Set<TreeNode> superLeafes = getLeafesFromLabels(sourceLeafLabels, tree); TreeNode lca = tree.findLeastCommonAncestor(new ArrayList<TreeNode>(superLeafes)); Set<String> superLeafLabels = getLeafLabels(lca); if (!sourceLeafLabels.equals(superLeafLabels)) { deletedNodes.add(treeNode); } } } } double costs = 0; for (TreeNode deletedNode : deletedNodes) { costs += deletedNode.getDistanceToParent(); } return costs; }
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; }
TreeNode treeNode = tree.findLeastCommonAncestor(aNode, bNode);
root = tree.findLeastCommonAncestor(nds); } else { for (TreeNode v : tree.vertices()) {
if (Thread.interrupted()) return -1; TreeNode third = leaves1[j]; int ab1 = tree1.findLeastCommonAncestor(first, second).getLevel(); int bc1 = tree1.findLeastCommonAncestor(second, third).getLevel(); int ac1 = tree1.findLeastCommonAncestor(first, third).getLevel(); int ab2 = tree2.findLeastCommonAncestor(first2, second2).getLevel(); int bc2 = tree2.findLeastCommonAncestor(second2, third2).getLevel(); int ac2 = tree2.findLeastCommonAncestor(first2, third2).getLevel();
TreeNode n = t.findLeastCommonAncestor(first, second); int levelfirstsecond = n.getLevel(); TreeNode d = t.findLeastCommonAncestor(second, third); int levelsecondthird = d.getLevel(); TreeNode y = t.findLeastCommonAncestor(first, third); int levelfirstthird = y.getLevel();
ac1 = lca1[i][j].getLevel(); } else { ab1 = tree1.findLeastCommonAncestor(first, second).getLevel(); bc1 = tree1.findLeastCommonAncestor(second, third).getLevel(); ac1 = tree1.findLeastCommonAncestor(first, third).getLevel(); ac2 = lca2[index_first][index_third].getLevel(); } else { ab2 = tree2.findLeastCommonAncestor(first2, second2).getLevel(); bc2 = tree2.findLeastCommonAncestor(second2, third2).getLevel(); ac2 = tree2.findLeastCommonAncestor(first2, third2).getLevel();
ac1 = lca1[i][j].getLevel(); } else { ab1 = tree1.findLeastCommonAncestor(first, second).getLevel(); bc1 = tree1.findLeastCommonAncestor(second, third).getLevel(); ac1 = tree1.findLeastCommonAncestor(first, third).getLevel(); ac2 = lca2[index_first][index_third].getLevel(); } else { ab2 = tree2.findLeastCommonAncestor(first2, second2).getLevel(); bc2 = tree2.findLeastCommonAncestor(second2, third2).getLevel(); ac2 = tree2.findLeastCommonAncestor(first2, third2).getLevel();
nodes.add(rootedModel.getVertex(s)); Edge<TreeNode> edgeToReroot = rootedModel.findLeastCommonAncestor(nodes).getEdgeToParent(); TreeUtils.reorderingBootstrapLabelsForRooting(edgeToReroot); TreeUtils.rerootToOutgroup(rootedModel, edgeToReroot.getTarget());
siblingLeaves.add(labelToNode.get(leaf)); TreeNode lca = t.findLeastCommonAncestor(siblingLeaves); TreeNode lcaParent = lca.getParent();
dummyAncestor = model.findLeastCommonAncestor(n, erster);
if (n != null) lcaNodes.add(n); TreeNode lca = tree.findLeastCommonAncestor(lcaNodes.toArray(new TreeNode[lcaNodes.size()])); TreeNode parent = lca2added.get(singleTaxon.lca.toString());
if (a.getGraph() != t || b.getGraph() != t) return; TreeNode ancestor = t.findLeastCommonAncestor(a, b);
public DistanceMatrix(Tree tree) { TreeNode[] leafes = tree.getLeaves(); matrix = new double[leafes.length][leafes.length]; for (int i = 0; i < leafes.length; i++) { leafToIndex.put(leafes[i].getLabel(), i); for (int j = 0; j < leafes.length; j++) { if (i != j) { TreeNode lca = tree.findLeastCommonAncestor(leafes[i], leafes[j]); //BigDecimal weight = new BigDecimal(leafes[i].getDistanceToParent()); double weight = leafes[i].getDistanceToParent(); TreeNode parent = leafes[i].getParent(); while (parent != lca) { //weight = weight.add(new BigDecimal(parent.getDistanceToParent())); weight = weight + parent.getDistanceToParent(); parent = parent.getParent(); } //weight = weight.add(new BigDecimal(leafes[j].getDistanceToParent())); weight = weight + leafes[j].getDistanceToParent(); parent = leafes[j].getParent(); while (parent != lca) { //weight = weight.add(new BigDecimal(parent.getDistanceToParent())); weight = weight + parent.getDistanceToParent(); parent = parent.getParent(); } matrix[i][j] = weight;//.doubleValue();//weight.multiply(new BigDecimal(ACCURACY)).doubleValue(); matrix[j][i] = matrix[i][j]; } } } }
for (int j = i + 1; j < innerNodes.size(); j++) { TreeNode leaf2 = innerNodes.get(j); TreeNode ancestor = tree.findLeastCommonAncestor(leaf1, leaf2); double distanceToAncestor1 = 0d; TreeNode parent = leaf1;
for (int j = i + 1; j < leafes.length; j++) { TreeNode leaf2 = leafes[j]; TreeNode ancestor = tree.findLeastCommonAncestor(leaf1, leaf2); double distanceToAncestor1 = leaf1.getDistanceToParent(); TreeNode parent = leaf1.getParent();