Debian Codegraph @

src/dr/app/beauti/util/NexusApplicationImporter.java

raw symbols SCIP
1 /*
2 * NexusApplicationImporter.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.app.beauti.util;
27
28 import dr.evolution.util.Taxa;
29 import dr.evolution.util.TaxonList;
30 import dr.evomodel.substmodel.nucleotide.NucModelType;
31 import dr.app.beauti.options.BeautiOptions;
32 import dr.app.beauti.options.PartitionSubstitutionModel;
33 import dr.app.beauti.options.PartitionTreeModel;
34 import dr.app.beauti.types.StartingTreeType;
35 import dr.evolution.alignment.Alignment;
36 import dr.evolution.alignment.CharSetAlignment;
37 import dr.evolution.io.NexusImporter;
38
39 import java.io.IOException;
40 import java.io.Reader;
41 import java.io.Writer;
42 import java.util.ArrayList;
43 import java.util.List;
44
45 /**
46 * Class for importing PAUP, MrBayes and Rhino NEXUS file format
47 *
48 * @author Andrew Rambaut
49 * @author Alexei Drummond
50 * @version $Id: NexusApplicationImporter.java,v 1.4 2005/07/11 14:07:25 rambaut Exp $
51 */
52 public class NexusApplicationImporter extends NexusImporter {
53
54 public static final NexusBlock ASSUMPTIONS_BLOCK = new NexusBlock("ASSUMPTIONS");
55 public static final NexusBlock SETS_BLOCK = new NexusBlock("SETS");
56 public static final NexusBlock PAUP_BLOCK = new NexusBlock("PAUP");
57 public static final NexusBlock MRBAYES_BLOCK = new NexusBlock("MRBAYES");
58
59 /**
60 * @param reader a reader to read the Nexus format from
61 */
62 public NexusApplicationImporter(Reader reader) {
63 super(reader);
64 setCommentDelimiters('[', ']', '\0');
65 }
66
67 public NexusApplicationImporter(Reader reader, Writer commentWriter) {
68 super(reader, commentWriter);
69 setCommentDelimiters('[', ']', '\0');
70 }
71
72 /**
73 * This function returns an enum class to specify what the
74 * block given by blockName is.
75 */
76 public NexusBlock findBlockName(String blockName) {
77 if (blockName.equalsIgnoreCase(ASSUMPTIONS_BLOCK.toString())) {
78 return ASSUMPTIONS_BLOCK;
79 } else if (blockName.equalsIgnoreCase(SETS_BLOCK.toString())) {
80 return SETS_BLOCK;
81 } else if (blockName.equalsIgnoreCase(PAUP_BLOCK.toString())) {
82 return PAUP_BLOCK;
83 } else if (blockName.equalsIgnoreCase(MRBAYES_BLOCK.toString())) {
84 return MRBAYES_BLOCK;
85 } else {
86 return super.findBlockName(blockName);
87 }
88 }
89
90 /**
91 * Parses an 'Assumptions' block.
92 *
93 * @param charSets a list of char sets to *add* to if any are defined in PAUP block
94 * @throws dr.evolution.io.Importer.ImportException
95 * if Assumptions block is poorly formed
96 * @throws java.io.IOException if I/O fails
97 */
98 public void parseAssumptionsBlock(List<CharSet> charSets, List<TaxSet> taxSets) throws ImportException, IOException {
99 boolean done = false;
100 while (!done) {
101 String command = readToken(";");
102 if (command.equalsIgnoreCase("ENDBLOCK") || command.equalsIgnoreCase("END")) {
103 done = true;
104 } else if (match("CHARSET", command, 5)) {
105 if (getLastDelimiter() != ';') {
106 charSets.add(readCharSetCommand());
107 }
108 } else if (match("TAXSET", command, 5)) {
109 if (getLastDelimiter() != ';') {
110 taxSets.add(readTaxSetCommand());
111 }
112 } else {
113 System.err.println("The command, '" + command + "', is not used by BEAST and has been ignored");
114 }
115 }
116 }
117
118 /**
119 * Parses a 'PAUP' block.
120 *
121 * @param options the BEAUti options
122 * @param charSets a list of char sets to *add* to if any are defined in PAUP block
123 * @return a partition model representing the model defined in the PAUP block
124 * @throws dr.evolution.io.Importer.ImportException
125 * if PAUP block is poorly formed
126 * @throws java.io.IOException if I/O fails
127 */
128 public PartitionSubstitutionModel parsePAUPBlock(BeautiOptions options, List<CharSet> charSets) throws ImportException, IOException {
129 PartitionSubstitutionModel model = new PartitionSubstitutionModel(options, "nucs");
130 readTopLevelBlock(options, model, charSets);
131 return model;
132 }
133
134 /**
135 * Parses a 'MRBAYES' block.
136 *
137 * @param options the BEAUti options
138 * @param charSets a list of char sets to *add* to if any are defined in PAUP block
139 * @return a partition model representing the model defined in the MRBAYES block
140 * @throws dr.evolution.io.Importer.ImportException
141 * if MRBAYES block is poorly formed
142 * @throws java.io.IOException if I/O fails
143 */
144 public PartitionSubstitutionModel parseMrBayesBlock(BeautiOptions options, List<CharSet> charSets) throws ImportException, IOException {
145 PartitionSubstitutionModel model = new PartitionSubstitutionModel(options, "nucs");
146 readTopLevelBlock(options, model, charSets);
147 return model;
148 }
149
150 private CharSet readCharSetCommand() throws ImportException, IOException {
151
152 String name = readToken("=;");
153
154 CharSet charSet = new CharSet(name);
155
156 // System.out.print("Char set " + name);
157
158 int from;
159 int to;
160 int every = 1;
161
162 while (getLastDelimiter() != ';') {
163
164 String token = readToken(";,");
165
166 // This was to remove spaces within each partition definition. But that means
167 // we can't read multiple definitions separated by spaces.
168 // while (getLastDelimiter() != ';' && getLastDelimiter() != ',') {
169 // token += readToken(";,");
170 // }
171
172 String[] parts = token.split("-");
173 // System.out.print(token + " ");
174
175 try {
176
177 if (parts.length == 2) {
178 from = Integer.parseInt(parts[0].trim());
179
180 String[] toParts = parts[1].split("\\\\");
181
182 if (toParts[0].trim().equals(".")) {
183 to = -1;
184 } else {
185 to = Integer.parseInt(toParts[0].trim());
186 }
187
188 every = 1;
189 if (toParts.length > 1) every = Integer.parseInt(toParts[1].trim());
190
191 } else if (parts.length == 1) {
192 from = Integer.parseInt(parts[0].trim());
193 to = from;
194 } else {
195 throw new ImportException("CharSet, " + name + ", unable to be parsed");
196 }
197 } catch (NumberFormatException nfe) {
198 throw new ImportException("CharSet, " + name + ", unable to be parsed");
199 }
200 charSet.addCharSetBlock(new CharSetBlock(from, to, every));
201
202 }
203 // System.out.println();
204
205 return charSet;
206 }
207
208 private TaxSet readTaxSetCommand() throws ImportException, IOException {
209
210 String name = readToken("=;");
211
212 TaxSet taxSet = new TaxSet(name);
213
214 // System.out.print("Char set " + name);
215
216 int from;
217 int to;
218
219 while (getLastDelimiter() != ';') {
220
221 String token = readToken(";,");
222
223 String[] parts = token.split("-");
224
225 try {
226
227 if (parts.length == 2) {
228 from = Integer.parseInt(parts[0].trim());
229 to = Integer.parseInt(parts[1].trim());
230 } else if (parts.length == 1) {
231 from = Integer.parseInt(parts[0].trim());
232 to = from;
233 } else {
234 throw new ImportException("TaxSet, " + name + ", unable to be parsed");
235 }
236 } catch (NumberFormatException nfe) {
237 throw new ImportException("TaxSet, " + name + ", unable to be parsed");
238 }
239 taxSet.addCharSetBlock(new CharSetBlock(from, to, 0));
240
241 }
242 // System.out.println();
243
244 return taxSet;
245 }
246
247 /**
248 * This method reads a PAUP or MrBayes block
249 *
250 * @param options the beauti options
251 * @param model the partition model
252 * @param charSets a list of char sets to *add* to if any are defined in PAUP block
253 * @throws dr.evolution.io.Importer.ImportException
254 * if top-level block is poorly formed
255 * @throws java.io.IOException if I/O fails
256 */
257 private void readTopLevelBlock(BeautiOptions options, PartitionSubstitutionModel model, List<CharSet> charSets)
258 throws ImportException, IOException {
259 boolean done = false;
260
261 while (!done) {
262 String command = readToken(";");
263 if (command.equalsIgnoreCase("ENDBLOCK") || command.equalsIgnoreCase("END")) {
264 done = true;
265 } else if (match("HSEARCH", command, 2)) {
266 // Once we reach a search in PAUP then stop
267 done = true;
268 } else if (match("MCMC", command, 4)) {
269 if (getLastDelimiter() != ';') {
270 readMCMCCommand(options);
271 }
272 done = true;
273 } else if (match("MCMCP", command, 5)) {
274 if (getLastDelimiter() != ';') {
275 readMCMCCommand(options);
276 }
277 } else if (match("LSET", command, 2)) {
278 if (getLastDelimiter() != ';') {
279 readLSETCommand(model);
280 }
281 } else if (match("CHARSET", command, 5)) {
282 if (getLastDelimiter() != ';') {
283 charSets.add(readCharSetCommand());
284 }
285 } else {
286 System.err.println("The command, '" + command + "', is not used by BEAST and has been ignored");
287 }
288 }
289 }
290
291 private void readLSETCommand(PartitionSubstitutionModel model) throws ImportException, IOException {
292 boolean done = false;
293
294 while (!done) {
295 String subcommand = readToken("=;");
296 if (match("NST", subcommand, 2)) {
297 int nst = readInteger(";");
298 if (nst == 1) {
299 model.setNucSubstitutionModel(NucModelType.JC);
300 } else if (nst == 2) {
301 model.setNucSubstitutionModel(NucModelType.HKY);
302 } else if (nst == 6) {
303 model.setNucSubstitutionModel(NucModelType.GTR);
304 } else {
305 throw new BadFormatException("Bad value for NST subcommand of LSET command");
306 }
307 } else if (match("RATES", subcommand, 2)) {
308 String token = readToken(";");
309
310 if (match("EQUAL", token, 1)) {
311 model.setGammaHetero(false);
312 model.setInvarHetero(false);
313 } else if (match("GAMMA", token, 1)) {
314 model.setGammaHetero(true);
315 model.setInvarHetero(false);
316 } else if (match("PROPINV", token, 1)) {
317 model.setGammaHetero(false);
318 model.setInvarHetero(true);
319 } else if (match("INVGAMMA", token, 1)) {
320 model.setGammaHetero(true);
321 model.setInvarHetero(true);
322 } else if (match("ADGAMMA", token, 1)) {
323 System.err.println("The option, 'RATES=ADGAMMA', in the LSET command is not used by BEAST and has been ignored");
324 } else if (match("SITESPEC", token, 1)) {
325 System.err.println("The option, 'RATES=SITESPEC', in the LSET command is not used by BEAST and has been ignored");
326 } else {
327 throw new BadFormatException("Unknown value, '" + token + "'");
328 }
329 } else if (match("NGAMMACAT", subcommand, 2)) {
330
331 model.setGammaCategories(readInteger(";"));
332 } else {
333
334 System.err.println("The option, '" + subcommand + "', in the LSET command is not used by BEAST and has been ignored");
335 }
336
337 if (getLastDelimiter() == ';') {
338 done = true;
339 }
340 }
341 }
342
343 private void readMCMCCommand(BeautiOptions options) throws ImportException, IOException {
344 boolean done = false;
345
346 while (!done) {
347 String subcommand = readToken("=;");
348 if (match("NGEN", subcommand, 2)) {
349 options.chainLength = readInteger(";");
350 } else if (match("SAMPLEFREQ", subcommand, 2)) {
351 options.logEvery = readInteger(";");
352 } else if (match("PRINTFREQ", subcommand, 1)) {
353 options.echoEvery = readInteger(";");
354 } else if (match("FILENAME", subcommand, 1)) {
355 options.fileName = readToken(";");
356 } else if (match("BURNIN", subcommand, 1)) {
357 options.burnIn = readInteger(";");
358 } else if (match("STARTINGTREE", subcommand, 2)) {
359 String token = readToken(";");
360 if (match("USER", token, 1)) {
361 // How do we know what tree to use?
362 // options.startingTreeType = StartingTreeType.USER;
363 for (PartitionTreeModel model : options.getPartitionTreeModels()) {
364 model.setStartingTreeType(StartingTreeType.USER);
365 }
366 } else if (match("RANDOM", token, 1)) {
367 // options.startingTreeType = StartingTreeType.RANDOM;
368 for (PartitionTreeModel model : options.getPartitionTreeModels()) {
369 model.setStartingTreeType(StartingTreeType.RANDOM);
370 }
371 } else {
372 throw new BadFormatException("Unknown value, '" + token + "'");
373 }
374 } else {
375
376 System.err.println("The option, '" + subcommand + "', in the MCMC command is not used by BEAST and has been ignored");
377 }
378
379 if (getLastDelimiter() == ';') {
380 done = true;
381 }
382 }
383 }
384
385 private boolean match(String reference, String target, int min) throws ImportException {
386 if (target.length() < min) {
387
388 //throw new BadFormatException("Ambiguous command or subcommand, '" + target + "'");
389 }
390
391 return reference.startsWith(target.toUpperCase());
392 }
393
394 public class CharSet {
395
396 String name;
397 List<CharSetBlock> blocks;
398
399 public CharSet(String name) {
400 this.name = name;
401 blocks = new ArrayList<CharSetBlock>();
402 }
403
404 public List<CharSetBlock> getBlocks() {
405 return blocks;
406 }
407
408 public String getName() {
409 return name;
410 }
411
412 public void addCharSetBlock(CharSetBlock b) {
413 blocks.add(b);
414 }
415
416 public Alignment constructCharSetAlignment(Alignment alignment) {
417
418 return new CharSetAlignment(this, alignment);
419 }
420 }
421
422 public class CharSetBlock {
423
424 public CharSetBlock(int fromSite, int toSite, int every) {
425
426 this.fromSite = fromSite;
427 this.toSite = toSite;
428 this.every = every;
429 }
430
431 public int getFromSite() {
432 return fromSite;
433 }
434
435 public int getToSite() {
436 return toSite;
437 }
438
439 public int getEvery() {
440 return every;
441 }
442
443 private final int fromSite;
444 private final int toSite;
445 private final int every;
446 }
447
448 public class TaxSet {
449
450 String name;
451 List<CharSetBlock> blocks; // use the CharSetBlocks
452
453 public TaxSet(String name) {
454 this.name = name;
455 blocks = new ArrayList<CharSetBlock>();
456 }
457
458 public List<CharSetBlock> getBlocks() {
459 return blocks;
460 }
461
462 public String getName() {
463 return name;
464 }
465
466 public void addCharSetBlock(CharSetBlock b) {
467 blocks.add(b);
468 }
469
470 public TaxonList constructTaxonSet(TaxonList taxa) {
471
472 return new Taxa();
473 }
474 }
475
476
477 }