Search in sources :

Example 41 with DefaultTreeModel

use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.

the class StarTreeModelParser method parseXMLObject.

/**
 * @return a tree object based on the XML element it was passed.
 */
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Tree tree = (Tree) xo.getChild(Tree.class);
    StarTreeModel treeModel = new StarTreeModel(xo.getId(), tree);
    Logger.getLogger("dr.evomodel").info("Creating a star tree model, '" + xo.getId() + "'");
    for (int i = 0; i < xo.getChildCount(); i++) {
        if (xo.getChild(i) instanceof XMLObject) {
            XMLObject cxo = (XMLObject) xo.getChild(i);
            if (cxo.getName().equals(ROOT_HEIGHT)) {
                if (cxo.getRawChild(0) instanceof Reference) {
                    // treeModel.setRootHeightParameter((Parameter) cxo.getChild(Parameter.class));
                    throw new XMLParseException("Can not provide idref to a new root height parameter");
                } else {
                    ParameterParser.replaceParameter(cxo, treeModel.getRootHeightParameter());
                }
            } else if (cxo.getName().equals(SHARE_ROOT)) {
                DefaultTreeModel sharedRoot = (DefaultTreeModel) cxo.getChild(DefaultTreeModel.class);
                treeModel.setSharedRootHeightParameter(sharedRoot);
            } else if (cxo.getName().equals(LEAF_HEIGHT)) {
                String taxonName;
                if (cxo.hasAttribute(TAXON)) {
                    taxonName = cxo.getStringAttribute(TAXON);
                } else {
                    throw new XMLParseException("taxa element missing from leafHeight element in treeModel element");
                }
                int index = treeModel.getTaxonIndex(taxonName);
                if (index == -1) {
                    throw new XMLParseException("taxon " + taxonName + " not found for leafHeight element in treeModel element");
                }
                NodeRef node = treeModel.getExternalNode(index);
                ParameterParser.replaceParameter(cxo, treeModel.getLeafHeightParameter(node));
            // } else if (cxo.getName().equals(NODE_HEIGHTS)) {
            // 
            // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
            // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
            // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
            // 
            // if (!rootNode && !internalNodes && !leafNodes) {
            // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeHeights element");
            // }
            // 
            // ParameterParser.replaceParameter(cxo, treeModel.createNodeHeightsParameter(rootNode, internalNodes, leafNodes));
            // 
            // } else if (cxo.getName().equals(NODE_RATES)) {
            // 
            // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
            // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
            // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
            // double[] initialValues = null;
            // 
            // if (cxo.hasAttribute(INITIAL_VALUE)) {
            // initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
            // }
            // 
            // if (!rootNode && !internalNodes && !leafNodes) {
            // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeRates element");
            // }
            // 
            // ParameterParser.replaceParameter(cxo, treeModel.createNodeRatesParameter(initialValues, rootNode, internalNodes, leafNodes));
            // 
            // } else if (cxo.getName().equals(NODE_TRAITS)) {
            // 
            // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
            // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
            // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
            // boolean fireTreeEvents = cxo.getAttribute(FIRE_TREE_EVENTS, false);
            // String name = cxo.getAttribute(NAME, "trait");
            // int dim = cxo.getAttribute(MULTIVARIATE_TRAIT, 1);
            // 
            // double[] initialValues = null;
            // if (cxo.hasAttribute(INITIAL_VALUE)) {
            // initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
            // }
            // 
            // if (!rootNode && !internalNodes && !leafNodes) {
            // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeTraits element");
            // }
            // 
            // ParameterParser.replaceParameter(cxo, treeModel.createNodeTraitsParameter(name, dim, initialValues, rootNode, internalNodes, leafNodes, fireTreeEvents));
            } else if (cxo.getName().equals(LEAF_TRAIT)) {
                String name = cxo.getAttribute(NAME, "trait");
                String taxonName;
                if (cxo.hasAttribute(TAXON)) {
                    taxonName = cxo.getStringAttribute(TAXON);
                } else {
                    throw new XMLParseException("taxa element missing from leafTrait element in treeModel element");
                }
                int index = treeModel.getTaxonIndex(taxonName);
                if (index == -1) {
                    throw new XMLParseException("taxon '" + taxonName + "' not found for leafTrait element in treeModel element");
                }
                NodeRef node = treeModel.getExternalNode(index);
                Parameter parameter = treeModel.getNodeTraitParameter(node, name);
                if (parameter == null)
                    throw new XMLParseException("trait '" + name + "' not found for leafTrait (taxon, " + taxonName + ") element in treeModel element");
                ParameterParser.replaceParameter(cxo, parameter);
            } else {
                throw new XMLParseException("illegal child element in " + getParserName() + ": " + cxo.getName());
            }
        } else if (xo.getChild(i) instanceof Tree) {
        // do nothing - already handled
        } else {
            throw new XMLParseException("illegal child element in  " + getParserName() + ": " + xo.getChildName(i) + " " + xo.getChild(i));
        }
    }
    Logger.getLogger("dr.evomodel").info("  initial tree topology = " + TreeUtils.uniqueNewick(treeModel, treeModel.getRoot()));
    Logger.getLogger("dr.evomodel").info("  tree height = " + treeModel.getNodeHeight(treeModel.getRoot()));
    return treeModel;
}
Also used : NodeRef(dr.evolution.tree.NodeRef) Tree(dr.evolution.tree.Tree) StarTreeModel(dr.evomodel.tree.StarTreeModel) Parameter(dr.inference.model.Parameter) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel)

