Search in sources :

Example 1 with SimpleTree

use of dr.evolution.tree.SimpleTree in project beast-mcmc by beast-dev.

the class TestCalibratedYuleModel method calibration.

private Tree calibration(final TaxonList taxa, DemographicModel demoModel) throws Exception {
    dr.evolution.coalescent.CoalescentSimulator simulator = new dr.evolution.coalescent.CoalescentSimulator();
    DemographicFunction demoFunction = demoModel.getDemographicFunction();
    SimpleNode[] firstHalfNodes = new SimpleNode[taxa.getTaxonCount() / 2];
    SimpleNode[] secondHalfNodes = new SimpleNode[taxa.getTaxonCount() - taxa.getTaxonCount() / 2];
    for (int i = 0; i < firstHalfNodes.length; i++) {
        firstHalfNodes[i] = new SimpleNode();
        firstHalfNodes[i].setTaxon(taxa.getTaxon(i));
    }
    for (int i = 0; i < secondHalfNodes.length; i++) {
        secondHalfNodes[i] = new SimpleNode();
        secondHalfNodes[i].setTaxon(taxa.getTaxon(i + taxa.getTaxonCount() / 2));
    }
    SimpleNode firstHalfRootNode = simulator.simulateCoalescent(firstHalfNodes, demoFunction);
    SimpleNode[] restNodes = simulator.simulateCoalescent(secondHalfNodes, demoFunction, 0, firstHalfRootNode.getHeight());
    SimpleNode[] together = new SimpleNode[restNodes.length + 1];
    for (int i = 0; i < restNodes.length; i++) {
        together[i + 1] = restNodes[i];
    }
    together[0] = firstHalfRootNode;
    SimpleNode root = simulator.simulateCoalescent(together, demoFunction);
    Tree tree = new SimpleTree(root);
    return tree;
}
Also used : DemographicFunction(dr.evolution.coalescent.DemographicFunction) SimpleTree(dr.evolution.tree.SimpleTree) Tree(dr.evolution.tree.Tree) SimpleTree(dr.evolution.tree.SimpleTree) SimpleNode(dr.evolution.tree.SimpleNode)

Example 2 with SimpleTree

use of dr.evolution.tree.SimpleTree in project beast-mcmc by beast-dev.

the class TraceCorrelationAssert method createPrimateTreeModel.

//************************** data ****************************
protected TreeModel createPrimateTreeModel() {
    SimpleNode[] nodes = new SimpleNode[10];
    for (int n = 0; n < 10; n++) {
        nodes[n] = new SimpleNode();
    }
    //        nodes[0].setHeight(0);
    // human
    nodes[0].setTaxon(taxa[0]);
    // chimp
    nodes[1].setTaxon(taxa[1]);
    // bonobo
    nodes[2].setTaxon(taxa[2]);
    nodes[3].setHeight(0.010772);
    nodes[3].addChild(nodes[1]);
    nodes[3].addChild(nodes[2]);
    nodes[4].setHeight(0.024003);
    nodes[4].addChild(nodes[0]);
    nodes[4].addChild(nodes[3]);
    // gorilla
    nodes[5].setTaxon(taxa[3]);
    nodes[6].setHeight(0.036038);
    nodes[6].addChild(nodes[4]);
    nodes[6].addChild(nodes[5]);
    // orangutan
    nodes[7].setTaxon(taxa[4]);
    nodes[8].setHeight(0.069125);
    nodes[8].addChild(nodes[6]);
    nodes[8].addChild(nodes[7]);
    // siamang
    nodes[9].setTaxon(taxa[5]);
    SimpleNode root = new SimpleNode();
    root.setHeight(0.099582);
    root.addChild(nodes[8]);
    root.addChild(nodes[9]);
    Tree tree = new SimpleTree(root);
    tree.setUnits(Units.Type.YEARS);
    //treeModel
    return new TreeModel(tree);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) SimpleTree(dr.evolution.tree.SimpleTree) Tree(dr.evolution.tree.Tree) SimpleTree(dr.evolution.tree.SimpleTree) SimpleNode(dr.evolution.tree.SimpleNode)

Example 3 with SimpleTree

use of dr.evolution.tree.SimpleTree in project beast-mcmc by beast-dev.

the class NormalizedSequenceLikelihoodTest method createSillyTreeModel.

protected TreeModel createSillyTreeModel(int numTaxa) {
    SimpleNode[] nodes = new SimpleNode[2 * numTaxa - 1];
    for (int n = 0; n < 2 * numTaxa - 1; n++) {
        nodes[n] = new SimpleNode();
    }
    nodes[0].setTaxon(taxa[0]);
    nodes[1].setTaxon(taxa[1]);
    nodes[2].setHeight(1);
    nodes[2].addChild(nodes[0]);
    nodes[2].addChild(nodes[1]);
    SimpleNode root = nodes[2];
    if (numTaxa == 3) {
        nodes[3].setTaxon(taxa[2]);
        nodes[4].setHeight(2);
        nodes[4].addChild(nodes[2]);
        nodes[4].addChild(nodes[3]);
        root = nodes[4];
    }
    Tree tree = new SimpleTree(root);
    tree.setUnits(Units.Type.YEARS);
    //treeModel
    return new TreeModel(tree);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) Tree(dr.evolution.tree.Tree) SimpleTree(dr.evolution.tree.SimpleTree) SimpleTree(dr.evolution.tree.SimpleTree) SimpleNode(dr.evolution.tree.SimpleNode)

Example 4 with SimpleTree

use of dr.evolution.tree.SimpleTree in project beast-mcmc by beast-dev.

