use of beast.evolution.branchratemodel.BranchRateModel 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.branchratemodel.BranchRateModel 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");
}
}
use of beast.evolution.branchratemodel.BranchRateModel in project beast2 by CompEvol.
the class BeautiDoc method determinePartitions.
public void determinePartitions() {
CompoundDistribution likelihood = (CompoundDistribution) pluginmap.get("likelihood");
if (likelihood == null) {
return;
}
partitionNames.clear();
possibleContexts.clear();
for (Distribution distr : likelihood.pDistributions.get()) {
if (distr instanceof GenericTreeLikelihood) {
GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
alignments.add(treeLikelihood.dataInput.get());
PartitionContext context = new PartitionContext(treeLikelihood);
partitionNames.add(context);
boolean found = false;
for (PartitionContext context2 : possibleContexts) {
if (context.equals(context2)) {
found = true;
}
}
if (!found) {
possibleContexts.add(context);
}
}
}
alignments.clear();
for (int i = 0; i < 3; i++) {
pPartitionByAlignments[i].clear();
pPartition[i].clear();
currentPartitions[i].clear();
}
List<GenericTreeLikelihood> treeLikelihoods = new ArrayList<>();
for (Distribution distr : likelihood.pDistributions.get()) {
if (distr instanceof GenericTreeLikelihood) {
GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
alignments.add(treeLikelihood.dataInput.get());
treeLikelihoods.add(treeLikelihood);
}
}
for (Distribution distr : likelihood.pDistributions.get()) {
if (distr instanceof GenericTreeLikelihood) {
GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
try {
// sync SiteModel, ClockModel and Tree to any changes that
// may have occurred
// this should only affect the clock model in practice
int partition = getPartitionNr((BEASTInterface) treeLikelihood.siteModelInput.get());
GenericTreeLikelihood treeLikelihood2 = treeLikelihoods.get(partition);
treeLikelihood.siteModelInput.setValue(treeLikelihood2.siteModelInput.get(), treeLikelihood);
currentPartitions[0].add(partition);
BranchRateModel rateModel = treeLikelihood.branchRateModelInput.get();
if (rateModel != null) {
partition = getPartitionNr((BEASTInterface) rateModel);
treeLikelihood2 = treeLikelihoods.get(partition);
treeLikelihood.branchRateModelInput.setValue(treeLikelihood2.branchRateModelInput.get(), treeLikelihood);
currentPartitions[1].add(partition);
} else {
currentPartitions[1].add(0);
}
partition = getPartitionNr((BEASTInterface) treeLikelihood.treeInput.get());
treeLikelihood2 = treeLikelihoods.get(partition);
treeLikelihood.treeInput.setValue(treeLikelihood2.treeInput.get(), treeLikelihood);
currentPartitions[2].add(partition);
} catch (Exception e) {
e.printStackTrace();
}
pPartitionByAlignments[0].add(treeLikelihood);
pPartitionByAlignments[1].add(treeLikelihood);
pPartitionByAlignments[2].add(treeLikelihood);
}
}
int partitionCount = partitionNames.size();
for (int i = 0; i < 3; i++) {
boolean[] usedPartition = new boolean[partitionCount];
for (int j = 0; j < partitionCount; j++) {
// getPartitionNr(m_pPartitionByAlignments[i].get(j));
int partitionIndex = currentPartitions[i].get(j);
usedPartition[partitionIndex] = true;
}
for (int j = 0; j < partitionCount; j++) {
if (usedPartition[j]) {
pPartition[i].add(pPartitionByAlignments[i].get(j));
}
}
}
Log.warning.println("PARTITIONS0:\n");
Log.warning.println(Arrays.toString(currentPartitions));
}
use of beast.evolution.branchratemodel.BranchRateModel in project beast2 by CompEvol.
the class BeautiDoc method collectClockModels.
private void collectClockModels() {
// collect branch rate models from model
CompoundDistribution likelihood = (CompoundDistribution) pluginmap.get("likelihood");
while (clockModels.size() < partitionNames.size()) {
try {
GenericTreeLikelihood treelikelihood = new GenericTreeLikelihood();
treelikelihood.branchRateModelInput.setValue(new StrictClockModel(), treelikelihood);
List<BeautiSubTemplate> availableBEASTObjects = inputEditorFactory.getAvailableTemplates(treelikelihood.branchRateModelInput, treelikelihood, null, this);
BEASTInterface beastObject = availableBEASTObjects.get(0).createSubNet(partitionNames.get(clockModels.size()), true);
clockModels.add((BranchRateModel.Base) beastObject);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int k = 0;
for (Distribution d : likelihood.pDistributions.get()) {
BranchRateModel clockModel = ((GenericTreeLikelihood) d).branchRateModelInput.get();
// sanity check
Tree tree = null;
try {
for (Input<?> input : ((BEASTInterface) clockModel).listInputs()) {
if (input.getName().equals("tree")) {
tree = (Tree) input.get();
}
}
if (tree != null && tree != ((GenericTreeLikelihood) d).treeInput.get()) {
clockModel = clockModels.get(k);
Log.warning.println("WARNING: unlinking clock model for " + d.getID());
// TODO #557: this should move to the event of clock model drop box
// JOptionPane.showMessageDialog(beauti.getSelectedComponent(),
// "Cannot link all clock model(s) except strict clock with different trees !");
((GenericTreeLikelihood) d).branchRateModelInput.setValue(clockModel, d);
}
} catch (Exception e) {
// ignore
}
if (clockModel != null) {
String id = ((BEASTInterface) clockModel).getID();
id = parsePartition(id);
String partition = alignments.get(k).getID();
if (id.equals(partition)) {
clockModels.set(k, clockModel);
}
k++;
}
}
}
use of beast.evolution.branchratemodel.BranchRateModel in project beast2 by CompEvol.
the class SequenceSimulator method main.
// printUsageAndExit
@SuppressWarnings("unchecked")
public static void main(String[] args) {
try {
// parse arguments
if (args.length < 2) {
printUsageAndExit();
}
String fileName = args[0];
int replications = Integer.parseInt(args[1]);
PrintStream out = System.out;
if (args.length == 3) {
File file = new File(args[2]);
out = new PrintStream(file);
}
// grab the file
String xml = "";
BufferedReader fin = new BufferedReader(new FileReader(fileName));
while (fin.ready()) {
xml += fin.readLine();
}
fin.close();
// parse the xml
XMLParser parser = new XMLParser();
BEASTInterface beastObject = parser.parseFragment(xml, true);
// find relevant objects from the model
TreeLikelihood treeLikelihood = getTreeLikelihood(beastObject);
if (treeLikelihood == null) {
throw new IllegalArgumentException("No treelikelihood found in file. Giving up now.");
}
Alignment data = ((Input<Alignment>) treeLikelihood.getInput("data")).get();
Tree tree = ((Input<Tree>) treeLikelihood.getInput("tree")).get();
SiteModel pSiteModel = ((Input<SiteModel>) treeLikelihood.getInput("siteModel")).get();
BranchRateModel pBranchRateModel = ((Input<BranchRateModel>) treeLikelihood.getInput("branchRateModel")).get();
// feed to sequence simulator and generate leaves
SequenceSimulator treeSimulator = new SequenceSimulator();
treeSimulator.init(data, tree, pSiteModel, pBranchRateModel, replications);
XMLProducer producer = new XMLProducer();
Alignment alignment = treeSimulator.simulate();
xml = producer.toRawXML(alignment);
out.println("<beast version='2.0'>");
out.println(xml);
out.println("</beast>");
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations