use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class JSONParser method processInput.
// setInputs
private void processInput(String name, JSONObject node, List<NameValuePair> map, List<InputType> inputs) throws JSONParserException, JSONException {
if (node.has(name)) {
if (!(name.equals("id") || name.equals("idref") || name.equals("spec") || name.equals("name"))) {
Object o = node.get(name);
if (o instanceof String) {
String value = (String) o;
if (value.startsWith("@")) {
String IDRef = value.substring(1);
JSONObject element = new JSONObject();
element.put("idref", IDRef);
BEASTInterface beastObject = createObject(element, BEAST_OBJECT_CLASS);
map.add(new NameValuePair(name, beastObject));
// setInput(node, parent, name, beastObject);
} else {
map.add(new NameValuePair(name, value));
// setInput(node, parent, name, value);
}
} else if (o instanceof Number) {
map.add(new NameValuePair(name, o));
// parent.setInputValue(name, o);
} else if (o instanceof Boolean) {
map.add(new NameValuePair(name, o));
// parent.setInputValue(name, o);
} else if (o instanceof JSONObject) {
JSONObject child = (JSONObject) o;
String className = getClassName(child, name, inputs);
BEASTInterface childItem = createObject(child, className);
if (childItem != null) {
map.add(new NameValuePair(name, childItem));
// setInput(node, parent, name, childItem);
}
// childElements++;
} else if (o instanceof JSONArray) {
JSONArray list = (JSONArray) o;
for (int i = 0; i < list.length(); i++) {
Object o2 = list.get(i);
if (o2 instanceof JSONObject) {
JSONObject child = (JSONObject) o2;
String className = getClassName(child, name, inputs);
BEASTInterface childItem = createObject(child, className);
if (childItem != null) {
map.add(new NameValuePair(name, childItem));
// setInput(node, parent, name, childItem);
}
} else {
map.add(new NameValuePair(name, o2));
// parent.setInputValue(name, o2);
}
}
} else {
throw new RuntimeException("Developer error: Don't know how to handle this JSON construction");
}
}
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class JSONProducer method main.
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, XMLParserException {
// convert BEAST 2 XML to BEAST JSON file
XMLParser parser = new XMLParser();
BEASTInterface beastObject = parser.parseFile(new File(args[0]));
String JSONFile = args[0].replace(".xml", ".json");
PrintStream out;
if (JSONFile.endsWith(".json")) {
out = new PrintStream(new File(JSONFile));
} else {
out = System.out;
}
JSONProducer writer = new JSONProducer();
String JSON = writer.toJSON(beastObject);
out.println(JSON);
out.close();
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class JSONProducer method toJSON.
public String toJSON(BEASTInterface beastObject, Collection<BEASTInterface> others) {
try {
StringBuffer buf = new StringBuffer();
// buf.append("{\"version\": \"2.0\",\n\"namespace\": \"" + DEFAULT_NAMESPACE + "\",\n\n" +
// "\"" + JSONParser.ANALYSIS_ELEMENT + "\": [\n");
buf.append("{version: \"" + (new BEASTVersion2()).getMajorVersion() + "\",\nnamespace: \"" + DEFAULT_NAMESPACE + "\",\n\n" + XMLParser.BEAST_ELEMENT + ": [\n");
// buf.append("\n\n");
isDone = new HashSet<>();
isInputsDone = new HashMap<>();
IDs = new HashSet<>();
indentCount = 1;
List<BEASTInterface> priorityBeastObjects = new ArrayList<>();
findPriorityBeastObjects(beastObject, priorityBeastObjects);
for (BEASTInterface beastObject2 : priorityBeastObjects) {
if (!isDone.contains(beastObject2)) {
// name = name.substring(name.lastIndexOf('.') + 1).toLowerCase();
beastObjectToJSON(beastObject2, BEASTInterface.class, buf, null, true);
buf.append(",");
}
}
buf.append("\n\n");
beastObjectToJSON(beastObject, BEASTInterface.class, buf, null, true);
String end = "\n]\n}";
buf.append(end);
String JSON = buf.toString();
String[] nameSpaces = DEFAULT_NAMESPACE.split(":");
for (String nameSpace : nameSpaces) {
// JSON = JSON.replaceAll("\"spec\": \"" + nameSpace + ".", "\"spec\": \"");
JSON = JSON.replaceAll("spec: \"" + nameSpace + ".", "spec: \"");
}
return JSON;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class NexusParser method processSets.
protected void processSets() {
// does not already have a calibration associated with it
for (TaxonSet taxonset : taxonsets) {
boolean found = false;
for (BEASTInterface o : taxonset.getOutputs()) {
if (o instanceof MRCAPrior) {
found = true;
break;
}
}
if (!found) {
MRCAPrior prior = new MRCAPrior();
prior.isMonophyleticInput.setValue(true, prior);
prior.taxonsetInput.setValue(taxonset, prior);
prior.setID(taxonset.getID() + ".prior");
// should set Tree before initialising, but we do not know the tree yet...
if (calibrations == null) {
calibrations = new ArrayList<>();
}
calibrations.add(prior);
}
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class XMLProducer method toXML.
public String toXML(BEASTInterface beastObject, Collection<BEASTInterface> others) {
try {
StringBuffer buf = new StringBuffer();
Set<String> requiredPacakges = getPackagesAndVersions(beastObject);
String required = requiredPacakges.toString();
required = required.substring(1, required.length() - 1);
required = required.replaceAll(", ", ":");
buf.append("<" + XMLParser.BEAST_ELEMENT + " version='" + new BEASTVersion2().getMajorVersion() + "'" + " required='" + required + "'" + " namespace='" + DEFAULT_NAMESPACE + "'>\n");
for (String element : element2ClassMap.keySet()) {
if (!reservedElements.contains(element)) {
buf.append("<map name='" + element + "'>" + element2ClassMap.get(element) + "</map>\n");
}
}
buf.append("\n\n");
isDone = new HashSet<>();
isInputsDone = new HashMap<>();
IDs = new HashSet<>();
indent = 0;
beastObjectToXML(beastObject, buf, null, true);
String endBeastString = "</" + XMLParser.BEAST_ELEMENT + ">";
buf.append(endBeastString);
// return buf.toString();
// beautify XML hierarchy
String xml = cleanUpXML(buf.toString(), m_sXMLBeuatifyXSL);
// TODO: fix m_sIDRefReplacementXSL script to deal with nested taxon sets
// String xml2 = cleanUpXML(xml, m_sIDRefReplacementXSL);
String xml2 = xml;
xml = findPlates(xml2);
// beatify by applying name spaces to spec attributes
String[] nameSpaces = DEFAULT_NAMESPACE.split(":");
for (String nameSpace : nameSpaces) {
xml = xml.replaceAll("spec=\"" + nameSpace + ".", "spec=\"");
}
buf = new StringBuffer();
if (others.size() > 0) {
for (BEASTInterface beastObject2 : others) {
if (!IDs.contains(beastObject2.getID())) {
beastObjectToXML(beastObject2, buf, null, false);
}
}
}
int endIndex = xml.indexOf(endBeastString);
String extras = buf.toString();
// prevent double -- inside XML comment, this can happen in sequences
extras = extras.replaceAll("--", "- - ");
xml = // + "\n\n<!-- " + DO_NOT_EDIT_WARNING + " \n\n" +
xml.substring(0, endIndex) + // extras + "\n\n-->\n\n"
endBeastString;
xml = xml.replaceAll("xmlns=\"http://www.w3.org/TR/xhtml1/strict\"", "");
xml = dedupName(xml);
xml = sortTags(xml);
// return buf2.toString();
return xml;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Aggregations