use of dr.evolution.tree.SimpleNode 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));
}
use of dr.evolution.tree.SimpleNode in project beast-mcmc by beast-dev.
the class AlloppDiploidHistory method makesimpletree.
// for testing
private SimpleTree makesimpletree() {
SimpleNode[] snodes = new SimpleNode[dhnodes.length];
for (int n = 0; n < dhnodes.length; n++) {
snodes[n] = new SimpleNode();
// I use taxon==null to identify joined leg node when removing hybtips
snodes[n].setTaxon(null);
}
makesimplesubtree(snodes, 0, dhnodes[rootn]);
return new SimpleTree(snodes[dhnodes.length - 1]);
}
use of dr.evolution.tree.SimpleNode in project beast-mcmc by beast-dev.
the class SimpleNodeParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
SimpleNode node = new SimpleNode();
Taxon taxon = null;
if (xo.hasAttribute(HEIGHT)) {
node.setHeight(xo.getDoubleAttribute(HEIGHT));
}
if (xo.hasAttribute(RATE)) {
node.setRate(xo.getDoubleAttribute(RATE));
}
for (int i = 0; i < xo.getChildCount(); i++) {
Object child = xo.getChild(i);
if (child instanceof dr.evolution.tree.SimpleNode) {
node.addChild((dr.evolution.tree.SimpleNode) child);
} else if (child instanceof Taxon) {
taxon = (Taxon) child;
} else if (child instanceof Date) {
node.setAttribute("date", child);
} else if (child instanceof Attribute) {
Attribute attr = (Attribute) child;
String name = attr.getAttributeName();
Object value = attr.getAttributeValue();
node.setAttribute(name, value);
} else if (child instanceof Attribute[]) {
Attribute[] attrs = (Attribute[]) child;
for (int j = 0; j < attrs.length; j++) {
String name = attrs[j].getAttributeName();
Object value = attrs[j].getAttributeValue();
node.setAttribute(name, value);
}
} else if (child instanceof XMLObject) {
XMLObject xoc = (XMLObject) child;
if (xoc.getName().equals(Attributable.ATTRIBUTE)) {
node.setAttribute(xoc.getStringAttribute(Attributable.NAME), xoc.getAttribute(Attributable.VALUE));
} else {
throw new XMLParseException("Unrecognized element" + xoc.getName() + " found in node element!");
}
} else {
throw new XMLParseException("Unrecognized element found in node element!");
}
}
if (taxon != null) {
node.setTaxon(taxon);
}
return node;
}
Aggregations