| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evoxml; |
| 27 |
|
| 28 |
import dr.evolution.tree.MutableTree; |
| 29 |
import dr.evolution.tree.SimpleNode; |
| 30 |
import dr.evolution.tree.SimpleTree; |
| 31 |
import dr.evolution.tree.Tree; |
| 32 |
import dr.evolution.util.TimeScale; |
| 33 |
import dr.evolution.util.Units; |
| 34 |
import dr.evoxml.util.XMLUnits; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class SimpleTreeParser extends AbstractXMLObjectParser { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static final String SIMPLE_TREE = "tree"; |
| 49 |
public static final String USING_DATES = "usingDates"; |
| 50 |
public static final String DATE = "date"; |
| 51 |
|
| 52 |
public String getParserName() { |
| 53 |
return SIMPLE_TREE; |
| 54 |
} |
| 55 |
|
| 56 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 57 |
|
| 58 |
Units.Type units = XMLUnits.Utils.getUnitsAttr(xo); |
| 59 |
boolean usingDates = xo.getAttribute(USING_DATES, false); |
| 60 |
|
| 61 |
SimpleNode root = (SimpleNode) xo.getChild(SimpleNode.class); |
| 62 |
|
| 63 |
if (root == null) { |
| 64 |
throw new XMLParseException("node element missing from tree"); |
| 65 |
} |
| 66 |
|
| 67 |
SimpleTree tree = new SimpleTree(root); |
| 68 |
|
| 69 |
if (usingDates) { |
| 70 |
|
| 71 |
dr.evolution.util.Date mostRecent = null; |
| 72 |
for (int i = 0; i < tree.getTaxonCount(); i++) { |
| 73 |
|
| 74 |
dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, DATE); |
| 75 |
|
| 76 |
if (date == null) { |
| 77 |
date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), DATE); |
| 78 |
} |
| 79 |
|
| 80 |
if (date != null && ((mostRecent == null) || date.after(mostRecent))) { |
| 81 |
mostRecent = date; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 86 |
dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), DATE); |
| 87 |
|
| 88 |
if (date != null && ((mostRecent == null) || date.after(mostRecent))) { |
| 89 |
mostRecent = date; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (mostRecent == null) { |
| 94 |
throw new XMLParseException("no date elements in tree (and usingDates attribute set)"); |
| 95 |
} |
| 96 |
|
| 97 |
TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue()); |
| 98 |
|
| 99 |
for (int i = 0; i < tree.getTaxonCount(); i++) { |
| 100 |
dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, DATE); |
| 101 |
|
| 102 |
if (date == null) { |
| 103 |
date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), DATE); |
| 104 |
} |
| 105 |
|
| 106 |
if (date != null) { |
| 107 |
double height = timeScale.convertTime(date.getTimeValue(), date); |
| 108 |
tree.setNodeHeight(tree.getExternalNode(i), height); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 113 |
dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), DATE); |
| 114 |
|
| 115 |
if (date != null) { |
| 116 |
double height = timeScale.convertTime(date.getTimeValue(), date); |
| 117 |
tree.setNodeHeight(tree.getInternalNode(i), height); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
MutableTree.Utils.correctHeightsForTips(tree); |
| 122 |
} |
| 123 |
|
| 124 |
tree.setUnits(units); |
| 125 |
|
| 126 |
return tree; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public String getParserDescription() { |
| 134 |
return "This element represents a rooted binary tree and associated attributes."; |
| 135 |
} |
| 136 |
|
| 137 |
public Class getReturnType() { |
| 138 |
return Tree.class; |
| 139 |
} |
| 140 |
|
| 141 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 142 |
return rules; |
| 143 |
} |
| 144 |
|
| 145 |
private final XMLSyntaxRule[] rules = { |
| 146 |
XMLUnits.SYNTAX_RULES[0], |
| 147 |
new ElementRule(SimpleNode.class) |
| 148 |
}; |
| 149 |
} |