Search in sources :

Example 61 with BEASTInterface

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

the class BeautiAlignmentProvider method getXMLData.

public static BEASTInterface getXMLData(File file) {
    String xml = "";
    try {
        // parse as BEAST 2 xml fragment
        XMLParser parser = new XMLParser();
        BufferedReader fin = new BufferedReader(new FileReader(file));
        while (fin.ready()) {
            xml += fin.readLine() + "\n";
        }
        fin.close();
        BEASTInterface runnable = parser.parseBareFragment(xml, false);
        BEASTInterface alignment = getAlignment(runnable);
        alignment.initAndValidate();
        return alignment;
    } catch (Exception ex) {
        // attempt to parse as BEAST 1 xml
        try {
            String ID = file.getName();
            ID = ID.substring(0, ID.lastIndexOf('.')).replaceAll("\\..*", "");
            BEASTInterface alignment = parseBeast1XML(ID, xml);
            if (alignment != null) {
                alignment.setID(file.getName().substring(0, file.getName().length() - 4).replaceAll("\\..*", ""));
            }
            return alignment;
        } catch (Exception ex2) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Loading of " + file.getName() + " failed: " + ex.getMessage() + "\n" + ex2.getMessage());
        }
        return null;
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 62 with BEASTInterface

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

the class BeautiAlignmentProvider method replaceItem.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void replaceItem(BeautiDoc doc, BEASTInterface oldData, BEASTInterface newData) {
    doc.pluginmap.remove(newData.getID());
    Set<BEASTInterface> outputs = new LinkedHashSet<>();
    outputs.addAll(oldData.getOutputs());
    for (BEASTInterface o : outputs) {
        for (Input i : o.listInputs()) {
            if (i.get() == oldData) {
                i.setValue(newData, o);
            } else if (i.get() != null && i.get() instanceof List) {
                List list = (List) i.get();
                int index = list.indexOf(oldData);
                if (index >= 0) {
                    list.set(index, newData);
                    newData.getOutputs().add(o);
                }
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Input(beast.core.Input) BEASTInterface(beast.core.BEASTInterface) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Example 63 with BEASTInterface

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

the class AlignmentListInputEditor method getPartition.

private String getPartition(Input<?> input) {
    BEASTInterface beastObject = (BEASTInterface) input.get();
    String id = beastObject.getID();
    String partition = BeautiDoc.parsePartition(id);
    return partition;
}
Also used : BEASTInterface(beast.core.BEASTInterface)

Example 64 with BEASTInterface

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

the class BeautiConnector method isActivated.

/**
 * check that conditions in the 'if' input are met *
 */
public boolean isActivated(PartitionContext partitionContext, List<BEASTInterface> posteriorPredecessors, List<BEASTInterface> likelihoodPredecessors, BeautiDoc doc) {
    if (atInitialisationOnly()) {
        return false;
    }
    if (methodnput.get() != null) {
        // if (method != null) {
        try {
            String fullMethod = methodnput.get();
            String className = fullMethod.substring(0, fullMethod.lastIndexOf('.'));
            String methodName = fullMethod.substring(fullMethod.lastIndexOf('.') + 1);
            Class<?> class_ = Class.forName(className);
            method = class_.getMethod(methodName, BeautiDoc.class);
            method.invoke(null, doc);
        } catch (Exception e) {
        // ignore
        }
    }
    boolean isActive = true;
    for (int i = 0; i < conditionIDs.length; i++) {
        // String id = conditionIDs[i].replaceAll("\\$\\(n\\)", partition);
        String id = BeautiDoc.translatePartitionNames(conditionIDs[i], partitionContext);
        BEASTInterface beastObject = doc.pluginmap.get(id);
        if (beastObject == null) {
            if (conditionOperations[i] != Operation.IS_NOT_AN_OPERTOR) {
                return false;
            }
        // System.err.println("isActivated::no beastObject found");
        }
        // System.err.println("isActivated::found " + id);
        try {
            switch(conditionOperations[i]) {
                case IS_IN_POSTERIOR:
                    if (!posteriorPredecessors.contains(beastObject)) {
                        // System.err.println("isActivated::is not in posterior, return false");
                        return false;
                    }
                    break;
                case IS_IN_LIKELIHOOD:
                    if (!likelihoodPredecessors.contains(beastObject)) {
                        // System.err.println("isActivated::is not in posterior, return false");
                        return false;
                    }
                    break;
                // System.err.println("isActivated::is in posterior");
                case IS_NOT_AN_OPERTOR:
                    List<Operator> operators = ((MCMC) doc.mcmc.get()).operatorsInput.get();
                    if (operators.contains(beastObject)) {
                        return false;
                    }
                    break;
                case EQUALS:
                    final Input<?> input = beastObject.getInput(conditionInputs[i]);
                    // System.err.println("isActivated::input " + input.get().toString() + " expected " + conditionValues[i]);
                    if (input.get() == null) {
                        if (!conditionValues[i].equals("null")) {
                            return false;
                        }
                    } else if (!input.get().toString().equals(conditionValues[i])) {
                        // System.err.println("isActivated::return false");
                        return false;
                    }
                    break;
                case NOT_EQUALS:
                    final Input<?> input2 = beastObject.getInput(conditionInputs[i]);
                    // System.err.println("isActivated::input " + input.get().toString() + " expected " + conditionValues[i]);
                    if (input2.get() == null) {
                        if (conditionValues[i].equals("null")) {
                            return false;
                        }
                    } else if (input2.get().toString().equals(conditionValues[i])) {
                        // System.err.println("isActivated::return false");
                        return false;
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unexpected operation: " + conditionOperations[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    // }
    return isActive;
}
Also used : Operator(beast.core.Operator) BEASTInterface(beast.core.BEASTInterface)

Example 65 with BEASTInterface

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

the class Beauti method addAlignmentProviderMenus.

private void addAlignmentProviderMenus(JMenu fileMenu) {
    List<BeautiAlignmentProvider> providers = doc.beautiConfig.alignmentProvider;
    for (BeautiAlignmentProvider provider : providers) {
        AbstractAction action = new AbstractAction() {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    setCursor(new Cursor(Cursor.WAIT_CURSOR));
                    // get user-specified alignments
                    List<BEASTInterface> beastObjects = provider.getAlignments(doc);
                    if (beastObjects != null) {
                        for (BEASTInterface o : beastObjects) {
                            if (o instanceof Alignment) {
                                try {
                                    BeautiDoc.createTaxonSet((Alignment) o, doc);
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                            }
                        }
                    }
                    doc.connectModel();
                    doc.fireDocHasChanged();
                    if (beastObjects != null) {
                        for (BEASTInterface o : beastObjects) {
                            if (o instanceof MRCAPrior) {
                                doc.addMRCAPrior((MRCAPrior) o);
                            }
                        }
                    }
                    a_save.setEnabled(true);
                    a_saveas.setEnabled(true);
                } catch (Exception exx) {
                    exx.printStackTrace();
                    String text = "Something went wrong importing the alignment:\n";
                    JTextArea textArea = new JTextArea(text);
                    textArea.setColumns(30);
                    textArea.setLineWrap(true);
                    textArea.setWrapStyleWord(true);
                    textArea.append(exx.getMessage());
                    textArea.setSize(textArea.getPreferredSize().width, 1);
                    textArea.setOpaque(false);
                    JOptionPane.showMessageDialog(null, textArea, "Error importing alignment", JOptionPane.WARNING_MESSAGE);
                }
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        };
        String providerInfo = provider.toString().replaceAll("Add ", "Add partition for ");
        action.putValue(Action.SHORT_DESCRIPTION, providerInfo);
        action.putValue(Action.LONG_DESCRIPTION, providerInfo);
        action.putValue(Action.NAME, provider.toString());
        fileMenu.add(action);
    }
}
Also used : Alignment(beast.evolution.alignment.Alignment) JTextArea(javax.swing.JTextArea) ActionEvent(java.awt.event.ActionEvent) MRCAPrior(beast.math.distributions.MRCAPrior) BEASTInterface(beast.core.BEASTInterface) Cursor(java.awt.Cursor) AbstractAction(javax.swing.AbstractAction) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

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