Search in sources :

Example 41 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class ExchangeOperatorTest method testNarrowExchange6Taxa.

@Test
public void testNarrowExchange6Taxa() throws Exception {
    int runs = 10000;
    Randomizer.setSeed(666);
    Sequence A = new Sequence("A", "A");
    Sequence B = new Sequence("B", "A");
    Sequence C = new Sequence("C", "A");
    Sequence D = new Sequence("D", "A");
    Sequence E = new Sequence("E", "A");
    Sequence F = new Sequence("F", "A");
    Alignment data = new Alignment();
    data.initByName("sequence", A, "sequence", B, "sequence", C, "sequence", D, "sequence", E, "sequence", F, "dataType", "nucleotide");
    // String sourceTree = "((((A:2.0,B:2.0):1.0,(C:1.0,D:1.0):2.0):1.0,E:4.0):1.0,F:5.0):0.0"; // ((((A,B),(C,D)),E),F)
    // String targetTree = "((((A:2.0,(C:1.0,D:1.0):1.0):1.0,B:3.0):1.0,E:4.0):1.0,F:5.0):0.0"; // ((((A,(C,D)),B),E),F)
    String sourceTree = "(((A:5.0,B:5.0):2.0,((C:5.0,D:5.0):1.0,E:6.0):1.0):1.0,F:8.0):0.0";
    String targetTree = "(((A:5.0,B:5.0):2.0,F:7.0):1.0,((C:5.0,D:5.0):1.0,E:6.0):2.0):0.0";
    testNarrowExchange(sourceTree, targetTree, runs, data);
}
Also used : Alignment(beast.evolution.alignment.Alignment) Sequence(beast.evolution.alignment.Sequence) Test(org.junit.Test)

Example 42 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class BirthDeathGernhard08ModelTest method testJC69Likelihood.

@Test
public void testJC69Likelihood() throws Exception {
    // Set up JC69 model: uniform freqs, kappa = 1, 0 gamma categories
    Alignment data = BEASTTestCase.getAlignment();
    Tree tree = BEASTTestCase.getTree(data);
    RealParameter birthDiffRate = new RealParameter("1.0");
    RealParameter relativeDeathRate = new RealParameter("0.5");
    RealParameter originHeight = new RealParameter("0.1");
    BirthDeathGernhard08Model likelihood = new BirthDeathGernhard08Model();
    likelihood.initByName("type", "unscaled", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate);
    double logP = 0;
    // -3.520936119641363
    logP = likelihood.calculateLogP();
    assertEquals(logP, 2.5878899503981287, BEASTTestCase.PRECISION);
    likelihood.initByName("type", "timesonly", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate);
    logP = likelihood.calculateLogP();
    assertEquals(logP, 9.16714116240823, BEASTTestCase.PRECISION);
    likelihood.initByName("type", "oriented", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate);
    logP = likelihood.calculateLogP();
    assertEquals(logP, 4.379649419626184, BEASTTestCase.PRECISION);
    likelihood.initByName("type", "labeled", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate);
    logP = likelihood.calculateLogP();
    assertEquals(logP, 1.2661341104158121, BEASTTestCase.PRECISION);
    likelihood.initByName("type", "labeled", "tree", tree, "birthDiffRate", birthDiffRate, "relativeDeathRate", relativeDeathRate, "originHeight", originHeight);
    logP = likelihood.calculateLogP();
    assertEquals(logP, 8.41413452832378, BEASTTestCase.PRECISION);
}
Also used : Alignment(beast.evolution.alignment.Alignment) BirthDeathGernhard08Model(beast.evolution.speciation.BirthDeathGernhard08Model) Tree(beast.evolution.tree.Tree) RealParameter(beast.core.parameter.RealParameter) Test(org.junit.Test)

Example 43 with Alignment

use of beast.evolution.alignment.Alignment 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);
    }
}
Also used : ArrayList(java.util.ArrayList) RealParameter(beast.core.parameter.RealParameter) Sequence(beast.evolution.alignment.Sequence) TaxonSet(beast.evolution.alignment.TaxonSet) TreeIntervals(beast.evolution.tree.coalescent.TreeIntervals) Alignment(beast.evolution.alignment.Alignment) ConstantPopulation(beast.evolution.tree.coalescent.ConstantPopulation) RandomTree(beast.evolution.tree.RandomTree) TraitSet(beast.evolution.tree.TraitSet) Test(org.junit.Test)

Example 44 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class MRCAPriorTest method testMRCATimePrior.

