Search in sources :

Example 21 with Tree

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

the class TreeTransitionMarker method run.

@Override
public void run() throws Exception {
    tag = tagInput.get();
    from = fromInput.get();
    to = toInput.get();
    markAll = markAllInput.get();
    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();
        out.println();
        out.print("tree STATE_" + i + " = ");
        mark(tree.getRoot());
        final String newick = tree.getRoot().toSortedNewick(new int[1], true);
        out.print(newick);
        out.print(";");
        i++;
    }
    out.println();
    out.println("end;");
}
Also used : PrintStream(java.io.PrintStream) TreeAnnotator(beast.app.treeannotator.TreeAnnotator) Tree(beast.evolution.tree.Tree) FastTreeSet(beast.app.treeannotator.TreeAnnotator.FastTreeSet)

Example 22 with Tree

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

the class BinnedMeanTree method calcFrechetMean.

private Tree calcFrechetMean(Tree[] trees, int start, int end) {
    if (trees.length == 0) {
        throw new IllegalArgumentException("expected at least one tree");
    }
    Tree tree = trees[start];
    RNNIMetric metric = new RNNIMetric();
    boolean progress = true;
    int i = start;
    // while (progress && i < trees.length) {
    while (i < end) {
        double d = metric.distance(tree, trees[i]);
        progress = ((int) d / (i + 1)) > 0;
        if (progress) {
            tree = metric.pathelement(tree, trees[i], (int) d / (i + 1));
        }
        i++;
    }
    return tree;
}
Also used : Tree(beast.evolution.tree.Tree) RNNIMetric(beast.evolution.tree.RNNIMetric)

Example 23 with Tree

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

the class FrechetMeanCentroid method run.

@Override
public void run() throws Exception {
    long start = System.currentTimeMillis();
    mode = modeInput.get();
    Tree[] trees = getTrees(treeFileInput.get(), burnInPercentageInput.get());
    // if focal tree file is specified, print sum of square RNNI distances and quite
    if (focalTreeFileInput.get() != null && !focalTreeFileInput.get().getName().equals("[[none]]")) {
        Tree[] focalTrees = getTrees(focalTreeFileInput.get(), 0);
        for (Tree tree : focalTrees) {
            double sos = sumOfSquaredDistances(trees, tree);
            Log.info(tree.getRoot().toNewick());
            Log.info("SoS = " + sos);
        }
        Log.warning("Done");
        return;
    }
    Tree centroid = calcCentroid(trees);
    if (randomizationAttemptsInput.get() > 1) {
        double bestSoS = sumOfSquaredDistances(trees, centroid);
        int onepercent = 1 + randomizationAttemptsInput.get() / 100;
        int tenpercent = 1 + randomizationAttemptsInput.get() / 10;
        for (int i = 1; i < randomizationAttemptsInput.get(); i++) {
            randomise(trees);
            Tree newCentroid = calcCentroid(trees);
            double sos = sumOfSquaredDistances(trees, newCentroid);
            if (sos < bestSoS) {
                bestSoS = sos;
                centroid = newCentroid;
                Log.warning(i + ": " + sos);
            }
            if (i % onepercent == 0) {
                if (i % tenpercent == 0) {
                    System.err.print("|");
                } else {
                    System.err.print(".");
                }
            }
        }
    }
    Log.info(centroid.getRoot().toNewick());
    Log.warning("SoS = " + sumOfSquaredDistances(trees, centroid));
    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());
    }
    centroid.init(out);
    out.println();
    centroid.log(0l, out);
    out.println();
    centroid.close(out);
    long end = System.currentTimeMillis();
    Log.warning("Done " + mode + " in " + (end - start) / 1000 + " seconds");
}
Also used : PrintStream(java.io.PrintStream) Tree(beast.evolution.tree.Tree)

Example 24 with Tree

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

the class FrechetMeanCentroid method getTrees.

private Tree[] getTrees(TreeFile treeFile, int burnInPercentage) {
    try {
        FastTreeSet srcTreeSet = new TreeAnnotator().new FastTreeSet(treeFile.getAbsolutePath(), burnInPercentage);
        srcTreeSet.reset();
        int n = 0;
        while (srcTreeSet.hasNext()) {
            srcTreeSet.next();
            n++;
        }
        Tree[] trees = new Tree[n];
        srcTreeSet.reset();
        n = 0;
        while (srcTreeSet.hasNext()) {
            trees[n] = srcTreeSet.next();
            n++;
        }
        return trees;
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException(e.getMessage());
    }
}
Also used : TreeAnnotator(beast.app.treeannotator.TreeAnnotator) Tree(beast.evolution.tree.Tree) IOException(java.io.IOException) FastTreeSet(beast.app.treeannotator.TreeAnnotator.FastTreeSet)

Example 25 with Tree

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

the class FrechetMeanTree method initAndValidate.

@Override
public void initAndValidate() {
    super.initAndValidate();
    if (treeFileInput.get() != null && !treeFileInput.get().getName().equals("[[none]]")) {
        try {
            FastTreeSet srcTreeSet = new TreeAnnotator().new FastTreeSet(treeFileInput.get().getAbsolutePath(), burnInPercentageInput.get());
            srcTreeSet.reset();
            int n = 0;
            while (srcTreeSet.hasNext()) {
                srcTreeSet.next();
                n++;
            }
            Tree[] trees = new Tree[n];
            srcTreeSet.reset();
            n = 0;
            while (srcTreeSet.hasNext()) {
                trees[n] = srcTreeSet.next();
                n++;
            }
            calcFrechetMean(trees);
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException(e.getMessage());
        }
    }
}
Also used : TreeAnnotator(beast.app.treeannotator.TreeAnnotator) Tree(beast.evolution.tree.Tree) IOException(java.io.IOException) 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