| 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.beast; |
| 27 |
|
| 28 |
import beagle.BeagleFlag; |
| 29 |
import beagle.BeagleInfo; |
| 30 |
import dr.app.checkpoint.BeastCheckpointer; |
| 31 |
import dr.app.plugin.Plugin; |
| 32 |
import dr.app.plugin.PluginLoader; |
| 33 |
import dr.app.util.Arguments; |
| 34 |
import dr.app.util.Utils; |
| 35 |
import dr.inference.mcmc.MCMC; |
| 36 |
import dr.inference.mcmcmc.MCMCMC; |
| 37 |
import dr.inference.mcmcmc.MCMCMCOptions; |
| 38 |
import dr.math.MathUtils; |
| 39 |
import dr.util.ErrorLogHandler; |
| 40 |
import dr.util.MessageLogHandler; |
| 41 |
import dr.util.Version; |
| 42 |
import dr.xml.XMLObjectParser; |
| 43 |
import dr.xml.XMLParser; |
| 44 |
import jam.util.IconUtils; |
| 45 |
|
| 46 |
import javax.swing.*; |
| 47 |
import java.io.File; |
| 48 |
import java.io.FileOutputStream; |
| 49 |
import java.io.FileReader; |
| 50 |
import java.io.IOException; |
| 51 |
import java.util.*; |
| 52 |
import java.util.logging.*; |
| 53 |
|
| 54 |
public class BeastMain { |
| 55 |
|
| 56 |
private final static Version version = new BeastVersion(); |
| 57 |
|
| 58 |
public static final double DEFAULT_DELTA = 1.0; |
| 59 |
public static final int DEFAULT_SWAP_CHAIN_EVERY = 100; |
| 60 |
|
| 61 |
static class BeastConsoleApp extends jam.console.ConsoleApplication { |
| 62 |
XMLParser parser = null; |
| 63 |
|
| 64 |
public BeastConsoleApp(String nameString, String titleString, String aboutString, javax.swing.Icon icon) throws IOException { |
| 65 |
super(nameString, titleString, aboutString, icon, false); |
| 66 |
getDefaultFrame().setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
| 67 |
} |
| 68 |
|
| 69 |
public void doStop() { |
| 70 |
Iterator iter = parser.getThreads(); |
| 71 |
while (iter.hasNext()) { |
| 72 |
Thread thread = (Thread) iter.next(); |
| 73 |
thread.stop(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public void setTitle(String title) { |
| 78 |
getDefaultFrame().setTitle(title); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public BeastMain(File inputFile, BeastConsoleApp consoleApp, int maxErrorCount, final boolean verbose, |
| 83 |
boolean parserWarning, boolean strictXML, List<String> additionalParsers, |
| 84 |
boolean useMC3, double[] chainTemperatures, int swapChainsEvery) { |
| 85 |
|
| 86 |
if (inputFile == null) { |
| 87 |
throw new RuntimeException("Error: no input file specified"); |
| 88 |
} |
| 89 |
|
| 90 |
String fileName = inputFile.getName(); |
| 91 |
|
| 92 |
final Logger infoLogger = Logger.getLogger("dr.app.beast"); |
| 93 |
try { |
| 94 |
|
| 95 |
FileReader fileReader = new FileReader(inputFile); |
| 96 |
|
| 97 |
XMLParser parser = new BeastParser(new String[]{fileName}, additionalParsers, verbose, parserWarning, strictXML, version); |
| 98 |
|
| 99 |
if (consoleApp != null) { |
| 100 |
consoleApp.parser = parser; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
Logger logger = Logger.getLogger("dr"); |
| 106 |
|
| 107 |
Handler messageHandler = new MessageLogHandler(); |
| 108 |
messageHandler.setFilter(new Filter() { |
| 109 |
public boolean isLoggable(LogRecord record) { |
| 110 |
return record.getLevel().intValue() < Level.WARNING.intValue(); |
| 111 |
} |
| 112 |
}); |
| 113 |
logger.addHandler(messageHandler); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
Handler errorHandler = new ConsoleHandler(); |
| 118 |
errorHandler.setFilter(new Filter() { |
| 119 |
public boolean isLoggable(LogRecord record) { |
| 120 |
if (verbose) { |
| 121 |
return record.getLevel().intValue() >= Level.WARNING.intValue(); |
| 122 |
} else { |
| 123 |
return record.getLevel().intValue() >= Level.SEVERE.intValue(); |
| 124 |
} |
| 125 |
} |
| 126 |
}); |
| 127 |
infoLogger.addHandler(errorHandler); |
| 128 |
|
| 129 |
if (System.getProperty("citations.filename") != null) { |
| 130 |
FileOutputStream citationStream = new FileOutputStream(System.getProperty("citations.filename")); |
| 131 |
Handler citationHandler = new MessageLogHandler(citationStream); |
| 132 |
Logger.getLogger("dr.apps.beast").addHandler(citationHandler); |
| 133 |
} |
| 134 |
|
| 135 |
logger.setUseParentHandlers(false); |
| 136 |
|
| 137 |
infoLogger.info("Parsing XML file: " + fileName); |
| 138 |
infoLogger.info(" File encoding: " + fileReader.getEncoding()); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
Logger errorLogger = Logger.getLogger("error"); |
| 144 |
messageHandler = new ErrorLogHandler(maxErrorCount); |
| 145 |
messageHandler.setLevel(Level.WARNING); |
| 146 |
errorLogger.addHandler(messageHandler); |
| 147 |
|
| 148 |
for (String pluginName : PluginLoader.getAvailablePlugins()) { |
| 149 |
Plugin plugin = PluginLoader.loadPlugin(pluginName); |
| 150 |
if (plugin != null) { |
| 151 |
Set<XMLObjectParser> parserSet = plugin.getParsers(); |
| 152 |
for (XMLObjectParser pluginParser : parserSet) { |
| 153 |
parser.addXMLObjectParser(pluginParser); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
new BeastCheckpointer(); |
| 161 |
|
| 162 |
if (!useMC3) { |
| 163 |
|
| 164 |
|
| 165 |
parser.parse(fileReader, true); |
| 166 |
|
| 167 |
} else { |
| 168 |
int chainCount = chainTemperatures.length; |
| 169 |
MCMC[] chains = new MCMC[chainCount]; |
| 170 |
MCMCMCOptions options = new MCMCMCOptions(chainTemperatures, swapChainsEvery); |
| 171 |
|
| 172 |
Logger.getLogger("dr.apps.beast").info("Starting cold chain plus hot chains with temperatures: "); |
| 173 |
for (int i = 1; i < chainTemperatures.length; i++) { |
| 174 |
Logger.getLogger("dr.apps.beast").info("Hot Chain " + i + ": " + chainTemperatures[i]); |
| 175 |
} |
| 176 |
|
| 177 |
Logger.getLogger("dr.apps.beast").info("Parsing XML file: " + fileName); |
| 178 |
|
| 179 |
|
| 180 |
chains[0] = (MCMC) parser.parse(fileReader, MCMC.class); |
| 181 |
if (chains[0] == null) { |
| 182 |
throw new dr.xml.XMLParseException("BEAST XML file is missing an MCMC element"); |
| 183 |
} |
| 184 |
fileReader.close(); |
| 185 |
|
| 186 |
chainTemperatures[0] = 1.0; |
| 187 |
|
| 188 |
for (int i = 1; i < chainCount; i++) { |
| 189 |
|
| 190 |
fileReader = new FileReader(inputFile); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
messageHandler.setLevel(Level.OFF); |
| 195 |
parser = new BeastParser(new String[]{fileName}, additionalParsers, verbose, parserWarning, strictXML, version); |
| 196 |
|
| 197 |
chains[i] = (MCMC) parser.parse(fileReader, MCMC.class); |
| 198 |
if (chains[i] == null) { |
| 199 |
throw new dr.xml.XMLParseException("BEAST XML file is missing an MCMC element"); |
| 200 |
} |
| 201 |
fileReader.close(); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
messageHandler.setLevel(Level.ALL); |
| 206 |
|
| 207 |
MCMCMC mc3 = new MCMCMC(chains, options); |
| 208 |
Thread thread = new Thread(mc3); |
| 209 |
thread.start(); |
| 210 |
} |
| 211 |
|
| 212 |
} catch (java.io.IOException ioe) { |
| 213 |
infoLogger.severe("File error: " + ioe.getMessage()); |
| 214 |
throw new RuntimeException("Terminate"); |
| 215 |
} catch (org.xml.sax.SAXParseException spe) { |
| 216 |
if (spe.getMessage() != null && spe.getMessage().equals("Content is not allowed in prolog")) { |
| 217 |
infoLogger.severe("Parsing error - the input file, " + fileName + ", is not a valid XML file."); |
| 218 |
} else { |
| 219 |
infoLogger.severe("Error running file: " + fileName); |
| 220 |
infoLogger.severe("Parsing error - poorly formed XML (possibly not an XML file):\n" + |
| 221 |
spe.getMessage()); |
| 222 |
} |
| 223 |
throw new RuntimeException("Terminate"); |
| 224 |
} catch (org.w3c.dom.DOMException dome) { |
| 225 |
infoLogger.severe("Error running file: " + fileName); |
| 226 |
infoLogger.severe("Parsing error - poorly formed XML:\n" + |
| 227 |
dome.getMessage()); |
| 228 |
throw new RuntimeException("Terminate"); |
| 229 |
} catch (dr.xml.XMLParseException pxe) { |
| 230 |
|
| 231 |
|
| 232 |
if (pxe.getMessage() != null && pxe.getMessage().equals("Unknown root document element, beauti")) { |
| 233 |
infoLogger.severe("Error running file: " + fileName); |
| 234 |
infoLogger.severe( |
| 235 |
"The file you just tried to run in BEAST is actually a BEAUti document.\n" + |
| 236 |
"Although this uses XML, it is not a format that BEAST understands.\n" + |
| 237 |
"These files are used by BEAUti to save and load your settings so that\n" + |
| 238 |
"you can go back and alter them. To generate a BEAST file you must\n" + |
| 239 |
"select the 'Generate BEAST File' option, either from the File menu or\n" + |
| 240 |
"the button at the bottom right of the window."); |
| 241 |
|
| 242 |
} else { |
| 243 |
infoLogger.severe("Parsing error - poorly formed BEAST file, " + fileName + ":\n" + |
| 244 |
pxe.getMessage() + "\n\nError thrown at: " + pxe.getStackTrace()[0] + "\n"); |
| 245 |
} |
| 246 |
throw new RuntimeException("Terminate"); |
| 247 |
} catch (RuntimeException rex) { |
| 248 |
|
| 249 |
if (rex.getMessage() != null && rex.getMessage().startsWith("The initial posterior is zero")) { |
| 250 |
infoLogger.severe("Error running file: " + fileName); |
| 251 |
infoLogger.severe( |
| 252 |
"The initial model is invalid because state has a zero probability.\n\n" + |
| 253 |
"If the log likelihood of the tree is -Inf, this may be because the\n" + |
| 254 |
"initial, random tree is so large that it has an extremely bad\n" + |
| 255 |
"likelihood which is being rounded to zero.\n\n" + |
| 256 |
"Alternatively, it may be that the product of starting mutation rate\n" + |
| 257 |
"and tree height is extremely small or extremely large. \n\n" + |
| 258 |
"Finally, it may be that the initial state is incompatible with\n" + |
| 259 |
"one or more 'hard' constraints (on monophyly or bounds on parameter\n" + |
| 260 |
"values. This will result in Priors with zero probability.\n\n" + |
| 261 |
"The individual components of the posterior are as follows:\n" + |
| 262 |
rex.getMessage() + "\n" + |
| 263 |
"For more information go to <http://beast.community>."); |
| 264 |
} else { |
| 265 |
|
| 266 |
|
| 267 |
infoLogger.warning("Error running file: " + fileName); |
| 268 |
System.err.println("Fatal exception: " + rex.getMessage()); |
| 269 |
rex.printStackTrace(System.err); |
| 270 |
System.err.flush(); |
| 271 |
} |
| 272 |
throw new RuntimeException("Terminate"); |
| 273 |
} catch (Exception ex) { |
| 274 |
infoLogger.warning("Error running file: " + fileName); |
| 275 |
infoLogger.severe("Fatal exception: " + ex.getMessage()); |
| 276 |
System.err.println("Fatal exception: " + ex.getMessage()); |
| 277 |
ex.printStackTrace(System.err); |
| 278 |
System.err.flush(); |
| 279 |
throw new RuntimeException("Terminate"); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
public static void centreLine(String line, int pageWidth) { |
| 284 |
int n = pageWidth - line.length(); |
| 285 |
int n1 = n / 2; |
| 286 |
for (int i = 0; i < n1; i++) { |
| 287 |
System.out.print(" "); |
| 288 |
} |
| 289 |
System.out.println(line); |
| 290 |
} |
| 291 |
|
| 292 |
public static void printTitle() { |
| 293 |
System.out.println(); |
| 294 |
centreLine("BEAST " + version.getVersionString() + ", " + version.getDateString(), 60); |
| 295 |
centreLine("Bayesian Evolutionary Analysis Sampling Trees", 60); |
| 296 |
for (String creditLine : version.getCredits()) { |
| 297 |
centreLine(creditLine, 60); |
| 298 |
} |
| 299 |
System.out.println(); |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
public static void printUsage(Arguments arguments) { |
| 304 |
|
| 305 |
arguments.printUsage("beast", "[<input-file-name>]"); |
| 306 |
System.out.println(); |
| 307 |
System.out.println(" Example: beast test.xml"); |
| 308 |
System.out.println(" Example: beast -window test.xml"); |
| 309 |
System.out.println(" Example: beast -help"); |
| 310 |
System.out.println(); |
| 311 |
} |
| 312 |
|
| 313 |
private static long updateSeedByRank(long seed, int rank) { |
| 314 |
return seed + 1000 * 1000 * rank; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
public static void main(String[] args) throws java.io.IOException { |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
Locale.setDefault(Locale.US); |
| 323 |
|
| 324 |
Arguments arguments = new Arguments( |
| 325 |
new Arguments.Option[]{ |
| 326 |
|
| 327 |
new Arguments.Option("verbose", "Give verbose XML parsing messages"), |
| 328 |
new Arguments.Option("warnings", "Show warning messages about BEAST XML file"), |
| 329 |
new Arguments.Option("strict", "Fail on non-conforming BEAST XML file"), |
| 330 |
new Arguments.Option("window", "Provide a console window"), |
| 331 |
new Arguments.Option("options", "Display an options dialog"), |
| 332 |
new Arguments.Option("working", "Change working directory to input file's directory"), |
| 333 |
new Arguments.LongOption("seed", "Specify a random number generator seed"), |
| 334 |
new Arguments.StringOption("prefix", "PREFIX", "Specify a prefix for all output log filenames"), |
| 335 |
new Arguments.Option("overwrite", "Allow overwriting of log files"), |
| 336 |
new Arguments.IntegerOption("errors", "Specify maximum number of numerical errors before stopping"), |
| 337 |
new Arguments.IntegerOption("threads", "The number of computational threads to use (default auto)"), |
| 338 |
new Arguments.Option("java", "Use Java only, no native implementations"), |
| 339 |
new Arguments.LongOption("tests", "The number of full evaluation tests to perform (default 1000)"), |
| 340 |
new Arguments.RealOption("threshold", 0.0, Double.MAX_VALUE, "Full evaluation test threshold (default 0.1)"), |
| 341 |
|
| 342 |
new Arguments.Option("adaptation_off", "Don't adapt operator sizes"), |
| 343 |
new Arguments.RealOption("adaptation_target", 0.0, 1.0, "Target acceptance rate for adaptive operators (default 0.234)"), |
| 344 |
|
| 345 |
new Arguments.Option("beagle", "Use BEAGLE library if available (default on)"), |
| 346 |
new Arguments.Option("beagle_info", "BEAGLE: show information on available resources"), |
| 347 |
new Arguments.StringOption("beagle_order", "order", "BEAGLE: set order of resource use"), |
| 348 |
new Arguments.IntegerOption("beagle_instances", "BEAGLE: divide site patterns amongst instances"), |
| 349 |
new Arguments.StringOption("beagle_multipartition", new String[]{"auto", "on", "off"}, |
| 350 |
false, "BEAGLE: use multipartition extensions if available (default auto)"), |
| 351 |
new Arguments.Option("beagle_CPU", "BEAGLE: use CPU instance"), |
| 352 |
new Arguments.Option("beagle_GPU", "BEAGLE: use GPU instance if available"), |
| 353 |
new Arguments.Option("beagle_SSE", "BEAGLE: use SSE extensions if available"), |
| 354 |
new Arguments.Option("beagle_SSE_off", "BEAGLE: turn off use of SSE extensions"), |
| 355 |
new Arguments.Option("beagle_threading_off", "BEAGLE: turn off auto threading for a CPU instance"), |
| 356 |
new Arguments.IntegerOption("beagle_thread_count", 1, Integer.MAX_VALUE, "BEAGLE: manually set number of threads for a CPU instance"), |
| 357 |
new Arguments.Option("beagle_cuda", "BEAGLE: use CUDA parallization if available"), |
| 358 |
new Arguments.Option("beagle_opencl", "BEAGLE: use OpenCL parallization if available"), |
| 359 |
new Arguments.Option("beagle_single", "BEAGLE: use single precision if available"), |
| 360 |
new Arguments.Option("beagle_double", "BEAGLE: use double precision if available"), |
| 361 |
new Arguments.Option("beagle_async", "BEAGLE: use asynchronous kernels if available"), |
| 362 |
new Arguments.StringOption("beagle_scaling", new String[]{"default", "dynamic", "delayed", "always", "none"}, |
| 363 |
false, "BEAGLE: specify scaling scheme to use"), |
| 364 |
new Arguments.Option("beagle_delay_scaling_off", "BEAGLE: don't wait until underflow for scaling option"), |
| 365 |
new Arguments.LongOption("beagle_rescale", "BEAGLE: frequency of rescaling (dynamic scaling only)"), |
| 366 |
new Arguments.Option("mpi", "Use MPI rank to label output"), |
| 367 |
|
| 368 |
new Arguments.StringOption("particles", "FOLDER", "Specify a folder of particle start states"), |
| 369 |
|
| 370 |
new Arguments.IntegerOption("mc3_chains", 1, Integer.MAX_VALUE, "number of chains"), |
| 371 |
new Arguments.RealOption("mc3_delta", 0.0, Double.MAX_VALUE, "temperature increment parameter"), |
| 372 |
new Arguments.RealArrayOption("mc3_temperatures", -1, "a comma-separated list of the hot chain temperatures"), |
| 373 |
new Arguments.IntegerOption("mc3_swap", 1, Integer.MAX_VALUE, "frequency at which chains temperatures will be swapped"), |
| 374 |
|
| 375 |
new Arguments.StringOption("load_state", "FILENAME", "Specify a filename to load a saved state from"), |
| 376 |
new Arguments.StringOption("save_stem", "FILENAME", "Specify a stem for the filenames to save states to"), |
| 377 |
new Arguments.LongOption("save_at", "Specify a state at which to save a state file"), |
| 378 |
new Arguments.LongOption("save_every", "Specify a frequency to save the state file"), |
| 379 |
new Arguments.StringOption("save_state", "FILENAME", "Specify a filename to save state to"), |
| 380 |
new Arguments.Option("force_resume", "Force resuming from a saved state"), |
| 381 |
|
| 382 |
new Arguments.StringOption("citations_file", "FILENAME", "Specify a filename to write a citation list to"), |
| 383 |
|
| 384 |
new Arguments.Option("version", "Print the version and credits and stop"), |
| 385 |
new Arguments.Option("help", "Print this information and stop"), |
| 386 |
}); |
| 387 |
|
| 388 |
int[] versionNumbers = BeagleInfo.getVersionNumbers(); |
| 389 |
if (versionNumbers.length != 0 && versionNumbers[0] >= 3 && versionNumbers[1] >= 1) { |
| 390 |
arguments.addOption("beagle_auto", |
| 391 |
"BEAGLE: automatically select fastest resource for analysis", |
| 392 |
"beagle_info"); |
| 393 |
}; |
| 394 |
|
| 395 |
int argumentCount = 0; |
| 396 |
|
| 397 |
StringBuilder commandLine = new StringBuilder(); |
| 398 |
for (int i = 0; i < args.length; i++) { |
| 399 |
if (i > 0) { |
| 400 |
commandLine.append(" "); |
| 401 |
} |
| 402 |
commandLine.append(args[i]); |
| 403 |
} |
| 404 |
System.setProperty("command_line", commandLine.toString()); |
| 405 |
|
| 406 |
try { |
| 407 |
argumentCount = arguments.parseArguments(args); |
| 408 |
} catch (Arguments.ArgumentException ae) { |
| 409 |
System.out.println(); |
| 410 |
System.out.println(ae.getMessage()); |
| 411 |
System.out.println(); |
| 412 |
printUsage(arguments); |
| 413 |
System.exit(1); |
| 414 |
} |
| 415 |
|
| 416 |
if (arguments.hasOption("version")) { |
| 417 |
printTitle(); |
| 418 |
} |
| 419 |
|
| 420 |
if (arguments.hasOption("help")) { |
| 421 |
printUsage(arguments); |
| 422 |
} |
| 423 |
|
| 424 |
if (arguments.hasOption("version") || arguments.hasOption("help")) { |
| 425 |
System.exit(0); |
| 426 |
} |
| 427 |
|
| 428 |
List<String> additionalParsers = new ArrayList<String>(); |
| 429 |
|
| 430 |
final boolean verbose = arguments.hasOption("verbose"); |
| 431 |
if (verbose) { |
| 432 |
System.setProperty("verbose_output", Boolean.toString(true)); |
| 433 |
} |
| 434 |
|
| 435 |
final boolean warnings = arguments.hasOption("warnings"); |
| 436 |
if (warnings) { |
| 437 |
System.setProperty("show_warnings", Boolean.toString(true)); |
| 438 |
} |
| 439 |
|
| 440 |
final boolean strictXML = arguments.hasOption("strict"); |
| 441 |
final boolean working = arguments.hasOption("working"); |
| 442 |
String fileNamePrefix = null; |
| 443 |
boolean allowOverwrite = arguments.hasOption("overwrite"); |
| 444 |
boolean useMPI = arguments.hasOption("mpi"); |
| 445 |
|
| 446 |
long seed = MathUtils.getSeed(); |
| 447 |
boolean useJava = false; |
| 448 |
|
| 449 |
int threadCount = -1; |
| 450 |
|
| 451 |
boolean usingSMC = false; |
| 452 |
|
| 453 |
boolean usingMC3 = false; |
| 454 |
double[] chainTemperatures = null; |
| 455 |
int swapChainsEvery = DEFAULT_SWAP_CHAIN_EVERY; |
| 456 |
|
| 457 |
if (arguments.hasOption("particles")) { |
| 458 |
System.setProperty("smc.particle_folder", arguments.getStringOption("particles")); |
| 459 |
usingSMC = true; |
| 460 |
|
| 461 |
System.setProperty("mcmc.evaluation.count", Long.toString(0)); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
final boolean window = !usingSMC && arguments.hasOption("window"); |
| 466 |
final boolean options = !usingSMC && (arguments.hasOption("options") || (argumentCount == 0)); |
| 467 |
|
| 468 |
if (!usingSMC) { |
| 469 |
if (arguments.hasOption("tests")) { |
| 470 |
long fullEvaluationCount = arguments.getLongOption("tests"); |
| 471 |
System.setProperty("mcmc.evaluation.count", Long.toString(fullEvaluationCount)); |
| 472 |
} |
| 473 |
|
| 474 |
if (arguments.hasOption("threshold")) { |
| 475 |
double fullEvaluationThreshold = arguments.getRealOption("threshold"); |
| 476 |
System.setProperty("mcmc.evaluation.threshold", Double.toString(fullEvaluationThreshold)); |
| 477 |
} |
| 478 |
|
| 479 |
if (arguments.hasOption("java")) { |
| 480 |
useJava = true; |
| 481 |
} |
| 482 |
|
| 483 |
if (arguments.hasOption("prefix")) { |
| 484 |
fileNamePrefix = arguments.getStringOption("prefix"); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
int chainCount = 1; |
| 490 |
if (arguments.hasOption("mc3_chains")) { |
| 491 |
chainCount = arguments.getIntegerOption("mc3_chains"); |
| 492 |
} else if (arguments.hasOption("mc3_temperatures")) { |
| 493 |
chainCount = 1 + arguments.getRealArrayOption("mc3_temperatures").length; |
| 494 |
} |
| 495 |
|
| 496 |
double delta = DEFAULT_DELTA; |
| 497 |
if (arguments.hasOption("mc3_delta")) { |
| 498 |
if (arguments.hasOption("mc3_temperatures")) { |
| 499 |
System.err.println("Either the -mc3_delta or the -mc3_temperatures option should be used, not both"); |
| 500 |
System.err.println(); |
| 501 |
printUsage(arguments); |
| 502 |
System.exit(1); |
| 503 |
} |
| 504 |
delta = arguments.getRealOption("mc3_delta"); |
| 505 |
} |
| 506 |
|
| 507 |
chainTemperatures = new double[chainCount]; |
| 508 |
chainTemperatures[0] = 1.0; |
| 509 |
if (arguments.hasOption("mc3_temperatures")) { |
| 510 |
double[] hotChainTemperatures = arguments.getRealArrayOption("mc3_temperatures"); |
| 511 |
assert hotChainTemperatures.length == chainCount - 1; |
| 512 |
|
| 513 |
System.arraycopy(hotChainTemperatures, 0, chainTemperatures, 1, chainCount - 1); |
| 514 |
} else { |
| 515 |
for (int i = 1; i < chainCount; i++) { |
| 516 |
chainTemperatures[i] = 1.0 / (1.0 + (delta * i)); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
if (arguments.hasOption("mc3_swap")) { |
| 521 |
swapChainsEvery = arguments.getIntegerOption("mc3_swap"); |
| 522 |
} |
| 523 |
|
| 524 |
usingMC3 = chainCount > 1; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
long beagleFlags = 0; |
| 529 |
|
| 530 |
boolean beagleShowInfo = arguments.hasOption("beagle_info"); |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
if (arguments.hasOption("beagle_auto")) { |
| 536 |
System.setProperty("beagle.resource.auto", Boolean.TRUE.toString()); |
| 537 |
} |
| 538 |
if (arguments.hasOption("beagle_CPU")) { |
| 539 |
beagleFlags |= BeagleFlag.PROCESSOR_CPU.getMask(); |
| 540 |
} |
| 541 |
if (arguments.hasOption("beagle_GPU")) { |
| 542 |
beagleFlags |= BeagleFlag.PROCESSOR_GPU.getMask(); |
| 543 |
} |
| 544 |
if (arguments.hasOption("beagle_cuda")) { |
| 545 |
beagleFlags |= BeagleFlag.FRAMEWORK_CUDA.getMask(); |
| 546 |
} |
| 547 |
if (arguments.hasOption("beagle_opencl")) { |
| 548 |
beagleFlags |= BeagleFlag.FRAMEWORK_OPENCL.getMask(); |
| 549 |
} |
| 550 |
if (!arguments.hasOption("beagle_SSE_off")) { |
| 551 |
beagleFlags |= BeagleFlag.VECTOR_SSE.getMask(); |
| 552 |
} else { |
| 553 |
beagleFlags |= BeagleFlag.VECTOR_NONE.getMask(); |
| 554 |
} |
| 555 |
if (arguments.hasOption("beagle_threading_off")) { |
| 556 |
beagleFlags |= BeagleFlag.THREADING_NONE.getMask(); |
| 557 |
} else { |
| 558 |
beagleFlags |= BeagleFlag.THREADING_CPP.getMask(); |
| 559 |
} |
| 560 |
if (arguments.hasOption("beagle_thread_count")) { |
| 561 |
int beagleThreadCount = arguments.getIntegerOption("beagle_thread_count"); |
| 562 |
if (beagleThreadCount == 1) { |
| 563 |
beagleFlags &= ~BeagleFlag.THREADING_CPP.getMask(); |
| 564 |
beagleFlags |= BeagleFlag.THREADING_NONE.getMask(); |
| 565 |
} |
| 566 |
System.setProperty("beagle.thread.count", Integer.toString(arguments.getIntegerOption("beagle_thread_count"))); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
if (arguments.hasOption("beagle_single")) { |
| 572 |
beagleFlags |= BeagleFlag.PRECISION_SINGLE.getMask(); |
| 573 |
} else { |
| 574 |
beagleFlags |= BeagleFlag.PRECISION_DOUBLE.getMask(); |
| 575 |
} |
| 576 |
if (arguments.hasOption("beagle_async")) { |
| 577 |
beagleFlags |= BeagleFlag.COMPUTATION_ASYNCH.getMask(); |
| 578 |
} |
| 579 |
|
| 580 |
if (arguments.hasOption("beagle_order")) { |
| 581 |
System.setProperty("beagle.resource.order", arguments.getStringOption("beagle_order")); |
| 582 |
} |
| 583 |
|
| 584 |
if (arguments.hasOption("beagle_instances")) { |
| 585 |
System.setProperty("beagle.instance.count", Integer.toString(arguments.getIntegerOption("beagle_instances"))); |
| 586 |
} |
| 587 |
|
| 588 |
if (arguments.hasOption("beagle_scaling")) { |
| 589 |
System.setProperty("beagle.scaling", arguments.getStringOption("beagle_scaling")); |
| 590 |
} |
| 591 |
|
| 592 |
if (arguments.hasOption("beagle_delay_scaling_off")) { |
| 593 |
System.setProperty("beagle.delay.scaling", Boolean.FALSE.toString()); |
| 594 |
} |
| 595 |
|
| 596 |
if (arguments.hasOption("beagle_rescale")) { |
| 597 |
System.setProperty("beagle.rescale", Long.toString(arguments.getLongOption("beagle_rescale"))); |
| 598 |
} |
| 599 |
|
| 600 |
if (arguments.hasOption("beagle_multipartition")) { |
| 601 |
|
| 602 |
if (arguments.getStringOption("beagle_multipartition").toLowerCase().equals("on")) { |
| 603 |
System.setProperty("beagle.multipartition.extensions", Boolean.TRUE.toString()); |
| 604 |
} else if (arguments.getStringOption("beagle_multipartition").toLowerCase().equals("off")) { |
| 605 |
System.setProperty("beagle.multipartition.extensions", Boolean.FALSE.toString()); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
if (arguments.hasOption("threads")) { |
| 611 |
|
| 612 |
threadCount = arguments.getIntegerOption("threads"); |
| 613 |
if (threadCount < 0) { |
| 614 |
printTitle(); |
| 615 |
System.err.println("The the number of threads should be >= 0"); |
| 616 |
System.exit(1); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
if (arguments.hasOption("seed")) { |
| 621 |
seed = arguments.getLongOption("seed"); |
| 622 |
if (seed <= 0) { |
| 623 |
printTitle(); |
| 624 |
System.err.println("The random number seed should be > 0"); |
| 625 |
System.exit(1); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
if (arguments.hasOption("adaptation_off")) { |
| 630 |
System.setProperty("mcmc.use_adaptation", Boolean.FALSE.toString()); |
| 631 |
} |
| 632 |
if (arguments.hasOption("adaptation_target")) { |
| 633 |
System.setProperty("adaptation_target", |
| 634 |
Double.toString(arguments.getRealOption("mcmc.adaptation_target"))); |
| 635 |
} |
| 636 |
|
| 637 |
if (!usingSMC) { |
| 638 |
|
| 639 |
|
| 640 |
if (arguments.hasOption("load_state")) { |
| 641 |
String stateFile = arguments.getStringOption("load_state"); |
| 642 |
System.setProperty(BeastCheckpointer.LOAD_STATE_FILE, stateFile); |
| 643 |
} |
| 644 |
|
| 645 |
if (arguments.hasOption("save_at")) { |
| 646 |
long saveAt = arguments.getLongOption("save_at"); |
| 647 |
System.setProperty(BeastCheckpointer.SAVE_STATE_AT, Long.toString(saveAt)); |
| 648 |
} |
| 649 |
|
| 650 |
if (arguments.hasOption("save_every")) { |
| 651 |
long saveEvery = arguments.getLongOption("save_every"); |
| 652 |
System.setProperty(BeastCheckpointer.SAVE_STATE_EVERY, Long.toString(saveEvery)); |
| 653 |
} |
| 654 |
|
| 655 |
if (arguments.hasOption("save_state")) { |
| 656 |
String stateFile = arguments.getStringOption("save_state"); |
| 657 |
System.setProperty(BeastCheckpointer.SAVE_STATE_FILE, stateFile); |
| 658 |
} |
| 659 |
|
| 660 |
if (arguments.hasOption("save_stem")) { |
| 661 |
String stemName = arguments.getStringOption("save_stem"); |
| 662 |
System.setProperty(BeastCheckpointer.SAVE_STEM, stemName); |
| 663 |
} |
| 664 |
|
| 665 |
if (arguments.hasOption("force_resume")) { |
| 666 |
System.setProperty("force.resume", Boolean.TRUE.toString()); |
| 667 |
} |
| 668 |
|
| 669 |
if (arguments.hasOption("citations_file")) { |
| 670 |
String debugStateFile = arguments.getStringOption("citations_file"); |
| 671 |
System.setProperty("citations.filename", debugStateFile); |
| 672 |
} |
| 673 |
|
| 674 |
if (useMPI) { |
| 675 |
String[] nullArgs = new String[0]; |
| 676 |
try { |
| 677 |
BeastMPI.Init(nullArgs); |
| 678 |
} catch (Exception e) { |
| 679 |
throw new RuntimeException("Unable to access MPI."); |
| 680 |
} |
| 681 |
int rank = BeastMPI.COMM_WORLD.Rank(); |
| 682 |
System.setProperty("mpi.rank.postfix", String.valueOf(rank)); |
| 683 |
|
| 684 |
} |
| 685 |
|
| 686 |
String rankProp = System.getProperty("mpi.rank.postfix"); |
| 687 |
if (rankProp != null) { |
| 688 |
int rank = Integer.valueOf(rankProp); |
| 689 |
seed = updateSeedByRank(seed, rank); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
int maxErrorCount = 0; |
| 694 |
if (arguments.hasOption("errors")) { |
| 695 |
maxErrorCount = arguments.getIntegerOption("errors"); |
| 696 |
if (maxErrorCount < 0) { |
| 697 |
maxErrorCount = 0; |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
BeastConsoleApp consoleApp = null; |
| 702 |
|
| 703 |
String nameString = "BEAST " + version.getVersionString(); |
| 704 |
|
| 705 |
if (window) { |
| 706 |
System.setProperty("com.apple.macos.useScreenMenuBar", "true"); |
| 707 |
System.setProperty("apple.laf.useScreenMenuBar", "true"); |
| 708 |
System.setProperty("apple.awt.showGrowBox", "true"); |
| 709 |
|
| 710 |
javax.swing.Icon icon = IconUtils.getIcon(BeastMain.class, "images/beast.png"); |
| 711 |
|
| 712 |
String titleString = "<html>" + |
| 713 |
"<div style=\"font: HelveticaNeue, Helvetica, Arial, sans-serif\">" + |
| 714 |
"<p style=\"font-weight: 100; font-size: 42px\">BEAST</p>" + |
| 715 |
"<p style=\"font-weight: 200; font-size: 12px\">Bayesian Evolutionary Analysis Sampling Trees</p>" + |
| 716 |
"<p style=\"font-weight: 300; font-size: 11px\">Version " + version.getVersionString() + ", " + version.getDateString() + "</p>" + |
| 717 |
"</div></html>"; |
| 718 |
|
| 719 |
String aboutString = "<html>" + |
| 720 |
"<div style=\"font-family:HelveticaNeue-Light, 'Helvetica Neue Light', Helvetica, Arial, 'Lucida Grande',sans-serif; font-weight: 100\">" + |
| 721 |
"<center>" + |
| 722 |
version.getHTMLCredits() + |
| 723 |
"</div></center></div></html>"; |
| 724 |
|
| 725 |
consoleApp = new BeastConsoleApp(nameString, titleString, aboutString, icon); |
| 726 |
consoleApp.initialize(); |
| 727 |
|
| 728 |
} |
| 729 |
|
| 730 |
printTitle(); |
| 731 |
|
| 732 |
File inputFile = null; |
| 733 |
|
| 734 |
if (options && !beagleShowInfo) { |
| 735 |
|
| 736 |
String titleString = "<html>" + |
| 737 |
"<div style=\"font: HelveticaNeue, Helvetica, Arial, sans-serif\">" + |
| 738 |
"<div style=\"font-weight: 100; font-size: 42px\">BEAST</div>" + |
| 739 |
"<div style=\"font-weight: 200; font-size: 11px\">Bayesian Evolutionary Analysis Sampling Trees</div>" + |
| 740 |
"<div style=\"font-weight: 300; font-size: 10px\">Version " + version.getVersionString() + ", " + version.getDateString() + "</div>" + |
| 741 |
"<div style=\"font-weight: 300; font-size: 10px\"><a href=\"" + version.getBuildString() + "\">" + |
| 742 |
version.getBuildString() + "</a></div>" + |
| 743 |
"</div></html>"; |
| 744 |
javax.swing.Icon icon = IconUtils.getIcon(BeastMain.class, "images/beast.png"); |
| 745 |
|
| 746 |
BeastDialog dialog = new BeastDialog(new JFrame(), titleString, icon); |
| 747 |
|
| 748 |
dialog.setAllowOverwrite(allowOverwrite); |
| 749 |
dialog.setSeed(seed); |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
if (BeagleFlag.PROCESSOR_GPU.isSet(beagleFlags)) { |
| 754 |
dialog.setPreferBeagleGPU(); |
| 755 |
} |
| 756 |
|
| 757 |
dialog.setPreferBeagleSSE(BeagleFlag.VECTOR_SSE.isSet(beagleFlags)); |
| 758 |
|
| 759 |
if (BeagleFlag.PRECISION_SINGLE.isSet(beagleFlags)) { |
| 760 |
dialog.setPreferBeagleSingle(); |
| 761 |
} |
| 762 |
|
| 763 |
if (!dialog.showDialog(nameString)) { |
| 764 |
return; |
| 765 |
} |
| 766 |
|
| 767 |
if (dialog.allowOverwrite()) { |
| 768 |
allowOverwrite = true; |
| 769 |
} |
| 770 |
|
| 771 |
seed = dialog.getSeed(); |
| 772 |
threadCount = dialog.getThreadPoolSize(); |
| 773 |
|
| 774 |
beagleShowInfo = dialog.showBeagleInfo(); |
| 775 |
if (dialog.preferBeagleCPU()) { |
| 776 |
beagleFlags |= BeagleFlag.PROCESSOR_CPU.getMask(); |
| 777 |
} |
| 778 |
if (dialog.preferBeagleSSE()) { |
| 779 |
beagleFlags |= BeagleFlag.VECTOR_SSE.getMask(); |
| 780 |
} else { |
| 781 |
beagleFlags &= ~BeagleFlag.VECTOR_SSE.getMask(); |
| 782 |
} |
| 783 |
if (dialog.preferBeagleGPU()) { |
| 784 |
beagleFlags |= BeagleFlag.PROCESSOR_GPU.getMask(); |
| 785 |
} |
| 786 |
if (dialog.preferBeagleDouble()) { |
| 787 |
beagleFlags |= BeagleFlag.PRECISION_DOUBLE.getMask(); |
| 788 |
} |
| 789 |
if (dialog.preferBeagleSingle()) { |
| 790 |
beagleFlags |= BeagleFlag.PRECISION_SINGLE.getMask(); |
| 791 |
} |
| 792 |
System.setProperty("beagle.scaling", dialog.scalingScheme()); |
| 793 |
|
| 794 |
inputFile = dialog.getInputFile(); |
| 795 |
if (!beagleShowInfo && inputFile == null) { |
| 796 |
System.err.println("No input file specified"); |
| 797 |
return; |
| 798 |
} |
| 799 |
|
| 800 |
} |
| 801 |
|
| 802 |
BeagleInfo.printVersionInformation(); |
| 803 |
|
| 804 |
if (BeagleInfo.getVersion().startsWith("1.")) { |
| 805 |
System.err.println("WARNING: You are currenly using BEAGLE v1.x. For best performance and compatibility\n" + |
| 806 |
"with models in BEAST, please upgrade to BEAGLE v3.x at http://github.com/beagle-dev/beagle-lib/\n"); |
| 807 |
} else if (BeagleInfo.getVersion().startsWith("2.")) { |
| 808 |
System.err.println("WARNING: You are currenly using BEAGLE v2.x. For best performance and compatibility\n" + |
| 809 |
"with models in BEAST, please upgrade to BEAGLE v3.x at http://github.com/beagle-dev/beagle-lib/\n"); |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
if (beagleShowInfo) { |
| 814 |
BeagleInfo.printResourceList(); |
| 815 |
return; |
| 816 |
} |
| 817 |
|
| 818 |
if (inputFile == null) { |
| 819 |
|
| 820 |
String[] args2 = arguments.getLeftoverArguments(); |
| 821 |
|
| 822 |
if (args2.length > 1) { |
| 823 |
System.err.println("Unknown option: " + args2[1]); |
| 824 |
System.err.println(); |
| 825 |
printUsage(arguments); |
| 826 |
return; |
| 827 |
} |
| 828 |
|
| 829 |
String inputFileName = null; |
| 830 |
|
| 831 |
|
| 832 |
if (args2.length > 0) { |
| 833 |
inputFileName = args2[0]; |
| 834 |
inputFile = new File(inputFileName); |
| 835 |
} |
| 836 |
|
| 837 |
if (inputFileName == null) { |
| 838 |
|
| 839 |
inputFile = Utils.getLoadFile("BEAST " + version.getVersionString() + " - Select XML input file"); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
if (inputFile != null && inputFile.getParent() != null && working) { |
| 844 |
System.setProperty("user.dir", inputFile.getParent()); |
| 845 |
} |
| 846 |
|
| 847 |
if (window) { |
| 848 |
if (inputFile == null) { |
| 849 |
consoleApp.setTitle("null"); |
| 850 |
} else { |
| 851 |
consoleApp.setTitle(inputFile.getName()); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
if (useJava) { |
| 856 |
System.setProperty("java.only", "true"); |
| 857 |
} |
| 858 |
|
| 859 |
if (fileNamePrefix != null && fileNamePrefix.trim().length() > 0) { |
| 860 |
System.setProperty("file.name.prefix", fileNamePrefix.trim()); |
| 861 |
} |
| 862 |
|
| 863 |
if (allowOverwrite) { |
| 864 |
System.setProperty("log.allow.overwrite", "true"); |
| 865 |
} |
| 866 |
|
| 867 |
additionalParsers.add("beagle"); |
| 868 |
|
| 869 |
if (beagleFlags != 0) { |
| 870 |
System.setProperty("beagle.preferred.flags", Long.toString(beagleFlags)); |
| 871 |
|
| 872 |
} |
| 873 |
|
| 874 |
if (threadCount >= 0) { |
| 875 |
System.setProperty("thread.count", String.valueOf(threadCount)); |
| 876 |
} |
| 877 |
|
| 878 |
MathUtils.setSeed(seed); |
| 879 |
|
| 880 |
System.out.println("Random number seed: " + seed); |
| 881 |
|
| 882 |
try { |
| 883 |
new BeastMain(inputFile, consoleApp, maxErrorCount, verbose, warnings, strictXML, additionalParsers, |
| 884 |
usingMC3, chainTemperatures, swapChainsEvery); |
| 885 |
} catch (RuntimeException rte) { |
| 886 |
|
| 887 |
|
| 888 |
if (window) { |
| 889 |
System.out.println(); |
| 890 |
System.out.println("BEAST has terminated with an error. Please select QUIT from the menu."); |
| 891 |
|
| 892 |
} else { |
| 893 |
System.out.flush(); |
| 894 |
System.err.flush(); |
| 895 |
System.exit(1); |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
if (useMPI) { |
| 900 |
BeastMPI.Finalize(); |
| 901 |
} |
| 902 |
|
| 903 |
if (!window) { |
| 904 |
System.exit(0); |
| 905 |
} |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
|