the class SimpleTreeParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Units.Type units = XMLUnits.Utils.getUnitsAttr(xo);
    boolean usingDates = xo.getAttribute(USING_DATES, false);
    SimpleNode root = (SimpleNode) xo.getChild(SimpleNode.class);
    if (root == null) {
        throw new XMLParseException("node element missing from tree");
    }
    SimpleTree tree = new SimpleTree(root);
    if (usingDates) {
        dr.evolution.util.Date mostRecent = null;
        for (int i = 0; i < tree.getTaxonCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, DATE);
            if (date == null) {
                date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), DATE);
            }
            if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
                mostRecent = date;
            }
        }
        for (int i = 0; i < tree.getInternalNodeCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), DATE);
            if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
                mostRecent = date;
            }
        }
        if (mostRecent == null) {
            throw new XMLParseException("no date elements in tree (and usingDates attribute set)");
        }
        TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
        for (int i = 0; i < tree.getTaxonCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, DATE);
            if (date == null) {
                date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), DATE);
            }
            if (date != null) {
                double height = timeScale.convertTime(date.getTimeValue(), date);
                tree.setNodeHeight(tree.getExternalNode(i), height);
            }
        }
        for (int i = 0; i < tree.getInternalNodeCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), DATE);
            if (date != null) {
                double height = timeScale.convertTime(date.getTimeValue(), date);
                tree.setNodeHeight(tree.getInternalNode(i), height);
            }
        }
        MutableTree.Utils.correctHeightsForTips(tree);
    }
    tree.setUnits(units);
    return tree;
}
Also used : SimpleTree(dr.evolution.tree.SimpleTree) TimeScale(dr.evolution.util.TimeScale) Units(dr.evolution.util.Units) XMLUnits(dr.evoxml.util.XMLUnits) SimpleNode(dr.evolution.tree.SimpleNode)

Example 5 with SimpleTree

use of dr.evolution.tree.SimpleTree in project beast-mcmc by beast-dev.

the class StructuredCoalescentSimulator method simulateTree.

/**
     * Simulates a coalescent tree, given a taxon list.
     *
     * @param taxa          the set of taxa to simulate a coalescent tree between
     * @param demoFunctions the demographic function to use
     */
public Tree simulateTree(TaxonList[] taxa, DemographicFunction[] demoFunctions, ColourChangeMatrix colourChangeMatrix) {
    SimpleNode[][] nodes = new SimpleNode[taxa.length][];
    for (int i = 0; i < taxa.length; i++) {
        nodes[i] = new SimpleNode[taxa[i].getTaxonCount()];
        for (int j = 0; j < taxa[i].getTaxonCount(); j++) {
            nodes[i][j] = new SimpleNode();
            nodes[i][j].setTaxon(taxa[i].getTaxon(j));
        }
    }
    dr.evolution.util.Date mostRecent = null;
    boolean usingDates = false;
    for (int i = 0; i < taxa.length; i++) {
        for (int j = 0; j < taxa[i].getTaxonCount(); j++) {
            if (TaxonList.Utils.hasAttribute(taxa[i], j, dr.evolution.util.Date.DATE)) {
                usingDates = true;
                dr.evolution.util.Date date = (dr.evolution.util.Date) taxa[i].getTaxonAttribute(j, dr.evolution.util.Date.DATE);
                if ((date != null) && (mostRecent == null || date.after(mostRecent))) {
                    mostRecent = date;
                }
            } else {
                // assume contemporaneous tips
                nodes[i][j].setHeight(0.0);
            }
        }
    }
    if (usingDates) {
        assert mostRecent != null;
        TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
        for (int i = 0; i < taxa.length; i++) {
            for (int j = 0; j < taxa[i].getTaxonCount(); j++) {
                dr.evolution.util.Date date = (dr.evolution.util.Date) taxa[i].getTaxonAttribute(j, dr.evolution.util.Date.DATE);
                if (date == null) {
                    throw new IllegalArgumentException("Taxon, " + taxa[i].getTaxonId(j) + ", is missing its date");
                }
                nodes[i][j].setHeight(timeScale.convertTime(date.getTimeValue(), date));
            }
            if (demoFunctions[0].getUnits() != mostRecent.getUnits()) {
            //throw new IllegalArgumentException("The units of the demographic model and the most recent date must match!");
            }
        }
    }
    return new SimpleTree(simulateCoalescent(nodes, demoFunctions, colourChangeMatrix));
}
Also used : TimeScale(dr.evolution.util.TimeScale) SimpleTree(dr.evolution.tree.SimpleTree) SimpleNode(dr.evolution.tree.SimpleNode)

Aggregations

SimpleTree (dr.evolution.tree.SimpleTree)12 SimpleNode (dr.evolution.tree.SimpleNode)7 Tree (dr.evolution.tree.Tree)7 TaxonList (dr.evolution.util.TaxonList)4 Taxa (dr.evolution.util.Taxa)3 CoalescentSimulator (dr.evomodel.coalescent.CoalescentSimulator)3 ArrayList (java.util.ArrayList)3 Taxon (dr.evolution.util.Taxon)2 TimeScale (dr.evolution.util.TimeScale)2 TreeModel (dr.evomodel.tree.TreeModel)2 XMLObject (dr.xml.XMLObject)2 XMLParseException (dr.xml.XMLParseException)2 DemographicFunction (dr.evolution.coalescent.DemographicFunction)1 NexusImporter (dr.evolution.io.NexusImporter)1 NodeRef (dr.evolution.tree.NodeRef)1 Units (dr.evolution.util.Units)1 DemographicModel (dr.evomodel.coalescent.DemographicModel)1 XMLUnits (dr.evoxml.util.XMLUnits)1 ParametricDistributionModel (dr.inference.distribution.ParametricDistributionModel)1 BufferedReader (java.io.BufferedReader)1