Search in sources :

Example 21 with StrictClockBranchRates

use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.

the class BeagleBranchLikelihood method main.

// END: LikelihoodColumn class
// ////////////
// ---TEST---//
// ////////////
public static void main(String[] args) {
    try {
        MathUtils.setSeed(666);
        int sequenceLength = 1000;
        ArrayList<Partition> partitionsList = new ArrayList<Partition>();
        // create tree
        NewickImporter importer = new NewickImporter("((SimSeq1:22.0,SimSeq2:22.0):12.0,(SimSeq3:23.1,SimSeq4:23.1):10.899999999999999);");
        Tree tree = importer.importTree(null);
        TreeModel treeModel = new TreeModel(tree);
        // create Frequency Model
        Parameter freqs = new Parameter.Default(new double[] { 0.25, 0.25, 0.25, 0.25 });
        FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE, freqs);
        // create branch model
        Parameter kappa1 = new Parameter.Default(1, 1);
        HKY hky1 = new HKY(kappa1, freqModel);
        BranchModel homogeneousBranchModel = new HomogeneousBranchModel(hky1);
        List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
        substitutionModels.add(hky1);
        List<FrequencyModel> freqModels = new ArrayList<FrequencyModel>();
        freqModels.add(freqModel);
        // create branch rate model
        Parameter rate = new Parameter.Default(1, 1.000);
        BranchRateModel branchRateModel = new StrictClockBranchRates(rate);
        // create site model
        GammaSiteRateModel siteRateModel = new GammaSiteRateModel("siteModel");
        // create partition
        Partition partition1 = new //
        Partition(//
        treeModel, //
        homogeneousBranchModel, //
        siteRateModel, //
        branchRateModel, //
        freqModel, // from
        0, // to
        sequenceLength - 1, // every
        1);
        partitionsList.add(partition1);
        // feed to sequence simulator and generate data
        BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(partitionsList);
        Alignment alignment = simulator.simulate(false, false);
        System.out.println(alignment);
        BeagleTreeLikelihood btl = new BeagleTreeLikelihood(alignment, treeModel, homogeneousBranchModel, siteRateModel, branchRateModel, null, false, PartialsRescalingScheme.DEFAULT, true);
        System.out.println("BTL(homogeneous) = " + btl.getLogLikelihood());
        BeagleBranchLikelihood bbl = new BeagleBranchLikelihood(alignment, treeModel, homogeneousBranchModel, siteRateModel, freqModel, branchRateModel);
        int branchIndex = 4;
        System.out.println(bbl.getBranchLogLikelihood(branchIndex));
        bbl.finalizeBeagle();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    } catch (Throwable e) {
        e.printStackTrace();
        System.exit(-1);
    }
// END: try-catch block
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) BranchModel(dr.evomodel.branchmodel.BranchModel) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates) BeagleSequenceSimulator(dr.app.beagle.tools.BeagleSequenceSimulator) TreeModel(dr.evomodel.tree.TreeModel) Alignment(dr.evolution.alignment.Alignment) NewickImporter(dr.evolution.io.NewickImporter) Tree(dr.evolution.tree.Tree) Partition(dr.app.beagle.tools.Partition) BeagleTreeLikelihood(dr.evomodel.treelikelihood.BeagleTreeLikelihood) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) GammaSiteRateModel(dr.evomodel.siteratemodel.GammaSiteRateModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) HKY(dr.evomodel.substmodel.nucleotide.HKY) Parameter(dr.inference.model.Parameter)

Example 22 with StrictClockBranchRates

use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.

the class PartitionData method createClockRateModel.

