Search in sources :

Example 1 with RealParameter

use of beast.core.parameter.RealParameter 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 2 with RealParameter

use of beast.core.parameter.RealParameter 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)

Example 3 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class ClockModelListInputEditor method setUpOperator.

/**
 * set up relative weights and parameter input *
 */
private void setUpOperator() {
    String weights = "";
    List<RealParameter> parameters = operator.parameterInput.get();
    parameters.clear();
    double commonClockRate = -1;
    boolean isAllClocksAreEqual = true;
    try {
        for (int i = 0; i < doc.alignments.size(); i++) {
            Alignment data = doc.alignments.get(i);
            int weight = data.getSiteCount();
            BranchRateModel.Base clockModel = (BranchRateModel.Base) doc.clockModels.get(i);
            RealParameter clockRate = clockModel.meanRateInput.get();
            // clockRate.m_bIsEstimated.setValue(true, clockRate);
            if (clockRate.isEstimatedInput.get()) {
                if (commonClockRate < 0) {
                    commonClockRate = clockRate.valuesInput.get().get(0);
                } else {
                    if (Math.abs(commonClockRate - clockRate.valuesInput.get().get(0)) > 1e-10) {
                        isAllClocksAreEqual = false;
                    }
                }
                weights += weight + " ";
                parameters.add(clockRate);
            }
        // doc.autoSetClockRate = false;
        }
        if (!fixMeanRatesCheckBox.isSelected()) {
            fixMeanRatesValidateLabel.setVisible(false);
            return;
        }
        if (parameters.size() == 0) {
            fixMeanRatesValidateLabel.setVisible(true);
            fixMeanRatesValidateLabel.m_circleColor = Color.red;
            fixMeanRatesValidateLabel.setToolTipText("The model is invalid: At least one clock rate should be estimated.");
            return;
        }
        IntegerParameter weightParameter = new IntegerParameter(weights);
        weightParameter.setID("weightparameter");
        weightParameter.isEstimatedInput.setValue(false, weightParameter);
        operator.parameterWeightsInput.setValue(weightParameter, operator);
        if (!isAllClocksAreEqual) {
            fixMeanRatesValidateLabel.setVisible(true);
            fixMeanRatesValidateLabel.m_circleColor = Color.orange;
            fixMeanRatesValidateLabel.setToolTipText("Not all clocks are equal. Are you sure this is what you want?");
        } else if (parameters.size() == 1) {
            fixMeanRatesValidateLabel.setVisible(true);
            fixMeanRatesValidateLabel.m_circleColor = Color.orange;
            fixMeanRatesValidateLabel.setToolTipText("At least 2 clock models should have their rate estimated");
        } else if (parameters.size() < doc.alignments.size()) {
            fixMeanRatesValidateLabel.setVisible(true);
            fixMeanRatesValidateLabel.m_circleColor = Color.orange;
            fixMeanRatesValidateLabel.setToolTipText("Not all partitions have their rate estimated");
        } else {
            fixMeanRatesValidateLabel.setVisible(false);
        }
        repaint();
    // doc.autoSetClockRate = true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) Alignment(beast.evolution.alignment.Alignment) BranchRateModel(beast.evolution.branchratemodel.BranchRateModel) RealParameter(beast.core.parameter.RealParameter)

Example 4 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class RealParameterListTest method test1.

@Test
public void test1() throws Exception {
    RealParameterList parameterList = new RealParameterList();
    // Parameters with which to initialise list
    RealParameter param1 = new RealParameter();
    param1.initByName("value", "2");
    RealParameter param2 = new RealParameter();
    param2.initByName("value", "3");
    // Initialise parameter list
    parameterList.initByName("initialParam", param1, "initialParam", param2);
    // Create dummy state to allow statenode editing
    State state = new State();
    state.initByName("stateNode", parameterList);
    state.initialise();
    // Test parameter value modification
    parameterList.get(0).setValue(20.0);
    // Test parameter creation and modification
    Parameter<Double> newParam = parameterList.addNewParam();
    newParam.setValue(53.0);
    assertEquals(parameterList.get(0).getValue(), 20.0, 1e-15);
    assertEquals(parameterList.get(0).getKey(), 0);
    assertEquals(parameterList.get(1).getValue(), 3.0, 1e-15);
    assertEquals(parameterList.get(1).getKey(), 1);
    assertEquals(parameterList.get(2).getValue(), 53.0, 1e-15);
    assertEquals(parameterList.get(2).getKey(), 2);
    assertEquals(parameterList.size(), 3);
    parameterList.remove(1);
    newParam = parameterList.addNewParam();
    newParam.setValue(42.0);
    assertEquals(parameterList.get(0).getValue(), 20.0, 1e-15);
    assertEquals(parameterList.get(0).getKey(), 0, 1e-15);
    assertEquals(parameterList.get(1).getValue(), 53.0, 1e-15);
    assertEquals(parameterList.get(1).getKey(), 2, 1e-15);
    assertEquals(parameterList.get(2).getValue(), 42.0, 1e-15);
    assertEquals(parameterList.get(2).getKey(), 1);
    assertEquals(parameterList.size(), 3);
    // Test state restore
    parameterList.restore();
    assertEquals(parameterList.get(0).getValue(), 2.0, 1e-15);
    assertEquals(parameterList.get(0).getKey(), 0);
    assertEquals(parameterList.get(1).getValue(), 3.0, 1e-15);
    assertEquals(parameterList.get(1).getKey(), 1);
    assertEquals(parameterList.size(), 2);
    // Test serialization
    String xmlStr = parameterList.toXML();
    assertEquals(xmlStr, "<statenode id='null'>" + "Dimension: [1, 1], " + "Bounds: [-Infinity,Infinity], " + "AvailableKeys: [], NextKey: 2, " + "Parameters: [[2.0],[3.0]], " + "ParameterKeys: [0,1]" + "</statenode>\n");
    // Test deserialization
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(xmlStr.getBytes()));
    doc.normalize();
    NodeList nodes = doc.getElementsByTagName("*");
    org.w3c.dom.Node docNode = nodes.item(0);
    RealParameterList newParameterList = new RealParameterList();
    newParameterList.initAndValidate();
    newParameterList.fromXML(docNode);
    assertEquals(newParameterList.get(0).getValue(), 2.0, 1e-15);
    assertEquals(newParameterList.get(0).getKey(), 0);
    assertEquals(newParameterList.get(1).getValue(), 3.0, 1e-15);
    assertEquals(newParameterList.get(1).getKey(), 1);
    assertEquals(newParameterList.size(), 2);
}
Also used : RealParameterList(beast.core.parameter.RealParameterList) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) RealParameter(beast.core.parameter.RealParameter) Document(org.w3c.dom.Document) ByteArrayInputStream(java.io.ByteArrayInputStream) State(beast.core.State) Test(org.junit.Test)

