Search in sources :

Example 81 with BEASTInterface

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

the class BeautiDoc method determinePartitions.

public void determinePartitions() {
    CompoundDistribution likelihood = (CompoundDistribution) pluginmap.get("likelihood");
    if (likelihood == null) {
        return;
    }
    partitionNames.clear();
    possibleContexts.clear();
    for (Distribution distr : likelihood.pDistributions.get()) {
        if (distr instanceof GenericTreeLikelihood) {
            GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
            alignments.add(treeLikelihood.dataInput.get());
            PartitionContext context = new PartitionContext(treeLikelihood);
            partitionNames.add(context);
            boolean found = false;
            for (PartitionContext context2 : possibleContexts) {
                if (context.equals(context2)) {
                    found = true;
                }
            }
            if (!found) {
                possibleContexts.add(context);
            }
        }
    }
    alignments.clear();
    for (int i = 0; i < 3; i++) {
        pPartitionByAlignments[i].clear();
        pPartition[i].clear();
        currentPartitions[i].clear();
    }
    List<GenericTreeLikelihood> treeLikelihoods = new ArrayList<>();
    for (Distribution distr : likelihood.pDistributions.get()) {
        if (distr instanceof GenericTreeLikelihood) {
            GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
            alignments.add(treeLikelihood.dataInput.get());
            treeLikelihoods.add(treeLikelihood);
        }
    }
    for (Distribution distr : likelihood.pDistributions.get()) {
        if (distr instanceof GenericTreeLikelihood) {
            GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
            try {
                // sync SiteModel, ClockModel and Tree to any changes that
                // may have occurred
                // this should only affect the clock model in practice
                int partition = getPartitionNr((BEASTInterface) treeLikelihood.siteModelInput.get());
                GenericTreeLikelihood treeLikelihood2 = treeLikelihoods.get(partition);
                treeLikelihood.siteModelInput.setValue(treeLikelihood2.siteModelInput.get(), treeLikelihood);
                currentPartitions[0].add(partition);
                BranchRateModel rateModel = treeLikelihood.branchRateModelInput.get();
                if (rateModel != null) {
                    partition = getPartitionNr((BEASTInterface) rateModel);
                    treeLikelihood2 = treeLikelihoods.get(partition);
                    treeLikelihood.branchRateModelInput.setValue(treeLikelihood2.branchRateModelInput.get(), treeLikelihood);
                    currentPartitions[1].add(partition);
                } else {
                    currentPartitions[1].add(0);
                }
                partition = getPartitionNr((BEASTInterface) treeLikelihood.treeInput.get());
                treeLikelihood2 = treeLikelihoods.get(partition);
                treeLikelihood.treeInput.setValue(treeLikelihood2.treeInput.get(), treeLikelihood);
                currentPartitions[2].add(partition);
            } catch (Exception e) {
                e.printStackTrace();
            }
            pPartitionByAlignments[0].add(treeLikelihood);
            pPartitionByAlignments[1].add(treeLikelihood);
            pPartitionByAlignments[2].add(treeLikelihood);
        }
    }
    int partitionCount = partitionNames.size();
    for (int i = 0; i < 3; i++) {
        boolean[] usedPartition = new boolean[partitionCount];
        for (int j = 0; j < partitionCount; j++) {
            // getPartitionNr(m_pPartitionByAlignments[i].get(j));
            int partitionIndex = currentPartitions[i].get(j);
            usedPartition[partitionIndex] = true;
        }
        for (int j = 0; j < partitionCount; j++) {
            if (usedPartition[j]) {
                pPartition[i].add(pPartitionByAlignments[i].get(j));
            }
        }
    }
    Log.warning.println("PARTITIONS0:\n");
    Log.warning.println(Arrays.toString(currentPartitions));
}
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) ArrayList(java.util.ArrayList) 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)

Example 82 with BEASTInterface

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

the class BeautiDoc method connect.

/**
 * connect source beastObject with target beastObject
 *
 * @ *
 */
public void connect(BeautiConnector connector, PartitionContext context) {
    if (!connector.isRegularConnector) {
        return;
    }
    String srcID = translatePartitionNames(connector.sourceID, context);
    BEASTInterface srcBEASTObject = pluginmap.get(srcID);
    if (srcBEASTObject == null) {
        throw new IllegalArgumentException("Could not find beastObject with id " + srcID + ". Typo in template perhaps?\n");
    }
    String targetID = translatePartitionNames(connector.targetID, context);
    connect(srcBEASTObject, targetID, connector.targetInput);
}
Also used : BEASTInterface(beast.core.BEASTInterface)

