| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 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 |
|
| 70 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 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 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 308 |
|
| 309 |
for (PartitionTreeModel model : options.getPartitionTreeModels()) { |
| 310 |
model.setStartingTreeType(StartingTreeType.USER); |
| 311 |
} |
| 312 |
} else if (match("RANDOM", token, 1)) { |
| 313 |
|
| 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 |
|
| 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 |
} |