Skip to content

Commit

Permalink
add -DFout option to BeastMain #894
Browse files Browse the repository at this point in the history
  • Loading branch information
rbouckaert committed Apr 23, 2020
1 parent ebe48dd commit 13945fd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
16 changes: 15 additions & 1 deletion src/beast/app/BeastMCMC.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public void parseArgs(String[] args) throws IOException, XMLParserException, JSO
boolean resume = false;
boolean useStrictVersions = false;
boolean sampleFromPrior = false;
boolean hasDF = false;
String outFile = null;
Map<String, String> parserDefinitions = new HashMap<>();

File beastFile = null;
Expand Down Expand Up @@ -166,6 +168,10 @@ public void parseArgs(String[] args) throws IOException, XMLParserException, JSO
Log.warning("Found definition of " + key + " " + jsonDictionary.getString(key).length());
parserDefinitions.put(key, jsonDictionary.getString(key));
}
hasDF = true;
i += 2;
} else if (args[i].equals("-DFout")) {
outFile = args[i + 1];
i += 2;
} else if (args[i].equals("-strictversions")) {
useStrictVersions = true;
Expand All @@ -189,6 +195,14 @@ public void parseArgs(String[] args) throws IOException, XMLParserException, JSO
throw new IllegalArgumentException("Error parsing command line arguments: " + Arrays.toString(args) + "\nArguments ignored\n\n" + getUsage());
}

if (hasDF && outFile == null && beastFile != null) {
outFile = beastFile.getAbsolutePath();
if (outFile.toLowerCase().endsWith(".xml")) {
outFile = outFile.substring(0, outFile.length() - 4);
}
outFile = outFile + ".out.xml";
}

if (beastFile == null) {
// Not resuming so get starting options...

Expand Down Expand Up @@ -308,7 +322,7 @@ public void parseArgs(String[] args) throws IOException, XMLParserException, JSO
m_runnable = new JSONParser(parserDefinitions).parseFile(beastFile, sampleFromPrior);
} else {
try {
m_runnable = new XMLParser(parserDefinitions).parseFile(beastFile, sampleFromPrior);
m_runnable = new XMLParser(parserDefinitions, outFile).parseFile(beastFile, sampleFromPrior);
} catch (SAXException | ParserConfigurationException e) {
throw new IllegalArgumentException(e);
}
Expand Down
7 changes: 6 additions & 1 deletion src/beast/app/beastapp/BeastMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ public static void main(final String[] args) throws java.io.IOException {
new Arguments.Option("version", "Print version and stop"),
new Arguments.Option("strictversions", "Use only package versions as specified in the 'required' attribute"),
new Arguments.StringOption("D", "DEFINITIONS", "attribute-value pairs to be replaced in the XML, e.g., -D \"arg1=10,arg2=20\"").allowMultipleUse(),
new Arguments.StringOption("DF", "DEFINITIONFILE", "as -D, but attribute-value pairs defined in file in a JSON file").allowMultipleUse(),
new Arguments.StringOption("DF", "DEFINITIONFILE", "as -D, but attribute-value pairs defined in file in JSON format").allowMultipleUse(),
new Arguments.StringOption("DFout", "DEFINITIONRESULTFILE", "BEAST XML file written when -DF option is used"),
new Arguments.Option("sampleFromPrior", "samples from prior for MCMC analysis (by adding sampleFromPrior=\"true\" in the first run element)"),
});

Expand Down Expand Up @@ -547,6 +548,10 @@ public void write(int b) {
MCMCargs.add(optionVal);
}
}
if (arguments.hasOption("DFout")) {
MCMCargs.add("-DFout");
MCMCargs.add(arguments.getStringOption("DFout"));
}

if (beagleShowInfo) {
Log.info.println("\n--- BEAGLE RESOURCES ---\n");
Expand Down
30 changes: 22 additions & 8 deletions src/beast/util/XMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ public HashMap<String, String> getElement2ClassMap() {
*/
RequiredInputProvider requiredInputProvider = null;
PartitionContext partitionContext = null;

/**
* if outFile is not null, an XML file merged with parser definitions processed
* will be saved here
*/
String outFile;


public XMLParser() {
this.parserDefinitions = new HashMap<>();
Expand All @@ -273,6 +280,10 @@ public XMLParser(java.util.Map<String,String> parserDefinitions) {
this.parserDefinitions = parserDefinitions;
}

public XMLParser(java.util.Map<String,String> parserDefinitions, String outFile) {
this.parserDefinitions = parserDefinitions;
this.outFile = outFile;
}


public Runnable parseFile(final File file) throws SAXException, IOException, ParserConfigurationException, XMLParserException {
Expand All @@ -288,14 +299,17 @@ public Runnable parseFile(final File file, boolean sampleFromPrior) throws SAXEx
xml = xml.replaceAll("\\$\\(" + key + "\\)", parserDefinitions.get(key));
}

// try {
// FileWriter outfile = new FileWriter("/tmp/beast.xml");
// outfile.write(xml);
// outfile.close();
// System.exit(0);
// } catch (IOException e) {
// // ignore
// }
try {
if (parserDefinitions != null && parserDefinitions.size() > 1) {
Log.warning("Outputting merged file to " + outFile);
FileWriter outfile = new FileWriter(outFile);
outfile.write(xml);
outfile.close();
}
} catch (IOException e) {
// ignore
Log.warning("Something went wroting outputting merged file: " + e.getMessage());
}


doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
Expand Down
10 changes: 8 additions & 2 deletions src/test/beast/integration/ExampleXmlParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,23 @@ public void checkExit(int status)
try {
BeastMain.main(new String[]{
"-D", "chainLength=1000",
"-DF", dir + "/RSV2.json",
"-DF", dir + "/RSV2.json",
"-DFout", "/tmp/RSV2.out.xml",
dir + "/RSV2.xml"});
} catch (ExitException e) {
if (e.status != 0) {
e.printStackTrace();
throw new RuntimeException("Exitted with status = " + e.status);
}
}

// reinstate System.exit() behaviour
System.setSecurityManager(sm) ;

if (!new File("/tmp/RSV2.out.xml").exists()) {
throw new RuntimeException("Could not find file /tmp/RSV2.out.xml");
}

} // test_ThatParameterisedXmlExamplesRuns


Expand Down

0 comments on commit 13945fd

Please sign in to comment.