Search in sources :

Example 6 with JukesCantor

use of beast.evolution.substitutionmodel.JukesCantor in project bacter by tgvaughan.

the class ACGLikelihoodTest method testBeagleLikelihood.

@Test
public void testBeagleLikelihood() throws Exception {
    Locus locus = new Locus("locus", getAlignment());
    // ConversionGraph
    ConversionGraph acg = new ConversionGraph();
    ClusterTree tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", locus.getAlignment());
    acg.assignFrom(tree);
    acg.initByName("locus", locus);
    // Site model:
    JukesCantor jc = new JukesCantor();
    jc.initByName();
    SiteModel siteModel = new SiteModel();
    siteModel.initByName("substModel", jc);
    // Likelihood
    ACGLikelihoodBeagle argLikelihood = new ACGLikelihoodBeagle();
    argLikelihood.initByName("locus", locus, "tree", acg, "siteModel", siteModel);
    ACGLikelihoodSlow argLikelihoodSlow = new ACGLikelihoodSlow();
    argLikelihoodSlow.initByName("locus", locus, "tree", acg, "siteModel", siteModel);
    acg.setEverythingDirty(true);
    try {
        double logP = argLikelihood.calculateLogP();
        double logPtrue = argLikelihoodSlow.calculateLogP();
        double relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + logP));
        assertTrue(relativeDiff < 1e-14);
        // 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 = 100;
        int endLocus = 200;
        Conversion recomb1 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
        acg.addConversion(recomb1);
        logP = argLikelihood.calculateLogP();
        logPtrue = argLikelihoodSlow.calculateLogP();
        relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + logP));
        assertTrue(relativeDiff < 1e-14);
        // Add another recombination event
        node1 = acg.getExternalNodes().get(0);
        node2 = acg.getNode(20);
        height1 = 0.75 * (node1.getHeight() + node1.getParent().getHeight());
        height2 = 0.5 * (node2.getHeight() + node2.getParent().getHeight());
        startLocus = 250;
        endLocus = 300;
        Conversion recomb2 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
        acg.addConversion(recomb2);
        logP = argLikelihood.calculateLogP();
        logPtrue = argLikelihoodSlow.calculateLogP();
        relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + logP));
        assertTrue(relativeDiff < 1e-14);
    } catch (RuntimeException ex) {
        System.err.println("Beagle library not found: skipping beagle likelihood test.");
    }
}
Also used : ClusterTree(beast.util.ClusterTree) Node(beast.evolution.tree.Node) SiteModel(beast.evolution.sitemodel.SiteModel) Locus(bacter.Locus) JukesCantor(beast.evolution.substitutionmodel.JukesCantor) ConversionGraph(bacter.ConversionGraph) Conversion(bacter.Conversion) Test(org.junit.Test)

Example 7 with JukesCantor

use of beast.evolution.substitutionmodel.JukesCantor in project bacter by tgvaughan.

the class ACGLikelihoodTest method testLikelihoodFixedData.

