use of beast.util.JSONParser in project beast2 by CompEvol.
the class ExampleJSONParsingTest method test_ThatJSONExamplesRun.
public void test_ThatJSONExamplesRun(String dir) {
try {
Logger.FILE_MODE = Logger.LogFileMode.overwrite;
System.out.println("Test that JSON Examples run in " + dir);
File exampleDir = new File(dir);
String[] exampleFiles = exampleDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".json");
}
});
List<String> failedFiles = new ArrayList<String>();
int seed = 127;
for (String fileName : exampleFiles) {
Randomizer.setSeed(seed);
// need more than one to prevent trouble with multiMCMC logs
seed += 10;
System.out.println("Processing " + fileName);
JSONParser parser = new JSONParser();
try {
beast.core.Runnable runable = parser.parseFile(new File(dir + "/" + fileName));
if (runable instanceof MCMC) {
MCMC mcmc = (MCMC) runable;
mcmc.setInputValue("preBurnin", 0);
mcmc.setInputValue("chainLength", 1000l);
mcmc.run();
}
} catch (Exception e) {
System.out.println("ExampleJSONParsing::Failed for " + fileName + ": " + e.getMessage());
failedFiles.add(fileName);
}
System.out.println("Done " + fileName);
}
if (failedFiles.size() > 0) {
System.out.println("\ntest_ThatJSONExamplesRun::Failed for : " + failedFiles.toString());
} else {
System.out.println("SUCCESS!!!");
}
assertTrue(failedFiles.toString(), failedFiles.size() == 0);
} catch (Exception e) {
System.out.println("exception thrown ");
System.out.println(e.getMessage());
;
}
}
use of beast.util.JSONParser 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);
}
use of beast.util.JSONParser in project beast2 by CompEvol.
the class JSONTest method testJSONFragmentParsing.
@Test
public void testJSONFragmentParsing() throws Exception {
JSONParser parser = new JSONParser();
String json = "{version: \"2.5\",\n" + "\n" + "beast: [\n" + "{spec:\"beast.core.parameter.RealParameter\",\n" + " value:\"2.345\"\n" + "}\n" + "]\n" + "}\n";
;
List<Object> objects = parser.parseFragment(json, true);
assertEquals(1, objects.size());
RealParameter p = (RealParameter) objects.get(0);
assertEquals(2.345, p.getValue(), 1e-13);
}
use of beast.util.JSONParser in project beast2 by CompEvol.
the class JSONTest method testJSONtoXMLtoJSON.
@Test
public void testJSONtoXMLtoJSON() throws Exception {
JSONParser parser = new JSONParser();
BEASTObject beastObject = parser.parseFile(new File(JSON_FILE));
JSONProducer producer = new JSONProducer();
// .replaceAll("\\s+", " ");
String actual = producer.toJSON(beastObject).trim();
// .replaceAll("\\s+", " ");
String expected = BeautiDoc.load(JSON_FILE).trim();
assertEquals("Produced JSON differs from original", expected, actual);
}
use of beast.util.JSONParser in project beast2 by CompEvol.
the class JSONTest method testAnnotatedConstructor.
@Test
public void testAnnotatedConstructor() throws Exception {
List<Taxon> taxa = new ArrayList<>();
taxa.add(new Taxon("first one"));
taxa.add(new Taxon("second one"));
AnnotatedRunnableTestClass t = new AnnotatedRunnableTestClass(3, taxa);
JSONProducer producer = new JSONProducer();
String json = producer.toJSON(t);
assertEquals(3, (int) t.getParam1());
FileWriter outfile = new FileWriter(new File("/tmp/JSONTest.json"));
outfile.write(json);
outfile.close();
JSONParser parser = new JSONParser();
BEASTInterface b = parser.parseFile(new File("/tmp/JSONTest.json"));
assertEquals(3, (int) ((AnnotatedRunnableTestClass) b).getParam1());
assertEquals(2, ((AnnotatedRunnableTestClass) b).getTaxon().size());
// test that default value for param1 comes through
String json2 = "{version: \"2.5\",\n" + "namespace: \"beast.core:beast.evolution.alignment:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihood\",\n" + "\n" + "beast: [\n" + "\n" + "\n" + " {id: \"JSONTest\",\n" + " spec: \"test.beast.util.AnnotatedRunnableTestClass\",\n" + " taxon: [\n" + " {id: \"first one\" },\n" + " {id: \"second one\" }\n" + " ]\n" + " }\n" + "]\n" + "}";
outfile = new FileWriter(new File("/tmp/JSONTest2.json"));
outfile.write(json2);
outfile.close();
parser = new JSONParser();
b = parser.parseFile(new File("/tmp/JSONTest2.json"));
assertEquals(10, (int) ((AnnotatedRunnableTestClass) b).getParam1());
assertEquals(2, ((AnnotatedRunnableTestClass) b).getTaxon().size());
// test that array of doubles comes through in second constructor
String json3 = "{version: \"2.5\",\n" + "namespace: \"beast.core:beast.evolution.alignment:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihood\",\n" + "\n" + "beast: [\n" + "\n" + "\n" + " {id: \"JSONTest\",\n" + " spec: \"test.beast.util.AnnotatedRunnableTestClass\",\n" + " array: [1.0, 2.0, 3.0]\n" + " }\n" + "]\n" + "}";
outfile = new FileWriter(new File("/tmp/JSONTest3.json"));
outfile.write(json3);
outfile.close();
parser = new JSONParser();
b = parser.parseFile(new File("/tmp/JSONTest3.json"));
assertEquals(3, ((AnnotatedRunnableTestClass) b).getArray().size());
}
Aggregations