Search in sources :

Example 16 with Tree

use of beast.evolution.tree.Tree in project Babel by rbouckaert.

the class TreeESS method processTreeFile.

private void processTreeFile(TreeFile f, List<Double> traceRF, List<Double> traceRNNI, List<Double> traceNNI, List<Double> traceRankSwitches, List<List<Double>> cladeTraces) throws IOException {
    MemoryFriendlyTreeSet srcTreeSet = new MemoryFriendlyTreeSet(f.getPath(), burnInPercentageInput.get());
    srcTreeSet.reset();
    int[] map;
    // do we need to update the focal tree set?
    if (!(focalTreeInput.get() != null && !focalTreeInput.get().getName().equals("[[none]]"))) {
        if (srcInput.get().size() > 1) {
            Log.warning("Warning: to make traces comparible, it is recommended to use a single external focal tree for multiple tree sets");
        }
        focalTree = srcTreeSet.next();
        // sort(focalTree.getRoot());
        focalClades = new HashSet<>();
        addFocalClades(focalTree.getRoot());
        if (cladeSupportThresholdInput.get() < 0 && !(cladeSetInputInput.get() != null && !cladeSetInputInput.get().getName().equals("[[none]]"))) {
            focalCladeArray = focalClades.toArray(new BitSet[] {});
        }
        // probably not useful to add first tree after burn-in to
        // trace, since the RF distance will be 0
        // trace.add(0.0);
        map = new int[focalTree.getLeafNodeCount()];
        for (int i = 0; i < map.length; i++) {
            map[i] = i;
        }
    } else {
        // set up mapping of taxon names in this tree set to that of the focal tree
        Tree tree = srcTreeSet.next();
        map = new int[focalTree.getLeafNodeCount()];
        String[] focalTaxa = focalTree.getTaxaNames();
        for (int i = 0; i < map.length; i++) {
            String name = tree.getTaxaNames()[i];
            map[i] = indexOf(focalTaxa, name);
        }
        double[] distance = new double[1];
        RFDistance(tree.getRoot(), distance, map, cladeTraces);
        traceRF.add(distance[0]);
        Double[] d = RNNIDistance(focalTree, tree);
        traceRNNI.add(d[0]);
        traceNNI.add(d[1]);
        traceRankSwitches.add(d[0] - d[1]);
        if (logClades) {
            for (int i = 0; i < focalCladeArray.length; i++) {
                if (cladeTraces.get(i).size() != 1) {
                    cladeTraces.get(i).add(0.0);
                }
            }
        }
    }
    while (srcTreeSet.hasNext()) {
        Tree tree = srcTreeSet.next();
        double[] distance = new double[1];
        RFDistance(tree.getRoot(), distance, map, cladeTraces);
        traceRF.add(distance[0]);
        Double[] d = RNNIDistance(focalTree, tree);
        traceRNNI.add(d[0]);
        traceNNI.add(d[1]);
        traceRankSwitches.add(d[0] - d[1]);
        if (logClades) {
            int n = traceRF.size();
            for (int i = 0; i < focalCladeArray.length; i++) {
                if (cladeTraces.get(i).size() != n) {
                    cladeTraces.get(i).add(0.0);
                }
            }
        }
    }
    double ESSRF = beast.core.util.ESS.calcESS(traceRF);
    Log.info("ESS(" + srcInput.getName() + "-RF) = " + ESSRF);
    double ESSRNNI = beast.core.util.ESS.calcESS(traceRNNI);
    Log.info("ESS(" + srcInput.getName() + "-RNNI) = " + ESSRNNI);
    double ESSNNI = beast.core.util.ESS.calcESS(traceNNI);
    Log.info("ESS(" + srcInput.getName() + "-NNI) = " + ESSNNI);
    double ESSRankSwitches = beast.core.util.ESS.calcESS(traceRankSwitches);
    Log.info("ESS(" + srcInput.getName() + "-RankSwitches) = " + ESSRankSwitches);
    if (logClades) {
        double sum = 0;
        double min = Double.MAX_VALUE;
        int k = 0;
        Log.warning.print("Clade ESSs: ");
        for (int i = 0; i < cladeTraces.size(); i++) {
            double ESS = beast.core.util.ESS.calcESS(cladeTraces.get(i));
            Log.warning.print(" " + ESS);
            if (Double.isFinite(ESS)) {
                sum += ESS;
                k++;
                min = Math.min(min, ESS);
            }
        }
        Log.warning.print("\n");
        sum /= k;
        Log.info("mean clade ESS(" + srcInput.getName() + ") = " + sum);
        Log.info("minimum clade ESS(" + srcInput.getName() + ") = " + min);
        sum = 0;
        min = Double.MAX_VALUE;
        k = 0;
        for (int i = 0; i < cladeTraces.size(); i++) {
            double entropy = calcEntropy(cladeTraces.get(i));
            Log.warning.print(" " + entropy);
            if (Double.isFinite(entropy)) {
                sum += entropy;
                k++;
                min = Math.min(min, entropy);
            }
        }
        Log.warning.print("\n");
        sum /= k;
        Log.info("mean clade entropy(" + srcInput.getName() + ") = " + sum);
        Log.info("minimum clade entropy(" + srcInput.getName() + ") = " + min);
    }
}
Also used : MemoryFriendlyTreeSet(babel.tools.utils.MemoryFriendlyTreeSet) BitSet(java.util.BitSet) Tree(beast.evolution.tree.Tree)

