use of beast.core.StateNodeInitialiser in project beast2 by CompEvol.
the class StateNodeInitialiserListInputEditor method addPluginItem.
@Override
protected InputEditor addPluginItem(Box itemBox, BEASTInterface beastObject) {
final StateNodeInitialiser currentInitialiser = (StateNodeInitialiser) beastObject;
Input initialInput = beastObject.getInput("initial");
List<BeautiSubTemplate> sAvailablePlugins = doc.getInputEditorFactory().getAvailableTemplates(initialInput, (BEASTInterface) beastObject, null, doc);
JComboBox<?> comboBox = null;
if (sAvailablePlugins.size() > 0) {
sAvailablePlugins.remove(sAvailablePlugins.size() - 1);
comboBox = new JComboBox<>(sAvailablePlugins.toArray());
String sID = beastObject.getID();
try {
sID = sID.substring(0, sID.indexOf('.'));
} catch (Exception e) {
throw new RuntimeException("Improperly formatted ID: " + sID);
}
for (BeautiSubTemplate template : sAvailablePlugins) {
if (template.matchesName(sID)) {
comboBox.setSelectedItem(template);
}
}
comboBox.setName("Initialiser");
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JComboBox<?> currentComboBox = (JComboBox<?>) e.getSource();
BeautiSubTemplate template = (BeautiSubTemplate) currentComboBox.getSelectedItem();
PartitionContext partitionContext;
partitionContext = doc.getContextFor(beastObject);
try {
Object o = template.createSubNet(partitionContext, true);
StateNodeInitialiser newInitialiser = (StateNodeInitialiser) o;
List<StateNodeInitialiser> inits = (List<StateNodeInitialiser>) m_input.get();
int i = inits.indexOf(currentInitialiser);
inits.set(i, newInitialiser);
System.out.println(inits.size());
} catch (Exception ex) {
ex.printStackTrace();
}
sync();
refreshPanel();
}
});
}
});
}
String name = beastObject.getID();
Object o = beastObject.getInput("initial").get();
if (o instanceof BEASTInterface) {
name = ((BEASTInterface) o).getID();
}
if (name == null || name.length() == 0) {
name = beastObject.getClass().getName();
name = name.substring(name.lastIndexOf('.') + 1);
}
JLabel label = new JLabel("Initial " + name + ":");
itemBox.add(Box.createRigidArea(new Dimension(5, 1)));
itemBox.add(label);
if (comboBox != null) {
itemBox.add(comboBox);
}
itemBox.add(Box.createHorizontalGlue());
return this;
}
use of beast.core.StateNodeInitialiser in project MultiTypeTree by tgvaughan.
the class MultiTypeTree method initAndValidate.
@Override
public void initAndValidate() {
if (m_initial.get() != null && !(this instanceof StateNodeInitialiser)) {
if (!(m_initial.get() instanceof MultiTypeTree)) {
throw new IllegalArgumentException("Attempted to initialise " + "multi-type tree with regular tree object.");
}
MultiTypeTree other = (MultiTypeTree) m_initial.get();
root = other.root.copy();
nodeCount = other.nodeCount;
internalNodeCount = other.internalNodeCount;
leafNodeCount = other.leafNodeCount;
}
if (nodeCount < 0) {
if (m_taxonset.get() != null) {
// make a caterpillar
List<String> sTaxa = m_taxonset.get().asStringList();
Node left = new MultiTypeNode();
left.labelNr = 0;
left.height = 0;
left.setID(sTaxa.get(0));
for (int i = 1; i < sTaxa.size(); i++) {
Node right = new MultiTypeNode();
right.labelNr = i;
right.height = 0;
right.setID(sTaxa.get(i));
Node parent = new MultiTypeNode();
parent.labelNr = sTaxa.size() + i - 1;
parent.height = i;
left.parent = parent;
parent.setLeft(left);
right.parent = parent;
parent.setRight(right);
left = parent;
}
root = left;
leafNodeCount = sTaxa.size();
nodeCount = leafNodeCount * 2 - 1;
internalNodeCount = leafNodeCount - 1;
} else {
// make dummy tree with a single root node
root = new MultiTypeNode();
root.labelNr = 0;
root.labelNr = 0;
root.m_tree = this;
nodeCount = 1;
internalNodeCount = 0;
leafNodeCount = 1;
}
}
if (nodeCount >= 0) {
initArrays();
}
typeLabel = typeLabelInput.get();
typeSet = typeSetInput.get();
processTraits(m_traitList.get());
// Ensure tree is compatible with traits.
if (hasDateTrait())
adjustTreeNodeHeights(root);
}
use of beast.core.StateNodeInitialiser in project beast2 by CompEvol.
the class StateNodeInitialiserListInputEditor method customConnector.
public static boolean customConnector(BeautiDoc doc) {
// scrub Tree initialisers
// 0. collect state node info
List<StateNodeInitialiser> inits = ((MCMC) doc.mcmc.get()).initialisersInput.get();
State state = ((MCMC) doc.mcmc.get()).startStateInput.get();
List<StateNode> stateNodes = state.stateNodeInput.get();
List<Tree> trees = new ArrayList<>();
for (StateNode s : stateNodes) {
if (s instanceof Tree) {
trees.add((Tree) s);
}
}
List<List<StateNode>> initStateNodes = new ArrayList<>();
for (StateNodeInitialiser init : inits) {
List<StateNode> initStateNodes0 = new ArrayList<>();
init.getInitialisedStateNodes(initStateNodes0);
for (int i = initStateNodes0.size() - 1; i >= 0; i--) {
if (!(initStateNodes0.get(i) instanceof Tree)) {
initStateNodes0.remove(i);
}
}
initStateNodes.add(initStateNodes0);
}
// 1. remove initialisers that have no stateNode in state
for (int i = inits.size() - 1; i >= 0; i--) {
boolean found = false;
for (StateNode stateNode : initStateNodes.get(i)) {
if (trees.contains(stateNode)) {
found = true;
break;
}
}
if (!found) {
inits.remove(i);
initStateNodes.remove(i);
}
}
// 2. remove initialisers that share stateNodes
for (int i = inits.size() - 1; i >= 0; i--) {
for (int j = i - 1; j >= 0; j--) {
boolean found = false;
for (StateNode stateNode : initStateNodes.get(i)) {
if (initStateNodes.get(j).contains(stateNode)) {
found = true;
break;
}
}
if (found) {
inits.remove(i);
initStateNodes.remove(i);
}
}
}
// 3. add RandomTree for those trees not having a stateNodeInitialiser
boolean[] hasInitialiser = new boolean[trees.size()];
for (int i = inits.size() - 1; i >= 0; i--) {
for (StateNode stateNode : initStateNodes.get(i)) {
int k = trees.indexOf(stateNode);
if (k >= 0) {
hasInitialiser[k] = true;
break;
}
}
}
for (int i = 0; i < hasInitialiser.length; i++) {
if (!hasInitialiser[i]) {
for (BeautiSubTemplate tmp : doc.beautiConfig.subTemplates) {
if (tmp.getID().equals("RandomTree")) {
PartitionContext partition = doc.getContextFor(trees.get(i));
Object o = tmp.createSubNet(partition, false);
inits.add((StateNodeInitialiser) o);
}
}
}
}
return true;
}
Aggregations