use of beast.evolution.sitemodel.SiteModelInterface 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();
}
use of beast.evolution.sitemodel.SiteModelInterface in project beast2 by CompEvol.
the class AlignmentListInputEditor method delItem.
void delItem() {
int[] selected = getTableRowSelection();
if (selected.length == 0) {
JOptionPane.showMessageDialog(this, "Select partitions to delete, before hitting the delete button");
}
// do the actual deleting
for (int i = selected.length - 1; i >= 0; i--) {
int rowNr = selected[i];
// before deleting, unlink site model, clock model and tree
// check whether any of the models are linked
BranchRateModel.Base clockModel = likelihoods[rowNr].branchRateModelInput.get();
SiteModelInterface siteModel = likelihoods[rowNr].siteModelInput.get();
TreeInterface tree = likelihoods[rowNr].treeInput.get();
List<GenericTreeLikelihood> cModels = new ArrayList<>();
List<GenericTreeLikelihood> models = new ArrayList<>();
List<GenericTreeLikelihood> tModels = new ArrayList<>();
for (GenericTreeLikelihood likelihood : likelihoods) {
if (likelihood != likelihoods[rowNr]) {
if (likelihood.branchRateModelInput.get() == clockModel) {
cModels.add(likelihood);
}
if (likelihood.siteModelInput.get() == siteModel) {
models.add(likelihood);
}
if (likelihood.treeInput.get() == tree) {
tModels.add(likelihood);
}
}
}
try {
if (cModels.size() > 0) {
// clock model is linked, so we need to unlink
if (doc.getPartitionNr(clockModel) != rowNr) {
tableData[rowNr][CLOCKMODEL_COLUMN] = getDoc().partitionNames.get(rowNr).partition;
} else {
int freePartition = doc.getPartitionNr(cModels.get(0));
tableData[rowNr][CLOCKMODEL_COLUMN] = getDoc().partitionNames.get(freePartition).partition;
}
updateModel(CLOCKMODEL_COLUMN, rowNr);
}
if (models.size() > 0) {
// site model is linked, so we need to unlink
if (doc.getPartitionNr((BEASTInterface) siteModel) != rowNr) {
tableData[rowNr][SITEMODEL_COLUMN] = getDoc().partitionNames.get(rowNr).partition;
} else {
int freePartition = doc.getPartitionNr(models.get(0));
tableData[rowNr][SITEMODEL_COLUMN] = getDoc().partitionNames.get(freePartition).partition;
}
updateModel(SITEMODEL_COLUMN, rowNr);
}
if (tModels.size() > 0) {
// tree is linked, so we need to unlink
if (doc.getPartitionNr((BEASTInterface) tree) != rowNr) {
tableData[rowNr][TREE_COLUMN] = getDoc().partitionNames.get(rowNr).partition;
} else {
int freePartition = doc.getPartitionNr(tModels.get(0));
tableData[rowNr][TREE_COLUMN] = getDoc().partitionNames.get(freePartition).partition;
}
updateModel(TREE_COLUMN, rowNr);
}
getDoc().delAlignmentWithSubnet(alignments.get(rowNr));
alignments.remove(rowNr);
// remove deleted likelihood from likelihoods array
GenericTreeLikelihood[] tmp = new GenericTreeLikelihood[likelihoods.length - 1];
int k = 0;
for (int j = 0; j < likelihoods.length; j++) {
if (j != rowNr) {
tmp[k] = likelihoods[j];
k++;
}
}
likelihoods = tmp;
partitionCount--;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Deletion failed: " + e.getMessage());
e.printStackTrace();
}
}
MRCAPriorInputEditor.customConnector(doc);
refreshPanel();
}
use of beast.evolution.sitemodel.SiteModelInterface in project beast2 by CompEvol.
the class BeautiPanel method cloneFrom.
/**
* Clones partition identified by sourceID to targetID and type (Site/Clock/Tree model)
* as stored in config.
* @param sourceID
* @param targetID
*/
public void cloneFrom(String sourceID, String targetID) {
if (sourceID.equals(targetID)) {
return;
}
String type = config.hasPartitionsInput.get().toString();
java.util.List<BEASTInterface> list = doc.getPartitions(type);
int source = -1, target = -1;
for (int i = 0; i < list.size(); i++) {
BEASTInterface partition = list.get(i);
if (type.equals("SiteModel")) {
partition = (BEASTInterface) ((GenericTreeLikelihood) partition).siteModelInput.get();
} else if (type.equals("ClockModel")) {
partition = ((GenericTreeLikelihood) partition).branchRateModelInput.get();
} else if (type.equals("Tree")) {
partition = (BEASTInterface) ((GenericTreeLikelihood) partition).treeInput.get();
}
String partitionID = partition.getID();
partitionID = partitionID.substring(partitionID.lastIndexOf('.') + 1);
if (partitionID.length() > 1 && partitionID.charAt(1) == ':') {
partitionID = partitionID.substring(2);
}
if (partitionID.equals(sourceID)) {
source = i;
}
if (partitionID.equals(targetID)) {
target = i;
}
}
if (target == -1) {
throw new RuntimeException("Programmer error: sourceID and targetID should be in list");
}
CompoundDistribution likelihoods = (CompoundDistribution) doc.pluginmap.get("likelihood");
GenericTreeLikelihood likelihoodSource = (GenericTreeLikelihood) likelihoods.pDistributions.get().get(source);
GenericTreeLikelihood likelihood = (GenericTreeLikelihood) likelihoods.pDistributions.get().get(target);
PartitionContext oldContext = doc.getContextFor(likelihoodSource);
PartitionContext newContext = doc.getContextFor(likelihood);
// this ensures the config.sync does not set any input value
config._input.setValue(null, config);
if (type.equals("SiteModel")) {
SiteModelInterface siteModelSource = likelihoodSource.siteModelInput.get();
SiteModelInterface siteModel = null;
try {
siteModel = (SiteModel.Base) BeautiDoc.deepCopyPlugin((BEASTInterface) siteModelSource, likelihood, (MCMC) doc.mcmc.get(), oldContext, newContext, doc, null);
} catch (RuntimeException e) {
JOptionPane.showMessageDialog(this, "Could not clone " + sourceID + " to " + targetID + " " + e.getMessage());
return;
}
likelihood.siteModelInput.setValue(siteModel, likelihood);
return;
} else if (type.equals("ClockModel")) {
BranchRateModel clockModelSource = likelihoodSource.branchRateModelInput.get();
BranchRateModel clockModel = null;
try {
clockModel = (BranchRateModel) BeautiDoc.deepCopyPlugin((BEASTInterface) clockModelSource, likelihood, (MCMC) doc.mcmc.get(), oldContext, newContext, doc, null);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Could not clone " + sourceID + " to " + targetID + " " + 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;
try {
for (Input<?> input : ((BEASTInterface) clockModel).listInputs()) {
if (input.getName().equals("tree")) {
tree = (TreeInterface) input.get();
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (tree != null && tree != likelihood.treeInput.get()) {
// likelihood.treeInput.setValue(tree, likelihood);
JOptionPane.showMessageDialog(null, "Cannot clone clock model with different trees");
return;
}
likelihood.branchRateModelInput.setValue(clockModel, likelihood);
return;
} else if (type.equals("Tree")) {
TreeInterface tree = null;
TreeInterface treeSource = likelihoodSource.treeInput.get();
try {
tree = (TreeInterface) BeautiDoc.deepCopyPlugin((BEASTInterface) treeSource, likelihood, (MCMC) doc.mcmc.get(), oldContext, newContext, doc, null);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Could not clone " + sourceID + " to " + targetID + " " + e.getMessage());
return;
}
// sanity check: make sure taxon sets are compatible
Taxon.assertSameTaxa(tree.getID(), tree.getTaxonset().getTaxaNames(), likelihood.dataInput.get().getID(), likelihood.dataInput.get().getTaxaNames());
likelihood.treeInput.setValue(tree, likelihood);
return;
} else {
throw new RuntimeException("Programmer error calling cloneFrom: Should only clone Site/Clock/Tree model");
}
}
Aggregations