Search in sources :

Example 6 with TimeScale

use of dr.evolution.util.TimeScale in project beast-mcmc by beast-dev.

the class TreeUtils method setHeightsFromDates.

/**
     * Sets the tip heights from the tip dates
     */
public static void setHeightsFromDates(MutableTree tree) {
    dr.evolution.util.Date mostRecent = null;
    for (int i = 0; i < tree.getExternalNodeCount(); i++) {
        Taxon taxon = tree.getNodeTaxon(tree.getExternalNode(i));
        dr.evolution.util.Date date = (dr.evolution.util.Date) taxon.getAttribute("date");
        if (date != null) {
            if ((mostRecent == null) || date.after(mostRecent)) {
                mostRecent = date;
            }
        }
    }
    TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
    for (int i = 0; i < tree.getExternalNodeCount(); i++) {
        NodeRef node = tree.getExternalNode(i);
        Taxon taxon = tree.getNodeTaxon(node);
        dr.evolution.util.Date date = (dr.evolution.util.Date) taxon.getAttribute("date");
        if (date != null) {
            double height = timeScale.convertTime(date.getTimeValue(), date);
            tree.setNodeHeight(node, height);
        } else {
            tree.setNodeHeight(node, 0.0);
        }
    }
    adjustInternalHeights(tree, tree.getRoot());
    if (mostRecent != null) {
        tree.setUnits(mostRecent.getUnits());
    }
}
Also used : NodeRef(dr.evolution.tree.NodeRef) Taxon(dr.evolution.util.Taxon) Date(dr.evolution.util.Date) TimeScale(dr.evolution.util.TimeScale) Date(dr.evolution.util.Date)

Example 7 with TimeScale

use of dr.evolution.util.TimeScale in project beast-mcmc by beast-dev.

the class TreeUtils method setHeightsFromDates.

/**
     * Sets the tip heights from the tip dates
     */
public static void setHeightsFromDates(MutableTree tree) {
    dr.evolution.util.Date mostRecent = null;
    for (int i = 0; i < tree.getExternalNodeCount(); i++) {
        Taxon taxon = tree.getNodeTaxon(tree.getExternalNode(i));
        dr.evolution.util.Date date = (dr.evolution.util.Date) taxon.getAttribute("date");
        if (date != null) {
            if ((mostRecent == null) || date.after(mostRecent)) {
                mostRecent = date;
            }
        }
    }
    TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
    for (int i = 0; i < tree.getExternalNodeCount(); i++) {
        NodeRef node = tree.getExternalNode(i);
        Taxon taxon = tree.getNodeTaxon(node);
        dr.evolution.util.Date date = (dr.evolution.util.Date) taxon.getAttribute("date");
        if (date != null) {
            double height = timeScale.convertTime(date.getTimeValue(), date);
            tree.setNodeHeight(node, height);
        } else {
            tree.setNodeHeight(node, 0.0);
        }
    }
    adjustInternalHeights(tree, tree.getRoot());
    if (mostRecent != null) {
        tree.setUnits(mostRecent.getUnits());
    }
}
Also used : NodeRef(dr.evolution.tree.NodeRef) Taxon(dr.evolution.util.Taxon) Date(dr.evolution.util.Date) TimeScale(dr.evolution.util.TimeScale) Date(dr.evolution.util.Date)

Example 8 with TimeScale

use of dr.evolution.util.TimeScale 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

TimeScale (dr.evolution.util.TimeScale)8 Date (dr.evolution.util.Date)5 NodeRef (dr.evolution.tree.NodeRef)4 Taxon (dr.evolution.util.Taxon)3 SimpleNode (dr.evolution.tree.SimpleNode)2 SimpleTree (dr.evolution.tree.SimpleTree)2 DistanceMatrix (dr.evolution.distance.DistanceMatrix)1 UPGMATree (dr.evolution.tree.UPGMATree)1 Units (dr.evolution.util.Units)1 XMLUnits (dr.evoxml.util.XMLUnits)1