use of beast.util.JSONParserException in project beast2 by CompEvol.
the class BeastMCMC method parseArgs.
/**
* parse command line arguments, and load file if specified
* @throws IOException
* @throws JSONException
* @throws JSONParserException
*/
public void parseArgs(String[] args) throws IOException, XMLParserException, JSONException {
int i = 0;
boolean resume = false;
boolean useStrictVersions = false;
Map<String, String> parserDefinitions = new HashMap<>();
File beastFile = null;
try {
while (i < args.length) {
int old = i;
if (i < args.length) {
if (args[i].equals("")) {
i += 1;
} else if (args[i].equals("-batch")) {
Logger.FILE_MODE = Logger.LogFileMode.only_new_or_exit;
i += 1;
} else if (args[i].equals("-resume")) {
resume = true;
Logger.FILE_MODE = Logger.LogFileMode.resume;
System.setProperty("beast.resume", "true");
System.setProperty("beast.debug", "false");
i += 1;
} else if (args[i].equals("-overwrite")) {
Logger.FILE_MODE = Logger.LogFileMode.overwrite;
i += 1;
} else if (args[i].equals("-seed")) {
if (args[i + 1].equals("random")) {
m_nSeed = Randomizer.getSeed();
} else {
m_nSeed = Long.parseLong(args[i + 1]);
}
i += 2;
} else if (args[i].equals("-threads")) {
m_nThreads = Integer.parseInt(args[i + 1]);
g_exec = Executors.newFixedThreadPool(m_nThreads);
i += 2;
// use BEAST environment variable to set Beast directories as colon separated list
// } else if (args[i].equals("-beastlib")) {
// ClassDiscovery.setJarPath(args[i + 1]);
// i += 2;
} else if (args[i].equals("-prefix")) {
System.setProperty("file.name.prefix", args[i + 1].trim());
i += 2;
} else if (args[i].equals("-D")) {
String[] strs = args[i + 1].split(",");
for (String str : strs) {
String[] strs2 = str.split("=");
if (strs2.length != 2) {
throw new IllegalArgumentException("Argument \"" + str + "\" is not well-formed: expecting name=value pairs");
}
String name = strs2[0];
String value = strs2[1];
parserDefinitions.put(name, value);
}
i += 2;
} else if (args[i].equals("-strictversions")) {
useStrictVersions = true;
i += 1;
}
if (i == old) {
if (i == args.length - 1) {
beastFile = new File(args[i]);
i++;
} else {
throw new IllegalArgumentException("Wrong argument");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException("Error parsing command line arguments: " + Arrays.toString(args) + "\nArguments ignored\n\n" + getUsage());
}
if (beastFile == null) {
// Not resuming so get starting options...
List<String> MCMCargs = new ArrayList<>();
Version version = new BEASTVersion2();
String titleString = "<html><center><p>Bayesian Evolutionary Analysis Sampling Trees<br>" + "Version " + version.getVersionString() + ", " + version.getDateString() + "</p></center></html>";
javax.swing.Icon icon = IconUtils.getIcon(BeastMain.class, "images/beast.png");
String nameString = "BEAST " + version.getVersionString();
BeastDialog dialog = new BeastDialog(new JFrame(), titleString, icon);
if (!dialog.showDialog(nameString, m_nSeed)) {
return;
}
switch(dialog.getLogginMode()) {
case 0:
/* do not ovewrite */
break;
case 1:
MCMCargs.add("-overwrite");
break;
case 2:
MCMCargs.add("-resume");
break;
}
MCMCargs.add("-seed");
MCMCargs.add(dialog.getSeed() + "");
if (dialog.getThreadPoolSize() > 0) {
MCMCargs.add("-threads");
MCMCargs.add(dialog.getThreadPoolSize() + "");
}
boolean useBeagle = dialog.useBeagle();
boolean beagleShowInfo = false;
long beagleFlags = 0;
if (useBeagle) {
beagleShowInfo = dialog.showBeagleInfo();
if (dialog.preferBeagleCPU()) {
beagleFlags |= BeagleFlag.PROCESSOR_CPU.getMask();
}
if (dialog.preferBeagleSSE()) {
beagleFlags |= BeagleFlag.VECTOR_SSE.getMask();
}
if (dialog.preferBeagleGPU()) {
beagleFlags |= BeagleFlag.PROCESSOR_GPU.getMask();
}
if (dialog.preferBeagleDouble()) {
beagleFlags |= BeagleFlag.PRECISION_DOUBLE.getMask();
}
if (dialog.preferBeagleSingle()) {
beagleFlags |= BeagleFlag.PRECISION_SINGLE.getMask();
}
}
if (beagleFlags != 0) {
System.setProperty("beagle.preferred.flags", Long.toString(beagleFlags));
}
if (!useBeagle) {
System.setProperty("java.only", "true");
}
File inputFile = dialog.getInputFile();
if (!beagleShowInfo && inputFile == null) {
System.err.println("No input file specified");
System.exit(1);
}
MCMCargs.add(inputFile.getAbsolutePath());
// BeastStartDialog dlg = new BeastStartDialog();
// if (dlg.m_bOK) {
// parseArgs(dlg.getArgs());
// }
parseArgs(MCMCargs.toArray(new String[0]));
return;
}
Log.warning.println("File: " + beastFile.getName() + " seed: " + m_nSeed + " threads: " + m_nThreads);
if (resume) {
Log.info.println("Resuming from file");
}
if (useStrictVersions) {
// grab "required" attribute from beast spec
if (beastFile.getPath().toLowerCase().endsWith(".json")) {
throw new IllegalArgumentException("The -strictversions flag is not implemented for JSON files yet (only XML files are supported).");
} else {
BufferedReader fin = new BufferedReader(new FileReader(beastFile));
StringBuffer buf = new StringBuffer();
String str = null;
int lineCount = 0;
while (fin.ready() && lineCount < 100) {
str = fin.readLine();
buf.append(str);
buf.append(' ');
}
fin.close();
str = buf.toString();
int start = str.indexOf("required=");
if (start < 0) {
throw new IllegalArgumentException("Could not find a 'required' attribute in the XML. Add the required attribute, or run without the -strictversions flag");
}
char c = str.charAt(start + 9);
start += 10;
int end = str.indexOf(c, start);
String packages = str.substring(start, end);
PackageManager.loadExternalJars(packages);
}
} else {
PackageManager.loadExternalJars();
}
// parse xml
Randomizer.setSeed(m_nSeed);
if (beastFile.getPath().toLowerCase().endsWith(".json")) {
m_runnable = new JSONParser(parserDefinitions).parseFile(beastFile);
} else {
try {
m_runnable = new XMLParser(parserDefinitions).parseFile(beastFile);
} catch (SAXException | ParserConfigurationException e) {
throw new IllegalArgumentException(e);
}
}
m_runnable.setStateFile(beastFile.getName() + ".state", resume);
}
Aggregations