Debian Codegraph @

src/dr/evolution/io/NewickImporter.java

raw symbols SCIP
1 /*
2 * NewickImporter.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.evolution.io;
27
28 import dr.evolution.tree.FlexibleNode;
29 import dr.evolution.tree.FlexibleTree;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.util.Taxon;
32 import dr.evolution.util.TaxonList;
33
34 import java.io.EOFException;
35 import java.io.IOException;
36 import java.io.Reader;
37 import java.io.StringReader;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.Map;
41
42 /**
43 * Class for importing Newick tree file format
44 *
45 * @author Andrew Rambaut
46 * @author Alexei Drummond
47 * @version $Id: NewickImporter.java,v 1.20 2005/12/07 11:25:35 rambaut Exp $
48 */
49 public class NewickImporter extends Importer implements TreeImporter {
50 public static final String COMMENT = "comment";
51
52 public class BranchMissingException extends ImportException {
53 /**
54 *
55 */
56 private static final long serialVersionUID = 777435104809244693L;
57
58 public BranchMissingException() {
59 super();
60 }
61
62 public BranchMissingException(String msg) {
63 super("Branch missing: " + msg);
64 System.err.println(msg);
65 }
66 }
67
68 /**
69 * @param reader A reader to a source containing a tree in Newick format
70 */
71 public NewickImporter(Reader reader) {
72 super(reader);
73 }
74
75 /**
76 * @param treeString a string containing a tree in newick format
77 */
78 public NewickImporter(String treeString) {
79 this(new StringReader(treeString));
80 }
81
82 /**
83 * importTree.
84 */
85 public Tree importTree(TaxonList taxonList) throws IOException, ImportException {
86 setCommentDelimiters('[', ']', '\0', '\0', '&');
87
88 try {
89 skipUntil("(");
90 unreadCharacter('(');
91
92 final FlexibleNode root = readInternalNode(taxonList);
93 if (getLastMetaComment() != null) {
94 root.setAttribute(COMMENT, getLastMetaComment());
95 }
96 // if (getLastDelimiter() != ';') {
97 // throw new BadFormatException("Expecting ';' after tree");
98 // }
99
100 Map<Taxon, Integer> taxonNumberMap = null;
101 if (taxonList != null) {
102 taxonNumberMap = new HashMap<Taxon, Integer>();
103 int i = 0;
104 for (Taxon taxon : taxonList) {
105 taxonNumberMap.put(taxon, i);
106 i++;
107 }
108 }
109
110 return new FlexibleTree(root, false, true, taxonNumberMap);
111
112 } catch (EOFException e) {
113 throw new ImportException("incomplete tree");
114 }
115 }
116
117 /**
118 * importTrees.
119 */
120 public Tree[] importTrees(TaxonList taxonList) throws IOException, ImportException {
121 boolean done = false;
122 ArrayList<FlexibleTree> array = new ArrayList<FlexibleTree>();
123
124 do {
125
126 try {
127
128 skipUntil("(");
129 unreadCharacter('(');
130
131 FlexibleNode root = readInternalNode(taxonList);
132 FlexibleTree tree = new FlexibleTree(root, false, true);
133 array.add(tree);
134
135 if (taxonList == null) {
136 taxonList = tree;
137 }
138
139 if (readCharacter() != ';') {
140 throw new BadFormatException("Expecting ';' after tree");
141 }
142
143 } catch (EOFException e) {
144 done = true;
145 }
146 } while (!done);
147
148 Tree[] trees = new Tree[array.size()];
149 array.toArray(trees);
150
151 return trees;
152 }
153
154 /**
155 * return whether another tree is available.
156 */
157 public boolean hasTree() throws IOException, ImportException {
158 try {
159 skipUntil("(");
160 unreadCharacter('(');
161 } catch (EOFException e) {
162 lastTree = null;
163 return false;
164 }
165
166 return true;
167 }
168
169 private Tree lastTree = null;
170
171 /**
172 * import the next tree.
173 * return the tree or null if no more trees are available
174 */
175 public Tree importNextTree() throws IOException, ImportException {
176 FlexibleTree tree = null;
177
178 try {
179 skipUntil("(");
180 unreadCharacter('(');
181
182 FlexibleNode root = readInternalNode(lastTree);
183
184 tree = new FlexibleTree(root, false, true);
185
186 } catch (EOFException e) {
187 //
188 }
189
190 lastTree = tree;
191
192 return tree;
193 }
194
195 /**
196 * Reads a branch in. This could be a node or a tip (calls readNode or readTip
197 * accordingly). It then reads the branch length and SimpleNode that will
198 * point at the new node or tip.
199 */
200 private FlexibleNode readBranch(TaxonList taxonList) throws IOException, ImportException {
201 double length = 0.0;
202 FlexibleNode branch;
203
204 if (nextCharacter() == '(') {
205 // is an internal node
206 branch = readInternalNode(taxonList);
207
208 } else {
209 // is an external node
210 branch = readExternalNode(taxonList);
211 }
212
213 final String comment = getLastMetaComment();
214 if (comment != null) {
215 branch.setAttribute(COMMENT, comment);
216 clearLastMetaComment();
217 }
218
219 if (getLastDelimiter() == ':') {
220 length = readDouble(",():;");
221 }
222
223 branch.setLength(length);
224
225 return branch;
226 }
227
228 /**
229 * Reads a node in. This could be a polytomy. Calls readBranch on each branch
230 * in the node.
231 */
232 private FlexibleNode readInternalNode(TaxonList taxonList) throws IOException, ImportException {
233 FlexibleNode node = new FlexibleNode();
234
235 // read the opening '('
236 final char ch = readCharacter();
237 assert ch == '(';
238
239 // read the first child
240 node.addChild(readBranch(taxonList));
241
242 if (getLastDelimiter() != ',') {
243 java.util.logging.Logger.getLogger("dr.evolution.io").warning("Internal node only has a single child.");
244 }
245
246
247 // read subsequent children
248 while (getLastDelimiter() == ',') {
249 node.addChild(readBranch(taxonList));
250 }
251
252 // should have had a closing ')'
253 if (getLastDelimiter() != ')') {
254 throw new BadFormatException("Missing closing ')' in tree");
255 }
256
257 // If there is a label before the colon, store it:
258 try {
259 String label = readToken(",():;");
260 if (label.length() > 0) {
261 node.setAttribute("label", label);
262 }
263 } catch (IOException ioe) {
264 // probably an end of file without a terminal ';'
265 // we are going to allow this and return the nodes...
266 }
267
268 return node;
269 }
270
271 /**
272 * Reads an external node in.
273 */
274 private FlexibleNode readExternalNode(TaxonList taxonList) throws IOException, ImportException {
275 FlexibleNode node = new FlexibleNode();
276
277 String label = readToken(":(),;");
278
279 Taxon taxon;
280
281 if (taxonList != null) {
282 // if a taxon list is given then the taxon must be in it...
283 int index = taxonList.getTaxonIndex(label);
284 if (index != -1) {
285 taxon = taxonList.getTaxon(index);
286 } else {
287 throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown");
288 }
289 } else {
290 // No taxon list given so create new taxa
291 taxon = new Taxon(label);
292 }
293
294 node.setTaxon(taxon);
295 return node;
296 }
297
298 }