use of beast.evolution.likelihood.TreeLikelihood in project beast2 by CompEvol.
the class TreeLikelihoodTest method testGTRGILikelihood.
@Test
public void testGTRGILikelihood() throws Exception {
// Set up GTR model: 4 gamma categories, gamma shape = 0.5, prop invariant = 0.5
Alignment data = BEASTTestCase.getAlignment();
Tree tree = BEASTTestCase.getTree(data);
Frequencies freqs = new Frequencies();
freqs.initByName("data", data);
GeneralSubstitutionModel gsm = new GeneralSubstitutionModel();
gsm.initByName("rates", "1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0", "frequencies", freqs);
SiteModel siteModel = new SiteModel();
siteModel.initByName("mutationRate", "1.0", "gammaCategoryCount", 4, "shape", "0.5", "proportionInvariant", "0.5", "substModel", gsm);
TreeLikelihood likelihood = newTreeLikelihood();
likelihood.initByName("data", data, "tree", tree, "siteModel", siteModel);
double logP = 0;
logP = likelihood.calculateLogP();
assertEquals(logP, -1947.5829396144961, BEASTTestCase.PRECISION);
likelihood.initByName("useAmbiguities", false, "data", data, "tree", tree, "siteModel", siteModel);
logP = likelihood.calculateLogP();
assertEquals(logP, -1947.5829396144961, BEASTTestCase.PRECISION);
}
use of beast.evolution.likelihood.TreeLikelihood in project beast2 by CompEvol.
the class TreeLikelihoodTest method testGTRGLikelihood.
@Test
public void testGTRGLikelihood() throws Exception {
// Set up GTR model: 4 gamma categories, gamma shape = 0.5
Alignment data = BEASTTestCase.getAlignment();
Tree tree = BEASTTestCase.getTree(data);
Frequencies freqs = new Frequencies();
freqs.initByName("data", data);
GeneralSubstitutionModel gsm = new GeneralSubstitutionModel();
gsm.initByName("rates", "1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0", "frequencies", freqs);
SiteModel siteModel = new SiteModel();
siteModel.initByName("mutationRate", "1.0", "gammaCategoryCount", 4, "shape", "0.5", "substModel", gsm);
TreeLikelihood likelihood = newTreeLikelihood();
likelihood.initByName("data", data, "tree", tree, "siteModel", siteModel);
double logP = 0;
logP = likelihood.calculateLogP();
assertEquals(logP, -1949.0360143622, BEASTTestCase.PRECISION);
likelihood.initByName("useAmbiguities", false, "data", data, "tree", tree, "siteModel", siteModel);
logP = likelihood.calculateLogP();
assertEquals(logP, -1949.0360143622, BEASTTestCase.PRECISION);
}
use of beast.evolution.likelihood.TreeLikelihood in project MultiTypeTree by tgvaughan.
the class InitMigrationModelConnector method customConnector.
public static boolean customConnector(BeautiDoc doc) {
for (BEASTInterface p : doc.getPartitions("Tree")) {
TreeLikelihood treeLikelihood = (TreeLikelihood) p;
StructuredCoalescentMultiTypeTree tree = (StructuredCoalescentMultiTypeTree) treeLikelihood.treeInput.get();
String pID = BeautiDoc.parsePartition(tree.getID());
SCMigrationModel migModel = (SCMigrationModel) doc.pluginmap.get("migModel.t:" + pID);
SCMigrationModel migModelInit = (SCMigrationModel) doc.pluginmap.get("migModelInit.t:" + pID);
String rateMatrixStr = getParameterString((RealParameter) migModel.rateMatrixInput.get());
String popSizesStr = getParameterString((RealParameter) migModel.popSizesInput.get());
// Ensure model has appropriate number of demes
int uniqueTraitCount = uniqueTraitsInData(tree).size();
StringBuilder rateMatrixStrBuilder = new StringBuilder();
StringBuilder popSizesStrBuilder = new StringBuilder();
migModel.getTypeSet().initAndValidate();
if (migModel.popSizesInput.get().getDimension() != migModel.getNTypes()) {
for (int i = 0; i < migModel.getNTypes(); i++) {
popSizesStrBuilder.append(" 1.0");
for (int j = 0; j < migModel.getNTypes(); j++) {
if (j == i)
continue;
rateMatrixStrBuilder.append(" 1.0");
}
}
popSizesStr = popSizesStrBuilder.toString();
rateMatrixStr = rateMatrixStrBuilder.toString();
((RealParameter) migModel.popSizesInput.get()).setDimension(migModel.getNTypes());
((RealParameter) migModel.popSizesInput.get()).valuesInput.setValue(popSizesStr, (RealParameter) migModel.popSizesInput.get());
((RealParameter) migModel.rateMatrixInput.get()).setDimension(migModel.getNTypes() * (migModel.getNTypes() - 1));
((RealParameter) migModel.rateMatrixInput.get()).valuesInput.setValue(rateMatrixStr, (RealParameter) migModel.rateMatrixInput.get());
((RealParameter) migModel.popSizesInput.get()).initAndValidate();
((RealParameter) migModel.rateMatrixInput.get()).initAndValidate();
migModel.initAndValidate();
}
((RealParameter) migModelInit.popSizesInput.get()).setDimension(migModel.getNTypes());
((RealParameter) migModelInit.popSizesInput.get()).valuesInput.setValue(popSizesStr, (RealParameter) migModelInit.popSizesInput.get());
((RealParameter) migModelInit.rateMatrixInput.get()).setDimension(migModel.getNTypes() * (migModel.getNTypes() - 1));
((RealParameter) migModelInit.rateMatrixInput.get()).valuesInput.setValue(rateMatrixStr, (RealParameter) migModelInit.rateMatrixInput.get());
try {
((RealParameter) migModelInit.popSizesInput.get()).initAndValidate();
((RealParameter) migModelInit.rateMatrixInput.get()).initAndValidate();
migModelInit.initAndValidate();
} catch (Exception ex) {
System.err.println("Error configuring initial migration model.");
}
}
return false;
}
use of beast.evolution.likelihood.TreeLikelihood in project bacter by tgvaughan.
the class ACGLikelihoodSlow method calculateLogP.
@Override
public double calculateLogP() {
logP = 0.0;
for (Region region : acg.getRegions(locus)) {
Alignment margAlign = createMarginalAlignment(alignment, acg, region);
Tree margTree = new Tree(new MarginalTree(acg, region).getRoot());
TreeLikelihood treeLikelihood = new TreeLikelihood();
treeLikelihood.initByName("data", margAlign, "tree", margTree, "siteModel", siteModel, "useAmbiguities", useAmbiguitiesInput.get());
logP += treeLikelihood.calculateLogP();
}
return logP;
}
use of beast.evolution.likelihood.TreeLikelihood in project beast2 by CompEvol.
the class UnorderedAlignmentsTest method testUnorderedAlignment.
@Test
public void testUnorderedAlignment() throws Exception {
TaxonSet taxa = getTaxa();
Tree tree = getTree(taxa);
SiteModel siteModel = getSiteModel();
double logP = 0.0;
double shuffledLogP = 0.0;
for (int i = 0; i < 3; ++i) {
Alignment data = getAlignment(taxa, tree, siteModel);
// First calculate in order
TreeLikelihood likelihood = new TreeLikelihood();
likelihood.initByName("data", data, "tree", tree, "siteModel", siteModel);
logP += likelihood.calculateLogP();
// Now calculate again, with shuffled taxon order
Collections.shuffle(data.sequenceInput.get());
likelihood = new TreeLikelihood();
likelihood.initByName("data", data, "tree", tree, "siteModel", siteModel);
shuffledLogP += likelihood.calculateLogP();
}
assertEquals(logP, shuffledLogP, 1E-9);
}
Aggregations