Example 42 with DefaultTreeModel

use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.

the class NNITest method testDoOperation.

/**
 * Test method for {@link SimpleMCMCOperator#doOperation()}.
 * @throws ImportException
 * @throws IOException
 */
public void testDoOperation() throws IOException, ImportException {
    // probability of picking B node is 1/(2n-4) = 1/6
    // probability of swapping it with C is 1/1
    // total = 1/6
    System.out.println("Test 1: Forward");
    String treeMatch = "((((A,C),B),D),E);";
    int count = 0;
    int reps = 100000;
    for (int i = 0; i < reps; i++) {
        TreeModel treeModel = new DefaultTreeModel("treeModel", tree5);
        NNI operator = new NNI(treeModel, 1);
        operator.doOperation();
        String tree = TreeUtils.newickNoLengths(treeModel);
        if (tree.equals(treeMatch)) {
            count += 1;
        }
    }
    double p_1 = (double) count / (double) reps;
    System.out.println("Number of proposals:\t" + count);
    System.out.println("Number of tries:\t" + reps);
    System.out.println("Number of ratio:\t" + p_1);
    System.out.println("Number of expected ratio:\t" + 1.0 / 6.0);
    assertExpectation(1.0 / 6.0, p_1, reps);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel) NNI(dr.evomodel.operators.NNI) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel)

Example 43 with DefaultTreeModel

use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.

the class GibbsSubtreeSwapTestProblem method testDoOperation.

/**
 * Test method for {@link dr.evomodel.operators.GibbsSubtreeSwap}.
 * @throws ImportException
 * @throws IOException
 */
