use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BeautiAlignmentProvider method getXMLData.
public static BEASTInterface getXMLData(File file) {
String xml = "";
try {
// parse as BEAST 2 xml fragment
XMLParser parser = new XMLParser();
BufferedReader fin = new BufferedReader(new FileReader(file));
while (fin.ready()) {
xml += fin.readLine() + "\n";
}
fin.close();
BEASTInterface runnable = parser.parseBareFragment(xml, false);
BEASTInterface alignment = getAlignment(runnable);
alignment.initAndValidate();
return alignment;
} catch (Exception ex) {
// attempt to parse as BEAST 1 xml
try {
String ID = file.getName();
ID = ID.substring(0, ID.lastIndexOf('.')).replaceAll("\\..*", "");
BEASTInterface alignment = parseBeast1XML(ID, xml);
if (alignment != null) {
alignment.setID(file.getName().substring(0, file.getName().length() - 4).replaceAll("\\..*", ""));
}
return alignment;
} catch (Exception ex2) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Loading of " + file.getName() + " failed: " + ex.getMessage() + "\n" + ex2.getMessage());
}
return null;
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BeautiAlignmentProvider method replaceItem.
@SuppressWarnings({ "rawtypes", "unchecked" })
private void replaceItem(BeautiDoc doc, BEASTInterface oldData, BEASTInterface newData) {
doc.pluginmap.remove(newData.getID());
Set<BEASTInterface> outputs = new LinkedHashSet<>();
outputs.addAll(oldData.getOutputs());
for (BEASTInterface o : outputs) {
for (Input i : o.listInputs()) {
if (i.get() == oldData) {
i.setValue(newData, o);
} else if (i.get() != null && i.get() instanceof List) {
List list = (List) i.get();
int index = list.indexOf(oldData);
if (index >= 0) {
list.set(index, newData);
newData.getOutputs().add(o);
}
}
}
}
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class AlignmentListInputEditor method getPartition.
private String getPartition(Input<?> input) {
BEASTInterface beastObject = (BEASTInterface) input.get();
String id = beastObject.getID();
String partition = BeautiDoc.parsePartition(id);
return partition;
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class BeautiConnector method isActivated.
/**
* check that conditions in the 'if' input are met *
*/
public boolean isActivated(PartitionContext partitionContext, List<BEASTInterface> posteriorPredecessors, List<BEASTInterface> likelihoodPredecessors, BeautiDoc doc) {
if (atInitialisationOnly()) {
return false;
}
if (methodnput.get() != null) {
// if (method != null) {
try {
String fullMethod = methodnput.get();
String className = fullMethod.substring(0, fullMethod.lastIndexOf('.'));
String methodName = fullMethod.substring(fullMethod.lastIndexOf('.') + 1);
Class<?> class_ = Class.forName(className);
method = class_.getMethod(methodName, BeautiDoc.class);
method.invoke(null, doc);
} catch (Exception e) {
// ignore
}
}
boolean isActive = true;
for (int i = 0; i < conditionIDs.length; i++) {
// String id = conditionIDs[i].replaceAll("\\$\\(n\\)", partition);
String id = BeautiDoc.translatePartitionNames(conditionIDs[i], partitionContext);
BEASTInterface beastObject = doc.pluginmap.get(id);
if (beastObject == null) {
if (conditionOperations[i] != Operation.IS_NOT_AN_OPERTOR) {
return false;
}
// System.err.println("isActivated::no beastObject found");
}
// System.err.println("isActivated::found " + id);
try {
switch(conditionOperations[i]) {
case IS_IN_POSTERIOR:
if (!posteriorPredecessors.contains(beastObject)) {
// System.err.println("isActivated::is not in posterior, return false");
return false;
}
break;
case IS_IN_LIKELIHOOD:
if (!likelihoodPredecessors.contains(beastObject)) {
// System.err.println("isActivated::is not in posterior, return false");
return false;
}
break;
// System.err.println("isActivated::is in posterior");
case IS_NOT_AN_OPERTOR:
List<Operator> operators = ((MCMC) doc.mcmc.get()).operatorsInput.get();
if (operators.contains(beastObject)) {
return false;
}
break;
case EQUALS:
final Input<?> input = beastObject.getInput(conditionInputs[i]);
// System.err.println("isActivated::input " + input.get().toString() + " expected " + conditionValues[i]);
if (input.get() == null) {
if (!conditionValues[i].equals("null")) {
return false;
}
} else if (!input.get().toString().equals(conditionValues[i])) {
// System.err.println("isActivated::return false");
return false;
}
break;
case NOT_EQUALS:
final Input<?> input2 = beastObject.getInput(conditionInputs[i]);
// System.err.println("isActivated::input " + input.get().toString() + " expected " + conditionValues[i]);
if (input2.get() == null) {
if (conditionValues[i].equals("null")) {
return false;
}
} else if (input2.get().toString().equals(conditionValues[i])) {
// System.err.println("isActivated::return false");
return false;
}
break;
default:
throw new IllegalArgumentException("Unexpected operation: " + conditionOperations[i]);
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
// }
return isActive;
}
use of beast.core.BEASTInterface in project beast2 by CompEvol.
the class Beauti method addAlignmentProviderMenus.
private void addAlignmentProviderMenus(JMenu fileMenu) {
List<BeautiAlignmentProvider> providers = doc.beautiConfig.alignmentProvider;
for (BeautiAlignmentProvider provider : providers) {
AbstractAction action = new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
try {
setCursor(new Cursor(Cursor.WAIT_CURSOR));
// get user-specified alignments
List<BEASTInterface> beastObjects = provider.getAlignments(doc);
if (beastObjects != null) {
for (BEASTInterface o : beastObjects) {
if (o instanceof Alignment) {
try {
BeautiDoc.createTaxonSet((Alignment) o, doc);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
doc.connectModel();
doc.fireDocHasChanged();
if (beastObjects != null) {
for (BEASTInterface o : beastObjects) {
if (o instanceof MRCAPrior) {
doc.addMRCAPrior((MRCAPrior) o);
}
}
}
a_save.setEnabled(true);
a_saveas.setEnabled(true);
} catch (Exception exx) {
exx.printStackTrace();
String text = "Something went wrong importing the alignment:\n";
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.append(exx.getMessage());
textArea.setSize(textArea.getPreferredSize().width, 1);
textArea.setOpaque(false);
JOptionPane.showMessageDialog(null, textArea, "Error importing alignment", JOptionPane.WARNING_MESSAGE);
}
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
};
String providerInfo = provider.toString().replaceAll("Add ", "Add partition for ");
action.putValue(Action.SHORT_DESCRIPTION, providerInfo);
action.putValue(Action.LONG_DESCRIPTION, providerInfo);
action.putValue(Action.NAME, provider.toString());
fileMenu.add(action);
}
}
Aggregations