| 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 test.dr.app.beauti; |
| 27 |
|
| 28 |
import dr.evomodel.substmodel.aminoacid.AminoAcidModelType; |
| 29 |
import dr.evomodel.substmodel.nucleotide.NucModelType; |
| 30 |
import dr.app.beauti.generator.BeastGenerator; |
| 31 |
import dr.app.beauti.options.BeautiOptions; |
| 32 |
import dr.app.beauti.options.PartitionData; |
| 33 |
import dr.app.beauti.options.PartitionSubstitutionModel; |
| 34 |
import dr.app.beauti.util.NexusApplicationImporter; |
| 35 |
import dr.evolution.alignment.Alignment; |
| 36 |
import dr.evolution.alignment.CharSetAlignment; |
| 37 |
import dr.evolution.alignment.ConvertAlignment; |
| 38 |
import dr.evolution.datatype.AminoAcids; |
| 39 |
import dr.evolution.datatype.GeneticCode; |
| 40 |
import dr.evolution.io.Importer; |
| 41 |
import dr.evolution.io.NexusImporter; |
| 42 |
import dr.evolution.tree.Tree; |
| 43 |
import dr.evolution.util.*; |
| 44 |
import dr.evoxml.util.DateUnitsType; |
| 45 |
|
| 46 |
import java.io.*; |
| 47 |
import java.util.ArrayList; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class BeautiTesterConfig { |
| 55 |
|
| 56 |
private PrintWriter scriptWriter; |
| 57 |
BeastGenerator generator; |
| 58 |
|
| 59 |
private String COMMEND = "beast "; |
| 60 |
|
| 61 |
public void setCOMMEND(String commend) { |
| 62 |
COMMEND = commend; |
| 63 |
} |
| 64 |
|
| 65 |
public BeautiTesterConfig() { |
| 66 |
} |
| 67 |
|
| 68 |
public BeautiOptions createOptions() { |
| 69 |
BeautiOptions beautiOptions = new BeautiOptions(); |
| 70 |
|
| 71 |
beautiOptions.fileNameStem = ""; |
| 72 |
beautiOptions.treeFileName.clear(); |
| 73 |
beautiOptions.substTreeLog = false; |
| 74 |
beautiOptions.substTreeFileName.clear(); |
| 75 |
|
| 76 |
|
| 77 |
beautiOptions.chainLength = 100; |
| 78 |
beautiOptions.logEvery = 100; |
| 79 |
beautiOptions.echoEvery = 100; |
| 80 |
beautiOptions.burnIn = 10; |
| 81 |
beautiOptions.fileName = null; |
| 82 |
beautiOptions.autoOptimize = true; |
| 83 |
|
| 84 |
|
| 85 |
beautiOptions.taxonList = null; |
| 86 |
|
| 87 |
beautiOptions.datesUnits = DateUnitsType.YEARS; |
| 88 |
beautiOptions.datesDirection = DateUnitsType.FORWARDS; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
beautiOptions.performTraceAnalysis = false; |
| 94 |
beautiOptions.generateCSV = true; |
| 95 |
|
| 96 |
beautiOptions.units = Units.Type.SUBSTITUTIONS; |
| 97 |
beautiOptions.maximumTipHeight = 0.0; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
generator = new BeastGenerator(beautiOptions, null); |
| 103 |
|
| 104 |
return beautiOptions; |
| 105 |
} |
| 106 |
|
| 107 |
public void closeScriptWriter() { |
| 108 |
scriptWriter.close(); |
| 109 |
} |
| 110 |
|
| 111 |
public void createScriptWriter(String pathAndFile) { |
| 112 |
try { |
| 113 |
scriptWriter = new PrintWriter(new FileWriter(pathAndFile)); |
| 114 |
} catch (IOException e) { |
| 115 |
e.printStackTrace(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public void printlnScriptWriter(String line) { |
| 120 |
scriptWriter.println(line); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
public void buildNucModels(String key, BeautiOptions beautiOptions) { |
| 125 |
PartitionSubstitutionModel model = beautiOptions.getPartitionSubstitutionModels().get(0); |
| 126 |
|
| 127 |
model.setNucSubstitutionModel(NucModelType.HKY); |
| 128 |
buildCodonModels(key + "HKY", beautiOptions); |
| 129 |
model.setNucSubstitutionModel(NucModelType.GTR); |
| 130 |
buildCodonModels(key + "GTR", beautiOptions); |
| 131 |
} |
| 132 |
|
| 133 |
public void buildCodonModels(String key, BeautiOptions beautiOptions) { |
| 134 |
PartitionSubstitutionModel model = beautiOptions.getPartitionSubstitutionModels().get(0); |
| 135 |
|
| 136 |
model.setCodonHeteroPattern(null); |
| 137 |
model.setUnlinkedSubstitutionModel(false); |
| 138 |
model.setUnlinkedHeterogeneityModel(false); |
| 139 |
buildHeteroModels(key + "", beautiOptions); |
| 140 |
|
| 141 |
model.setCodonHeteroPattern("123"); |
| 142 |
buildHeteroModels(key + "+C123", beautiOptions); |
| 143 |
|
| 144 |
model.setUnlinkedSubstitutionModel(true); |
| 145 |
model.setUnlinkedHeterogeneityModel(false); |
| 146 |
buildHeteroModels(key + "+C123^S", beautiOptions); |
| 147 |
|
| 148 |
model.setUnlinkedSubstitutionModel(false); |
| 149 |
model.setUnlinkedHeterogeneityModel(true); |
| 150 |
buildHeteroModels(key + "+C123^H", beautiOptions); |
| 151 |
|
| 152 |
model.setUnlinkedSubstitutionModel(true); |
| 153 |
model.setUnlinkedHeterogeneityModel(true); |
| 154 |
buildHeteroModels(key + "+C123^SH", beautiOptions); |
| 155 |
|
| 156 |
model.setCodonHeteroPattern("112"); |
| 157 |
buildHeteroModels(key + "+C112", beautiOptions); |
| 158 |
|
| 159 |
model.setUnlinkedSubstitutionModel(true); |
| 160 |
model.setUnlinkedHeterogeneityModel(false); |
| 161 |
buildHeteroModels(key + "+C112^S", beautiOptions); |
| 162 |
|
| 163 |
model.setUnlinkedSubstitutionModel(false); |
| 164 |
model.setUnlinkedHeterogeneityModel(true); |
| 165 |
buildHeteroModels(key + "+C112^H", beautiOptions); |
| 166 |
|
| 167 |
model.setUnlinkedSubstitutionModel(true); |
| 168 |
model.setUnlinkedHeterogeneityModel(true); |
| 169 |
buildHeteroModels(key + "+C112^SH", beautiOptions); |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
public void buildHeteroModels(String key, BeautiOptions beautiOptions) { |
| 174 |
PartitionSubstitutionModel model = beautiOptions.getPartitionSubstitutionModels().get(0); |
| 175 |
|
| 176 |
model.setGammaHetero(false); |
| 177 |
model.setGammaCategories(4); |
| 178 |
model.setInvarHetero(false); |
| 179 |
buildTreePriorModels(key + "", beautiOptions); |
| 180 |
|
| 181 |
model.setGammaHetero(true); |
| 182 |
model.setInvarHetero(false); |
| 183 |
buildTreePriorModels(key + "+G", beautiOptions); |
| 184 |
|
| 185 |
model.setGammaHetero(false); |
| 186 |
model.setInvarHetero(true); |
| 187 |
buildTreePriorModels(key + "+I", beautiOptions); |
| 188 |
|
| 189 |
model.setGammaHetero(true); |
| 190 |
model.setInvarHetero(true); |
| 191 |
buildTreePriorModels(key + "+GI", beautiOptions); |
| 192 |
} |
| 193 |
|
| 194 |
public void buildAAModels(String key, BeautiOptions beautiOptions) { |
| 195 |
PartitionSubstitutionModel model = beautiOptions.getPartitionSubstitutionModels().get(0); |
| 196 |
|
| 197 |
model.setAaSubstitutionModel(AminoAcidModelType.BLOSUM_62); |
| 198 |
buildHeteroModels(key + "BLOSUM62", beautiOptions); |
| 199 |
|
| 200 |
model.setAaSubstitutionModel(AminoAcidModelType.CP_REV_45); |
| 201 |
buildHeteroModels(key + "CPREV45", beautiOptions); |
| 202 |
|
| 203 |
model.setAaSubstitutionModel(AminoAcidModelType.DAYHOFF); |
| 204 |
buildHeteroModels(key + "DAYHOFF", beautiOptions); |
| 205 |
|
| 206 |
model.setAaSubstitutionModel(AminoAcidModelType.JTT); |
| 207 |
buildHeteroModels(key + "JTT", beautiOptions); |
| 208 |
|
| 209 |
model.setAaSubstitutionModel(AminoAcidModelType.MT_REV_24); |
| 210 |
buildHeteroModels(key + "MTREV24", beautiOptions); |
| 211 |
|
| 212 |
model.setAaSubstitutionModel(AminoAcidModelType.WAG); |
| 213 |
buildHeteroModels(key + "WAG", beautiOptions); |
| 214 |
|
| 215 |
model.setAaSubstitutionModel(AminoAcidModelType.LG); |
| 216 |
buildHeteroModels(key + "LG", beautiOptions); |
| 217 |
} |
| 218 |
|
| 219 |
public void buildTreePriorModels(String key, BeautiOptions options) { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
public void buildClockModels(String key, BeautiOptions options) { |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
public void generate(String name, BeautiOptions options) { |
| 256 |
options.logFileName = name + ".log"; |
| 257 |
|
| 258 |
|
| 259 |
System.out.println("Generating: " + name); |
| 260 |
String fileName = name + ".xml"; |
| 261 |
try { |
| 262 |
generator.generateXML(new File(fileName)); |
| 263 |
} catch (Exception e) { |
| 264 |
e.printStackTrace(); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
printlnScriptWriter(COMMEND + fileName); |
| 269 |
} |
| 270 |
|
| 271 |
public void importFromFile(String fileName, BeautiOptions options, boolean translate) { |
| 272 |
|
| 273 |
TaxonList taxa = null; |
| 274 |
Alignment alignment = null; |
| 275 |
Tree tree = null; |
| 276 |
PartitionSubstitutionModel model = null; |
| 277 |
java.util.List<NexusApplicationImporter.CharSet> charSets = new ArrayList<NexusApplicationImporter.CharSet>(); |
| 278 |
|
| 279 |
try { |
| 280 |
FileReader reader = new FileReader(fileName); |
| 281 |
|
| 282 |
NexusApplicationImporter importer = new NexusApplicationImporter(reader); |
| 283 |
|
| 284 |
boolean done = false; |
| 285 |
|
| 286 |
while (!done) { |
| 287 |
try { |
| 288 |
|
| 289 |
NexusImporter.NexusBlock block = importer.findNextBlock(); |
| 290 |
|
| 291 |
if (block == NexusImporter.TAXA_BLOCK) { |
| 292 |
|
| 293 |
if (taxa != null) { |
| 294 |
throw new NexusImporter.MissingBlockException("TAXA block already defined"); |
| 295 |
} |
| 296 |
|
| 297 |
taxa = importer.parseTaxaBlock(); |
| 298 |
|
| 299 |
} else if (block == NexusImporter.CALIBRATION_BLOCK) { |
| 300 |
if (taxa == null) { |
| 301 |
throw new NexusImporter.MissingBlockException("TAXA or DATA block must be defined before a CALIBRATION block"); |
| 302 |
} |
| 303 |
|
| 304 |
importer.parseCalibrationBlock(taxa); |
| 305 |
|
| 306 |
} else if (block == NexusImporter.CHARACTERS_BLOCK) { |
| 307 |
|
| 308 |
if (taxa == null) { |
| 309 |
throw new NexusImporter.MissingBlockException("TAXA block must be defined before a CHARACTERS block"); |
| 310 |
} |
| 311 |
|
| 312 |
if (alignment != null) { |
| 313 |
throw new NexusImporter.MissingBlockException("CHARACTERS or DATA block already defined"); |
| 314 |
} |
| 315 |
|
| 316 |
alignment = importer.parseCharactersBlock(options.taxonList); |
| 317 |
|
| 318 |
} else if (block == NexusImporter.DATA_BLOCK) { |
| 319 |
|
| 320 |
if (alignment != null) { |
| 321 |
throw new NexusImporter.MissingBlockException("CHARACTERS or DATA block already defined"); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
alignment = importer.parseDataBlock(options.taxonList); |
| 327 |
if (taxa == null) { |
| 328 |
taxa = alignment; |
| 329 |
} |
| 330 |
|
| 331 |
} else if (block == NexusImporter.TREES_BLOCK) { |
| 332 |
|
| 333 |
if (taxa == null) { |
| 334 |
throw new NexusImporter.MissingBlockException("TAXA or DATA block must be defined before a TREES block"); |
| 335 |
} |
| 336 |
|
| 337 |
if (tree != null) { |
| 338 |
throw new NexusImporter.MissingBlockException("TREES block already defined"); |
| 339 |
} |
| 340 |
|
| 341 |
Tree[] trees = importer.parseTreesBlock(taxa); |
| 342 |
if (trees.length > 0) { |
| 343 |
tree = trees[0]; |
| 344 |
} |
| 345 |
|
| 346 |
} else if (block == NexusApplicationImporter.PAUP_BLOCK) { |
| 347 |
|
| 348 |
model = importer.parsePAUPBlock(options, charSets); |
| 349 |
|
| 350 |
} else if (block == NexusApplicationImporter.MRBAYES_BLOCK) { |
| 351 |
|
| 352 |
model = importer.parseMrBayesBlock(options, charSets); |
| 353 |
|
| 354 |
} else if (block == NexusApplicationImporter.ASSUMPTIONS_BLOCK) { |
| 355 |
|
| 356 |
importer.parseAssumptionsBlock(charSets, null); |
| 357 |
|
| 358 |
} else { |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
} catch (EOFException ex) { |
| 363 |
done = true; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
if (alignment == null && taxa == null) { |
| 369 |
throw new NexusImporter.MissingBlockException("TAXON, DATA or CHARACTERS block is missing"); |
| 370 |
} |
| 371 |
|
| 372 |
} catch (FileNotFoundException fnfe) { |
| 373 |
System.err.println("File not found: " + fnfe); |
| 374 |
System.exit(1); |
| 375 |
|
| 376 |
} catch (Importer.ImportException ime) { |
| 377 |
System.err.println("Error parsing imported file: " + ime); |
| 378 |
System.exit(1); |
| 379 |
} catch (IOException ioex) { |
| 380 |
System.err.println("File I/O Error: " + ioex); |
| 381 |
System.exit(1); |
| 382 |
} catch (Exception ex) { |
| 383 |
System.err.println("Fatal exception: " + ex); |
| 384 |
System.exit(1); |
| 385 |
} |
| 386 |
|
| 387 |
if (options.taxonList == null) { |
| 388 |
|
| 389 |
|
| 390 |
options.taxonList = new Taxa(taxa); |
| 391 |
|
| 392 |
|
| 393 |
boolean foundAmp = false; |
| 394 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 395 |
String name = taxa.getTaxon(i).getId(); |
| 396 |
if (name.indexOf('&') >= 0) { |
| 397 |
foundAmp = true; |
| 398 |
} |
| 399 |
} |
| 400 |
if (foundAmp) { |
| 401 |
System.err.println("One or more taxon names include an illegal character ('&')."); |
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 407 |
if (taxa.getTaxonAttribute(i, "date") == null) { |
| 408 |
java.util.Date origin = new java.util.Date(0); |
| 409 |
|
| 410 |
dr.evolution.util.Date date = dr.evolution.util.Date.createTimeSinceOrigin(0.0, Units.Type.YEARS, origin); |
| 411 |
taxa.getTaxon(i).setAttribute("date", date); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
} else { |
| 416 |
|
| 417 |
|
| 418 |
java.util.List<String> oldTaxa = new ArrayList<String>(); |
| 419 |
for (int i = 0; i < options.taxonList.getTaxonCount(); i++) { |
| 420 |
oldTaxa.add(options.taxonList.getTaxon(i).getId()); |
| 421 |
} |
| 422 |
java.util.List<String> newTaxa = new ArrayList<String>(); |
| 423 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 424 |
newTaxa.add(taxa.getTaxon(i).getId()); |
| 425 |
} |
| 426 |
|
| 427 |
if (!(oldTaxa.containsAll(newTaxa) && oldTaxa.size() == newTaxa.size())) { |
| 428 |
System.err.println("This file contains different taxa from the previously loaded"); |
| 429 |
return; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
String fileNameStem = dr.app.util.Utils.trimExtensions(fileName, |
| 434 |
new String[]{"NEX", "NEXUS", "TRE", "TREE"}); |
| 435 |
|
| 436 |
if (alignment != null) { |
| 437 |
if (translate) { |
| 438 |
alignment = new ConvertAlignment(AminoAcids.INSTANCE, GeneticCode.UNIVERSAL, alignment); |
| 439 |
} |
| 440 |
|
| 441 |
java.util.List<PartitionData> partitions = new ArrayList<PartitionData>(); |
| 442 |
if (charSets != null && charSets.size() > 0) { |
| 443 |
for (NexusApplicationImporter.CharSet charSet : charSets) { |
| 444 |
partitions.add(new PartitionData(options, charSet.getName(), fileName, |
| 445 |
new CharSetAlignment(charSet, alignment))); |
| 446 |
} |
| 447 |
} else { |
| 448 |
partitions.add(new PartitionData(options, fileNameStem, fileName, alignment)); |
| 449 |
} |
| 450 |
for (PartitionData partition : partitions) { |
| 451 |
if (model != null) { |
| 452 |
partition.setPartitionSubstitutionModel(model); |
| 453 |
|
| 454 |
} else { |
| 455 |
for (PartitionSubstitutionModel pm : options.getPartitionSubstitutionModels()) { |
| 456 |
if (pm.getDataType() == alignment.getDataType()) { |
| 457 |
partition.setPartitionSubstitutionModel(pm); |
| 458 |
} |
| 459 |
} |
| 460 |
if (partition.getPartitionSubstitutionModel() == null) { |
| 461 |
PartitionSubstitutionModel pm = new PartitionSubstitutionModel(options, "default", partition); |
| 462 |
partition.setPartitionSubstitutionModel(pm); |
| 463 |
|
| 464 |
} |
| 465 |
} |
| 466 |
options.dataPartitions.add(partition); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
calculateHeights(options); |
| 471 |
} |
| 472 |
|
| 473 |
private void calculateHeights(BeautiOptions options) { |
| 474 |
|
| 475 |
options.maximumTipHeight = 0.0; |
| 476 |
if (!options.hasData()) return; |
| 477 |
|
| 478 |
dr.evolution.util.Date mostRecent = null; |
| 479 |
for (int i = 0; i < options.taxonList.getTaxonCount(); i++) { |
| 480 |
Date date = options.taxonList.getTaxon(i).getDate(); |
| 481 |
if ((date != null) && (mostRecent == null || date.after(mostRecent))) { |
| 482 |
mostRecent = date; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
if (mostRecent != null) { |
| 487 |
TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue()); |
| 488 |
double time0 = timeScale.convertTime(mostRecent.getTimeValue(), mostRecent); |
| 489 |
|
| 490 |
for (int i = 0; i < options.taxonList.getTaxonCount(); i++) { |
| 491 |
Date date = options.taxonList.getTaxon(i).getDate(); |
| 492 |
if (date != null) { |
| 493 |
double height = timeScale.convertTime(date.getTimeValue(), date) - time0; |
| 494 |
if (height > options.maximumTipHeight) options.maximumTipHeight = height; |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
} |
| 502 |
|