public void testDoOperation() throws IOException, ImportException {
    // assumes that the posterior is equal for all trees!!!
    // probability of picking (A,B) node is 1/(2n-3) = 1/7
    // probability of swapping with D is 1/2
    // total = 1/14
    // probability of picking {D} node is 1/(2n-3) = 1/7
    // probability of picking {A,B} is 1/5
    // total = 1/35
    // total = 1/14 + 1/35 = 7/70 = 0.1
    // now we calculate the same for the backward proposal
    // this is needed for the Hastings ratio
    // probability of picking (A,B) node is 1/(2n-3) = 1/7
    // probability of swapping with D is 1/3
    // total = 1/21
    // probability of picking {D} node is 1/(2n-3) = 1/7
    // probability of picking {A,B} is 1/4
    // total = 1/28
    // total = 1/21 + 1/28 = 4/84 + 3/84 = 7/84 = 1/12
    System.out.println("Test 1: Forward");
    String treeMatch = "(((D,C),(A,B)),E);";
    int count = 0;
    int reps = 100000;
    for (int i = 0; i < reps; i++) {
        DefaultTreeModel treeModel = new DefaultTreeModel("treeModel", tree5);
        GibbsSubtreeSwap operator = new GibbsSubtreeSwap(treeModel, false, 1.0);
        double hr = operator.operate(null);
        String tree = TreeUtils.newickNoLengths(treeModel);
        if (tree.equals(treeMatch)) {
            // System.out.println("Expected Hastings ratio = " + 5.0/6.0 + " in log = " + Math.log(5.0/6.0));
            // System.out.println("Obtained Hastings ratio = " + Math.exp(hr) + " in log = " + hr);
            TestCase.assertEquals(Math.log(5.0 / 6.0), hr, 0.00000001);
            count += 1;
        }
    }
    double p_1 = (double) count / (double) reps;
    System.out.println("Number of proposals:\t" + count);
    System.out.println("Number of tries:\t" + reps);
    System.out.println("Number of ratio:\t" + p_1);
    System.out.println("Number of expected ratio:\t" + 0.1);
    assertExpectation(0.1, p_1, reps);
}
Also used : GibbsSubtreeSwap(dr.evomodel.operators.GibbsSubtreeSwap) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel)

Example 44 with DefaultTreeModel

use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.

the class AncestralStateTreeLikelihoodTest method testJointLikelihood.

public void testJointLikelihood() {
    TreeModel treeModel = new DefaultTreeModel("treeModel", tree);
    Sequence[] sequence = new Sequence[3];
    sequence[0] = new Sequence(new Taxon("0"), "A");
    sequence[1] = new Sequence(new Taxon("1"), "C");
    sequence[2] = new Sequence(new Taxon("2"), "C");
    Taxa taxa = new Taxa();
    for (Sequence s : sequence) {
        taxa.addTaxon(s.getTaxon());
    }
    SimpleAlignment alignment = new SimpleAlignment();
    for (Sequence s : sequence) {
        alignment.addSequence(s);
    }
    Parameter mu = new Parameter.Default(1, 1.0);
    Parameter kappa = new Parameter.Default(1, 1.0);
    double[] pi = { 0.25, 0.25, 0.25, 0.25 };
    Parameter freqs = new Parameter.Default(pi);
    FrequencyModel f = new FrequencyModel(Nucleotides.INSTANCE, freqs);
    HKY hky = new HKY(kappa, f);
    AncestralStateTreeLikelihood treeLikelihood = new AncestralStateTreeLikelihood(alignment, treeModel, new GammaSiteModel(hky), new StrictClockBranchRates(mu), false, true, Nucleotides.INSTANCE, "state", false, // useMap = true
    true, false);
    double logLike = treeLikelihood.getLogLikelihood();
    StringBuffer buffer = new StringBuffer();
    TreeUtils.newick(treeModel, treeModel.getRoot(), false, TreeUtils.BranchLengthType.LENGTHS_AS_TIME, null, null, new TreeTraitProvider[] { treeLikelihood }, null, buffer);
    System.out.println(buffer);
    System.out.println("t_CA(2) = " + t(false, 2.0));
    System.out.println("t_CC(1) = " + t(true, 1.0));
    double trueValue = 0.25 * t(false, 2.0) * Math.pow(t(true, 1.0), 3.0);
    assertEquals(logLike, Math.log(trueValue), 1e-6);
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) Taxon(dr.evolution.util.Taxon) AncestralStateTreeLikelihood(dr.oldevomodel.treelikelihood.AncestralStateTreeLikelihood) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel) Sequence(dr.evolution.sequence.Sequence) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates) TreeModel(dr.evomodel.tree.TreeModel) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel) Taxa(dr.evolution.util.Taxa) GammaSiteModel(dr.oldevomodel.sitemodel.GammaSiteModel) SimpleAlignment(dr.evolution.alignment.SimpleAlignment) HKY(dr.oldevomodel.substmodel.HKY) Parameter(dr.inference.model.Parameter)

