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);
}
}
}
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);
}
}
}
}
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());
}
}
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;
}
Aggregations