use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class ThreadedTreeLikelihood method duplicate.
/**
* create new instance of src object, connecting all inputs from src object
* Note if input is a SubstModel, it is duplicated as well.
* @param src object to be copied
* @param i index used to extend ID with.
* @return copy of src object
*/
private Object duplicate(BEASTInterface src, int i) {
if (src == null) {
return null;
}
BEASTInterface copy;
try {
copy = src.getClass().newInstance();
copy.setID(src.getID() + "_" + i);
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException("Programmer error: every object in the model should have a default constructor that is publicly accessible: " + src.getClass().getName());
}
for (Input<?> input : src.listInputs()) {
if (input.get() != null) {
if (input.get() instanceof List) {
// ((List)copy.getInput(input.getName())).clear();
for (Object o : (List<?>) input.get()) {
if (o instanceof BEASTInterface) {
// make sure it is not already in the list
copy.setInputValue(input.getName(), o);
}
}
} else if (input.get() instanceof SubstitutionModel) {
// duplicate subst models
BEASTInterface substModel = (BEASTInterface) duplicate((BEASTInterface) input.get(), i);
copy.setInputValue(input.getName(), substModel);
} else {
// it is some other value
copy.setInputValue(input.getName(), input.get());
}
}
}
copy.initAndValidate();
return copy;
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class XMLProducerTest method test_ThatNexusExamplesProduces.
// @Test
// public void test_ThatXmlExamplesProduces() {
// System.setProperty("java.only", "true");
// String dir = System.getProperty("user.dir") + "/examples";
// //String dir = "/tmp";
// List<String> exceptions = new ArrayList<String>();
// exceptions.add("testExponentialGrowth.xml");
// test_ThatXmlExamplesProduces(dir, exceptions);
// }
@Test
public void test_ThatNexusExamplesProduces() {
try {
String dirName = System.getProperty("user.dir") + "/examples/nexus";
System.out.println("Test Nexus Examples in " + dirName);
File exampleDir = new File(dirName);
String[] exampleFiles = exampleDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".nex") || name.endsWith(".nxs");
}
});
List<String> failedFiles = new ArrayList<>();
for (String fileName : exampleFiles) {
System.out.println("Processing " + fileName);
NexusParser parser = new NexusParser();
try {
parser.parseFile(new File(dirName + "/" + fileName));
} catch (Exception e) {
System.out.println("ExampleNexusParsing::Failed for " + fileName + ": " + e.getMessage());
failedFiles.add(fileName);
}
XMLProducer producer = new XMLProducer();
BEASTInterface o = parser.m_alignment;
if (o != null) {
String xml = producer.toXML(o);
// FileWriter outfile = new FileWriter(new File("/tmp/XMLProducerTest.xml"));
// outfile.write(xml);
// outfile.close();
XMLParser parser2 = new XMLParser();
try {
BEASTInterface o2 = parser2.parseFragment(xml, false);
System.out.println(o2);
} catch (Exception e) {
System.out.println("test_ThatNexusExamplesProduces::Failed for " + fileName + ": " + e.getMessage());
failedFiles.add(fileName);
}
}
System.out.println("Done " + fileName);
}
if (failedFiles.size() > 0) {
System.out.println("\test_ThatNexusExamplesProduces::Failed for : " + failedFiles.toString());
} else {
System.out.println("\test_ThatNexusExamplesProduces::Success");
}
assertTrue(failedFiles.toString(), failedFiles.size() == 0);
} catch (Exception e) {
System.out.println("exception thrown ");
System.out.println(e.getMessage());
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BEASTObjectDialog method main.
/**
* rudimentary test *
*/
public static void main(String[] args) {
BEASTObjectDialog dlg = null;
try {
if (args.length == 0) {
dlg = new BEASTObjectDialog(new BEASTObjectPanel(new MCMC(), Runnable.class, null), null);
} else if (args[0].equals("-x")) {
StringBuilder text = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = new Scanner(new File(args[1]));
try {
while (scanner.hasNextLine()) {
text.append(scanner.nextLine() + NL);
}
} finally {
scanner.close();
}
BEASTInterface beastObject = new beast.util.XMLParser().parseBareFragment(text.toString(), false);
dlg = new BEASTObjectDialog(new BEASTObjectPanel(beastObject, beastObject.getClass(), null), null);
} else if (args.length == 1) {
dlg = new BEASTObjectDialog(new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[0]), null), null);
} else if (args.length == 2) {
dlg = new BEASTObjectDialog(new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[1]), null), null);
} else {
throw new IllegalArgumentException("Incorrect number of arguments");
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Usage: " + BEASTObjectDialog.class.getName() + " [-x file ] [class [type]]\n" + "where [class] (optional, default MCMC) is a BEASTObject to edit\n" + "and [type] (optional only if class is specified, default Runnable) the type of the BEASTObject.\n" + "for example\n" + "");
System.exit(1);
}
dlg.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
if (dlg.showDialog()) {
BEASTInterface beastObject = dlg.m_panel.m_beastObject;
String xml = new XMLProducer().modelToXML(beastObject);
System.out.println(xml);
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class TreeDistributionInputEditor method init.
@Override
public void init(Input<?> input, BEASTInterface beastObject, int listItemNr, ExpandOption isExpandOption, boolean addButtons) {
m_bAddButtons = addButtons;
m_input = input;
m_beastObject = beastObject;
this.itemNr = listItemNr;
Box itemBox = Box.createHorizontalBox();
TreeDistribution distr = (TreeDistribution) beastObject;
String text = "";
if (distr.treeInput.get() != null) {
text += distr.treeInput.get().getID();
} else {
text += distr.treeIntervalsInput.get().treeInput.get().getID();
}
JLabel label = new JLabel(text);
Font font = label.getFont();
Dimension size = new Dimension(font.getSize() * 200 / 12, font.getSize() * 2);
label.setMinimumSize(size);
label.setPreferredSize(size);
itemBox.add(label);
// List<String> availableBEASTObjects =
// PluginPanel.getAvailablePlugins(m_input, m_beastObject, null);
List<BeautiSubTemplate> availableBEASTObjects = doc.getInputEditorFactory().getAvailableTemplates(m_input, m_beastObject, null, doc);
// make sure we are dealing with a TreeDistribution
for (int i = availableBEASTObjects.size() - 1; i >= 0; i--) {
BeautiSubTemplate t = availableBEASTObjects.get(i);
Class<?> c = t._class;
if (!(TreeDistribution.class.isAssignableFrom(c))) {
availableBEASTObjects.remove(i);
}
}
JComboBox<BeautiSubTemplate> comboBox = new JComboBox<>(availableBEASTObjects.toArray(new BeautiSubTemplate[] {}));
comboBox.setName("TreeDistribution");
for (int i = availableBEASTObjects.size() - 1; i >= 0; i--) {
if (!TreeDistribution.class.isAssignableFrom(availableBEASTObjects.get(i)._class)) {
availableBEASTObjects.remove(i);
}
}
String id = distr.getID();
try {
// id = BeautiDoc.parsePartition(id);
id = id.substring(0, id.indexOf('.'));
} catch (Exception e) {
throw new RuntimeException("Improperly formatted ID: " + distr.getID());
}
for (BeautiSubTemplate template : availableBEASTObjects) {
if (template.matchesName(id)) {
// getMainID().replaceAll(".\\$\\(n\\)",
// "").equals(id)) {
comboBox.setSelectedItem(template);
}
}
comboBox.addActionListener(e -> {
m_e = e;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
@SuppressWarnings("unchecked") JComboBox<BeautiSubTemplate> currentComboBox = (JComboBox<BeautiSubTemplate>) m_e.getSource();
@SuppressWarnings("unchecked") List<BEASTInterface> list = (List<BEASTInterface>) m_input.get();
BeautiSubTemplate template = (BeautiSubTemplate) currentComboBox.getSelectedItem();
PartitionContext partitionContext = doc.getContextFor(list.get(itemNr));
try {
template.createSubNet(partitionContext, list, itemNr, true);
} catch (Exception ex) {
ex.printStackTrace();
}
sync();
refreshPanel();
}
});
});
itemBox.add(comboBox);
itemBox.add(Box.createGlue());
m_validateLabel = new SmallLabel("x", new Color(200, 0, 0));
m_validateLabel.setVisible(false);
validateInput();
itemBox.add(m_validateLabel);
add(itemBox);
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BEASTObjectInputEditor method addComboBox.
// expandedInit
/**
* add combobox with BEASTObjects to choose from
* On choosing a new value, create beastObject (if is not already an object)
* Furthermore, if expanded, update expanded inputs
*/
protected void addComboBox(JComponent box, Input<?> input, BEASTInterface beastObject0) {
if (itemNr >= 0) {
box.add(new JLabel(beastObject0.getID()));
box.add(Box.createGlue());
return;
}
List<BeautiSubTemplate> availableTemplates = doc.getInputEditorFactory().getAvailableTemplates(m_input, m_beastObject, null, doc);
if (availableTemplates.size() > 0) {
// if (m_input.getRule() != Validate.REQUIRED || beastObject == null) {
// availableBEASTObjects.add(NO_VALUE);
// }
// for (int i = 0; i < availableBEASTObjects.size(); i++) {
// String beastObjectName = availableBEASTObjects.get(i);
// if (beastObjectName.startsWith("new ")) {
// beastObjectName = beastObjectName.substring(beastObjectName.lastIndexOf('.'));
// availableBEASTObjects.set(i, beastObjectName);
// }
//
// }
m_selectBEASTObjectBox = new JComboBox<>(availableTemplates.toArray());
m_selectBEASTObjectBox.setName(input.getName());
Object o = input.get();
if (itemNr >= 0) {
o = ((List<?>) o).get(itemNr);
}
String id2;
if (o == null) {
id2 = beastObject0.getID();
} else {
id2 = ((BEASTInterface) o).getID();
}
if (id2.indexOf('.') >= 0) {
id2 = id2.substring(0, id2.indexOf('.'));
}
for (BeautiSubTemplate template : availableTemplates) {
if (template.matchesName(id2)) {
m_selectBEASTObjectBox.setSelectedItem(template);
}
}
m_selectBEASTObjectBox.addActionListener(e -> {
// SwingUtilities.invokeLater(new Runnable() {
//
// @Override
// public void run() {
// get a handle of the selected beastObject
BeautiSubTemplate selected = (BeautiSubTemplate) m_selectBEASTObjectBox.getSelectedItem();
BEASTInterface beastObject = (BEASTInterface) m_input.get();
String id = beastObject.getID();
String partition = id.indexOf('.') >= 0 ? id.substring(id.indexOf('.') + 1) : "";
if (partition.indexOf(':') >= 0) {
partition = id.substring(id.indexOf(':') + 1);
}
if (selected.equals(NO_VALUE)) {
beastObject = null;
// } else if (PluginPanel.g_plugins.containsKey(newID)) {
// beastObject = PluginPanel.g_plugins.get(newID);
} else {
try {
beastObject = selected.createSubNet(doc.getContextFor(beastObject), m_beastObject, m_input, true);
// PluginPanel.addPluginToMap(beastObject);
// tricky: try to connect up new inputs with old inputs of existing name
// beastObject oldPlugin = (beastObject) m_input.get();
// for (Input<?> oldInput: oldPlugin.listInputs()) {
// String name = oldInput.getName();
// try {
// Input<?> newInput = beastObject.getInput(name);
// if (newInput.get() instanceof List) {
// List<?> values = (List<?>) oldInput.get();
// for (Object value: values) {
// newInput.setValue(value, beastObject);
// }
// } else {
// newInput.setValue(oldInput.get(), beastObject);
// }
// } catch (Exception ex) {
// // ignore
// }
// }
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Could not select beastObject: " + ex.getClass().getName() + " " + ex.getMessage());
}
}
try {
if (beastObject == null) {
m_selectBEASTObjectBox.setSelectedItem(NO_VALUE);
// is this input expanded?
if (m_expansionBox != null) {
// remove items from Expansion Box, if any
for (int i = 1; i < m_expansionBox.getComponentCount(); i++) {
m_expansionBox.remove(i);
}
} else {
// not expanded
if (m_bAddButtons && m_editBEASTObjectButton != null) {
m_editBEASTObjectButton.setEnabled(false);
}
}
} else {
if (!m_input.canSetValue(beastObject, m_beastObject)) {
throw new IllegalArgumentException("Cannot set input to this value");
}
// // get handle on ID of the beastObject, and add to combobox if necessary
// String id = beastObject.getID();
// // TODO RRB: have to remove ID first, then add it
// // The addition is necessary to make the items in the expansionBox scale and show up
// // Is there another way?
// m_selectPluginBox.removeItem(id);
// m_selectPluginBox.addItem(id);
// m_selectPluginBox.setSelectedItem(id);
id = beastObject.getID();
if (id.indexOf('.') != -1) {
id = id.substring(0, id.indexOf('.'));
}
for (int i = 0; i < m_selectBEASTObjectBox.getItemCount(); i++) {
BeautiSubTemplate template = (BeautiSubTemplate) m_selectBEASTObjectBox.getItemAt(i);
if (template.getMainID().replaceAll(".\\$\\(n\\)", "").equals(id) || template.getMainID().replaceAll(".s:\\$\\(n\\)", "").equals(id) || template.getMainID().replaceAll(".c:\\$\\(n\\)", "").equals(id) || template.getMainID().replaceAll(".t:\\$\\(n\\)", "").equals(id)) {
m_selectBEASTObjectBox.setSelectedItem(template);
}
}
}
setValue(beastObject);
if (m_expansionBox != null) {
// remove items from Expansion Box
for (int i = 1; i < m_expansionBox.getComponentCount(); ) {
m_expansionBox.remove(i);
}
// add new items to Expansion Box
if (beastObject != null) {
doc.getInputEditorFactory().addInputs(m_expansionBox, beastObject, _this, _this, doc);
}
} else {
// it is not expanded, enable the edit button
if (m_bAddButtons && m_editBEASTObjectButton != null) {
m_editBEASTObjectButton.setEnabled(true);
}
validateInput();
}
sync();
refreshPanel();
} catch (Exception ex) {
id = ((BEASTInterface) m_input.get()).getID();
m_selectBEASTObjectBox.setSelectedItem(id);
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Could not change beastObject: " + ex.getClass().getName() + " " + ex.getMessage());
}
});
m_selectBEASTObjectBox.setToolTipText(input.getHTMLTipText());
int fontsize = m_selectBEASTObjectBox.getFont().getSize();
m_selectBEASTObjectBox.setMaximumSize(new Dimension(1024, 200 * fontsize / 13));
box.add(m_selectBEASTObjectBox);
}
}
Aggregations