use of beast.evolution.tree.coalescent.ConstantPopulation 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]) + ")");
}
}
use of beast.evolution.tree.coalescent.ConstantPopulation in project beast2 by CompEvol.
the class RandomTreeTest method testCoalescentTimes.
@Test
public void testCoalescentTimes() throws Exception {
Randomizer.setSeed(53);
int Nleaves = 10;
int Niter = 5000;
// (Serially sampled) coalescent time means and variances
// estimated from 50000 trees simulated using MASTER
double[] coalTimeMeansTruth = { 1.754662, 2.833337, 3.843532, 4.850805, 5.849542, 6.847016, 7.8482, 8.855137, 10.15442 };
double[] coalTimeVarsTruth = { 0.2751625, 0.2727121, 0.2685172, 0.2705117, 0.2678611, 0.2671793, 0.2686952, 0.2828477, 1.076874 };
// Assemble BEASTObjects needed by RandomTree
StringBuilder traitSB = new StringBuilder();
List<Sequence> seqList = new ArrayList<Sequence>();
for (int i = 0; i < Nleaves; i++) {
String taxonID = "t " + i;
seqList.add(new Sequence(taxonID, "?"));
if (i > 0)
traitSB.append(",");
traitSB.append(taxonID).append("=").append(i);
}
Alignment alignment = new Alignment(seqList, "nucleotide");
TaxonSet taxonSet = new TaxonSet(alignment);
TraitSet timeTrait = new TraitSet();
timeTrait.initByName("traitname", "date-backward", "taxa", taxonSet, "value", traitSB.toString());
ConstantPopulation popFunc = new ConstantPopulation();
popFunc.initByName("popSize", new RealParameter("1.0"));
// Create RandomTree and TreeInterval instances
RandomTree tree = new RandomTree();
TreeIntervals intervals = new TreeIntervals();
// Estimate coalescence time moments
double[] coalTimeMeans = new double[Nleaves - 1];
double[] coalTimeVars = new double[Nleaves - 1];
double[] coalTimes = new double[Nleaves - 1];
for (int i = 0; i < Niter; i++) {
tree.initByName("taxa", alignment, "populationModel", popFunc, "trait", timeTrait);
intervals.initByName("tree", tree);
intervals.getCoalescentTimes(coalTimes);
for (int j = 0; j < Nleaves - 1; j++) {
coalTimeMeans[j] += coalTimes[j];
coalTimeVars[j] += coalTimes[j] * coalTimes[j];
}
}
// Normalise means and variances
for (int j = 0; j < Nleaves - 1; j++) {
coalTimeMeans[j] /= Niter;
coalTimeVars[j] /= Niter;
coalTimeVars[j] -= coalTimeMeans[j] * coalTimeMeans[j];
}
// Test means and variances against independently estimated values
for (int j = 0; j < Nleaves - 1; j++) {
assert (relError(coalTimeMeans[j], coalTimeMeansTruth[j]) < 5e-3);
assert (relError(coalTimeVars[j], coalTimeVarsTruth[j]) < 5e-2);
}
}
use of beast.evolution.tree.coalescent.ConstantPopulation 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);
}
use of beast.evolution.tree.coalescent.ConstantPopulation in project beast2 by CompEvol.
the class ConstantPopulationInputEditor method init.
@Override
public void init(Input<?> input, BEASTInterface beastObject, int itemNr, ExpandOption isExpandOption, boolean addButtons) {
ConstantPopulation population = (ConstantPopulation) input.get();
try {
InputEditor editor = doc.inputEditorFactory.createInputEditor(population.popSizeParameter, population, doc);
add(editor.getComponent());
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
// super.init(input, beastObject, itemNr, isExpandOption, addButtons);
}
use of beast.evolution.tree.coalescent.ConstantPopulation in project bacter by tgvaughan.
the class ACGLikelihoodTest method testLikelihoodCaching.
@Test
public void testLikelihoodCaching() throws Exception {
ConstantPopulation popFunc = new ConstantPopulation();
popFunc.initByName("popSize", new RealParameter("1.0"));
Locus locus = new Locus("locus", 10000);
TaxonSet taxonSet = getTaxonSet(10);
ConversionGraph acg = new SimulatedACG();
acg.initByName("rho", 5.0 / locus.getSiteCount(), "delta", 1000.0, "populationModel", popFunc, "locus", locus, "taxonset", taxonSet);
State state = new State();
state.initByName("stateNode", acg);
state.initialise();
System.out.println(acg);
// Site model:
JukesCantor jc = new JukesCantor();
jc.initByName();
SiteModel siteModel = new SiteModel();
siteModel.initByName("mutationRate", new RealParameter("1"), "substModel", jc);
// Simulate alignment:
SimulatedAlignment alignment = new SimulatedAlignment();
alignment.initByName("acg", acg, "siteModel", siteModel, "outputFileName", "simulated_alignment.nexus", "useNexus", true);
// Calculate likelihood 1:
ACGLikelihood argLikelihood = new ACGLikelihood();
argLikelihood.initByName("locus", locus, "data", alignment, "tree", acg, "siteModel", siteModel);
ACGLikelihoodSlow argLikelihoodSlow = new ACGLikelihoodSlow();
argLikelihoodSlow.initByName("locus", locus, "data", alignment, "tree", acg, "siteModel", siteModel);
double logP1 = argLikelihood.calculateLogP();
double logP1prime = argLikelihoodSlow.calculateLogP();
double relError = 2.0 * Math.abs(logP1 - logP1prime) / Math.abs(logP1 + logP1prime);
System.out.format("logP=%g\nlogPprime=%g\nrelError=%g\n", logP1, logP1prime, relError);
assertTrue(relError < 1e-13);
// Add a single recombination event
Node node1 = acg.getExternalNodes().get(0);
Node node2 = node1.getParent();
double height1 = 0.5 * (node1.getHeight() + node1.getParent().getHeight());
double height2 = 0.5 * (node2.getHeight() + node2.getParent().getHeight());
int startLocus = 500;
int endLocus = 600;
Conversion recomb1 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
acg.addConversion(recomb1);
double logP2 = argLikelihood.calculateLogP();
double logP2prime = argLikelihoodSlow.calculateLogP();
relError = 2.0 * Math.abs(logP2 - logP2prime) / Math.abs(logP2 + logP2prime);
System.out.format("logP=%g\nlogPprime=%g\nrelError=%g\n", logP2, logP2prime, relError);
assertTrue(relError < 1e-13);
state.restore();
double logP3 = argLikelihood.calculateLogP();
double logP3prime = argLikelihoodSlow.calculateLogP();
relError = 2.0 * Math.abs(logP3 - logP3prime) / Math.abs(logP3 + logP3prime);
System.out.format("logP=%g\nlogPprime=%g\nrelError=%g\n", logP3, logP3prime, relError);
assertTrue(relError < 1e-13);
}
Aggregations