use of beast.util.XMLParser in project beast2 by CompEvol.
the class BeautiDoc method processBeautiConfig.
void processBeautiConfig(Document doc) throws XMLParserException, TransformerException {
// find configuration elements, process and remove
NodeList nodes = doc.getElementsByTagName("beauticonfig");
Node topNode = doc.getElementsByTagName("*").item(0);
String nameSpaceStr = XMLParser.getAttribute(topNode, "namespace");
for (int configElementIndex = 0; configElementIndex < nodes.getLength(); configElementIndex++) {
Node configElement = nodes.item(configElementIndex);
String xml = nodeToString(configElement);
XMLParser parser = new XMLParser();
parser.setNameSpace(nameSpaceStr);
parser.parseBareFragment(xml, true);
configElement.getParentNode().removeChild(configElement);
}
}
use of beast.util.XMLParser 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.util.XMLParser 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.util.XMLParser in project beast2 by CompEvol.
the class BeautiSubTemplate method createSubNet.
private BEASTInterface createSubNet(PartitionContext context, /*BeautiDoc doc,*/
HashMap<String, BEASTInterface> idMap, boolean init) {
subNetDepth++;
if (subNetDepth > 10) {
// looks like we cannot find what we are looking for
throw new IllegalArgumentException("Potential programmer error: It looks like there is a required input that was not specified in the tenmplate");
}
// wrap in a beast element with appropriate name spaces
String _sXML = "<beast version='2.0' \n" + "namespace='beast.app.beauti:beast.core:beast.evolution.branchratemodel:beast.evolution.speciation:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihood:beast.evolution:beast.math.distributions'>\n" + xml + "</beast>\n";
// resolve alignment references
_sXML = _sXML.replaceAll("idref=[\"']data['\"]", "idref='" + context.partition + "'");
_sXML = _sXML.replaceAll("[\"']@data['\"]", "'@" + context.partition + "'");
// ensure uniqueness of IDs
// _sXML.replaceAll("\\$\\(n\\)", partition);
_sXML = BeautiDoc.translatePartitionNames(_sXML, context);
XMLParser parser = new XMLParser();
parser.setRequiredInputProvider(doc, context);
List<BEASTInterface> beastObjects = null;
try {
beastObjects = parser.parseTemplate(_sXML, idMap, true);
for (BEASTInterface beastObject : beastObjects) {
doc.addPlugin(beastObject);
try {
Log.warning.println("Adding " + beastObject.getClass().getName() + " " + beastObject);
} catch (Exception e) {
Log.err.println("Adding " + beastObject.getClass().getName());
}
}
for (BeautiConnector connector : connectors) {
if (init && connector.atInitialisationOnly()) {
// ||
doc.connect(connector, context);
}
// System.out.println(connector.sourceID + " == " + connector.targetID);
if (connector.targetID != null && connector.targetID.equals("prior")) {
Log.warning.println(">>> No description for connector " + connector.sourceID + " == " + connector.targetID);
}
if (connector.getTipText() != null) {
String ID = BeautiDoc.translatePartitionNames(connector.sourceID, context);
String tipText = BeautiDoc.translatePartitionNames(connector.getTipText(), context).trim().replaceAll("\\s+", " ");
// System.out.println(ID + " -> " + tipText);
doc.tipTextMap.put(ID, tipText);
}
}
if (suppressedInputs.get() != null) {
String[] inputs = suppressedInputs.get().split(",");
for (String input : inputs) {
input = input.trim();
doc.beautiConfig.suppressBEASTObjects.add(input);
}
}
if (inlineInput.get() != null) {
String[] inputs = inlineInput.get().split(",");
for (String input : inputs) {
input = input.trim();
doc.beautiConfig.inlineBEASTObject.add(input);
}
}
if (collapsedInput.get() != null) {
String[] inputs = collapsedInput.get().split(",");
for (String input : inputs) {
input = input.trim();
doc.beautiConfig.collapsedBEASTObjects.add(input);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (mainID.equals("[top]")) {
subNetDepth--;
return beastObjects.get(0);
}
String id = mainID;
// id.replaceAll("\\$\\(n\\)", partition);
id = BeautiDoc.translatePartitionNames(id, context);
BEASTInterface beastObject = doc.pluginmap.get(id);
if (this == doc.beautiConfig.partitionTemplate.get()) {
// HACK: need to make sure the subst model is of the correct type
BEASTInterface treeLikelihood = doc.pluginmap.get("treeLikelihood." + context.partition);
if (treeLikelihood != null && ((GenericTreeLikelihood) treeLikelihood).siteModelInput.get() instanceof SiteModel.Base) {
SiteModel.Base siteModel = (SiteModel.Base) ((GenericTreeLikelihood) treeLikelihood).siteModelInput.get();
SubstitutionModel substModel = siteModel.substModelInput.get();
try {
if (!siteModel.canSetSubstModel(substModel)) {
setUpSubstModel(siteModel, context);
}
} catch (Exception e) {
setUpSubstModel(siteModel, context);
}
}
// HACK2: rename file name for trace log if it has the default value
Logger logger = (Logger) doc.pluginmap.get("tracelog");
if (logger != null) {
String fileName = logger.fileNameInput.get();
if (fileName.startsWith("beast.") && treeLikelihood != null) {
Alignment data = ((GenericTreeLikelihood) treeLikelihood).dataInput.get();
while (data instanceof FilteredAlignment) {
data = ((FilteredAlignment) data).alignmentInput.get();
}
fileName = data.getID() + fileName.substring(5);
try {
logger.fileNameInput.setValue(fileName, logger);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
subNetDepth--;
// System.err.println(new XMLProducer().toXML(beastObject));
return beastObject;
}
use of beast.util.XMLParser in project beast2 by CompEvol.
the class BeautiBase method makeSureXMLParses.
void makeSureXMLParses() {
warning("Make sure that XML that BEAUti produces parses");
File XMLFile = new File(org.fest.util.Files.temporaryFolder() + "/x.xml");
if (XMLFile.exists()) {
XMLFile.delete();
}
saveFile("" + org.fest.util.Files.temporaryFolder(), "x.xml");
// JFileChooserFixture fileChooser = findFileChooser().using(robot());
// fileChooser.setCurrentDirectory(org.fest.util.Files.temporaryFolder());
// fileChooser.selectFile(new File("x.xml")).approve();
XMLParser parser = new XMLParser();
XMLFile = new File(org.fest.util.Files.temporaryFolder() + "/x.xml");
try {
parser.parseFile(XMLFile);
} catch (Exception e) {
e.printStackTrace();
assertThat(0).as("Parser exception: " + e.getMessage()).isEqualTo(1);
}
}
Aggregations