Search in sources :

Example 6 with BEASTInterface

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

the class BeautiDoc method loadTemplate.

void loadTemplate(String xml) throws XMLParserException, SAXException, IOException, ParserConfigurationException {
    // load the template and its beauti configuration parts
    XMLParser parser = new XMLParser();
    BEASTObjectPanel.init();
    List<BEASTInterface> beastObjects = parser.parseTemplate(xml, new HashMap<>(), true);
    for (BEASTInterface beastObject : beastObjects) {
        if (beastObject instanceof beast.core.Runnable) {
            mcmc.setValue(beastObject, this);
        } else if (beastObject instanceof BeautiConfig) {
            beautiConfig = (BeautiConfig) beastObject;
            beautiConfig.setDoc(this);
        } else {
            Log.warning.println("template item " + beastObject.getID() + " is ignored");
        }
        BEASTObjectPanel.addPluginToMap(beastObject, this);
    }
}
Also used : BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser)

Example 7 with BEASTInterface

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

the class BeautiDoc method setClockRate.

void setClockRate() {
    boolean needsEstimationBySPTree = false;
    if (pluginmap.containsKey("Tree.t:Species")) {
        Tree sptree = (Tree) pluginmap.get("Tree.t:Species");
        // check whether there is a calibration
        for (Object beastObject : sptree.getOutputs()) {
            if (beastObject instanceof MRCAPrior) {
                MRCAPrior prior = (MRCAPrior) beastObject;
                if (prior.distInput.get() != null) {
                    needsEstimationBySPTree = true;
                }
            }
        }
    }
    BEASTInterface likelihood = pluginmap.get("likelihood");
    if (likelihood instanceof CompoundDistribution) {
        int i = 0;
        RealParameter firstClock = null;
        for (Distribution distr : ((CompoundDistribution) likelihood).pDistributions.get()) {
            if (distr instanceof GenericTreeLikelihood) {
                GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
                boolean needsEstimation = needsEstimationBySPTree;
                if (i > 0) {
                    BranchRateModel.Base model = treeLikelihood.branchRateModelInput.get();
                    needsEstimation = (model.meanRateInput.get() != firstClock) || firstClock.isEstimatedInput.get();
                } else {
                    // TODO: this might not be a valid type conversion from TreeInterface to Tree
                    Tree tree = (Tree) treeLikelihood.treeInput.get();
                    // check whether there are tip dates
                    if (tree.hasDateTrait()) {
                        needsEstimation = true;
                    }
                    // check whether there is a calibration
                    for (Object beastObject : tree.getOutputs()) {
                        if (beastObject instanceof MRCAPrior) {
                            MRCAPrior prior = (MRCAPrior) beastObject;
                            if (prior.distInput.get() != null) {
                                needsEstimation = true;
                            }
                        }
                    }
                }
                BranchRateModel.Base model = treeLikelihood.branchRateModelInput.get();
                if (model != null) {
                    RealParameter clockRate = model.meanRateInput.get();
                    clockRate.isEstimatedInput.setValue(needsEstimation, clockRate);
                    if (firstClock == null) {
                        firstClock = clockRate;
                    }
                }
                i++;
            }
        }
    }
}
Also used : CompoundDistribution(beast.core.util.CompoundDistribution) BranchRateModel(beast.evolution.branchratemodel.BranchRateModel) CompoundDistribution(beast.core.util.CompoundDistribution) ParametricDistribution(beast.math.distributions.ParametricDistribution) Distribution(beast.core.Distribution) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) MRCAPrior(beast.math.distributions.MRCAPrior) Tree(beast.evolution.tree.Tree) RealParameter(beast.core.parameter.RealParameter) BEASTObject(beast.core.BEASTObject) BEASTInterface(beast.core.BEASTInterface)

Example 8 with BEASTInterface

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

the class BeautiDoc method mergeSequences.

/**
 * Merge sequence data with xml specification.
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 * @throws XMLParserException
 */
void mergeSequences(String xml) throws XMLParserException, SAXException, IOException, ParserConfigurationException {
    if (xml == null) {
        xml = processTemplate(STANDARD_TEMPLATE);
    }
    loadTemplate(xml);
    // create XML for alignments
    if (beautiConfig != null) {
        for (Alignment alignment : alignments) {
            beautiConfig.partitionTemplate.get().createSubNet(alignment, this, true);
        }
    } else {
        // replace alignment
        for (BEASTInterface beastObject : pluginmap.values()) {
            if (beastObject instanceof Alignment) {
                // use toArray to prevent ConcurrentModificationException
                for (Object output : beastObject.getOutputs().toArray()) {
                    replaceInputs((BEASTInterface) output, beastObject, alignments.get(0));
                }
            }
        }
        return;
    }
    determinePartitions();
}
Also used : FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) BEASTInterface(beast.core.BEASTInterface) BEASTObject(beast.core.BEASTObject)

Example 9 with BEASTInterface

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

the class BeautiDoc method suggestedLinks.

/**
 * return all RealParameters that have
 * the same ID in another partition, or
 * the same partition with the same substitution model as output
 *
 * @param beastObject
 * @return
 */