public BranchRateModel createClockRateModel() {
    BranchRateModel branchRateModel = null;
    if (this.clockModelIndex == 0) {
        // Strict Clock
        Parameter rateParameter = new Parameter.Default(1, clockParameterValues[0]);
        branchRateModel = new StrictClockBranchRates(rateParameter);
    } else if (this.clockModelIndex == LRC_INDEX) {
        // Lognormal relaxed clock
        double numberOfBranches = 2 * (createTreeModel().getTaxonCount() - 1);
        Parameter rateCategoryParameter = new Parameter.Default(numberOfBranches);
        Parameter mean = new Parameter.Default(LogNormalDistributionModelParser.MEAN, 1, clockParameterValues[1]);
        Parameter stdev = new Parameter.Default(LogNormalDistributionModelParser.STDEV, 1, clockParameterValues[2]);
        // TODO: choose between log scale / real scale
        ParametricDistributionModel distributionModel = new LogNormalDistributionModel(mean, stdev, clockParameterValues[3], lrcParametersInRealSpace);
        branchRateModel = new // 
        DiscretizedBranchRates(// 
        createTreeModel(), // 
        rateCategoryParameter, // 
        distributionModel, // 
        1, // 
        false, // 
        Double.NaN, // randomizeRates
        true, // keepRates
        false, // cacheRates
        false);
    } else if (this.clockModelIndex == 2) {
        // Exponential relaxed clock
        double numberOfBranches = 2 * (createTreeModel().getTaxonCount() - 1);
        Parameter rateCategoryParameter = new Parameter.Default(numberOfBranches);
        Parameter mean = new Parameter.Default(DistributionModelParser.MEAN, 1, clockParameterValues[4]);
        ParametricDistributionModel distributionModel = new ExponentialDistributionModel(mean, clockParameterValues[5]);
        // branchRateModel = new DiscretizedBranchRates(createTreeModel(), rateCategoryParameter,
        // distributionModel, 1, false, Double.NaN);
        branchRateModel = new // 
        DiscretizedBranchRates(// 
        createTreeModel(), // 
        rateCategoryParameter, // 
        distributionModel, // 
        1, // 
        false, // 
        Double.NaN, // randomizeRates
        true, // keepRates
        false, // cacheRates
        false);
    } else if (this.clockModelIndex == 3) {
        // Inverse Gaussian
        double numberOfBranches = 2 * (createTreeModel().getTaxonCount() - 1);
        Parameter rateCategoryParameter = new Parameter.Default(numberOfBranches);
        Parameter mean = new Parameter.Default(InverseGaussianDistributionModelParser.MEAN, 1, clockParameterValues[6]);
        Parameter stdev = new Parameter.Default(InverseGaussianDistributionModelParser.STDEV, 1, clockParameterValues[7]);
        ParametricDistributionModel distributionModel = new InverseGaussianDistributionModel(mean, stdev, clockParameterValues[8], false);
        branchRateModel = new // 
        DiscretizedBranchRates(// 
        createTreeModel(), // 
        rateCategoryParameter, // 
        distributionModel, // 
        1, // 
        false, // 
        Double.NaN, // randomizeRates
        true, // keepRates
        false, // cacheRates
        false);
    } else {
        System.out.println("Not yet implemented");
    }
    return branchRateModel;
}
Also used : DiscretizedBranchRates(dr.evomodel.branchratemodel.DiscretizedBranchRates) InverseGaussianDistributionModel(dr.inference.distribution.InverseGaussianDistributionModel) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) LogNormalDistributionModel(dr.inference.distribution.LogNormalDistributionModel) ParametricDistributionModel(dr.inference.distribution.ParametricDistributionModel) ExponentialDistributionModel(dr.inference.distribution.ExponentialDistributionModel) Parameter(dr.inference.model.Parameter) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates)

Example 23 with StrictClockBranchRates

use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.

the class ApproximatePoissonTreeLikelihoodTest method testOtherChildUpdates.

public void testOtherChildUpdates() throws IOException, Importer.ImportException, TreeUtils.MissingTaxonException {
    branchRateModel = new StrictClockBranchRates(new Parameter.Default(1.0));
    NewickImporter importer = new NewickImporter("((1:1.0,2:0.5,3:2.0):0.1,4:0.3,(7:0.2,8:0.1,9:0.3):0.2,6:0.01)");
    NewickImporter importer2 = new NewickImporter("((6:1,(4:1,((9:1,7:1):2,8:2):3.0):6):4,(1:1,(3:3,2:2):1):1);");
    tree = importer.importTree(null);
    treeModel = new BigFastTreeModel(importer2.importTree(null));
    CladeNodeModel cladeModel = new CladeNodeModel(tree, treeModel);
    BranchLengthProvider constrainedBranchLengthProvider = new ConstrainedTreeBranchLengthProvider(cladeModel);
    approximatePoissonTreeLikelihood = new ApproximatePoissonTreeLikelihood("approximateTreeLikelihood", 1, treeModel, branchRateModel, constrainedBranchLengthProvider);
    approximatePoissonTreeLikelihood.getLogLikelihood();
    NodeRef node = treeModel.getNode(1);
    NodeRef sibling = treeModel.getNode(9);
    NodeRef parent = treeModel.getParent(node);
    NodeRef grandparent = treeModel.getParent(parent);
    NodeRef root = treeModel.getRoot();
    CladeRef clade = cladeModel.getClade(parent);
    treeModel.beginTreeEdit();
    treeModel.removeChild(grandparent, parent);
    treeModel.removeChild(parent, sibling);
    treeModel.setNodeHeight(parent, treeModel.getNodeHeight(root) + 1);
    treeModel.setRoot(parent);
    // cladeModel.setRootNode(clade, parent);
    treeModel.addChild(parent, root);
    treeModel.addChild(grandparent, sibling);
    treeModel.endTreeEdit();
    double LL = approximatePoissonTreeLikelihood.getLogLikelihood();
    approximatePoissonTreeLikelihood.makeDirty();
    double newLL = approximatePoissonTreeLikelihood.getLogLikelihood();
    assertEquals(LL, newLL);
}
Also used : NodeRef(dr.evolution.tree.NodeRef) NewickImporter(dr.evolution.io.NewickImporter) ConstrainedTreeBranchLengthProvider(dr.evomodel.bigfasttree.constrainedtree.ConstrainedTreeBranchLengthProvider) CladeNodeModel(dr.evomodel.bigfasttree.constrainedtree.CladeNodeModel) ConstrainedTreeBranchLengthProvider(dr.evomodel.bigfasttree.constrainedtree.ConstrainedTreeBranchLengthProvider) CladeRef(dr.evomodel.bigfasttree.constrainedtree.CladeRef) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates)