Example 17 with Tree

use of beast.evolution.tree.Tree in project Babel by rbouckaert.

the class TreeMerger method run.

@Override
public void run() throws Exception {
    MemoryFriendlyTreeSet srcTreeSet = new TreeAnnotator().new MemoryFriendlyTreeSet(srcInput.get().getPath(), 0);
    srcTreeSet.reset();
    Tree tree = srcTreeSet.next();
    processCfgFile();
    PrintStream out = System.out;
    if (outputInput.get() != null && !outputInput.get().getName().equals("[[none]]")) {
        Log.warning("Writing to file " + outputInput.get().getName());
        out = new PrintStream(outputInput.get());
    }
    srcTreeSet.reset();
    int k = 0;
    while (srcTreeSet.hasNext()) {
        tree = srcTreeSet.next();
        for (int i = 0; i < subTreeCount; i++) {
            Node src = getMRCA(tree, subTaxonSets[i]);
            Node parent = src.getParent();
            if (subTreeSet[i].hasNext()) {
                Tree subTree = subTreeSet[i].next();
                Node replacement = getMRCA(subTree, subTaxonSets[i]);
                if (parent.getHeight() < replacement.getHeight()) {
                    squeezeToFit(parent, replacement);
                }
                boolean replaced = false;
                for (int j = 0; j < parent.getChildCount(); j++) {
                    if (parent.getChild(j) == src) {
                        src.getParent().setChild(j, replacement);
                        replacement.setParent(parent);
                        replaced = true;
                    }
                }
                if (!replaced) {
                    throw new RuntimeException("Something went wrong replacing node");
                }
            } else {
                throw new IllegalArgumentException("Tree sets are of different sizes: treeset " + i + " is smaler than source set");
            }
        }
        out.println(tree.getRoot().toNewick());
        k++;
        if (k % 100 == 0) {
            Log.err.print("|" + k + "|");
        } else if (k % 25 == 0) {
            Log.err.print(".");
        }
    }
    Log.err("Done!");
    out.close();
}
Also used : MemoryFriendlyTreeSet(beast.app.treeannotator.TreeAnnotator.MemoryFriendlyTreeSet) PrintStream(java.io.PrintStream) Node(beast.evolution.tree.Node) TreeAnnotator(beast.app.treeannotator.TreeAnnotator) Tree(beast.evolution.tree.Tree)

Example 18 with Tree

use of beast.evolution.tree.Tree in project Babel by rbouckaert.

the class TreeRelabeller method run.