Example 5 with RealParameter

use of beast.core.parameter.RealParameter in project beast2 by CompEvol.

the class UnorderedAlignmentsTest method getSiteModel.

public static SiteModel getSiteModel() throws Exception {
    Frequencies frequencies = new Frequencies();
    frequencies.initByName("frequencies", new RealParameter("0.25 0.25 0.25 0.25"));
    HKY hky = new HKY();
    hky.initByName("kappa", new RealParameter("1.0"), "frequencies", frequencies);
    SiteModel siteModel = new SiteModel();
    siteModel.initByName("mutationRate", new RealParameter("0.005"), "substModel", hky);
    return siteModel;
}
Also used : HKY(beast.evolution.substitutionmodel.HKY) RealParameter(beast.core.parameter.RealParameter) SiteModel(beast.evolution.sitemodel.SiteModel) Frequencies(beast.evolution.substitutionmodel.Frequencies)

Aggregations

RealParameter (beast.core.parameter.RealParameter)97 Test (org.junit.Test)39 IntegerParameter (beast.core.parameter.IntegerParameter)23 Tree (beast.evolution.tree.Tree)16 State (beast.core.State)14 SCMigrationModel (beast.evolution.tree.SCMigrationModel)13 Alignment (beast.evolution.alignment.Alignment)11 TypeSet (beast.evolution.tree.TypeSet)11 MCMC (beast.core.MCMC)10 SiteModel (beast.evolution.sitemodel.SiteModel)10 Frequencies (beast.evolution.substitutionmodel.Frequencies)10 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)10 StructuredCoalescentTreeDensity (multitypetree.distributions.StructuredCoalescentTreeDensity)10 TaxonSet (beast.evolution.alignment.TaxonSet)9 MultiTypeTreeStatLogger (multitypetree.util.MultiTypeTreeStatLogger)9 MultiTypeTreeFromNewick (beast.evolution.tree.MultiTypeTreeFromNewick)8 Node (beast.evolution.tree.Node)8 Operator (beast.core.Operator)7 RandomTree (beast.evolution.tree.RandomTree)7 Locus (bacter.Locus)6