| 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.io.Importer; |
| 29 |
import dr.evolution.io.NewickImporter; |
| 30 |
import dr.evolution.tree.*; |
| 31 |
import dr.evolution.util.Taxon; |
| 32 |
import dr.evolution.util.Units; |
| 33 |
import dr.evoxml.util.XMLUnits; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.io.IOException; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class NewickParser extends AbstractXMLObjectParser { |
| 44 |
|
| 45 |
public static final String NEWICK = "newick"; |
| 46 |
public static final String UNITS = "units"; |
| 47 |
public static final String RESCALE_HEIGHT = "rescaleHeight"; |
| 48 |
public static final String RESCALE_LENGTH = "rescaleLength"; |
| 49 |
public static final String USING_DATES = SimpleTreeParser.USING_DATES; |
| 50 |
public static final String USING_HEIGHTS = "usingHeights"; |
| 51 |
|
| 52 |
public NewickParser() { |
| 53 |
rules = new XMLSyntaxRule[]{ |
| 54 |
AttributeRule.newBooleanRule(USING_DATES, true), |
| 55 |
AttributeRule.newBooleanRule(USING_HEIGHTS, true), |
| 56 |
AttributeRule.newDoubleRule(RESCALE_HEIGHT, true, "Attempt to rescale the tree to the given root height"), |
| 57 |
AttributeRule.newDoubleRule(RESCALE_LENGTH, true, "Attempt to rescale the tree to the given total length"), |
| 58 |
new StringAttributeRule(UNITS, "The branch length units of this tree", Units.UNIT_NAMES, true), |
| 59 |
new ElementRule(String.class, "The NEWICK format tree. Tip labels are taken to be Taxon IDs") |
| 60 |
}; |
| 61 |
} |
| 62 |
|
| 63 |
public String getParserName() { |
| 64 |
return NEWICK; |
| 65 |
} |
| 66 |
|
| 67 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 68 |
|
| 69 |
final Units.Type units = XMLUnits.Utils.getUnitsAttr(xo); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
boolean usingDates = true; |
| 74 |
if (xo.hasAttribute(USING_DATES)) { |
| 75 |
usingDates = xo.getAttribute(USING_DATES, true); |
| 76 |
} |
| 77 |
|
| 78 |
boolean usingHeights = false; |
| 79 |
if (xo.hasAttribute(USING_HEIGHTS)) { |
| 80 |
usingHeights = xo.getAttribute(USING_HEIGHTS, true); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
if (usingDates && usingHeights) { |
| 86 |
throw new XMLParseException("Unable to use both dates and node heights. Specify value of usingDates attribute."); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
StringBuffer buffer = new StringBuffer(); |
| 93 |
|
| 94 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 95 |
if (xo.getChild(i) instanceof String) { |
| 96 |
buffer.append((String) xo.getChild(i)); |
| 97 |
} else { |
| 98 |
throw new XMLParseException("illegal element in newick element"); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
java.io.Reader reader = new java.io.StringReader(buffer.toString()); |
| 103 |
NewickImporter importer = new NewickImporter(reader); |
| 104 |
|
| 105 |
FlexibleTree tree; |
| 106 |
|
| 107 |
try { |
| 108 |
tree = (FlexibleTree) importer.importTree(null); |
| 109 |
} catch (IOException ioe) { |
| 110 |
throw new XMLParseException("error parsing tree in newick element"); |
| 111 |
} catch (NewickImporter.BranchMissingException bme) { |
| 112 |
throw new XMLParseException("branch missing in tree in newick element"); |
| 113 |
} catch (Importer.ImportException ime) { |
| 114 |
throw new XMLParseException("error parsing tree in newick element - " + ime.getMessage()); |
| 115 |
} |
| 116 |
|
| 117 |
if (tree == null) { |
| 118 |
throw new XMLParseException("Failed to read tree"); |
| 119 |
} |
| 120 |
|
| 121 |
tree.setUnits(units); |
| 122 |
|
| 123 |
for (int i = 0; i < tree.getTaxonCount(); i++) { |
| 124 |
|
| 125 |
FlexibleNode node = (FlexibleNode) tree.getExternalNode(i); |
| 126 |
|
| 127 |
String id = node.getTaxon().getId(); |
| 128 |
Taxon taxon = null; |
| 129 |
|
| 130 |
XMLObject obj = getStore().get(id); |
| 131 |
|
| 132 |
if (obj != null && obj.getNativeObject() instanceof Taxon) { |
| 133 |
|
| 134 |
taxon = (Taxon) obj.getNativeObject(); |
| 135 |
} |
| 136 |
|
| 137 |
if (taxon != null) { |
| 138 |
|
| 139 |
node.setTaxon(taxon); |
| 140 |
|
| 141 |
} else { |
| 142 |
throw new XMLParseException("unknown taxon, " + id + ", in newick tree"); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (usingDates) { |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
double fixedDiff = 0.0; |
| 151 |
boolean translateNodes = true; |
| 152 |
|
| 153 |
for (int i = 0; i < tree.getTaxonCount(); i++) { |
| 154 |
|
| 155 |
NodeRef node = tree.getExternalNode(i); |
| 156 |
|
| 157 |
dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, dr.evolution.util.Date.DATE); |
| 158 |
|
| 159 |
if (date == null) { |
| 160 |
date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), dr.evolution.util.Date.DATE); |
| 161 |
} |
| 162 |
|
| 163 |
double height = 0.0; |
| 164 |
double nodeHeight = tree.getNodeHeight(node); |
| 165 |
if (date != null) { |
| 166 |
height = Taxon.getHeightFromDate(date); |
| 167 |
} |
| 168 |
|
| 169 |
double diff = height - nodeHeight; |
| 170 |
|
| 171 |
if (i == 0) { |
| 172 |
fixedDiff = diff; |
| 173 |
} else if (Math.abs(diff - fixedDiff) > 1e-5) { |
| 174 |
translateNodes = false; |
| 175 |
} |
| 176 |
|
| 177 |
if (Math.abs(diff) > 1e-8 && (i == 0 || !translateNodes) ) { |
| 178 |
|
| 179 |
System.out.println(" Changing height of node " + tree.getTaxon(node.getNumber()) + " from " + nodeHeight + " to " + height); |
| 180 |
tree.setNodeHeight(node, height); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (translateNodes) { |
| 185 |
System.out.println(" Changing height of all nodes by " + fixedDiff); |
| 186 |
} |
| 187 |
|
| 188 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 189 |
NodeRef node = tree.getInternalNode(i); |
| 190 |
|
| 191 |
dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(node, dr.evolution.util.Date.DATE); |
| 192 |
|
| 193 |
if (date != null) { |
| 194 |
double height = Taxon.getHeightFromDate(date); |
| 195 |
tree.setNodeHeight(node, height); |
| 196 |
} else if (translateNodes) { |
| 197 |
tree.setNodeHeight(node, tree.getNodeHeight(node) + fixedDiff); |
| 198 |
} |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
MutableTree.Utils.correctHeightsForTips(tree); |
| 203 |
|
| 204 |
} else if (!usingDates && !usingHeights) { |
| 205 |
|
| 206 |
System.out.println("Tree is assumed to be ultrametric"); |
| 207 |
|
| 208 |
|
| 209 |
for (int i = 0; i < tree.getTaxonCount(); i++) { |
| 210 |
final NodeRef leaf = tree.getExternalNode(i); |
| 211 |
final double h = tree.getNodeHeight(leaf); |
| 212 |
|
| 213 |
if (h != 0.0) { |
| 214 |
double zero = 0.0; |
| 215 |
System.out.println(" Changing height of leaf node " + tree.getTaxon(leaf.getNumber()) + " from " + h + " to " + zero); |
| 216 |
tree.setNodeHeight(leaf, zero); |
| 217 |
} |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
} else { |
| 222 |
|
| 223 |
System.out.println("Using node heights."); |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
if (xo.hasAttribute(RESCALE_HEIGHT)) { |
| 228 |
double rescaleHeight = xo.getDoubleAttribute(RESCALE_HEIGHT); |
| 229 |
double scale = rescaleHeight / tree.getNodeHeight(tree.getRoot()); |
| 230 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 231 |
NodeRef n = tree.getInternalNode(i); |
| 232 |
tree.setNodeHeight(n, tree.getNodeHeight(n) * scale); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if (xo.hasAttribute(RESCALE_LENGTH)) { |
| 237 |
double rescaleLength = xo.getDoubleAttribute(RESCALE_LENGTH); |
| 238 |
double scale = rescaleLength / TreeUtils.getTreeLength(tree, tree.getRoot()); |
| 239 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 240 |
NodeRef n = tree.getInternalNode(i); |
| 241 |
tree.setNodeHeight(n, tree.getNodeHeight(n) * scale); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
return tree; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public String getParserDescription() { |
| 253 |
return "Constructs a tree from a NEWICK format tree description"; |
| 254 |
} |
| 255 |
|
| 256 |
public String getExample() { |
| 257 |
return "<" + getParserName() + " " + UNITS + "=\"" + Units.Utils.getDefaultUnitName(Units.Type.YEARS) + "\">" + " ((A:1.0, B:1.0):1.0,(C:2.0, D:2.0):1.0); </" + getParserName() + ">"; |
| 258 |
} |
| 259 |
|
| 260 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 261 |
return rules; |
| 262 |
} |
| 263 |
|
| 264 |
private final XMLSyntaxRule[] rules; |
| 265 |
|
| 266 |
public Class getReturnType() { |
| 267 |
return Tree.class; |
| 268 |
} |
| 269 |
} |