@Override
public void run() throws Exception {
    // read label map
    Map<String, String> labelMap = new HashMap<>();
    BufferedReader fin = new BufferedReader(new FileReader(labelMapInput.get()));
    String s;
    while ((s = readLine(fin)) != null) {
        String[] strs = s.split("\t");
        if (strs.length >= 2) {
            labelMap.put(strs[0], strs[1]);
        }
    }
    fin.close();
    // open file for writing
    PrintStream out = System.out;
    if (outputInput.get() != null) {
        out = new PrintStream(outputInput.get());
        Log.warning("Writing to file " + outputInput.get().getPath());
    }
    FastTreeSet trees = new TreeAnnotator().new FastTreeSet(treesInput.get().getAbsolutePath(), 0);
    trees.reset();
    Tree tree = trees.next();
    relabel(tree.getRoot(), labelMap);
    tree.init(out);
    out.println();
    trees.reset();
    int i = 0;
    while (trees.hasNext()) {
        tree = trees.next();
        out.println();
        out.print("tree STATE_" + i + " = ");
        final String newick = tree.getRoot().toSortedNewick(new int[1], true);
        out.print(newick);
        out.print(";");
        i++;
    }
    out.println();
    out.println("end;");
    if (false) {
        fin = new BufferedReader(new FileReader(treesInput.get()));
        // read to first non-empty line within trees block
        String str = fin.readLine().trim();
        while (str != null && !str.toLowerCase().contains("translate")) {
            str = fin.readLine().trim();
        }
        while (!str.trim().equals(";")) {
            str = readLine(fin);
        }
        // process set of trees
        while ((str = readLine(fin)) != null) {
            out.println(str);
        }
    // trees.reset();
    // int i = 0;
    // while (trees.hasNext()) {
    // tree = trees.next();
    // tree.log(i, out);
    // i++;
    // }
    // out.println();
    }
    fin.close();
    if (out != System.out) {
        out.close();
    }
    Log.warning("All done. " + (outputInput.get() != null ? "Results in " + outputInput.get().getPath() : ""));
}
Also used : PrintStream(java.io.PrintStream) HashMap(java.util.HashMap) BufferedReader(java.io.BufferedReader) TreeAnnotator(beast.app.treeannotator.TreeAnnotator) Tree(beast.evolution.tree.Tree) FileReader(java.io.FileReader) FastTreeSet(beast.app.treeannotator.TreeAnnotator.FastTreeSet)

Example 19 with Tree

use of beast.evolution.tree.Tree in project Babel by rbouckaert.

the class TreeGrafter method run.

