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); }
/** * 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; }
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))); } } } }
private void setNewTrees(List<Tree> trees) { //create Distance Matrices for all source this.trees = new ArrayList<Tree>(trees); //building global leafList Set<String> leafSet = new HashSet<String>(); for (Tree tree : this.trees) { for (TreeNode leaf : tree.getLeaves()) { leafSet.add(leaf.getLabel()); } } leafes = new ArrayList<String>(leafSet); reset(); matrixMap = new HashMap<Tree, DistanceMatrix>(trees.size()); for (Tree tree : this.trees) { matrixMap.put(tree, new DistanceMatrix(tree)); } }
private static int[] calculateNumberOfMissingClades(Tree calculated, Tree model) { int[] fnfp = new int[4]; Set<String> allTaxa = new HashSet<String>(); for (TreeNode leaf : calculated.getLeaves()) { allTaxa.add(leaf.getLabel()); } Set<TreePartition> modelParitions = getPartitions(model,allTaxa); fnfp[2] = modelParitions.size(); Set<TreePartition> calaculatedParitions = getPartitions(calculated,allTaxa); fnfp[3] = calaculatedParitions.size(); Set<TreePartition> tmp; tmp = new HashSet<TreePartition>(modelParitions); tmp.removeAll(calaculatedParitions); fnfp[0] = tmp.size(); tmp = new HashSet<TreePartition>(calaculatedParitions); tmp.removeAll(modelParitions); fnfp[1] = tmp.size(); return fnfp; }
public int mast() { List<String> labels = new ArrayList<String>(); for (Tree tree : trees) { for (TreeNode node : tree.getLeaves()) { if (!labels.contains(node.getLabel())) labels.add(node.getLabel()); } } int mst = Integer.MIN_VALUE; for (String a : labels) { for (String b : labels) { mst = Math.max(mast(a, b), mst); } } return mst; }
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); }
private static boolean mergeTrees(Map<Set<String>, TreeNode> mergedTreeAsMap, Tree mergedTree, Set<Map<Set<String>, TreeNode>> trees2AddAsMap, Map<Map<Set<String>, TreeNode>, Tree> trees2Add) { //merge edges to leaves for (Map<Set<String>, TreeNode> tree2AddAsMap : trees2AddAsMap) { Tree tree2Add = trees2Add.get(tree2AddAsMap); for (TreeNode leaf : mergedTree.getLeaves()) { Edge e = leaf.getEdgeToParent(); e.setWeight(tree2Add.getVertex(leaf.getLabel()).getDistanceToParent() + e.getWeight()); } } //merge inner edges Iterator<Set<String>> tMerge = mergedTreeAsMap.keySet().iterator(); while (tMerge.hasNext()) { Set<String> nodeMerge = tMerge.next(); for (Map<Set<String>, TreeNode> tree2AddAsMap : trees2AddAsMap) { Iterator<Set<String>> t2Add = tree2AddAsMap.keySet().iterator(); while (t2Add.hasNext()) { Set<String> node2Add = t2Add.next(); if (node2Add.equals(nodeMerge)) { double weight = tree2AddAsMap.get(node2Add).getDistanceToParent(); tree2AddAsMap.remove(node2Add); Edge edge = mergedTreeAsMap.get(nodeMerge).getEdgeToParent(); edge.setWeight(edge.getWeight() + weight); break; } } } } return true; }
for (TreeNode tn : tree.getRoot().depthFirstIterator()) { for (TreeNode t : tree.getLeaves()) { final String translated = NexusTaxaBlock.translate(t.getLabel()); matrix[taxaNames.indexOf(translated)][counter] = 2; for (TreeNode n : tree.getSubtree(tn).getLeaves()) { final String translated = NexusTaxaBlock.translate(n.getLabel()); matrix[taxaNames.indexOf(translated)][counter] = 1;
public void normalizMeanToReferenceMatrix() { if (referenceMatrix != null) { reset(); for (int i = 0; i < factors.length; i++) { Tree tree = trees.get(i); DistanceMatrix treematrix = matrixMap.get(tree); TreeNode[] leafes = tree.getLeaves(); LinkedList<Double> dist = new LinkedList<Double>(); for (int j = 0; j < leafes.length - 1; j++) { for (int k = j + 1; k < leafes.length; k++) { double d = referenceMatrix.getDistance(leafes[j].getLabel(), leafes[k].getLabel()); if (d != Double.POSITIVE_INFINITY) { dist.add(d / treematrix.getDistance(leafes[j].getLabel(), leafes[k].getLabel())); } } } factors[i] = computeAlphaTrimmedMean(dist, 0.25f); System.out.println("Fractor for tree number: " + i + " computed"); } } System.out.println("FINISH"); }
List<String> s = new ArrayList<String>(); Tree tree = trees[0]; for (TreeNode node : tree.getLeaves()) { if (node.getLabel() != null & node.getLabel().length() > 0) { String l = node.getLabel();
for (TreeNode tn : tree.getRoot().depthFirstIterator()) { for (TreeNode t : tree.getLeaves()) { final String translated = NexusTaxaBlock.translate(t.getLabel()); matrix[taxaNames.indexOf(translated)][counter] = 2; for (TreeNode n : tree.getSubtree(tn).getLeaves()) { final String translated = NexusTaxaBlock.translate(n.getLabel()); matrix[taxaNames.indexOf(translated)][counter] = 1;
private int reinsertSingleTaxa(Tree t) { Map<String, TreeNode> labelToNode = new THashMap<>(); for (TreeNode leaf : t.getLeaves()) { labelToNode.put(leaf.getLabel(), leaf);
for (TreeNode treeNode : tree.getLeaves()) { if (treeNode.getLabel() != null && treeNode.getLabel().length() > 0) { nodes.put(treeNode.getLabel(), treeNode);
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 (TreeNode leaf : tree.getLeaves()) { TreeNode inner = leaf.getParent(); if (inner.isInnerNode())
public static Tree rerootByLongestPathMidPoint(Tree tree) { TreeNode[] leafes = tree.getLeaves(); double pathlength = -1; double distToAnc1 = -1;