@Test
public void testMRCATimePrior() throws Exception {
    Alignment data = BEASTTestCase.getAlignment();
    TreeParser tree = new TreeParser();
    tree.initByName("taxa", data, "newick", "((human:0.024003,(chimp:0.010772,bonobo:0.010772):0.013231):0.012035," + "(gorilla:0.024003,(orangutan:0.010772,siamang:0.010772):0.013231):0.012035);", "IsLabelledNewick", true);
    Taxon human = new Taxon();
    human.setID("human");
    Taxon bonobo = new Taxon();
    bonobo.setID("bonobo");
    Taxon chimp = new Taxon();
    chimp.setID("chimp");
    Taxon gorilla = new Taxon();
    gorilla.setID("gorilla");
    Taxon orangutan = new Taxon();
    orangutan.setID("orangutan");
    Taxon siamang = new Taxon();
    siamang.setID("siamang");
    MRCAPrior prior = new MRCAPrior();
    TaxonSet set = new TaxonSet();
    set.initByName("taxon", human, "taxon", bonobo, "taxon", chimp);
    Exponential exp = new Exponential();
    /* get distribution for set (human, bonobo, chimp) */
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true, "distr", exp);
    double logP = prior.calculateLogP();
    assertEquals(-0.024003, logP, BEASTTestCase.PRECISION);
    /* get distribution for set (human, chimp), do not require the set to by monophyletic */
    set = new TaxonSet();
    set.initByName("taxon", human, "taxon", chimp);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", false);
    logP = prior.calculateLogP();
    assertEquals(-0.024003, logP, BEASTTestCase.PRECISION);
    /* get distribution for set (human, chimp), DO require the set to by monophyletic */
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    logP = prior.calculateLogP();
    assertEquals(Double.NEGATIVE_INFINITY, logP, 0);
    /* get distribution for set (human, gorilla) = root, not monophyletic */
    set = new TaxonSet();
    set.initByName("taxon", human, "taxon", gorilla);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", false);
    logP = prior.calculateLogP();
    assertEquals(-0.024003 - 0.012035, logP, BEASTTestCase.PRECISION);
}
Also used : Alignment(beast.evolution.alignment.Alignment) TreeParser(beast.util.TreeParser) Taxon(beast.evolution.alignment.Taxon) MRCAPrior(beast.math.distributions.MRCAPrior) Exponential(beast.math.distributions.Exponential) TaxonSet(beast.evolution.alignment.TaxonSet) Test(org.junit.Test)

Example 45 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class MRCAPriorTest method testSingleMonophyleticConstraint.

@Test
public void testSingleMonophyleticConstraint() throws Exception {
    Alignment data = BEASTTestCase.getAlignment();
    TreeParser tree = new TreeParser();
    tree.initByName("taxa", data, "newick", "((human:0.024003,(chimp:0.010772,bonobo:0.010772):0.013231):0.012035," + "(gorilla:0.024003,(orangutan:0.010772,siamang:0.010772):0.013231):0.012035);", "IsLabelledNewick", true);
    Taxon human = new Taxon();
    human.setID("human");
    Taxon bonobo = new Taxon();
    bonobo.setID("bonobo");
    Taxon chimp = new Taxon();
    chimp.setID("chimp");
    Taxon gorilla = new Taxon();
    gorilla.setID("gorilla");
    Taxon orangutan = new Taxon();
    orangutan.setID("orangutan");
    Taxon siamang = new Taxon();
    siamang.setID("siamang");
    MRCAPrior prior = new MRCAPrior();
    /* check (human, bonobo, chimp) is monophyletic **/
    TaxonSet set = new TaxonSet();
    set.initByName("taxon", human, "taxon", bonobo, "taxon", chimp);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    double logP = prior.calculateLogP();
    assertEquals(logP, 0, 0);
    /* check (gorilla, siamang) is NOT monophyletic **/
    set = new TaxonSet();
    set.initByName("taxon", gorilla, "taxon", siamang);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    logP = prior.calculateLogP();
    assertEquals(logP, Double.NEGATIVE_INFINITY, 0);
    /* check (gorilla, orangutan, siamang) is monophyletic **/
    set = new TaxonSet();
    set.initByName("taxon", gorilla, "taxon", orangutan, "taxon", siamang);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    logP = prior.calculateLogP();
    assertEquals(logP, 0, 0);
    /* check (human, gorilla) is NOT monophyletic **/
    set = new TaxonSet();
    set.initByName("taxon", human, "taxon", gorilla);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    logP = prior.calculateLogP();
    assertEquals(logP, Double.NEGATIVE_INFINITY, 0);
    set.setID("test");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    prior.init(ps);
    String log = new String(baos.toByteArray(), StandardCharsets.UTF_8);
    assertEquals(log, "mrcatime(test)\t");
    baos = new ByteArrayOutputStream();
    ps = new PrintStream(baos);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true, "useOriginate", true);
    prior.init(ps);
    log = new String(baos.toByteArray(), StandardCharsets.UTF_8);
    assertEquals(log, "mrcatime(test.originate)\t");
}
Also used : PrintStream(java.io.PrintStream) Alignment(beast.evolution.alignment.Alignment) TreeParser(beast.util.TreeParser) Taxon(beast.evolution.alignment.Taxon) MRCAPrior(beast.math.distributions.MRCAPrior) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TaxonSet(beast.evolution.alignment.TaxonSet) Test(org.junit.Test)

Aggregations

Alignment (beast.evolution.alignment.Alignment)102 Test (org.junit.Test)43 Sequence (beast.evolution.alignment.Sequence)31 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)30 Tree (beast.evolution.tree.Tree)29 SiteModel (beast.evolution.sitemodel.SiteModel)27 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)22 BeagleTreeLikelihood (beast.evolution.likelihood.BeagleTreeLikelihood)20 ArrayList (java.util.ArrayList)17 UncertainAlignmentTest (test.beast.evolution.alignment.UncertainAlignmentTest)17 Frequencies (beast.evolution.substitutionmodel.Frequencies)13 RealParameter (beast.core.parameter.RealParameter)12 TaxonSet (beast.evolution.alignment.TaxonSet)11 BEASTInterface (beast.core.BEASTInterface)10 ClusterTree (beast.util.ClusterTree)9 Taxon (beast.evolution.alignment.Taxon)6 HKY (beast.evolution.substitutionmodel.HKY)6 Node (beast.evolution.tree.Node)6 TreeParser (beast.util.TreeParser)6 IOException (java.io.IOException)6