@Override
public void run() throws Exception {
    MemoryFriendlyTreeSet srcTreeSet = new TreeAnnotator().new MemoryFriendlyTreeSet(srcInput.get().getPath(), 0);
    srcTreeSet.reset();
    Tree tree = srcTreeSet.next();
    PrintStream out = System.out;
    if (outputInput.get() != null && !outputInput.get().getName().equals("[[none]]")) {
        Log.warning("Writing to file " + outputInput.get().getPath());
        out = new PrintStream(outputInput.get());
    }
    Set<String> taxa = new LinkedHashSet<>();
    for (String taxon : tree.getTaxaNames()) {
        taxa.add(taxon);
    }
    processCfgFile(taxa);
    srcTreeSet.reset();
    int n = taxonName.length;
    int k = 0;
    while (srcTreeSet.hasNext()) {
        tree = srcTreeSet.next();
        System.err.println("Processing tree " + (k++));
        for (int i = 0; i < n; i++) {
            Node src = getMRCA(tree, subTaxonSets[i]);
            Node parent = src.getParent();
            double len = src.getLength();
            // create intermediary node on branch
            double newHeight = src.getHeight() + Randomizer.nextDouble() * len;
            if (src.getHeight() + len > taxonHeight[i]) {
                while (newHeight <= taxonHeight[i]) {
                    newHeight = src.getHeight() + Randomizer.nextDouble() * len;
                }
            } else {
                newHeight = src.getHeight() + len;
            }
            Node newNode = new Node();
            newNode.setHeight(newHeight);
            newNode.setParent(parent);
            for (int j = 0; j < parent.getChildCount(); j++) {
                if (parent.getChild(j) == src) {
                    parent.setChild(j, newNode);
                }
            }
            newNode.addChild(src);
            src.setParent(newNode);
            // create new leaf node
            Node leaf = new Node();
            leaf.setID(taxonName[i]);
            Log.warning("Adding " + taxonName[i]);
            leaf.setHeight(taxonHeight[i]);
            newNode.addChild(leaf);
            leaf.setParent(newNode);
        }
        out.print(tree.getRoot().toNewick());
        out.println(";");
    }
    Log.err("Done!");
    out.close();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MemoryFriendlyTreeSet(beast.app.treeannotator.TreeAnnotator.MemoryFriendlyTreeSet) PrintStream(java.io.PrintStream) Node(beast.evolution.tree.Node) TreeAnnotator(beast.app.treeannotator.TreeAnnotator) Tree(beast.evolution.tree.Tree)

Example 20 with Tree

use of beast.evolution.tree.Tree in project Babel by rbouckaert.

the class TreeScalerApp method run.

@Override
public void run() throws Exception {
    double scaleFactor = scaleInput.get() == null ? 1.0 : scaleInput.get();
    if (heightInput.get() != null) {
        // calculate scale factor based on heightInput and mean tree height
        FastTreeSet trees = new TreeAnnotator().new FastTreeSet(treesInput.get().getAbsolutePath(), 0);
        trees.reset();
        double sum = 0;
        int n = 0;
        while (trees.hasNext()) {
            double h = trees.next().getRoot().getHeight();
            sum += h;
            n++;
        }
        double meanHeight = sum / n;
        scaleFactor = heightInput.get() / meanHeight;
    }
    // open file for writing
    PrintStream out = System.out;
    if (outputInput.get() != null) {
        out = new PrintStream(outputInput.get());
        Log.warning("Writing to file " + outputInput.get().getPath());
    }
    FastTreeSet trees = new TreeAnnotator().new FastTreeSet(treesInput.get().getAbsolutePath(), 0);
    trees.reset();
    Tree tree = trees.next();
    tree.init(out);
    out.println();
    trees.reset();
    int i = 0;
    while (trees.hasNext()) {
        tree = trees.next();
        scale(tree, scaleFactor);
        out.println();
        out.print("tree STATE_" + i + " = ");
        final String newick = tree.getRoot().toNewick(false);
        out.print(newick);
        out.print(";");
        i++;
    }
    out.println();
    out.println("end;");
    if (out != System.out) {
        out.close();
    }
    Log.warning("All done. " + (outputInput.get() != null ? "Results in " + outputInput.get().getPath() : ""));
}
Also used : PrintStream(java.io.PrintStream) TreeAnnotator(beast.app.treeannotator.TreeAnnotator) Tree(beast.evolution.tree.Tree) FastTreeSet(beast.app.treeannotator.TreeAnnotator.FastTreeSet)

Aggregations

Tree (beast.evolution.tree.Tree)206 Alignment (beast.evolution.alignment.Alignment)71 Test (org.junit.Test)62 SiteModel (beast.evolution.sitemodel.SiteModel)59 Node (beast.evolution.tree.Node)52 Frequencies (beast.evolution.substitutionmodel.Frequencies)47 HKY (beast.evolution.substitutionmodel.HKY)28 RealParameter (beast.core.parameter.RealParameter)27 PrintStream (java.io.PrintStream)27 TreeAnnotator (beast.app.treeannotator.TreeAnnotator)24 ArrayList (java.util.ArrayList)23 ExperimentalTreeLikelihood (beast.evolution.likelihood.ExperimentalTreeLikelihood)22 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)22 BeagleTreeLikelihood (beast.evolution.likelihood.BeagleTreeLikelihood)19 GeneralSubstitutionModel (beast.evolution.substitutionmodel.GeneralSubstitutionModel)17 UncertainAlignmentTest (test.beast.evolution.alignment.UncertainAlignmentTest)17 TaxonSet (beast.evolution.alignment.TaxonSet)16 ThreadedTreeLikelihood (beast.evolution.likelihood.ThreadedTreeLikelihood)15 RandomTree (beast.evolution.tree.RandomTree)14 ClusterTree (beast.util.ClusterTree)14