Debian Codegraph @

src/dr/evomodel/antigenic/phyloclustering/misc/obsolete/TiterImporter.java

raw symbols SCIP
1 package dr.evomodel.antigenic.phyloclustering.misc.obsolete;
2 /*
3 * NewickImporter.java
4 *
5 * Copyright (C) 2002-2010 Alexei Drummond and Andrew Rambaut
6 *
7 * This file is part of BEAST.
8 * See the NOTICE file distributed with this work for additional
9 * information regarding copyright ownership and licensing.
10 *
11 * BEAST is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as
13 * published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * BEAST is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with BEAST; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 */
26
27 //package dr.evolution.io;
28
29 import dr.evolution.io.Importer;
30 import dr.evolution.tree.FlexibleNode;
31 import dr.evolution.tree.FlexibleTree;
32 import dr.evolution.tree.Tree;
33 import dr.evolution.util.Taxon;
34 import dr.evolution.util.TaxonList;
35
36 import java.io.EOFException;
37 import java.io.IOException;
38 import java.io.Reader;
39 import java.io.StringReader;
40 import java.util.ArrayList;
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 TiterImporter extends Importer {
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 TiterImporter(Reader reader) {
72 super(reader);
73 }
74
75 /**
76 * @param treeString a string containing a tree in newick format
77 */
78 public TiterImporter(String treeString) {
79 this(new StringReader(treeString));
80 }
81
82 private String virusStrain[];
83 private String transformed_titer[];
84 private String titer[];
85
86 public void readTiter(int numNodes) throws IOException, ImportException{
87
88 virusStrain = new String[numNodes];
89 transformed_titer = new String[numNodes];
90 titer = new String[numNodes];
91
92 readToken();
93 readToken();
94 readToken();
95 readToken();
96 //System.out.println(readToken());
97 //System.out.println(readToken());
98 //System.out.println(readToken());
99 //System.out.println(readToken());
100
101 for(int i=0; i < (numNodes); i++){
102 //System.out.println(readInteger()); // we just assume 1 to numNodes now
103 readInteger();
104 virusStrain[i] = readToken();
105 //System.out.println(virusStrain[i]);
106
107 transformed_titer[i] = readToken();
108 //System.out.println(transformed_titer[i]);
109
110 try{
111 //titer[i] = read() ;
112 titer[i] = readLine();
113 //System.out.println(titer[i]);
114 } catch (EOFException e) {
115 //throw new RuntimeException(ite.getMessage());
116 System.out.println("err");
117 }
118
119
120 }
121
122 }
123
124 public String getVirusStrain(int i) {
125 return virusStrain[i];
126 }
127
128 public String getTransformed_titer(int i) {
129 return transformed_titer[i];
130 }
131
132 public String getTiter(int i) {
133 return titer[i];
134 }
135
136 /**
137 * importTree.
138 */
139 public Tree importTree(TaxonList taxonList) throws IOException, ImportException {
140 setCommentDelimiters('[', ']', '\0', '\0', '&');
141
142 try {
143 skipUntil("(");
144 unreadCharacter('(');
145
146 final FlexibleNode root = readInternalNode(taxonList);
147 if (getLastMetaComment() != null) {
148 root.setAttribute(COMMENT, getLastMetaComment());
149 }
150 // if (getLastDelimiter() != ';') {
151 // throw new BadFormatException("Expecting ';' after tree");
152 // }
153
154 return new FlexibleTree(root, false, true);
155
156 } catch (EOFException e) {
157 throw new ImportException("incomplete tree");
158 }
159 }
160
161 /**
162 * importTrees.
163 */
164 public Tree[] importTrees(TaxonList taxonList) throws IOException, ImportException {
165 boolean done = false;
166 ArrayList<FlexibleTree> array = new ArrayList<FlexibleTree>();
167
168 do {
169
170 try {
171
172 skipUntil("(");
173 unreadCharacter('(');
174
175 FlexibleNode root = readInternalNode(taxonList);
176 FlexibleTree tree = new FlexibleTree(root, false, true);
177 array.add(tree);
178
179 if (taxonList == null) {
180 taxonList = tree;
181 }
182
183 if (readCharacter() != ';') {
184 throw new BadFormatException("Expecting ';' after tree");
185 }
186
187 } catch (EOFException e) {
188 done = true;
189 }
190 } while (!done);
191
192 Tree[] trees = new Tree[array.size()];
193 array.toArray(trees);
194
195 return trees;
196 }
197
198 /**
199 * return whether another tree is available.
200 */
201 public boolean hasTree() throws IOException, ImportException {
202 try {
203 skipUntil("(");
204 unreadCharacter('(');
205 } catch (EOFException e) {
206 lastTree = null;
207 return false;
208 }
209
210 return true;
211 }
212
213 private Tree lastTree = null;
214
215 /**
216 * import the next tree.
217 * return the tree or null if no more trees are available
218 */
219 public Tree importNextTree() throws IOException, ImportException {
220 FlexibleTree tree = null;
221
222 try {
223 skipUntil("(");
224 unreadCharacter('(');
225
226 FlexibleNode root = readInternalNode(lastTree);
227
228 tree = new FlexibleTree(root, false, true);
229
230 } catch (EOFException e) {
231 //
232 }
233
234 lastTree = tree;
235
236 return tree;
237 }
238
239 /**
240 * Reads a branch in. This could be a node or a tip (calls readNode or readTip
241 * accordingly). It then reads the branch length and SimpleNode that will
242 * point at the new node or tip.
243 */
244 private FlexibleNode readBranch(TaxonList taxonList) throws IOException, ImportException {
245 double length = 0.0;
246 FlexibleNode branch;
247
248 if (nextCharacter() == '(') {
249 // is an internal node
250 branch = readInternalNode(taxonList);
251
252 } else {
253 // is an external node
254 branch = readExternalNode(taxonList);
255 }
256
257 final String comment = getLastMetaComment();
258 if (comment != null) {
259 branch.setAttribute(COMMENT, comment);
260 clearLastMetaComment();
261 }
262
263 if (getLastDelimiter() == ':') {
264 length = readDouble(",():;");
265 }
266
267 branch.setLength(length);
268
269 return branch;
270 }
271
272 /**
273 * Reads a node in. This could be a polytomy. Calls readBranch on each branch
274 * in the node.
275 */
276 private FlexibleNode readInternalNode(TaxonList taxonList) throws IOException, ImportException {
277 FlexibleNode node = new FlexibleNode();
278
279 // read the opening '('
280 final char ch = readCharacter();
281 assert ch == '(';
282
283 // read the first child
284 node.addChild(readBranch(taxonList));
285
286 // an internal node must have at least 2 children
287 if (getLastDelimiter() != ',') {
288 throw new BadFormatException("Expecting ',' in tree, but got '" + (char) getLastDelimiter() + "'");
289 }
290
291 // read subsequent children
292 do {
293 node.addChild(readBranch(taxonList));
294
295 } while (getLastDelimiter() == ',');
296
297 // should have had a closing ')'
298 if (getLastDelimiter() != ')') {
299 throw new BadFormatException("Missing closing ')' in tree");
300 }
301
302 // If there is a label before the colon, store it:
303 try {
304 String label = readToken(",():;");
305 if (label.length() > 0) {
306 node.setAttribute("label", label);
307 }
308 } catch (IOException ioe) {
309 // probably an end of file without a terminal ';'
310 // we are going to allow this and return the nodes...
311 }
312
313 return node;
314 }
315
316 /**
317 * Reads an external node in.
318 */
319 private FlexibleNode readExternalNode(TaxonList taxonList) throws IOException, ImportException {
320 FlexibleNode node = new FlexibleNode();
321
322 String label = readToken(":(),;");
323
324 Taxon taxon;
325
326 if (taxonList != null) {
327 // if a taxon list is given then the taxon must be in it...
328 int index = taxonList.getTaxonIndex(label);
329 if (index != -1) {
330 taxon = taxonList.getTaxon(index);
331 } else {
332 throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown");
333 }
334 } else {
335 // No taxon list given so create new taxa
336 taxon = new Taxon(label);
337 }
338
339 node.setTaxon(taxon);
340 return node;
341 }
342
343 }