Search in sources :

Example 11 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class BeautiDoc method connect.

public void connect(BEASTInterface srcBEASTObject, String targetID, String inputName) {
    try {
        BEASTInterface target = pluginmap.get(targetID);
        if (target == null) {
            Log.trace.println("BeautiDoc: Could not find object " + targetID);
            return;
        }
        // prevent duplication inserts in list
        Object o = target.getInputValue(inputName);
        if (o instanceof List) {
            // System.err.println("   " + ((List)o).size());
            if (((List<?>) o).contains(srcBEASTObject)) {
                warning("   " + targetID + "/" + inputName + " already contains " + (srcBEASTObject == null ? "nulls" : srcBEASTObject.getID()) + "\n");
                return;
            }
        }
        target.setInputValue(inputName, srcBEASTObject);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BEASTInterface(beast.core.BEASTInterface) BEASTObject(beast.core.BEASTObject) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 12 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class BeautiDoc method disconnect.

/**
 * disconnect source beastObject with target beastObject *
 */
public void disconnect(BeautiConnector connector, PartitionContext context) {
    if (!connector.isRegularConnector) {
        return;
    }
    BEASTInterface srcBEASTObject = pluginmap.get(translatePartitionNames(connector.sourceID, context));
    String targetID = translatePartitionNames(connector.targetID, context);
    disconnect(srcBEASTObject, targetID, connector.targetInput);
}
Also used : BEASTInterface(beast.core.BEASTInterface)

Example 13 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class BeautiDoc method determineLinks.

// methods for dealing with linking
void determineLinks() {
    if (!allowLinking) {
        return;
    }
    linked.clear();
    for (BEASTInterface beastObject : posteriorPredecessors) {
        Map<String, Integer> outputIDs = new HashMap<>();
        for (Object output : beastObject.getOutputs()) {
            if (posteriorPredecessors.contains(output)) {
                String id = ((BEASTInterface) output).getID();
                if (id.indexOf('.') >= 0) {
                    id = id.substring(0, id.indexOf('.'));
                    if (outputIDs.containsKey(id)) {
                        outputIDs.put(id, outputIDs.get(id) + 1);
                    } else {
                        outputIDs.put(id, 1);
                    }
                }
            }
        }
        for (Object output : beastObject.getOutputs()) {
            if (posteriorPredecessors.contains(output)) {
                String id = ((BEASTInterface) output).getID();
                if (id.indexOf('.') >= 0) {
                    id = id.substring(0, id.indexOf('.'));
                    if (outputIDs.get(id) > 1) {
                        addLink(beastObject, (BEASTInterface) output);
                    }
                }
            }
        }
        // add parameters that have more than 1 outputs into susbtitution models
        if (beastObject instanceof Parameter<?>) {
            for (Object output : beastObject.getOutputs()) {
                if (posteriorPredecessors.contains(output)) {
                    if (output instanceof SubstitutionModel) {
                        int nrOfSubstModelsInOutput = 0;
                        try {
                            for (Input<?> input : ((BEASTInterface) output).listInputs()) {
                                if (input.get() != null && input.get().equals(beastObject)) {
                                    nrOfSubstModelsInOutput++;
                                }
                            }
                        } catch (Exception e) {
                        // ignore
                        }
                        if (nrOfSubstModelsInOutput > 1) {
                            addLink(beastObject, (BEASTInterface) output);
                        }
                    }
                }
            }
        }
    }
    hasLinkedAtLeastOnce = false;
    for (Input<?> input : linked) {
        if (input.getType().isAssignableFrom(RealParameter.class)) {
            hasLinkedAtLeastOnce = true;
            break;
        }
    }
}
Also used : HashMap(java.util.HashMap) RealParameter(beast.core.parameter.RealParameter) Parameter(beast.core.parameter.Parameter) BEASTInterface(beast.core.BEASTInterface) BEASTObject(beast.core.BEASTObject) SubstitutionModel(beast.evolution.substitutionmodel.SubstitutionModel) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 14 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class BeautiDoc method extractSequences.

void extractSequences(String xml) throws XMLParserException, SAXException, IOException, ParserConfigurationException {
    // parse the XML fragment into a DOM document
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    doc.normalize();
    // find top level beast element
    NodeList nodes = doc.getElementsByTagName("*");
    Node topNode = nodes.item(0);
    String beautiTemplate = XMLParser.getAttribute(topNode, "beautitemplate");
    if (beautiTemplate == null) {
        int choice = JOptionPane.showConfirmDialog(getFrame(), "This file does not appear to be generated by BEAUti. If you load it, unexpected behaviour may follow");
        if (choice != JOptionPane.OK_OPTION) {
            return;
        }
        // load standard template
        if (beautiConfig == null) {
            String templateXML = processTemplate(STANDARD_TEMPLATE);
            loadTemplate(templateXML);
        }
    } else {
        String templateXML = processTemplate(beautiTemplate + ".xml");
        loadTemplate(templateXML);
    }
    String beautiStatus = XMLParser.getAttribute(topNode, "beautistatus");
    if (beautiStatus == null) {
        beautiStatus = "";
    }
    autoSetClockRate = !beautiStatus.contains("noAutoSetClockRate");
    beauti.autoSetClockRate.setSelected(autoSetClockRate);
    allowLinking = beautiStatus.contains("allowLinking");
    beauti.allowLinking.setSelected(allowLinking);
    autoUpdateFixMeanSubstRate = !beautiStatus.contains("noAutoUpdateFixMeanSubstRate");
    beauti.autoUpdateFixMeanSubstRate.setSelected(autoUpdateFixMeanSubstRate);
    // parse file
    XMLParser parser = new XMLParser();
    BEASTInterface MCMC = parser.parseFragment(xml, true);
    mcmc.setValue(MCMC, this);
    BEASTObjectPanel.addPluginToMap(MCMC, this);
    // reconstruct all objects from templates
    try {
        CompoundDistribution posterior = (CompoundDistribution) ((beast.core.MCMC) mcmc.get()).posteriorInput.get();
        for (Distribution distr : posterior.pDistributions.get()) {
            if (distr.getID().equals("likelihood")) {
                for (Distribution likelihood : ((CompoundDistribution) distr).pDistributions.get()) {
                    if (likelihood instanceof GenericTreeLikelihood) {
                        GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) likelihood;
                        PartitionContext context = new PartitionContext(treeLikelihood);
                        try {
                            beautiConfig.partitionTemplate.get().createSubNet(context, false);
                        } catch (Exception e) {
                        // e.printStackTrace();
                        }
                        for (BeautiSubTemplate subTemplate : beautiConfig.subTemplates) {
                            try {
                                subTemplate.createSubNet(context, false);
                            } catch (Exception e) {
                            // e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    // MCMC = parser.parseFragment(xml, true);
    // mcmc.setValue(MCMC, this);
    // PluginPanel.addPluginToMap(MCMC, this);
    // if (xml.indexOf(XMLProducer.DO_NOT_EDIT_WARNING) > 0) {
    // int start = xml.indexOf(XMLProducer.DO_NOT_EDIT_WARNING);
    // int end = xml.lastIndexOf("-->");
    // xml = xml.substring(start, end);
    // xml = xml.replaceAll(XMLProducer.DO_NOT_EDIT_WARNING, "");
    // xml = "<beast namespace='" + XMLProducer.DEFAULT_NAMESPACE + "'>" + xml + "</beast>";
    // List<BEASTObject> beastObjects = parser.parseBareFragments(xml, true);
    // for (BEASTObject beastObject : beastObjects) {
    // PluginPanel.addPluginToMap(beastObject, this);
    // }
    // }
    // extract alignments
    determinePartitions();
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) StateNode(beast.core.StateNode) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) MCMC(beast.core.MCMC) Document(org.w3c.dom.Document) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) CompoundDistribution(beast.core.util.CompoundDistribution) CompoundDistribution(beast.core.util.CompoundDistribution) ParametricDistribution(beast.math.distributions.ParametricDistribution) Distribution(beast.core.Distribution) StringReader(java.io.StringReader) BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser)

Example 15 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class BeautiDoc method applyBeautiRules.

void applyBeautiRules(List<BeautiSubTemplate> templates, boolean isInitial, PartitionContext context) {
    for (BeautiSubTemplate template : templates) {
        String templateID = translatePartitionNames(template.getMainID(), context);
        BEASTInterface beastObject = pluginmap.get(templateID);
        // check if template is in use
        if (beastObject != null) {
            // if so, run through all connectors
            for (BeautiConnector connector : template.connectors) {
                if (connector.atInitialisationOnly()) {
                    if (isInitial) {
                        warning("connect: " + connector.toString(context) + "\n");
                        connect(connector, context);
                    }
                } else if (connector.isActivated(context, posteriorPredecessors, likelihoodPredecessors, this)) {
                    warning("connect: " + connector.toString(context) + "\n");
                    try {
                        connect(connector, context);
                    } catch (Exception e) {
                        warning(e.getMessage());
                    }
                } else {
                    warning("DISconnect: " + connector.toString(context) + "\n");
                    try {
                        disconnect(connector, context);
                    } catch (Exception e) {
                        warning(e.getMessage() + "\n");
                    }
                }
            }
        }
    }
}
Also used : BEASTInterface(beast.core.BEASTInterface) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

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