Debian Codegraph @

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