use of beast.core.util.CompoundDistribution 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++;
}
}
}
use of beast.core.util.CompoundDistribution in project beast2 by CompEvol.
the class SiteModelInputEditor method setUpOperator.
/**
* set up relative weights and parameter input *
*/
public void setUpOperator() {
boolean isAllClocksAreEqual = true;
try {
boolean hasOneEstimatedRate = customConnector(doc);
if (doc.autoUpdateFixMeanSubstRate) {
fixMeanRatesCheckBox.setSelected(hasOneEstimatedRate);
doFixMeanRates(hasOneEstimatedRate);
}
try {
double commonClockRate = -1;
CompoundDistribution likelihood = (CompoundDistribution) doc.pluginmap.get("likelihood");
for (Distribution d : likelihood.pDistributions.get()) {
GenericTreeLikelihood treelikelihood = (GenericTreeLikelihood) d;
if (treelikelihood.siteModelInput.get() instanceof SiteModel) {
SiteModel siteModel = (SiteModel) treelikelihood.siteModelInput.get();
RealParameter mutationRate = siteModel.muParameterInput.get();
// clockRate.m_bIsEstimated.setValue(true, clockRate);
if (mutationRate.isEstimatedInput.get()) {
if (commonClockRate < 0) {
commonClockRate = mutationRate.valuesInput.get().get(0);
} else {
if (Math.abs(commonClockRate - mutationRate.valuesInput.get().get(0)) > 1e-10) {
isAllClocksAreEqual = false;
}
}
}
}
}
} catch (Exception e) {
}
List<RealParameter> parameters = operator.parameterInput.get();
if (!fixMeanRatesCheckBox.isSelected()) {
fixMeanRatesValidateLabel.setVisible(false);
repaint();
return;
}
if (parameters.size() == 0) {
fixMeanRatesValidateLabel.setVisible(true);
fixMeanRatesValidateLabel.m_circleColor = Color.red;
fixMeanRatesValidateLabel.setToolTipText("The model is invalid: At least one substitution rate should be estimated.");
repaint();
return;
}
if (!isAllClocksAreEqual) {
fixMeanRatesValidateLabel.setVisible(true);
fixMeanRatesValidateLabel.m_circleColor = Color.orange;
fixMeanRatesValidateLabel.setToolTipText("Not all substitution rates 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.getPartitions("SiteModel").size()) {
fixMeanRatesValidateLabel.setVisible(true);
fixMeanRatesValidateLabel.m_circleColor = Color.orange;
fixMeanRatesValidateLabel.setToolTipText("Not all partitions have their rate estimated");
} else {
fixMeanRatesValidateLabel.setVisible(false);
}
repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
use of beast.core.util.CompoundDistribution in project beast2 by CompEvol.
the class MCMC method reportLogLikelihoods.
/*
* report posterior and subcomponents recursively, for debugging
* incorrectly recalculated posteriors *
*/
protected void reportLogLikelihoods(final Distribution distr, final String tabString) {
final double full = distr.logP, last = distr.storedLogP;
final String changed = full == last ? "" : " **";
Log.info.println(tabString + "P(" + distr.getID() + ") = " + full + " (was " + last + ")" + changed);
if (distr instanceof CompoundDistribution) {
for (final Distribution distr2 : ((CompoundDistribution) distr).pDistributions.get()) {
reportLogLikelihoods(distr2, tabString + "\t");
}
}
}
use of beast.core.util.CompoundDistribution in project beast2 by CompEvol.
the class MCMC method initAndValidate.
@Override
public void initAndValidate() {
Log.info.println("===============================================================================");
Log.info.println("Citations for this model:");
Log.info.println(getCitations());
Log.info.println("===============================================================================");
operatorSchedule = operatorScheduleInput.get();
for (final Operator op : operatorsInput.get()) {
operatorSchedule.addOperator(op);
}
if (sampleFromPriorInput.get()) {
// remove beastObject with id likelihood from posterior, if it is a CompoundDistribution
if (posteriorInput.get() instanceof CompoundDistribution) {
final CompoundDistribution posterior = (CompoundDistribution) posteriorInput.get();
final List<Distribution> distrs = posterior.pDistributions.get();
final int distrCount = distrs.size();
for (int i = 0; i < distrCount; i++) {
final Distribution distr = distrs.get(i);
final String id = distr.getID();
if (id != null && id.equals("likelihood")) {
distrs.remove(distr);
break;
}
}
if (distrs.size() == distrCount) {
throw new RuntimeException("Sample from prior flag is set, but distribution with id 'likelihood' is " + "not an input to posterior.");
}
} else {
throw new RuntimeException("Don't know how to sample from prior since posterior is not a compound distribution. " + "Suggestion: set sampleFromPrior flag to false.");
}
}
// StateNode initialisation, only required when the state is not read from file
if (restoreFromFile) {
final HashSet<StateNode> initialisedStateNodes = new HashSet<>();
for (final StateNodeInitialiser initialiser : initialisersInput.get()) {
// make sure that the initialiser does not re-initialises a StateNode
final List<StateNode> list = new ArrayList<>(1);
initialiser.getInitialisedStateNodes(list);
for (final StateNode stateNode : list) {
if (initialisedStateNodes.contains(stateNode)) {
throw new RuntimeException("Trying to initialise stateNode (id=" + stateNode.getID() + ") more than once. " + "Remove an initialiser from MCMC to fix this.");
}
}
initialisedStateNodes.addAll(list);
// do the initialisation
// initialiser.initStateNodes();
}
}
// State initialisation
final HashSet<StateNode> operatorStateNodes = new HashSet<>();
for (final Operator op : operatorsInput.get()) {
for (final StateNode stateNode : op.listStateNodes()) {
operatorStateNodes.add(stateNode);
}
}
if (startStateInput.get() != null) {
this.state = startStateInput.get();
if (storeEveryInput.get() > 0) {
this.state.m_storeEvery.setValue(storeEveryInput.get(), this.state);
}
} else {
// create state from scratch by collecting StateNode inputs from Operators
this.state = new State();
for (final StateNode stateNode : operatorStateNodes) {
this.state.stateNodeInput.setValue(stateNode, this.state);
}
this.state.m_storeEvery.setValue(storeEveryInput.get(), this.state);
}
// grab the interval for storing the state to file
if (storeEveryInput.get() > 0) {
storeEvery = storeEveryInput.get();
} else {
storeEvery = state.m_storeEvery.get();
}
this.state.initialise();
this.state.setPosterior(posteriorInput.get());
// sanity check: all operator state nodes should be in the state
final List<StateNode> stateNodes = this.state.stateNodeInput.get();
for (final Operator op : operatorsInput.get()) {
List<StateNode> nodes = op.listStateNodes();
if (nodes.size() == 0) {
throw new RuntimeException("Operator " + op.getID() + " has no state nodes in the state. " + "Each operator should operate on at least one estimated state node in the state. " + "Remove the operator or add its statenode(s) to the state and/or set estimate='true'.");
// otherwise the chain may hang without obvious reason
}
for (final StateNode stateNode : op.listStateNodes()) {
if (!stateNodes.contains(stateNode)) {
throw new RuntimeException("Operator " + op.getID() + " has a statenode " + stateNode.getID() + " in its inputs that is missing from the state.");
}
}
}
// sanity check: at least one operator required to run MCMC
if (operatorsInput.get().size() == 0) {
Log.warning.println("Warning: at least one operator required to run the MCMC properly, but none found.");
}
// sanity check: all state nodes should be operated on
for (final StateNode stateNode : stateNodes) {
if (!operatorStateNodes.contains(stateNode)) {
Log.warning.println("Warning: state contains a node " + stateNode.getID() + " for which there is no operator.");
}
}
}
Aggregations