Search in sources :

Example 36 with Date

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

the class PartitionedTreeModelParser method setPrecisionBounds.

private void setPrecisionBounds(Parameter newParameter, Taxon taxon) {
    Date date = taxon.getDate();
    if (date != null) {
        double precision = date.getUncertainty();
        if (precision > 0.0) {
            // taxon date not specified to exact value so add appropriate bounds
            double upper = Taxon.getHeightFromDate(date);
            double lower = Taxon.getHeightFromDate(date);
            if (date.isBackwards()) {
                upper += precision;
            } else {
                lower -= precision;
            }
            // set the bounds for the given precision
            newParameter.addBounds(new Parameter.DefaultBounds(upper, lower, 1));
            // set the initial value to be mid-point
            newParameter.setParameterValue(0, (upper + lower) / 2);
        }
    }
}
Also used : CompoundParameter(dr.inference.model.CompoundParameter) Parameter(dr.inference.model.Parameter) Date(dr.evolution.util.Date)

Example 37 with Date

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

the class TemporalRooting method setHeightsFromDates.

private void setHeightsFromDates(FlexibleTree tree) {
    dr.evolution.util.Date mostRecent = null;
    for (int i = 0; i < taxa.getTaxonCount(); i++) {
        Date date = taxa.getTaxon(i).getDate();
        if ((date != null) && (mostRecent == null || date.after(mostRecent))) {
            mostRecent = date;
        }
    }
    if (mostRecent != null) {
        TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
        double time0 = timeScale.convertTime(mostRecent.getTimeValue(), mostRecent);
        for (int i = 0; i < tree.getExternalNodeCount(); i++) {
            NodeRef tip = tree.getExternalNode(i);
            Date date = tree.getNodeTaxon(tip).getDate();
            if (date != null) {
                tree.setNodeHeight(tip, timeScale.convertTime(date.getTimeValue(), date) - time0);
            } else {
                tree.setNodeHeight(tip, 0.0);
            }
        }
    }
}
Also used : java.util(java.util) dr.evolution.util(dr.evolution.util) Date(dr.evolution.util.Date) Date(dr.evolution.util.Date)

Example 38 with Date

use of dr.evolution.util.Date 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 39 with Date

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

the class TransmissionTreeToVirusTree method makeTreelet.

// events are only relevant if there is a sampling event somewhere further up the tree
private FlexibleTree makeTreelet(InfectedUnit unit, ArrayList<Event> relevantEvents) {
    if (relevantEvents.size() == 0) {
        return null;
    }
    ArrayList<SimpleNode> nodes = new ArrayList<SimpleNode>();
    unit.sortEvents();
    double lastRelevantEventTime = Double.NEGATIVE_INFINITY;
    for (Event event : relevantEvents) {
        if (event.time > lastRelevantEventTime) {
            lastRelevantEventTime = event.time;
        }
    }
    double activeTime = lastRelevantEventTime - unit.infectionEvent.time;
    for (Event event : relevantEvents) {
        Taxon taxon;
        if (event.type == EventType.INFECTION) {
            taxon = new Taxon(event.infectee.id + "_infected_by_" + event.infector.id + "_" + event.time);
        } else {
            taxon = new Taxon(unit.id + "_sampled_" + event.time);
        }
        taxon.setDate(new Date(event.time - unit.infectionEvent.time, Units.Type.YEARS, false));
        SimpleNode node = new SimpleNode();
        node.setTaxon(taxon);
        nodes.add(node);
        node.setHeight(unit.infectionEvent.time - event.time);
        node.setAttribute("Event", event);
    }
    FlexibleNode treeletRoot;
    if (nodes.size() > 1) {
        treeletRoot = simulateCoalescent(nodes, demFunct, activeTime);
    } else {
        treeletRoot = new FlexibleNode(new SimpleTree(nodes.get(0)), nodes.get(0), true);
        treeletRoot.setHeight(0);
    }
    // add the root branch length
    FlexibleNode infectionNode = new FlexibleNode();
    infectionNode.setHeight(activeTime);
    infectionNode.addChild(treeletRoot);
    treeletRoot.setLength(activeTime - treeletRoot.getHeight());
    infectionNode.setAttribute("Event", unit.infectionEvent);
    FlexibleTree outTree = new FlexibleTree(infectionNode);
    for (int i = 0; i < outTree.getNodeCount(); i++) {
        FlexibleNode node = (FlexibleNode) outTree.getNode(i);
        node.setAttribute("Unit", unit.id);
    }
    return outTree;
}
Also used : Taxon(dr.evolution.util.Taxon) ArrayList(java.util.ArrayList) Date(dr.evolution.util.Date)

Aggregations

Date (dr.evolution.util.Date)39 Taxon (dr.evolution.util.Taxon)19 Taxa (dr.evolution.util.Taxa)5 TimeScale (dr.evolution.util.TimeScale)5 Parameter (dr.inference.model.Parameter)5 java.util (java.util)5 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)4 Sequence (dr.evolution.sequence.Sequence)4 NodeRef (dr.evolution.tree.NodeRef)4 dr.evolution.util (dr.evolution.util)4 Units (dr.evolution.util.Units)3 CompoundParameter (dr.inference.model.CompoundParameter)3 ArrayList (java.util.ArrayList)3 Tree (dr.evolution.tree.Tree)2 TaxonList (dr.evolution.util.TaxonList)2 Attribute (dr.util.Attribute)2 NexusExporter (dr.app.tools.NexusExporter)1 CoalescentSimulator (dr.evolution.coalescent.CoalescentSimulator)1 PiecewiseLinearPopulation (dr.evolution.coalescent.PiecewiseLinearPopulation)1 SimpleNode (dr.evolution.tree.SimpleNode)1