Example 24 with StrictClockBranchRates

use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.

the class ApproximatePoissonTreeLikelihoodTest method testRootAndRootChildUpdates.

// This tests updating when the root and rootchild2  change
// +- 6
// |
// +------| gp       +- 4 *
// |      |          |
// |      +----------| p       + 9
// |                 |     +---|
// |                 +-----|   + 7
// | r                     |
// |                       +--- 8
// |
// |+- 1
// +|
// | +----- 3
// +-|
// +--- 2
// To
// +-------------------- 4 *
// |
// |       +- 6
// |p      |
// |+------| gp                + 9
// ||      |               +---|
// ||      +---------------|   + 7
// +|                      |
// | r                    +--- 8
// |
// | +- 1
// +-|
// | +---- 3
// +-|
// +-- 2
// 
// 
public void testRootAndRootChildUpdates() throws IOException, Importer.ImportException, TreeUtils.MissingTaxonException {
    branchRateModel = new StrictClockBranchRates(new Parameter.Default(1.0));
    NewickImporter importer = new NewickImporter("((1:1.0,2:0.5,3:2.0):0.1,4:0.3,(7:0.2,8:0.1,9:0.3):0.2,6:0.01)");
    NewickImporter importer2 = new NewickImporter("((6:1,(4:1,((9:1,7:1):2,8:2):3.0):6):4,(1:1,(3:3,2:2):1):1);");
    tree = importer.importTree(null);
    treeModel = new BigFastTreeModel(importer2.importTree(null));
    CladeNodeModel cladeModel = new CladeNodeModel(tree, treeModel);
    BranchLengthProvider constrainedBranchLengthProvider = new ConstrainedTreeBranchLengthProvider(cladeModel);
    approximatePoissonTreeLikelihood = new ApproximatePoissonTreeLikelihood("approximateTreeLikelihood", 1, treeModel, branchRateModel, constrainedBranchLengthProvider);
    approximatePoissonTreeLikelihood.getLogLikelihood();
    NodeRef node = treeModel.getNode(9);
    NodeRef sibling = treeModel.getNode(1);
    NodeRef parent = treeModel.getParent(node);
    NodeRef grandparent = treeModel.getParent(parent);
    NodeRef root = treeModel.getRoot();
    CladeRef clade = cladeModel.getClade(node);
    treeModel.beginTreeEdit();
    treeModel.removeChild(grandparent, parent);
    treeModel.removeChild(parent, sibling);
    treeModel.setNodeHeight(parent, treeModel.getNodeHeight(root) + 1);
    treeModel.setRoot(parent);
    // cladeModel.setRootNode(clade, parent);
    treeModel.addChild(parent, root);
    treeModel.addChild(grandparent, sibling);
    treeModel.endTreeEdit();
    double LL = approximatePoissonTreeLikelihood.getLogLikelihood();
    approximatePoissonTreeLikelihood.makeDirty();
    double newLL = approximatePoissonTreeLikelihood.getLogLikelihood();
    assertEquals(LL, newLL);
}
Also used : NodeRef(dr.evolution.tree.NodeRef) NewickImporter(dr.evolution.io.NewickImporter) ConstrainedTreeBranchLengthProvider(dr.evomodel.bigfasttree.constrainedtree.ConstrainedTreeBranchLengthProvider) CladeNodeModel(dr.evomodel.bigfasttree.constrainedtree.CladeNodeModel) ConstrainedTreeBranchLengthProvider(dr.evomodel.bigfasttree.constrainedtree.ConstrainedTreeBranchLengthProvider) CladeRef(dr.evomodel.bigfasttree.constrainedtree.CladeRef) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates)