Example 45 with DefaultTreeModel

use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.

the class MsatFullLikelihoodTest method setUpExample3.

private void setUpExample3() throws Exception {
    // taxa
    ArrayList<Taxon> taxonList3 = new ArrayList<Taxon>();
    Collections.addAll(taxonList3, new Taxon("taxon1"), new Taxon("taxon2"), new Taxon("taxon3"), new Taxon("taxon4"), new Taxon("taxon5"), new Taxon("taxon6"), new Taxon("taxon7"));
    Taxa taxa3 = new Taxa(taxonList3);
    // msat datatype
    Microsatellite msat = new Microsatellite(1, 4);
    Patterns msatPatterns = new Patterns(msat, taxa3);
    // pattern in the correct code form.
    msatPatterns.addPattern(new int[] { 0, 3, 1, 2, 3, 0, 1 });
    // create tree
    NewickImporter importer = new NewickImporter("(((taxon1:0.3,taxon2:0.3):0.6,taxon3:0.9):0.9,((taxon4:0.5,taxon5:0.5):0.3,(taxon6:0.7,taxon7:0.7):0.1):1.0);");
    Tree tree = importer.importTree(null);
    // treeModel
    TreeModel treeModel = new DefaultTreeModel(tree);
    // msatsubstModel
    AsymmetricQuadraticModel aqm3 = new AsymmetricQuadraticModel(msat, null);
    // siteModel
    GammaSiteModel siteModel = new GammaSiteModel(aqm3);
    // treeLikelihood
    treeLikelihood3 = new TreeLikelihood(msatPatterns, treeModel, siteModel, null, null, false, false, true, false, false);
}
Also used : Microsatellite(dr.evolution.datatype.Microsatellite) Taxon(dr.evolution.util.Taxon) ArrayList(java.util.ArrayList) TreeLikelihood(dr.oldevomodel.treelikelihood.TreeLikelihood) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel) Taxa(dr.evolution.util.Taxa) TreeModel(dr.evomodel.tree.TreeModel) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel) GammaSiteModel(dr.oldevomodel.sitemodel.GammaSiteModel) NewickImporter(dr.evolution.io.NewickImporter) AsymmetricQuadraticModel(dr.oldevomodel.substmodel.AsymmetricQuadraticModel) Tree(dr.evolution.tree.Tree) Patterns(dr.evolution.alignment.Patterns)

Aggregations

DefaultTreeModel (dr.evomodel.tree.DefaultTreeModel)46 Tree (dr.evolution.tree.Tree)21 NewickImporter (dr.evolution.io.NewickImporter)19 Parameter (dr.inference.model.Parameter)19 ArrayList (java.util.ArrayList)14 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)10 TreeModel (dr.evomodel.tree.TreeModel)10 GammaSiteModel (dr.oldevomodel.sitemodel.GammaSiteModel)10 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)9 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)9 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)9 TreeLikelihood (dr.oldevomodel.treelikelihood.TreeLikelihood)9 BeagleSequenceSimulator (dr.app.beagle.tools.BeagleSequenceSimulator)8 Partition (dr.app.beagle.tools.Partition)8 ImportException (dr.evolution.io.Importer.ImportException)8 Taxa (dr.evolution.util.Taxa)8 Taxon (dr.evolution.util.Taxon)8 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)8 IOException (java.io.IOException)8 ExchangeOperator (dr.evomodel.operators.ExchangeOperator)7