Search in sources :

Example 66 with BEASTInterface

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

the class BeautiConfig method selectAlignments.

/**
 * if fileArray == null, then use getAlignments(doc)
 * @param doc
 * @param parent
 * @param fileArray
 * @return a list of alignments based on the user selected alignment provider
 */
public List<BEASTInterface> selectAlignments(BeautiDoc doc, JComponent parent, File[] fileArray) {
    List<BeautiAlignmentProvider> providers = alignmentProvider;
    BeautiAlignmentProvider selectedProvider = null;
    if (providers.size() == 1) {
        selectedProvider = providers.get(0);
    } else {
        selectedProvider = (BeautiAlignmentProvider) JOptionPane.showInputDialog(parent, "Select what to add", "Add partition", JOptionPane.QUESTION_MESSAGE, null, providers.toArray(), providers.get(0));
        if (selectedProvider == null) {
            return null;
        }
    }
    List<BEASTInterface> beastObjects = selectedProvider.getAlignments(doc, fileArray);
    // create taxon sets, if any
    if (beastObjects != null) {
        for (BEASTInterface o : beastObjects) {
            if (o instanceof Alignment) {
                try {
                    BeautiDoc.createTaxonSet((Alignment) o, doc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    doc.connectModel();
    if (beastObjects != null) {
        for (BEASTInterface o : beastObjects) {
            if (o instanceof MRCAPrior) {
                doc.addMRCAPrior((MRCAPrior) o);
            }
        }
    }
    return beastObjects;
}
Also used : Alignment(beast.evolution.alignment.Alignment) MRCAPrior(beast.math.distributions.MRCAPrior) BEASTInterface(beast.core.BEASTInterface)

Example 67 with BEASTInterface

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

the class BeautiPanel method updateList.

public void updateList() {
    if (listModel == null) {
        return;
    }
    listModel.clear();
    if (listModel.size() > 0) {
        // this is a weird bit of code, since listModel.clear should ensure that size()==0, but it doesn't
        return;
    }
    String type = config.hasPartitionsInput.get().toString();
    for (BEASTInterface partition : doc.getPartitions(type)) {
        if (type.equals("SiteModel")) {
            partition = (BEASTInterface) ((GenericTreeLikelihood) partition).siteModelInput.get();
        } else if (type.equals("ClockModel")) {
            partition = ((GenericTreeLikelihood) partition).branchRateModelInput.get();
        } else if (type.equals("Tree")) {
            partition = (BEASTInterface) ((GenericTreeLikelihood) partition).treeInput.get();
        }
        String partitionID = partition.getID();
        partitionID = partitionID.substring(partitionID.lastIndexOf('.') + 1);
        if (partitionID.length() > 1 && partitionID.charAt(1) == ':') {
            partitionID = partitionID.substring(2);
        }
        listModel.addElement(partitionID);
    }
    if (partitionIndex >= 0 && listModel.size() > 0)
        listOfPartitions.setSelectedIndex(partitionIndex);
}
Also used : BEASTInterface(beast.core.BEASTInterface)

Example 68 with BEASTInterface

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

the class BeautiPanel method cloneFrom.

/**
 * Clones partition identified by sourceID to targetID and type (Site/Clock/Tree model)
 * as stored in config.
 * @param sourceID
 * @param targetID
 */
public void cloneFrom(String sourceID, String targetID) {
    if (sourceID.equals(targetID)) {
        return;
    }
    String type = config.hasPartitionsInput.get().toString();
    java.util.List<BEASTInterface> list = doc.getPartitions(type);
    int source = -1, target = -1;
    for (int i = 0; i < list.size(); i++) {
        BEASTInterface partition = list.get(i);
        if (type.equals("SiteModel")) {
            partition = (BEASTInterface) ((GenericTreeLikelihood) partition).siteModelInput.get();
        } else if (type.equals("ClockModel")) {
            partition = ((GenericTreeLikelihood) partition).branchRateModelInput.get();
        } else if (type.equals("Tree")) {
            partition = (BEASTInterface) ((GenericTreeLikelihood) partition).treeInput.get();
        }
        String partitionID = partition.getID();
        partitionID = partitionID.substring(partitionID.lastIndexOf('.') + 1);
        if (partitionID.length() > 1 && partitionID.charAt(1) == ':') {
            partitionID = partitionID.substring(2);
        }
        if (partitionID.equals(sourceID)) {
            source = i;
        }
        if (partitionID.equals(targetID)) {
            target = i;
        }
    }
    if (target == -1) {
        throw new RuntimeException("Programmer error: sourceID and targetID should be in list");
    }
    CompoundDistribution likelihoods = (CompoundDistribution) doc.pluginmap.get("likelihood");
    GenericTreeLikelihood likelihoodSource = (GenericTreeLikelihood) likelihoods.pDistributions.get().get(source);
    GenericTreeLikelihood likelihood = (GenericTreeLikelihood) likelihoods.pDistributions.get().get(target);
    PartitionContext oldContext = doc.getContextFor(likelihoodSource);
    PartitionContext newContext = doc.getContextFor(likelihood);
    // this ensures the config.sync does not set any input value
    config._input.setValue(null, config);
    if (type.equals("SiteModel")) {
        SiteModelInterface siteModelSource = likelihoodSource.siteModelInput.get();
        SiteModelInterface siteModel = null;
        try {
            siteModel = (SiteModel.Base) BeautiDoc.deepCopyPlugin((BEASTInterface) siteModelSource, likelihood, (MCMC) doc.mcmc.get(), oldContext, newContext, doc, null);
        } catch (RuntimeException e) {
            JOptionPane.showMessageDialog(this, "Could not clone " + sourceID + " to " + targetID + " " + e.getMessage());
            return;
        }
        likelihood.siteModelInput.setValue(siteModel, likelihood);
        return;
    } else if (type.equals("ClockModel")) {
        BranchRateModel clockModelSource = likelihoodSource.branchRateModelInput.get();
        BranchRateModel clockModel = null;
        try {
            clockModel = (BranchRateModel) BeautiDoc.deepCopyPlugin((BEASTInterface) clockModelSource, likelihood, (MCMC) doc.mcmc.get(), oldContext, newContext, doc, null);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Could not clone " + sourceID + " to " + targetID + " " + e.getMessage());
            return;
        }
        // make sure that *if* the clock model has a tree as input, it is
        // the same as for the likelihood
        TreeInterface tree = null;
        try {
            for (Input<?> input : ((BEASTInterface) clockModel).listInputs()) {
                if (input.getName().equals("tree")) {
                    tree = (TreeInterface) input.get();
                }
            }
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (tree != null && tree != likelihood.treeInput.get()) {
            // likelihood.treeInput.setValue(tree, likelihood);
            JOptionPane.showMessageDialog(null, "Cannot clone clock model with different trees");
            return;
        }
        likelihood.branchRateModelInput.setValue(clockModel, likelihood);
        return;
    } else if (type.equals("Tree")) {
        TreeInterface tree = null;
        TreeInterface treeSource = likelihoodSource.treeInput.get();
        try {
            tree = (TreeInterface) BeautiDoc.deepCopyPlugin((BEASTInterface) treeSource, likelihood, (MCMC) doc.mcmc.get(), oldContext, newContext, doc, null);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Could not clone " + sourceID + " to " + targetID + " " + e.getMessage());
            return;
        }
        // sanity check: make sure taxon sets are compatible
        Taxon.assertSameTaxa(tree.getID(), tree.getTaxonset().getTaxaNames(), likelihood.dataInput.get().getID(), likelihood.dataInput.get().getTaxaNames());
        likelihood.treeInput.setValue(tree, likelihood);
        return;
    } else {
        throw new RuntimeException("Programmer error calling cloneFrom: Should only clone Site/Clock/Tree model");
    }
}
Also used : GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) MCMC(beast.core.MCMC) SiteModel(beast.evolution.sitemodel.SiteModel) InvocationTargetException(java.lang.reflect.InvocationTargetException) TreeInterface(beast.evolution.tree.TreeInterface) CompoundDistribution(beast.core.util.CompoundDistribution) Input(beast.core.Input) BranchRateModel(beast.evolution.branchratemodel.BranchRateModel) BEASTInterface(beast.core.BEASTInterface) SiteModelInterface(beast.evolution.sitemodel.SiteModelInterface)

Example 69 with BEASTInterface

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

the class BeautiPanel method refreshInputPanel.

void refreshInputPanel() throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    doc.currentInputEditors.clear();
    InputEditor.Base.g_nLabelWidth = config.labelWidthInput.get();
    BEASTInterface beastObject = config;
    final Input<?> input = config.resolveInput(doc, partitionIndex);
    boolean addButtons = config.addButtons();
    ExpandOption forceExpansion = config.forceExpansion();
    refreshInputPanel(beastObject, input, addButtons, forceExpansion);
}
Also used : BEASTInterface(beast.core.BEASTInterface) ExpandOption(beast.app.draw.InputEditor.ExpandOption)

Example 70 with BEASTInterface

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

the class XMLParser method createBeastObject.

// createObject
/**
 * create BEASTInterface either using Inputs, or using annotated constructor *
 */
private BEASTInterface createBeastObject(Node node, String ID, String clazzName, List<NameValuePair> inputInfo) throws XMLParserException {
    BEASTInterface beastObject = useAnnotatedConstructor(node, ID, clazzName, inputInfo);
    if (beastObject != null) {
        return beastObject;
    }
    // create new instance using class name
    Object o = null;
    try {
        Class<?> c = Class.forName(clazzName);
        o = c.newInstance();
    } catch (InstantiationException e) {
        // created for instance because it is abstract
        throw new XMLParserException(node, "Cannot instantiate class. Please check the spec attribute.", 1006);
    } catch (ClassNotFoundException e) {
    // ignore -- class was found in beastObjectNames before
    } catch (IllegalAccessException e) {
        // T O D O Auto-generated catch block
        e.printStackTrace();
        throw new XMLParserException(node, "Cannot access class. Please check the spec attribute.", 1011);
    }
    // set id
    beastObject = (BEASTInterface) o;
    beastObject.setID(ID);
    // hack required to make log-parsing easier
    if (o instanceof State) {
        m_state = (State) o;
    }
    // process inputs for annotated constructors
    for (NameValuePair pair : inputInfo) {
        if (pair.value instanceof BEASTInterface) {
            setInput(node, beastObject, pair.name, (BEASTInterface) pair.value);
        } else if (pair.value instanceof String) {
            setInput(node, beastObject, pair.name, (String) pair.value);
        } else {
            throw new RuntimeException("Programmer error: value should be String or BEASTInterface");
        }
    }
    // fill in missing inputs, if an input provider is available
    try {
        if (requiredInputProvider != null) {
            for (Input<?> input : beastObject.listInputs()) {
                if (input.get() == null && input.getRule() == Validate.REQUIRED) {
                    Object o2 = requiredInputProvider.createInput(beastObject, input, partitionContext);
                    if (o2 != null) {
                        input.setValue(o2, beastObject);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new XMLParserException(node, e.getMessage(), 1008);
    }
    // sanity check: all attributes should be valid input names
    if (!(beastObject instanceof Map)) {
        for (NameValuePair pair : inputInfo) {
            String name = pair.name;
            if (!(name.equals("id") || name.equals("idref") || name.equals("spec") || name.equals("name"))) {
                try {
                    beastObject.getInput(name);
                } catch (Exception e) {
                    throw new XMLParserException(node, e.getMessage(), 1009);
                }
            }
        }
    }
    // make sure object o is in outputs of inputs
    for (NameValuePair pair : inputInfo) {
        if (pair.value instanceof BEASTInterface) {
            ((BEASTInterface) pair.value).getOutputs().add((BEASTInterface) o);
        }
    }
    register(node, beastObject);
    return beastObject;
}
Also used : IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) State(beast.core.State) BEASTInterface(beast.core.BEASTInterface) HashMap(java.util.HashMap) Map(beast.core.parameter.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

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