Search in sources :

Example 26 with BEASTInterface

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");
            }
        }
    }
}
Also used : NameValuePair(beast.util.XMLParser.NameValuePair) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) BEASTInterface(beast.core.BEASTInterface)

Example 27 with BEASTInterface

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();
}
Also used : PrintStream(java.io.PrintStream) BEASTInterface(beast.core.BEASTInterface) File(java.io.File)

Example 28 with BEASTInterface

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;
    }
}
Also used : ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) BEASTVersion2(beast.app.BEASTVersion2) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 29 with BEASTInterface

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);
        }
    }
}
Also used : BEASTInterface(beast.core.BEASTInterface)

Example 30 with BEASTInterface

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;
    }
}
Also used : BEASTInterface(beast.core.BEASTInterface) BEASTVersion2(beast.app.BEASTVersion2) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

BEASTInterface (beast.core.BEASTInterface)111 ArrayList (java.util.ArrayList)43 List (java.util.List)27 IOException (java.io.IOException)22 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)18 SAXException (org.xml.sax.SAXException)18 NodeList (org.w3c.dom.NodeList)13 Input (beast.core.Input)12 MRCAPrior (beast.math.distributions.MRCAPrior)12 File (java.io.File)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 XMLParser (beast.util.XMLParser)11 TransformerException (javax.xml.transform.TransformerException)11 Alignment (beast.evolution.alignment.Alignment)10 XMLParserException (beast.util.XMLParserException)10 BEASTObject (beast.core.BEASTObject)9 Distribution (beast.core.Distribution)9 XMLProducer (beast.util.XMLProducer)9 CompoundDistribution (beast.core.util.CompoundDistribution)8 BranchRateModel (beast.evolution.branchratemodel.BranchRateModel)8