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);
}
}
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++;
}
}
}
}
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();
}
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;
}
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());
}
}
Aggregations