public List<BEASTInterface> suggestedLinks(BEASTInterface beastObject) {
    String id = beastObject.getID();
    List<BEASTInterface> list = new ArrayList<>();
    String partitionID = null;
    if (id.indexOf('.') >= 0) {
        partitionID = id.substring(id.indexOf('.') + 1);
        id = id.substring(0, id.indexOf('.'));
    } else {
        return list;
    }
    for (BEASTInterface candidate : posteriorPredecessors) {
        String id2 = candidate.getID();
        if (id2.indexOf('.') >= 0) {
            String partitionID2 = id2.substring(id2.indexOf('.') + 1);
            id2 = id2.substring(0, id2.indexOf('.'));
            if (id2.equals(id)) {
                list.add(candidate);
            }
            if (beastObject instanceof Parameter<?> && partitionID2.equals(partitionID) && candidate.getClass().equals(beastObject.getClass())) {
                boolean dimensionMatches = true;
                if (((Parameter<?>) beastObject).getDimension() != ((Parameter<?>) candidate).getDimension()) {
                    dimensionMatches = false;
                }
                // ensure they share an output
                boolean foundCommonOutput = false;
                for (Object out1 : beastObject.getOutputs()) {
                    for (Object out2 : candidate.getOutputs()) {
                        if (out1 == out2 && out1 instanceof SubstitutionModel) {
                            foundCommonOutput = true;
                            break;
                        }
                    }
                }
                if (dimensionMatches && foundCommonOutput) {
                    list.add(candidate);
                }
            }
        }
    }
    list.remove(beastObject);
    return list;
}
Also used : ArrayList(java.util.ArrayList) RealParameter(beast.core.parameter.RealParameter) Parameter(beast.core.parameter.Parameter) BEASTInterface(beast.core.BEASTInterface) BEASTObject(beast.core.BEASTObject) SubstitutionModel(beast.evolution.substitutionmodel.SubstitutionModel)

Example 10 with BEASTInterface

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

the class BeautiDoc method scrubAll.

// TreeDistribution getTreePrior(String partition) {
// int k = 0;
// for (Alignment data : alignments) {
// if (data.getID().equals(partition)) {
// return treePriors.get(k);
// }
// k++;
// }
// return null;
// }
public synchronized void scrubAll(boolean useNotEstimatedStateNodes, boolean isInitial) {
    try {
        if (autoSetClockRate) {
            setClockRate();
        }
        if (autoUpdateFixMeanSubstRate) {
            SiteModelInputEditor.customConnector(this);
        }
        // }
        if (pluginmap.containsKey("Tree.t:Species")) {
            Tree tree = (Tree) pluginmap.get("Tree.t:Species");
            tree.isEstimatedInput.setValue(true, tree);
        }
        // go through all templates, and process connectors in relevant ones
        boolean progress = true;
        while (progress) {
            warning("============================ start scrubbing ===========================");
            progress = false;
            setUpActivePlugins();
            // process MRCA priors
            for (String id : pluginmap.keySet()) {
                if (id != null && id.endsWith(".prior")) {
                    BEASTInterface beastObject = pluginmap.get(id);
                    if (beastObject instanceof MRCAPrior) {
                        MRCAPrior prior = (MRCAPrior) beastObject;
                        if (prior.treeInput.get().isEstimatedInput.get() == false) {
                            // disconnect
                            disconnect(beastObject, "prior", "distribution");
                        } else {
                            // connect
                            connect(beastObject, "prior", "distribution");
                        }
                    }
                }
            }
            List<BeautiSubTemplate> templates = new ArrayList<>();
            templates.add(beautiConfig.partitionTemplate.get());
            templates.addAll(beautiConfig.subTemplates);
            for (PartitionContext context : possibleContexts) {
                applyBeautiRules(templates, isInitial, context);
            }
            // add 'Species' as special partition name
            applyBeautiRules(templates, isInitial, new PartitionContext("Species"));
            // if the model changed, some rules that use inposterior() may
            // not have been triggered properly
            // so we need to check that the model changed, and if so,
            // revisit the BeautiConnectors
            List<BEASTInterface> posteriorPredecessors2 = new ArrayList<>();
            collectPredecessors(((MCMC) mcmc.get()).posteriorInput.get(), posteriorPredecessors2);
            if (posteriorPredecessors.size() != posteriorPredecessors2.size()) {
                progress = true;
            } else {
                for (BEASTInterface beastObject : posteriorPredecessors2) {
                    if (!posteriorPredecessors.contains(beastObject)) {
                        progress = true;
                        break;
                    }
                }
            }
        }
        List<BeautiSubTemplate> templates = new ArrayList<>();
        templates.add(beautiConfig.hyperPriorTemplate);
        for (BEASTInterface beastObject : pluginmap.values()) {
            if (beastObject instanceof RealParameter) {
                if (beastObject.getID() != null && beastObject.getID().startsWith("parameter.")) {
                    PartitionContext context = new PartitionContext(beastObject.getID().substring("parameter.".length()));
                    applyBeautiRules(templates, isInitial, context);
                }
            }
        }
        collectClockModels();
        // collectTreePriors();
        Log.warning.println("PARTITIONS:\n");
        Log.warning.println(Arrays.toString(currentPartitions));
        determineLinks();
    } catch (Exception e) {
        Log.err.println(e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) MCMC(beast.core.MCMC) RealParameter(beast.core.parameter.RealParameter) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MRCAPrior(beast.math.distributions.MRCAPrior) Tree(beast.evolution.tree.Tree) BEASTInterface(beast.core.BEASTInterface)

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