use of beast.core.StateNode in project beast2 by CompEvol.
the class BeautiDoc method deepCopyPlugin.
/**
* Create a deep copy of a beastObject, but in a different partition context
* First, find all beastObjects that are predecessors of the beastObject to be copied
* that are ancestors of StateNodes
*
* @param beastObject
* @param parent
* @return
*/
public static BEASTInterface deepCopyPlugin(BEASTInterface beastObject, BEASTInterface parent, MCMC mcmc, PartitionContext oldContext, PartitionContext newContext, BeautiDoc doc, List<BEASTInterface> tabooList) {
/**
* taboo = list of beastObjects that should not be copied *
*/
Set<BEASTInterface> taboo = new HashSet<>();
taboo.add(parent);
// add state
taboo.add(mcmc.startStateInput.get());
// add likelihood and prior
if (mcmc.posteriorInput.get() instanceof CompoundDistribution) {
for (Distribution distr : ((CompoundDistribution) mcmc.posteriorInput.get()).pDistributions.get()) {
if (distr instanceof CompoundDistribution) {
taboo.add(distr);
}
}
}
// add posterior
taboo.add(mcmc.posteriorInput.get());
// parent of operators
taboo.add(mcmc);
// add loggers
taboo.addAll(mcmc.loggersInput.get());
// add exception for *BEAST logger (perhaps need to be generalised?)
if (doc.pluginmap.containsKey("SpeciesTreeLoggerX")) {
taboo.add(doc.pluginmap.get("SpeciesTreeLoggerX"));
}
// add trees
for (StateNode node : mcmc.startStateInput.get().stateNodeInput.get()) {
if (node instanceof Tree) {
taboo.add(node);
}
}
// add MRCAPriors
for (String id : doc.pluginmap.keySet()) {
BEASTInterface o = doc.pluginmap.get(id);
if (o instanceof MRCAPrior) {
taboo.add(o);
}
}
if (tabooList != null) {
taboo.addAll(tabooList);
}
// find predecessors of beastObject to be copied
List<BEASTInterface> predecessors = new ArrayList<>();
collectPredecessors(beastObject, predecessors);
// find ancestors of StateNodes that are predecessors + the beastObject
// itself
Set<BEASTInterface> ancestors = new HashSet<>();
collectAncestors(beastObject, ancestors, taboo);
Log.info.print(Arrays.toString(ancestors.toArray()));
for (BEASTInterface beastObject2 : predecessors) {
if (beastObject2 instanceof StateNode) {
Set<BEASTInterface> ancestors2 = new HashSet<>();
collectAncestors(beastObject2, ancestors2, taboo);
ancestors.addAll(ancestors2);
} else if (beastObject2 instanceof Alignment || beastObject2 instanceof FilteredAlignment) {
for (Object output : beastObject2.getOutputs()) {
if (!taboo.contains(output)) {
Set<BEASTInterface> ancestors2 = new HashSet<>();
collectAncestors((BEASTInterface) output, ancestors2, taboo);
ancestors.addAll(ancestors2);
}
}
}
}
// collect priors
predecessors.addAll(ancestors);
for (BEASTInterface o : predecessors) {
if (o instanceof Prior) {
List<BEASTInterface> priorPredecessors = new ArrayList<>();
collectPredecessors(o, priorPredecessors);
ancestors.addAll(priorPredecessors);
}
}
Log.info.print(Arrays.toString(predecessors.toArray()));
for (BEASTInterface p : ancestors) {
Log.info.print("(");
try {
for (BEASTInterface p2 : p.listActiveBEASTObjects()) {
if (ancestors.contains(p2)) {
Log.info.print(p2.getID() + " ");
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
Log.info.print(") ");
Log.info.println(p.getID());
}
// now the ancestors contain all beastObjects to be copied
// make a copy of all individual BEASTObjects, before connecting them up
Map<String, BEASTInterface> copySet = new HashMap<>();
for (BEASTInterface beastObject2 : ancestors) {
String id = beastObject2.getID();
if (id == null) {
id = beastObject.getClass().getName().replaceAll(".*\\.", "");
int i = 0;
while (doc.pluginmap.containsKey(id + "." + i)) {
i++;
}
id = id + "." + i;
beastObject2.setID(id);
}
String copyID = renameId(id, oldContext, newContext);
if (!id.equals(copyID)) {
if (doc.pluginmap.containsKey(copyID)) {
BEASTInterface org = doc.pluginmap.get(copyID);
copySet.put(id, org);
} else {
BEASTInterface copy;
try {
copy = beastObject2.getClass().newInstance();
copy.setID(copyID);
copySet.put(id, copy);
} 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");
}
}
}
Log.warning.println("Copy: " + id + " -> " + copyID);
}
// set all inputs of copied beastObjects + outputs to taboo
for (BEASTInterface beastObject2 : ancestors) {
String id = beastObject2.getID();
BEASTInterface copy = copySet.get(id);
if (copy != null) {
Log.warning.println("Processing: " + id + " -> " + copy.getID());
// set inputs
for (Input<?> input : beastObject2.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) {
BEASTInterface value = getCopyValue((BEASTInterface) o, copySet, oldContext, newContext, doc);
// make sure it is not already in the list
Object o2 = copy.getInput(input.getName()).get();
boolean alreadyInList = false;
if (o2 instanceof List) {
List<?> currentList = (List<?>) o2;
for (Object v : currentList) {
if (v == value) {
alreadyInList = true;
break;
}
}
}
if (!alreadyInList) {
// add to the list
copy.setInputValue(input.getName(), value);
}
} else {
// it is a primitive value
if (copy instanceof Parameter.Base && input.getName().equals("value")) {
// // prevent appending to parameter values
Parameter.Base<?> p = ((Parameter.Base<?>) copy);
((List<?>) p.valuesInput.get()).clear();
}
copy.setInputValue(input.getName(), input.get());
}
}
} else if (input.get() instanceof BEASTInterface) {
// handle BEASTObject
BEASTInterface value = getCopyValue((BEASTInterface) input.get(), copySet, oldContext, newContext, doc);
copy.setInputValue(input.getName(), value);
} else if (input.get() instanceof String) {
// may need to replace partition info
String s = (String) input.get();
s = s.replaceAll("\\.c:[a-zA-Z0-9_]*", ".c:" + newContext.clockModel);
s = s.replaceAll("\\.s:[a-zA-Z0-9_]*", ".s:" + newContext.siteModel);
s = s.replaceAll("\\.t:[a-zA-Z0-9_]*", ".t:" + newContext.tree);
copy.setInputValue(input.getName(), s);
} else {
// it is a primitive value
copy.setInputValue(input.getName(), input.get());
}
}
}
// set outputs
for (Object output : beastObject2.getOutputs()) {
if (taboo.contains(output) && output != parent) {
BEASTInterface output2 = getCopyValue((BEASTInterface) output, copySet, oldContext, newContext, doc);
for (Input<?> input : ((BEASTInterface) output).listInputs()) {
// do not add state node initialisers automatically
if (input.get() instanceof List && // do not update state node initialisers
!(taboo.contains(output2) && input.getName().equals("init"))) {
List<?> list = (List<?>) input.get();
if (list.contains(beastObject2)) {
List<?> list2 = (List<?>) output2.getInput(input.getName()).get();
if (!list2.contains(copy)) {
output2.setInputValue(input.getName(), copy);
}
}
}
}
}
}
copySet.put(id, copy);
// Log.warning.println(base.operatorsAsString());
}
}
// deep copy must be obtained from copyset, before sorting
// since the sorting changes (deletes items) from the copySet map
BEASTInterface deepCopy = copySet.get(beastObject.getID());
// first need to sort copySet by topology, before we can initAndValidate
// them
List<BEASTInterface> sorted = new ArrayList<>();
Collection<BEASTInterface> values = copySet.values();
while (values.size() > 0) {
for (BEASTInterface copy : values) {
boolean found = false;
for (BEASTInterface beastObject2 : copy.listActiveBEASTObjects()) {
if (values.contains(beastObject2)) {
found = true;
break;
}
}
if (!found) {
sorted.add(copy);
}
}
values.remove(sorted.get(sorted.size() - 1));
}
// initialise copied beastObjects
Set<BEASTInterface> done = new HashSet<>();
for (BEASTInterface copy : sorted) {
try {
if (!done.contains(copy)) {
copy.initAndValidate();
done.add(copy);
}
} catch (Exception e) {
// ignore
Log.warning.print(e.getMessage());
}
if (doc != null) {
doc.addPlugin(copy);
}
}
doc.scrubAll(true, false);
return deepCopy;
}
use of beast.core.StateNode in project beast2 by CompEvol.
the class BeautiBase method assertStateEquals.
void assertStateEquals(String... ids) {
System.err.println("assertStateEquals");
State state = (State) doc.pluginmap.get("state");
List<StateNode> stateNodes = state.stateNodeInput.get();
asserListsEqual(stateNodes, ids);
}
use of beast.core.StateNode in project beast2 by CompEvol.
the class TestOperator method register.
public static void register(Operator operator, final Object... operands) {
HashMap<String, StateNode> operandsMap;
operandsMap = new HashMap<String, StateNode>();
if (operands.length % 2 == 1) {
throw new RuntimeException("Expected even number of arguments, name-value pairs");
}
for (int i = 0; i < operands.length; i += 2) {
if (operands[i] instanceof String) {
final String name = (String) operands[i];
if (operands[i + 1] instanceof StateNode) {
final StateNode node = (StateNode) operands[i + 1];
operandsMap.put(name, node);
} else {
throw new IllegalArgumentException("Expected a StateNode in " + (i + 1) + "th argument ");
}
} else {
throw new IllegalArgumentException("Expected a String in " + i + "th argument ");
}
}
State state = new State();
state.initByName("stateNode", new ArrayList<StateNode>(operandsMap.values()));
state.initialise();
Object[] operandsAndWeight = new Object[operands.length + 2];
for (int i = 0; i < operands.length; ++i) {
operandsAndWeight[i] = operands[i];
}
operandsAndWeight[operands.length] = "weight";
operandsAndWeight[operands.length + 1] = "1";
operator.initByName(operandsAndWeight);
operator.validateInputs();
}
use of beast.core.StateNode in project beast2 by CompEvol.
the class SpeciesTreeLogger method log.
@Override
public void log(final long sample, final PrintStream out) {
// make sure we get the current version of the inputs
final Tree tree = (Tree) treeInput.get().getCurrent();
Function metadata = parameterInput.get();
if (metadata instanceof StateNode) {
metadata = ((StateNode) metadata).getCurrent();
}
Function metadataTop = parameterTopInput.get();
if (metadataTop != null && metadataTop instanceof StateNode) {
metadataTop = ((StateNode) metadataTop).getCurrent();
}
List<Function> metadataList = metadataInput.get();
for (int i = 0; i < metadataList.size(); i++) {
if (metadataList.get(i) instanceof StateNode) {
metadataList.set(i, ((StateNode) metadataList.get(i)).getCurrent());
}
}
// write out the log tree with meta data
out.print("tree STATE_" + sample + " = ");
tree.getRoot().sort();
out.print(toNewick(tree.getRoot(), metadata, metadataTop, metadataList));
// out.print(tree.getRoot().toShortNewick(false));
out.print(";");
}
use of beast.core.StateNode in project beast2 by CompEvol.
the class AlignmentListInputEditor method updateModel.
/**
* set partition of type columnNr to partition model nr rowNr *
*/
void updateModel(int columnNr, int rowNr) {
Log.warning.println("updateModel: " + rowNr + " " + columnNr + " " + table.getSelectedRow() + " " + table.getSelectedColumn());
for (int i = 0; i < partitionCount; i++) {
Log.warning.println(i + " " + tableData[i][0] + " " + tableData[i][SITEMODEL_COLUMN] + " " + tableData[i][CLOCKMODEL_COLUMN] + " " + tableData[i][TREE_COLUMN]);
}
getDoc();
String partition = (String) tableData[rowNr][columnNr];
// check if partition needs renaming
String oldName = null;
boolean isRenaming = false;
try {
switch(columnNr) {
case SITEMODEL_COLUMN:
if (!doc.pluginmap.containsKey("SiteModel.s:" + partition)) {
String id = ((BEASTInterface) likelihoods[rowNr].siteModelInput.get()).getID();
oldName = BeautiDoc.parsePartition(id);
doc.renamePartition(BeautiDoc.SITEMODEL_PARTITION, oldName, partition);
isRenaming = true;
}
break;
case CLOCKMODEL_COLUMN:
{
String id = likelihoods[rowNr].branchRateModelInput.get().getID();
String clockModelName = id.substring(0, id.indexOf('.')) + ".c:" + partition;
if (!doc.pluginmap.containsKey(clockModelName)) {
oldName = BeautiDoc.parsePartition(id);
doc.renamePartition(BeautiDoc.CLOCKMODEL_PARTITION, oldName, partition);
isRenaming = true;
}
}
break;
case TREE_COLUMN:
if (!doc.pluginmap.containsKey("Tree.t:" + partition)) {
String id = likelihoods[rowNr].treeInput.get().getID();
oldName = BeautiDoc.parsePartition(id);
doc.renamePartition(BeautiDoc.TREEMODEL_PARTITION, oldName, partition);
isRenaming = true;
}
break;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Cannot rename item: " + e.getMessage());
tableData[rowNr][columnNr] = oldName;
return;
}
if (isRenaming) {
doc.determinePartitions();
initTableData();
setUpComboBoxes();
table.repaint();
return;
}
int partitionID = BeautiDoc.ALIGNMENT_PARTITION;
switch(columnNr) {
case SITEMODEL_COLUMN:
partitionID = BeautiDoc.SITEMODEL_PARTITION;
break;
case CLOCKMODEL_COLUMN:
partitionID = BeautiDoc.CLOCKMODEL_PARTITION;
break;
case TREE_COLUMN:
partitionID = BeautiDoc.TREEMODEL_PARTITION;
break;
}
int partitionNr = doc.getPartitionNr(partition, partitionID);
GenericTreeLikelihood treeLikelihood = null;
if (partitionNr >= 0) {
// we ar linking
treeLikelihood = likelihoods[partitionNr];
}
// (TreeLikelihood) doc.pluginmap.get("treeLikelihood." +
// tableData[rowNr][NAME_COLUMN]);
boolean needsRePartition = false;
PartitionContext oldContext = new PartitionContext(this.likelihoods[rowNr]);
switch(columnNr) {
case SITEMODEL_COLUMN:
{
SiteModelInterface siteModel = null;
if (treeLikelihood != null) {
// getDoc().getPartitionNr(partition,
// BeautiDoc.SITEMODEL_PARTITION) !=
// rowNr) {
siteModel = treeLikelihood.siteModelInput.get();
} else {
siteModel = (SiteModel) doc.pluginmap.get("SiteModel.s:" + partition);
if (siteModel != likelihoods[rowNr].siteModelInput.get()) {
PartitionContext context = getPartitionContext(rowNr);
try {
siteModel = (SiteModel.Base) BeautiDoc.deepCopyPlugin((BEASTInterface) likelihoods[rowNr].siteModelInput.get(), likelihoods[rowNr], (MCMC) doc.mcmc.get(), oldContext, context, doc, null);
} catch (RuntimeException e) {
JOptionPane.showMessageDialog(this, "Could not clone site model: " + e.getMessage());
return;
}
}
}
SiteModelInterface target = this.likelihoods[rowNr].siteModelInput.get();
if (target instanceof SiteModel.Base && siteModel instanceof SiteModel.Base) {
if (!((SiteModel.Base) target).substModelInput.canSetValue(((SiteModel.Base) siteModel).substModelInput.get(), (SiteModel.Base) target)) {
throw new IllegalArgumentException("Cannot link site model: substitution models (" + ((SiteModel.Base) target).substModelInput.get().getClass().toString() + " and " + ((SiteModel.Base) siteModel).substModelInput.get().getClass().toString() + ") are incompatible");
}
} else {
throw new IllegalArgumentException("Don't know how to link this site model");
}
needsRePartition = (this.likelihoods[rowNr].siteModelInput.get() != siteModel);
this.likelihoods[rowNr].siteModelInput.setValue(siteModel, this.likelihoods[rowNr]);
partition = ((BEASTInterface) likelihoods[rowNr].siteModelInput.get()).getID();
partition = BeautiDoc.parsePartition(partition);
getDoc().setCurrentPartition(BeautiDoc.SITEMODEL_PARTITION, rowNr, partition);
}
break;
case CLOCKMODEL_COLUMN:
{
BranchRateModel clockModel = null;
if (treeLikelihood != null) {
// getDoc().getPartitionNr(partition,
// BeautiDoc.CLOCKMODEL_PARTITION)
// != rowNr) {
clockModel = treeLikelihood.branchRateModelInput.get();
} else {
clockModel = getDoc().getClockModel(partition);
if (clockModel != likelihoods[rowNr].branchRateModelInput.get()) {
PartitionContext context = getPartitionContext(rowNr);
try {
clockModel = (BranchRateModel) BeautiDoc.deepCopyPlugin(likelihoods[rowNr].branchRateModelInput.get(), likelihoods[rowNr], (MCMC) doc.mcmc.get(), oldContext, context, doc, null);
} catch (RuntimeException e) {
JOptionPane.showMessageDialog(this, "Could not clone clock model: " + e.getMessage());
return;
}
}
}
// make sure that *if* the clock model has a tree as input, it is
// the same as
// for the likelihood
TreeInterface tree = null;
for (Input<?> input : ((BEASTInterface) clockModel).listInputs()) {
if (input.getName().equals("tree")) {
tree = (TreeInterface) input.get();
}
}
if (tree != null && tree != this.likelihoods[rowNr].treeInput.get()) {
JOptionPane.showMessageDialog(this, "Cannot link clock model with different trees");
throw new IllegalArgumentException("Cannot link clock model with different trees");
}
needsRePartition = (this.likelihoods[rowNr].branchRateModelInput.get() != clockModel);
this.likelihoods[rowNr].branchRateModelInput.setValue(clockModel, this.likelihoods[rowNr]);
partition = likelihoods[rowNr].branchRateModelInput.get().getID();
partition = BeautiDoc.parsePartition(partition);
getDoc().setCurrentPartition(BeautiDoc.CLOCKMODEL_PARTITION, rowNr, partition);
}
break;
case TREE_COLUMN:
{
TreeInterface tree = null;
if (treeLikelihood != null) {
// getDoc().getPartitionNr(partition,
// BeautiDoc.TREEMODEL_PARTITION) !=
// rowNr) {
tree = treeLikelihood.treeInput.get();
} else {
tree = (TreeInterface) doc.pluginmap.get("Tree.t:" + partition);
if (tree != likelihoods[rowNr].treeInput.get()) {
PartitionContext context = getPartitionContext(rowNr);
try {
tree = (TreeInterface) BeautiDoc.deepCopyPlugin((BEASTInterface) likelihoods[rowNr].treeInput.get(), likelihoods[rowNr], (MCMC) doc.mcmc.get(), oldContext, context, doc, null);
} catch (RuntimeException e) {
JOptionPane.showMessageDialog(this, "Could not clone tree model: " + e.getMessage());
return;
}
State state = ((MCMC) doc.mcmc.get()).startStateInput.get();
List<StateNode> stateNodes = new ArrayList<>();
stateNodes.addAll(state.stateNodeInput.get());
for (StateNode s : stateNodes) {
if (s.getID().endsWith(".t:" + oldContext.tree) && !(s instanceof TreeInterface)) {
try {
@SuppressWarnings("unused") StateNode copy = (StateNode) BeautiDoc.deepCopyPlugin(s, likelihoods[rowNr], (MCMC) doc.mcmc.get(), oldContext, context, doc, null);
} catch (RuntimeException e) {
JOptionPane.showMessageDialog(this, "Could not clone tree model: " + e.getMessage());
return;
}
}
}
}
}
// sanity check: make sure taxon sets are compatible
Taxon.assertSameTaxa(tree.getID(), tree.getTaxonset().getTaxaNames(), likelihoods[rowNr].dataInput.get().getID(), likelihoods[rowNr].dataInput.get().getTaxaNames());
needsRePartition = (this.likelihoods[rowNr].treeInput.get() != tree);
Log.warning.println("needsRePartition = " + needsRePartition);
if (needsRePartition) {
TreeInterface oldTree = this.likelihoods[rowNr].treeInput.get();
List<TreeInterface> tModels = new ArrayList<>();
for (GenericTreeLikelihood likelihood : likelihoods) {
if (likelihood.treeInput.get() == oldTree) {
tModels.add(likelihood.treeInput.get());
}
}
if (tModels.size() == 1) {
// remove old tree from model
((BEASTInterface) oldTree).setInputValue("estimate", false);
// use toArray to prevent ConcurrentModificationException
for (Object beastObject : BEASTInterface.getOutputs(oldTree).toArray()) {
// .toArray(new BEASTInterface[0])) {
for (Input<?> input : ((BEASTInterface) beastObject).listInputs()) {
try {
if (input.get() == oldTree) {
if (input.getRule() != Input.Validate.REQUIRED) {
input.setValue(tree, /*null*/
(BEASTInterface) beastObject);
// } else {
// input.setValue(tree, (BEASTInterface) beastObject);
}
} else if (input.get() instanceof List) {
@SuppressWarnings("unchecked") List<TreeInterface> list = (List<TreeInterface>) input.get();
if (list.contains(oldTree)) {
// && input.getRule() != Validate.REQUIRED) {
list.remove(oldTree);
if (!list.contains(tree)) {
list.add(tree);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
likelihoods[rowNr].treeInput.setValue(tree, likelihoods[rowNr]);
// TreeDistribution d = getDoc().getTreePrior(partition);
// CompoundDistribution prior = (CompoundDistribution)
// doc.pluginmap.get("prior");
// if (!getDoc().posteriorPredecessors.contains(d)) {
// prior.pDistributions.setValue(d, prior);
// }
partition = likelihoods[rowNr].treeInput.get().getID();
partition = BeautiDoc.parsePartition(partition);
getDoc().setCurrentPartition(BeautiDoc.TREEMODEL_PARTITION, rowNr, partition);
}
}
tableData[rowNr][columnNr] = partition;
if (needsRePartition) {
List<BeautiSubTemplate> templates = new ArrayList<>();
templates.add(doc.beautiConfig.partitionTemplate.get());
templates.addAll(doc.beautiConfig.subTemplates);
// keep applying rules till model does not change
doc.setUpActivePlugins();
int n;
do {
n = doc.posteriorPredecessors.size();
doc.applyBeautiRules(templates, false, oldContext);
doc.setUpActivePlugins();
} while (n != doc.posteriorPredecessors.size());
doc.determinePartitions();
}
if (treeLikelihood == null) {
initTableData();
setUpComboBoxes();
}
updateStatus();
}
Aggregations