use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BeautiDoc method connect.
public void connect(BEASTInterface srcBEASTObject, String targetID, String inputName) {
try {
BEASTInterface target = pluginmap.get(targetID);
if (target == null) {
Log.trace.println("BeautiDoc: Could not find object " + targetID);
return;
}
// prevent duplication inserts in list
Object o = target.getInputValue(inputName);
if (o instanceof List) {
// System.err.println(" " + ((List)o).size());
if (((List<?>) o).contains(srcBEASTObject)) {
warning(" " + targetID + "/" + inputName + " already contains " + (srcBEASTObject == null ? "nulls" : srcBEASTObject.getID()) + "\n");
return;
}
}
target.setInputValue(inputName, srcBEASTObject);
} catch (Exception e) {
e.printStackTrace();
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BeautiDoc method disconnect.
/**
* disconnect source beastObject with target beastObject *
*/
public void disconnect(BeautiConnector connector, PartitionContext context) {
if (!connector.isRegularConnector) {
return;
}
BEASTInterface srcBEASTObject = pluginmap.get(translatePartitionNames(connector.sourceID, context));
String targetID = translatePartitionNames(connector.targetID, context);
disconnect(srcBEASTObject, targetID, connector.targetInput);
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BeautiDoc method determineLinks.
// methods for dealing with linking
void determineLinks() {
if (!allowLinking) {
return;
}
linked.clear();
for (BEASTInterface beastObject : posteriorPredecessors) {
Map<String, Integer> outputIDs = new HashMap<>();
for (Object output : beastObject.getOutputs()) {
if (posteriorPredecessors.contains(output)) {
String id = ((BEASTInterface) output).getID();
if (id.indexOf('.') >= 0) {
id = id.substring(0, id.indexOf('.'));
if (outputIDs.containsKey(id)) {
outputIDs.put(id, outputIDs.get(id) + 1);
} else {
outputIDs.put(id, 1);
}
}
}
}
for (Object output : beastObject.getOutputs()) {
if (posteriorPredecessors.contains(output)) {
String id = ((BEASTInterface) output).getID();
if (id.indexOf('.') >= 0) {
id = id.substring(0, id.indexOf('.'));
if (outputIDs.get(id) > 1) {
addLink(beastObject, (BEASTInterface) output);
}
}
}
}
// add parameters that have more than 1 outputs into susbtitution models
if (beastObject instanceof Parameter<?>) {
for (Object output : beastObject.getOutputs()) {
if (posteriorPredecessors.contains(output)) {
if (output instanceof SubstitutionModel) {
int nrOfSubstModelsInOutput = 0;
try {
for (Input<?> input : ((BEASTInterface) output).listInputs()) {
if (input.get() != null && input.get().equals(beastObject)) {
nrOfSubstModelsInOutput++;
}
}
} catch (Exception e) {
// ignore
}
if (nrOfSubstModelsInOutput > 1) {
addLink(beastObject, (BEASTInterface) output);
}
}
}
}
}
}
hasLinkedAtLeastOnce = false;
for (Input<?> input : linked) {
if (input.getType().isAssignableFrom(RealParameter.class)) {
hasLinkedAtLeastOnce = true;
break;
}
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BeautiDoc method extractSequences.
void extractSequences(String xml) throws XMLParserException, SAXException, IOException, ParserConfigurationException {
// parse the XML fragment into a DOM document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
doc.normalize();
// find top level beast element
NodeList nodes = doc.getElementsByTagName("*");
Node topNode = nodes.item(0);
String beautiTemplate = XMLParser.getAttribute(topNode, "beautitemplate");
if (beautiTemplate == null) {
int choice = JOptionPane.showConfirmDialog(getFrame(), "This file does not appear to be generated by BEAUti. If you load it, unexpected behaviour may follow");
if (choice != JOptionPane.OK_OPTION) {
return;
}
// load standard template
if (beautiConfig == null) {
String templateXML = processTemplate(STANDARD_TEMPLATE);
loadTemplate(templateXML);
}
} else {
String templateXML = processTemplate(beautiTemplate + ".xml");
loadTemplate(templateXML);
}
String beautiStatus = XMLParser.getAttribute(topNode, "beautistatus");
if (beautiStatus == null) {
beautiStatus = "";
}
autoSetClockRate = !beautiStatus.contains("noAutoSetClockRate");
beauti.autoSetClockRate.setSelected(autoSetClockRate);
allowLinking = beautiStatus.contains("allowLinking");
beauti.allowLinking.setSelected(allowLinking);
autoUpdateFixMeanSubstRate = !beautiStatus.contains("noAutoUpdateFixMeanSubstRate");
beauti.autoUpdateFixMeanSubstRate.setSelected(autoUpdateFixMeanSubstRate);
// parse file
XMLParser parser = new XMLParser();
BEASTInterface MCMC = parser.parseFragment(xml, true);
mcmc.setValue(MCMC, this);
BEASTObjectPanel.addPluginToMap(MCMC, this);
// reconstruct all objects from templates
try {
CompoundDistribution posterior = (CompoundDistribution) ((beast.core.MCMC) mcmc.get()).posteriorInput.get();
for (Distribution distr : posterior.pDistributions.get()) {
if (distr.getID().equals("likelihood")) {
for (Distribution likelihood : ((CompoundDistribution) distr).pDistributions.get()) {
if (likelihood instanceof GenericTreeLikelihood) {
GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) likelihood;
PartitionContext context = new PartitionContext(treeLikelihood);
try {
beautiConfig.partitionTemplate.get().createSubNet(context, false);
} catch (Exception e) {
// e.printStackTrace();
}
for (BeautiSubTemplate subTemplate : beautiConfig.subTemplates) {
try {
subTemplate.createSubNet(context, false);
} catch (Exception e) {
// e.printStackTrace();
}
}
}
}
}
}
} catch (Exception e) {
// e.printStackTrace();
}
// MCMC = parser.parseFragment(xml, true);
// mcmc.setValue(MCMC, this);
// PluginPanel.addPluginToMap(MCMC, this);
// if (xml.indexOf(XMLProducer.DO_NOT_EDIT_WARNING) > 0) {
// int start = xml.indexOf(XMLProducer.DO_NOT_EDIT_WARNING);
// int end = xml.lastIndexOf("-->");
// xml = xml.substring(start, end);
// xml = xml.replaceAll(XMLProducer.DO_NOT_EDIT_WARNING, "");
// xml = "<beast namespace='" + XMLProducer.DEFAULT_NAMESPACE + "'>" + xml + "</beast>";
// List<BEASTObject> beastObjects = parser.parseBareFragments(xml, true);
// for (BEASTObject beastObject : beastObjects) {
// PluginPanel.addPluginToMap(beastObject, this);
// }
// }
// extract alignments
determinePartitions();
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BeautiDoc method applyBeautiRules.
void applyBeautiRules(List<BeautiSubTemplate> templates, boolean isInitial, PartitionContext context) {
for (BeautiSubTemplate template : templates) {
String templateID = translatePartitionNames(template.getMainID(), context);
BEASTInterface beastObject = pluginmap.get(templateID);
// check if template is in use
if (beastObject != null) {
// if so, run through all connectors
for (BeautiConnector connector : template.connectors) {
if (connector.atInitialisationOnly()) {
if (isInitial) {
warning("connect: " + connector.toString(context) + "\n");
connect(connector, context);
}
} else if (connector.isActivated(context, posteriorPredecessors, likelihoodPredecessors, this)) {
warning("connect: " + connector.toString(context) + "\n");
try {
connect(connector, context);
} catch (Exception e) {
warning(e.getMessage());
}
} else {
warning("DISconnect: " + connector.toString(context) + "\n");
try {
disconnect(connector, context);
} catch (Exception e) {
warning(e.getMessage() + "\n");
}
}
}
}
}
}
Aggregations