Debian Codegraph @

src/dr/evoxml/NewickParser.java

raw symbols SCIP
1 /*
2 * NewickParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Alexei Drummond
40 * @author Andrew Rambaut
41 * @version $Id: NewickParser.java,v 1.7 2006/04/25 14:41:08 rambaut Exp $
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 // boolean usingDates = xo.getAttribute(USING_DATES, true);
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 // System.out.println("UsingDates=" + usingDates + " usingHeights= " + usingHeights);
84
85 if (usingDates && usingHeights) {
86 throw new XMLParseException("Unable to use both dates and node heights. Specify value of usingDates attribute.");
87 }
88 // else if (!usingDates && !usingHeights) {
89 // System.out.println("Tree is assumed to be ultrametric");
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 // are all the tips just being translated by a fixed amount?
149 // in which case we can just translate the internal nodes.
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 }// END: i loop
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 // not using dates or heights
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 }// END: i loop
220
221 } else {
222
223 System.out.println("Using node heights.");
224
225 }// END: usingDates check
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 //System.out.println("Constructed newick tree = " + Tree.Utils.uniqueNewick(tree, tree.getRoot()));
245 return tree;
246 }
247
248 //************************************************************************
249 // AbstractXMLObjectParser implementation
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 }