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