@Test
public void testLikelihoodFixedData() throws Exception {
    Locus locus = new Locus("locus", getAlignment());
    // ConversionGraph
    ConversionGraph acg = new ConversionGraph();
    ClusterTree tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", locus.getAlignment());
    acg.assignFrom(tree);
    acg.initByName("locus", locus);
    // Site model:
    JukesCantor jc = new JukesCantor();
    jc.initByName();
    SiteModel siteModel = new SiteModel();
    siteModel.initByName("substModel", jc);
    // Likelihood
    ACGLikelihood argLikelihood = new ACGLikelihood();
    argLikelihood.initByName("locus", locus, "tree", acg, "siteModel", siteModel);
    ACGLikelihoodSlow argLikelihoodSlow = new ACGLikelihoodSlow();
    argLikelihoodSlow.initByName("locus", locus, "tree", acg, "siteModel", siteModel);
    acg.setEverythingDirty(true);
    double logP = argLikelihood.calculateLogP();
    double logPtrue = argLikelihoodSlow.calculateLogP();
    double relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + logP));
    assertTrue(relativeDiff < 1e-14);
    // 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 = 100;
    int endLocus = 200;
    Conversion recomb1 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(recomb1);
    logP = argLikelihood.calculateLogP();
    logPtrue = argLikelihoodSlow.calculateLogP();
    relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + logP));
    assertTrue(relativeDiff < 1e-14);
    // Add another recombination event
    node1 = acg.getExternalNodes().get(0);
    node2 = acg.getNode(20);
    height1 = 0.75 * (node1.getHeight() + node1.getParent().getHeight());
    height2 = 0.5 * (node2.getHeight() + node2.getParent().getHeight());
    startLocus = 250;
    endLocus = 300;
    Conversion recomb2 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(recomb2);
    logP = argLikelihood.calculateLogP();
    logPtrue = argLikelihoodSlow.calculateLogP();
    relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + logP));
    assertTrue(relativeDiff < 1e-14);
}
Also used : ClusterTree(beast.util.ClusterTree) Node(beast.evolution.tree.Node) SiteModel(beast.evolution.sitemodel.SiteModel) Locus(bacter.Locus) JukesCantor(beast.evolution.substitutionmodel.JukesCantor) ConversionGraph(bacter.ConversionGraph) Conversion(bacter.Conversion) Test(org.junit.Test)

Example 8 with JukesCantor

use of beast.evolution.substitutionmodel.JukesCantor in project beast2 by CompEvol.

the class Utils method main.