Example 25 with StrictClockBranchRates

use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.

the class ApproximatePoissonTreeLikelihoodTest method testRootPolytomy.

public void testRootPolytomy() throws IOException, Importer.ImportException, TreeUtils.MissingTaxonException {
    branchRateModel = new StrictClockBranchRates(new Parameter.Default(1.0));
    NewickImporter importer = new NewickImporter("((1:1.0,2:1.0):1.0,3:1.0,4:1.0);");
    NewickImporter importer2 = new NewickImporter("(((1:1.0,2:1.0):1.0,3:1.1):0.1,4:1.0);");
    tree = importer.importTree(null);
    treeModel = new BigFastTreeModel(importer2.importTree(null));
    CladeNodeModel cladeModel = new CladeNodeModel(tree, treeModel);
    BranchLengthProvider constrainedBranchLengthProvider = new ConstrainedTreeBranchLengthProvider(cladeModel);
    approximatePoissonTreeLikelihood = new ApproximatePoissonTreeLikelihood("approximateTreeLikelihood", 1, treeModel, branchRateModel, constrainedBranchLengthProvider);
    approximatePoissonTreeLikelihood.getLogLikelihood();
    expectedLL = 0;
    double[] expectations = { 1d, 1d, 1d, 1.1, 1.1 };
    // time
    double[] mutations = { 1d, 1d, 1d, 1.0, 1 };
    for (int i = 0; i < expectations.length; i++) {
        PoissonDistribution p = new PoissonDistribution(expectations[i]);
        expectedLL += p.logPdf(mutations[i]);
    }
    NodeRef rootNode = treeModel.getRoot();
    NodeRef rootNode1 = treeModel.getNode(5);
    NodeRef tip3 = treeModel.getNode(2);
    CladeRef clade = cladeModel.getClade(rootNode);
    treeModel.beginTreeEdit();
    treeModel.removeChild(rootNode, rootNode1);
    treeModel.setRoot(rootNode1);
    treeModel.removeChild(rootNode1, tip3);
    treeModel.addChild(rootNode, tip3);
    treeModel.addChild(rootNode1, rootNode);
    treeModel.setNodeHeight(rootNode1, treeModel.getNodeHeight(rootNode) + 1);
    treeModel.endTreeEdit();
    // cladeModel.setRootNode(clade,rootNode1);
    double LL = approximatePoissonTreeLikelihood.getLogLikelihood();
    approximatePoissonTreeLikelihood.makeDirty();
    double newLL = approximatePoissonTreeLikelihood.getLogLikelihood();
    assertEquals(LL, newLL, 1E-13);
}
Also used : PoissonDistribution(dr.math.distributions.PoissonDistribution) ConstrainedTreeBranchLengthProvider(dr.evomodel.bigfasttree.constrainedtree.ConstrainedTreeBranchLengthProvider) CladeNodeModel(dr.evomodel.bigfasttree.constrainedtree.CladeNodeModel) ConstrainedTreeBranchLengthProvider(dr.evomodel.bigfasttree.constrainedtree.ConstrainedTreeBranchLengthProvider) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates) NodeRef(dr.evolution.tree.NodeRef) NewickImporter(dr.evolution.io.NewickImporter) CladeRef(dr.evomodel.bigfasttree.constrainedtree.CladeRef)

Aggregations

StrictClockBranchRates (dr.evomodel.branchratemodel.StrictClockBranchRates)41 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)32 ArrayList (java.util.ArrayList)29 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)26 Parameter (dr.inference.model.Parameter)26 TreeDataLikelihood (dr.evomodel.treedatalikelihood.TreeDataLikelihood)21 MultivariateElasticModel (dr.evomodel.continuous.MultivariateElasticModel)18 MatrixParameter (dr.inference.model.MatrixParameter)15 ArbitraryBranchRates (dr.evomodel.branchratemodel.ArbitraryBranchRates)10 NewickImporter (dr.evolution.io.NewickImporter)8 DiagonalMatrix (dr.inference.model.DiagonalMatrix)7 CladeNodeModel (dr.evomodel.bigfasttree.constrainedtree.CladeNodeModel)5 ConstrainedTreeBranchLengthProvider (dr.evomodel.bigfasttree.constrainedtree.ConstrainedTreeBranchLengthProvider)5 NodeRef (dr.evolution.tree.NodeRef)4 DefaultTreeModel (dr.evomodel.tree.DefaultTreeModel)4 TreeModel (dr.evomodel.tree.TreeModel)4 Tree (dr.evolution.tree.Tree)3 Taxon (dr.evolution.util.Taxon)3 CladeRef (dr.evomodel.bigfasttree.constrainedtree.CladeRef)3 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)3