Search in sources :

Example 1 with Coalescent

use of beast.evolution.tree.coalescent.Coalescent in project beast2 by CompEvol.

the class WilsonBaldingTest method topologyDistribution.

/**
 * Test topology distribution.
 * @throws Exception
 */
@Test
public void topologyDistribution() throws Exception {
    // Fix seed: will hopefully ensure success of test unless something
    // goes terribly wrong.
    Randomizer.setSeed(42);
    // Assemble model:
    ConstantPopulation constantPop = new ConstantPopulation();
    constantPop.initByName("popSize", new RealParameter("10000.0"));
    List<Object> alignmentInitArgs = new ArrayList<Object>();
    for (int i = 0; i < 4; i++) {
        Sequence thisSeq = new Sequence();
        thisSeq.initByName("taxon", String.valueOf(i), "value", "?");
        alignmentInitArgs.add("sequence");
        alignmentInitArgs.add(thisSeq);
    }
    Alignment alignment = new Alignment();
    alignment.initByName(alignmentInitArgs.toArray());
    Tree tree = new RandomTree();
    tree.initByName("taxa", alignment, "populationModel", constantPop);
    TreeIntervals treeIntervals = new TreeIntervals();
    treeIntervals.initByName("tree", tree);
    Coalescent coalescentDistrib = new Coalescent();
    coalescentDistrib.initByName("treeIntervals", treeIntervals, "populationModel", constantPop);
    // Set up state:
    State state = new State();
    state.initByName("stateNode", tree);
    // Set up operator:
    WilsonBalding wilsonBalding = new WilsonBalding();
    wilsonBalding.initByName("weight", "1", "tree", tree);
    // Set up logger:
    TreeReport treeReport = new TreeReport();
    treeReport.initByName("logEvery", "100", "burnin", "200000", "credibleSetPercentage", "95.0", "log", tree, "silent", true);
    // Set up MCMC:
    MCMC mcmc = new MCMC();
    mcmc.initByName("chainLength", "2000000", "state", state, "distribution", coalescentDistrib, "operator", wilsonBalding, "logger", treeReport);
    // Run MCMC:
    mcmc.run();
    // Obtain analysis results:
    TreeTraceAnalysis analysis = treeReport.getAnalysis();
    Map<String, Integer> topologyCounts = analysis.getTopologyCounts();
    int totalTreesUsed = analysis.getNTrees();
    // Test topology distribution against ideal:
    double tol = 0.005;
    for (int i = 0; i < topologies.length; i++) {
        double thisProb = topologyCounts.get(topologies[i]) / (double) totalTreesUsed;
        boolean withinTol = (thisProb > probs[i] - tol && thisProb < probs[i] + tol);
        Assert.assertTrue(withinTol);
        System.err.format("Topology %s rel. freq. %.3f", topologies[i], thisProb);
        if (withinTol)
            System.err.println(" (Within tolerance " + tol + " of " + String.valueOf(probs[i]) + ")");
        else
            System.err.println(" (FAILURE: outside tolerance " + tol + " of " + String.valueOf(probs[i]) + ")");
    }
}
Also used : ArrayList(java.util.ArrayList) RealParameter(beast.core.parameter.RealParameter) Sequence(beast.evolution.alignment.Sequence) TreeIntervals(beast.evolution.tree.coalescent.TreeIntervals) ConstantPopulation(beast.evolution.tree.coalescent.ConstantPopulation) Alignment(beast.evolution.alignment.Alignment) TreeTraceAnalysis(beast.evolution.tree.TreeTraceAnalysis) RandomTree(beast.evolution.tree.RandomTree) Tree(beast.evolution.tree.Tree) RandomTree(beast.evolution.tree.RandomTree) Coalescent(beast.evolution.tree.coalescent.Coalescent) WilsonBalding(beast.evolution.operators.WilsonBalding) Test(org.junit.Test)

Example 2 with Coalescent

use of beast.evolution.tree.coalescent.Coalescent in project beast2 by CompEvol.

the class CoalescentTest method testConstantPopulation.

public void testConstantPopulation() throws Exception {
    // *********** 3 taxon **********
    Tree tree = getTree(data, trees[0]);
    TreeIntervals treeIntervals = new TreeIntervals();
    treeIntervals.initByName("tree", tree);
    ConstantPopulation cp = new ConstantPopulation();
    cp.initByName("popSize", Double.toString(pop));
    Coalescent coal = new Coalescent();
    coal.initByName("treeIntervals", treeIntervals, "populationModel", cp);
    double logL = coal.calculateLogP();
    assertEquals(logL, -(4 / pop) - 2 * Math.log(pop), PRECISION);
// *********** 4 taxon **********
// tree = getTree(data, trees[1]);
// treeIntervals = new TreeIntervals();
// treeIntervals.initByName("tree", tree);
// 
// cp = new ConstantPopulation();
// cp.initByName("popSize", Double.toString(pop));
// 
// coal = new Coalescent();
// coal.initByName("treeIntervals", treeIntervals, "populationModel", cp);
// 
// logL = coal.calculateLogP();
// 
// assertEquals(logL, -(4 / pop) - 2 * Math.log(pop), PRECISION);
}
Also used : ConstantPopulation(beast.evolution.tree.coalescent.ConstantPopulation) Tree(beast.evolution.tree.Tree) Coalescent(beast.evolution.tree.coalescent.Coalescent) TreeIntervals(beast.evolution.tree.coalescent.TreeIntervals)

Aggregations

Tree (beast.evolution.tree.Tree)2 Coalescent (beast.evolution.tree.coalescent.Coalescent)2 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)2 TreeIntervals (beast.evolution.tree.coalescent.TreeIntervals)2 RealParameter (beast.core.parameter.RealParameter)1 Alignment (beast.evolution.alignment.Alignment)1 Sequence (beast.evolution.alignment.Sequence)1 WilsonBalding (beast.evolution.operators.WilsonBalding)1 RandomTree (beast.evolution.tree.RandomTree)1 TreeTraceAnalysis (beast.evolution.tree.TreeTraceAnalysis)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1