public static void main(String[] args) {
    try {
        Sequence a = new Sequence("A", "A");
        Sequence b = new Sequence("B", "A");
        Sequence c = new Sequence("C", "A");
        Sequence d = new Sequence("D", "A");
        Alignment data = new Alignment();
        data.initByName("sequence", a, "sequence", b, "sequence", c, "sequence", d, "dataType", "nucleotide");
        TreeParser tree = new TreeParser();
        tree.initByName("taxa", data, "newick", "(((A:1,B:1):1,C:2):1,D:3)", "IsLabelledNewick", true);
        JukesCantor JC = new JukesCantor();
        JC.initAndValidate();
        SiteModel siteModel = new SiteModel();
        siteModel.initByName("mutationRate", "1.0", "gammaCategoryCount", 1, "substModel", JC);
        BeagleTreeLikelihood likelihood = new BeagleTreeLikelihood();
        likelihood.initByName("data", data, "tree", tree, "siteModel", siteModel);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Success");
    // if we got this far, exit with status 0
    System.exit(0);
}
Also used : BeagleTreeLikelihood(beast.evolution.likelihood.BeagleTreeLikelihood) Alignment(beast.evolution.alignment.Alignment) TreeParser(beast.util.TreeParser) SiteModel(beast.evolution.sitemodel.SiteModel) Sequence(beast.evolution.alignment.Sequence) JukesCantor(beast.evolution.substitutionmodel.JukesCantor) UnsupportedLookAndFeelException(javax.swing.UnsupportedLookAndFeelException) IOException(java.io.IOException)

Example 9 with JukesCantor

use of beast.evolution.substitutionmodel.JukesCantor in project beast2 by CompEvol.

the class TreeLikelihoodTest method testBeagleRNALikelihood.

/**
 * Test only effective when BEAGLE installed - otherwise it will always
 * pass since BEAGLE code will never be run.
 *
 * @throws Exception
 */
@Test
public void testBeagleRNALikelihood() throws Exception {
    Sequence seq1 = new Sequence("t1", "GUACGUACGUAC");
    Sequence seq2 = new Sequence("t2", "UACGUACGUACG");
    Sequence seq3 = new Sequence("t3", "ACGUACGUACGU");
    Alignment data = new Alignment();
    data.initByName("sequence", seq1, "sequence", seq2, "sequence", seq3, "dataType", "nucleotide");
    Tree tree = BEASTTestCase.getTree(data, "((t1:0.5,t2:0.5):0.5,t3:1.0):0.0;");
    SiteModel siteModel = new SiteModel();
    siteModel.initByName("gammaCategoryCount", 1, "substModel", new JukesCantor());
    TreeLikelihood likelihoodNoBeagle = newTreeLikelihood();
    likelihoodNoBeagle.initByName("data", data, "tree", tree, "siteModel", siteModel);
    double logLnoBeagle = likelihoodNoBeagle.calculateLogP();
    System.setProperty("java.only", "false");
    TreeLikelihood likelihoodBeagle = new TreeLikelihood();
    likelihoodBeagle.initByName("data", data, "tree", tree, "siteModel", siteModel);
    double logLBeagle = likelihoodBeagle.calculateLogP();
    assertEquals(logLBeagle, logLnoBeagle, BEASTTestCase.PRECISION);
}
Also used : Alignment(beast.evolution.alignment.Alignment) BeagleTreeLikelihood(beast.evolution.likelihood.BeagleTreeLikelihood) TreeLikelihood(beast.evolution.likelihood.TreeLikelihood) Tree(beast.evolution.tree.Tree) SiteModel(beast.evolution.sitemodel.SiteModel) Sequence(beast.evolution.alignment.Sequence) JukesCantor(beast.evolution.substitutionmodel.JukesCantor) UncertainAlignmentTest(test.beast.evolution.alignment.UncertainAlignmentTest) Test(org.junit.Test)

Example 10 with JukesCantor

use of beast.evolution.substitutionmodel.JukesCantor in project beast2 by CompEvol.

the class TreeLikelihoodTest 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);
    JukesCantor JC = new JukesCantor();
    JC.initAndValidate();
    SiteModel siteModel = new SiteModel();
    siteModel.initByName("mutationRate", "1.0", "gammaCategoryCount", 1, "substModel", JC);
    TreeLikelihood likelihood = newTreeLikelihood();
    likelihood.initByName("data", data, "tree", tree, "siteModel", siteModel);
    double logP = 0;
    logP = likelihood.calculateLogP();
    assertEquals(logP, -1992.2056440317247, BEASTTestCase.PRECISION);
    likelihood.initByName("useAmbiguities", true, "data", data, "tree", tree, "siteModel", siteModel);
    logP = likelihood.calculateLogP();
    assertEquals(logP, -1992.2056440317247, BEASTTestCase.PRECISION);
}
Also used : Alignment(beast.evolution.alignment.Alignment) BeagleTreeLikelihood(beast.evolution.likelihood.BeagleTreeLikelihood) TreeLikelihood(beast.evolution.likelihood.TreeLikelihood) Tree(beast.evolution.tree.Tree) SiteModel(beast.evolution.sitemodel.SiteModel) JukesCantor(beast.evolution.substitutionmodel.JukesCantor) UncertainAlignmentTest(test.beast.evolution.alignment.UncertainAlignmentTest) Test(org.junit.Test)

Aggregations

SiteModel (beast.evolution.sitemodel.SiteModel)11 JukesCantor (beast.evolution.substitutionmodel.JukesCantor)11 Test (org.junit.Test)8 ConversionGraph (bacter.ConversionGraph)5 Locus (bacter.Locus)5 Alignment (beast.evolution.alignment.Alignment)5 ClusterTree (beast.util.ClusterTree)5 Conversion (bacter.Conversion)4 BeagleTreeLikelihood (beast.evolution.likelihood.BeagleTreeLikelihood)4 Node (beast.evolution.tree.Node)4 RealParameter (beast.core.parameter.RealParameter)3 Sequence (beast.evolution.alignment.Sequence)3 TaxonSet (beast.evolution.alignment.TaxonSet)3 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)3 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)3 Tree (beast.evolution.tree.Tree)2 UncertainAlignmentTest (test.beast.evolution.alignment.UncertainAlignmentTest)2 State (beast.core.State)1 TreeParser (beast.util.TreeParser)1 IOException (java.io.IOException)1