Example 83 with BEASTInterface

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

the class BeautiDoc method collectClockModels.

private void collectClockModels() {
    // collect branch rate models from model
    CompoundDistribution likelihood = (CompoundDistribution) pluginmap.get("likelihood");
    while (clockModels.size() < partitionNames.size()) {
        try {
            GenericTreeLikelihood treelikelihood = new GenericTreeLikelihood();
            treelikelihood.branchRateModelInput.setValue(new StrictClockModel(), treelikelihood);
            List<BeautiSubTemplate> availableBEASTObjects = inputEditorFactory.getAvailableTemplates(treelikelihood.branchRateModelInput, treelikelihood, null, this);
            BEASTInterface beastObject = availableBEASTObjects.get(0).createSubNet(partitionNames.get(clockModels.size()), true);
            clockModels.add((BranchRateModel.Base) beastObject);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    int k = 0;
    for (Distribution d : likelihood.pDistributions.get()) {
        BranchRateModel clockModel = ((GenericTreeLikelihood) d).branchRateModelInput.get();
        // sanity check
        Tree tree = null;
        try {
            for (Input<?> input : ((BEASTInterface) clockModel).listInputs()) {
                if (input.getName().equals("tree")) {
                    tree = (Tree) input.get();
                }
            }
            if (tree != null && tree != ((GenericTreeLikelihood) d).treeInput.get()) {
                clockModel = clockModels.get(k);
                Log.warning.println("WARNING: unlinking clock model for " + d.getID());
                // TODO #557: this should move to the event of clock model drop box
                // JOptionPane.showMessageDialog(beauti.getSelectedComponent(),
                // "Cannot link all clock model(s) except strict clock with different trees !");
                ((GenericTreeLikelihood) d).branchRateModelInput.setValue(clockModel, d);
            }
        } catch (Exception e) {
        // ignore
        }
        if (clockModel != null) {
            String id = ((BEASTInterface) clockModel).getID();
            id = parsePartition(id);
            String partition = alignments.get(k).getID();
            if (id.equals(partition)) {
                clockModels.set(k, clockModel);
            }
            k++;
        }
    }
}
Also used : GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) 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) StrictClockModel(beast.evolution.branchratemodel.StrictClockModel) BranchRateModel(beast.evolution.branchratemodel.BranchRateModel) CompoundDistribution(beast.core.util.CompoundDistribution) ParametricDistribution(beast.math.distributions.ParametricDistribution) Distribution(beast.core.Distribution) Tree(beast.evolution.tree.Tree) BEASTInterface(beast.core.BEASTInterface)

Example 84 with BEASTInterface

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

the class BeautiDoc method toJSON.

// save
private String toJSON() {
    Set<BEASTInterface> beastObjects = new HashSet<>();
    String json = new JSONProducer().toJSON(mcmc.get(), beastObjects);
    json = json.replaceFirst("\\{", "{ beautitemplate:\"" + templateName + "\", beautistatus:\"" + getBeautiStatus() + "\", ");
    return json + "\n";
}
Also used : JSONProducer(beast.util.JSONProducer) BEASTInterface(beast.core.BEASTInterface) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 85 with BEASTInterface

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

the class BeautiDoc method setUpActivePlugins.

// scrubAll
protected void setUpActivePlugins() {
    posteriorPredecessors = new ArrayList<>();
    collectPredecessors(((MCMC) mcmc.get()).posteriorInput.get(), posteriorPredecessors);
    likelihoodPredecessors = new ArrayList<>();
    if (pluginmap.containsKey("likelihood")) {
        collectPredecessors(pluginmap.get("likelihood"), likelihoodPredecessors);
    }
    Log.trace.print("InPosterior=");
    for (BEASTInterface o : posteriorPredecessors) {
        pluginmap.put(o.getID(), o);
        Log.trace.print(o.getID() + " ");
    // if (!pluginmap.containsKey(o)) {
    // System.err.println("MISSING: " + o.getID());
    // }
    }
    Log.trace.println();
}
Also used : MCMC(beast.core.MCMC) 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