use of beast.core.parameter.Parameter 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.parameter.Parameter in project beast2 by CompEvol.
the class BeautiDoc method suggestedLinks.
/**
* return all RealParameters that have
* the same ID in another partition, or
* the same partition with the same substitution model as output
*
* @param beastObject
* @return
*/
public List<BEASTInterface> suggestedLinks(BEASTInterface beastObject) {
String id = beastObject.getID();
List<BEASTInterface> list = new ArrayList<>();
String partitionID = null;
if (id.indexOf('.') >= 0) {
partitionID = id.substring(id.indexOf('.') + 1);
id = id.substring(0, id.indexOf('.'));
} else {
return list;
}
for (BEASTInterface candidate : posteriorPredecessors) {
String id2 = candidate.getID();
if (id2.indexOf('.') >= 0) {
String partitionID2 = id2.substring(id2.indexOf('.') + 1);
id2 = id2.substring(0, id2.indexOf('.'));
if (id2.equals(id)) {
list.add(candidate);
}
if (beastObject instanceof Parameter<?> && partitionID2.equals(partitionID) && candidate.getClass().equals(beastObject.getClass())) {
boolean dimensionMatches = true;
if (((Parameter<?>) beastObject).getDimension() != ((Parameter<?>) candidate).getDimension()) {
dimensionMatches = false;
}
// ensure they share an output
boolean foundCommonOutput = false;
for (Object out1 : beastObject.getOutputs()) {
for (Object out2 : candidate.getOutputs()) {
if (out1 == out2 && out1 instanceof SubstitutionModel) {
foundCommonOutput = true;
break;
}
}
}
if (dimensionMatches && foundCommonOutput) {
list.add(candidate);
}
}
}
}
list.remove(beastObject);
return list;
}
use of beast.core.parameter.Parameter in project beast2 by CompEvol.
the class BeautiDoc method determineLinks.
// methods for dealing with linking
void determineLinks() {
if (!allowLinking) {
return;
}
linked.clear();
for (BEASTInterface beastObject : posteriorPredecessors) {
Map<String, Integer> outputIDs = new HashMap<>();
for (Object output : beastObject.getOutputs()) {
if (posteriorPredecessors.contains(output)) {
String id = ((BEASTInterface) output).getID();
if (id.indexOf('.') >= 0) {
id = id.substring(0, id.indexOf('.'));
if (outputIDs.containsKey(id)) {
outputIDs.put(id, outputIDs.get(id) + 1);
} else {
outputIDs.put(id, 1);
}
}
}
}
for (Object output : beastObject.getOutputs()) {
if (posteriorPredecessors.contains(output)) {
String id = ((BEASTInterface) output).getID();
if (id.indexOf('.') >= 0) {
id = id.substring(0, id.indexOf('.'));
if (outputIDs.get(id) > 1) {
addLink(beastObject, (BEASTInterface) output);
}
}
}
}
// add parameters that have more than 1 outputs into susbtitution models
if (beastObject instanceof Parameter<?>) {
for (Object output : beastObject.getOutputs()) {
if (posteriorPredecessors.contains(output)) {
if (output instanceof SubstitutionModel) {
int nrOfSubstModelsInOutput = 0;
try {
for (Input<?> input : ((BEASTInterface) output).listInputs()) {
if (input.get() != null && input.get().equals(beastObject)) {
nrOfSubstModelsInOutput++;
}
}
} catch (Exception e) {
// ignore
}
if (nrOfSubstModelsInOutput > 1) {
addLink(beastObject, (BEASTInterface) output);
}
}
}
}
}
}
hasLinkedAtLeastOnce = false;
for (Input<?> input : linked) {
if (input.getType().isAssignableFrom(RealParameter.class)) {
hasLinkedAtLeastOnce = true;
break;
}
}
}
use of beast.core.parameter.Parameter in project beast2 by CompEvol.
the class TreeWithMetaDataLogger method toNewick.
String toNewick(Node node, List<Function> metadataList, BranchRateModel.Base branchRateModel) {
StringBuffer buf = new StringBuffer();
if (node.getLeft() != null) {
buf.append("(");
buf.append(toNewick(node.getLeft(), metadataList, branchRateModel));
if (node.getRight() != null) {
buf.append(',');
buf.append(toNewick(node.getRight(), metadataList, branchRateModel));
}
buf.append(")");
} else {
buf.append(node.labelNr + 1);
}
if (someMetaDataNeedsLogging) {
buf.append("[&");
if (metadataList.size() > 0) {
for (Function metadata : metadataList) {
buf.append(((BEASTObject) metadata).getID());
buf.append('=');
if (metadata instanceof Parameter<?>) {
Parameter<?> p = (Parameter<?>) metadata;
int dim = p.getMinorDimension1();
if (dim > 1) {
buf.append('{');
for (int i = 0; i < dim; i++) {
if (metadata instanceof RealParameter) {
RealParameter rp = (RealParameter) metadata;
appendDouble(buf, rp.getMatrixValue(node.labelNr, i));
} else {
buf.append(p.getMatrixValue(node.labelNr, i));
}
if (i < dim - 1) {
buf.append(',');
}
}
buf.append('}');
} else {
if (metadata instanceof RealParameter) {
RealParameter rp = (RealParameter) metadata;
appendDouble(buf, rp.getArrayValue(node.labelNr));
} else {
buf.append(metadata.getArrayValue(node.labelNr));
}
}
} else {
buf.append(metadata.getArrayValue(node.labelNr));
}
if (metadataList.indexOf(metadata) < metadataList.size() - 1) {
buf.append(",");
}
}
if (branchRateModel != null) {
buf.append(",");
}
}
if (branchRateModel != null) {
buf.append("rate=");
appendDouble(buf, branchRateModel.getRateForBranch(node));
}
buf.append(']');
}
buf.append(":");
if (substitutions) {
appendDouble(buf, node.getLength() * branchRateModel.getRateForBranch(node));
} else {
appendDouble(buf, node.getLength());
}
return buf.toString();
}
use of beast.core.parameter.Parameter in project beast2 by CompEvol.
the class ParameterInputEditor method addComboBox.
@Override
protected void addComboBox(JComponent box, Input<?> input, BEASTInterface beastObject) {
Box paramBox = Box.createHorizontalBox();
Parameter.Base<?> parameter = null;
if (itemNr >= 0) {
parameter = (Parameter.Base<?>) ((List<?>) input.get()).get(itemNr);
} else {
parameter = (Parameter.Base<?>) input.get();
}
if (parameter == null) {
super.addComboBox(box, input, beastObject);
} else {
setUpEntry();
paramBox.add(m_entry);
if (doc.allowLinking) {
boolean isLinked = doc.isLinked(m_input);
if (isLinked || doc.suggestedLinks((BEASTInterface) m_input.get()).size() > 0) {
JButton linkbutton = new JButton(Utils.getIcon(BeautiPanel.ICONPATH + (isLinked ? "link.png" : "unlink.png")));
linkbutton.setBorder(BorderFactory.createEmptyBorder());
linkbutton.setToolTipText("link/unlink this parameter with another compatible parameter");
linkbutton.addActionListener(e -> {
if (doc.isLinked(m_input)) {
// unlink
try {
BEASTInterface candidate = doc.getUnlinkCandidate(m_input, m_beastObject);
m_input.setValue(candidate, m_beastObject);
doc.deLink(m_input);
} catch (RuntimeException e2) {
e2.printStackTrace();
JOptionPane.showMessageDialog(this, "Could not unlink: " + e2.getMessage());
}
} else {
// create a link
List<BEASTInterface> candidates = doc.suggestedLinks((BEASTInterface) m_input.get());
JComboBox<BEASTInterface> jcb = new JComboBox<>(candidates.toArray(new BEASTInterface[] {}));
JOptionPane.showMessageDialog(null, jcb, "select parameter to link with", JOptionPane.QUESTION_MESSAGE);
BEASTInterface candidate = (BEASTInterface) jcb.getSelectedItem();
if (candidate != null) {
try {
m_input.setValue(candidate, m_beastObject);
doc.addLink(m_input);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
refreshPanel();
});
paramBox.add(linkbutton);
}
}
paramBox.add(Box.createHorizontalGlue());
m_isEstimatedBox = new JCheckBox(doc.beautiConfig.getInputLabel(parameter, parameter.isEstimatedInput.getName()));
m_isEstimatedBox.setName(input.getName() + ".isEstimated");
if (input.get() != null) {
m_isEstimatedBox.setSelected(parameter.isEstimatedInput.get());
}
m_isEstimatedBox.setToolTipText(parameter.isEstimatedInput.getHTMLTipText());
boolean isClockRate = false;
for (Object output : parameter.getOutputs()) {
if (output instanceof BranchRateModel.Base) {
isClockRate |= ((BranchRateModel.Base) output).meanRateInput.get() == parameter;
}
}
m_isEstimatedBox.setEnabled(!isClockRate || !getDoc().autoSetClockRate);
m_isEstimatedBox.addActionListener(e -> {
try {
Parameter.Base<?> parameter2 = (Parameter.Base<?>) m_input.get();
parameter2.isEstimatedInput.setValue(m_isEstimatedBox.isSelected(), parameter2);
if (isParametricDistributionParameter) {
String id = parameter2.getID();
if (id.startsWith("RealParameter")) {
ParametricDistribution parent = null;
for (Object beastObject2 : parameter2.getOutputs()) {
if (beastObject2 instanceof ParametricDistribution) {
parent = (ParametricDistribution) beastObject2;
break;
}
}
Distribution grandparent = null;
for (Object beastObject2 : parent.getOutputs()) {
if (beastObject2 instanceof Distribution) {
grandparent = (Distribution) beastObject2;
break;
}
}
id = "parameter.hyper" + parent.getClass().getSimpleName() + "-" + m_input.getName() + "-" + grandparent.getID();
doc.pluginmap.remove(parameter2.getID());
parameter2.setID(id);
doc.addPlugin(parameter2);
}
PartitionContext context = new PartitionContext(id.substring("parameter.".length()));
Log.warning.println(context + " " + id);
doc.beautiConfig.hyperPriorTemplate.createSubNet(context, true);
}
refreshPanel();
} catch (Exception ex) {
Log.err.println("ParameterInputEditor " + ex.getMessage());
}
});
paramBox.add(m_isEstimatedBox);
// only show the estimate flag if there is an operator that works on this parameter
m_isEstimatedBox.setVisible(doc.isExpertMode());
m_isEstimatedBox.setToolTipText("Estimate value of this parameter in the MCMC chain");
// m_bAddButtons = false;
if (itemNr < 0) {
for (Object beastObject2 : ((BEASTInterface) m_input.get()).getOutputs()) {
if (beastObject2 instanceof ParametricDistribution) {
m_isEstimatedBox.setVisible(true);
isParametricDistributionParameter = true;
break;
}
}
for (Object beastObject2 : ((BEASTInterface) m_input.get()).getOutputs()) {
if (beastObject2 instanceof Operator) {
m_isEstimatedBox.setVisible(true);
// m_editPluginButton.setVisible(true);
break;
}
}
} else {
for (Object beastObject2 : ((BEASTInterface) ((List<?>) m_input.get()).get(itemNr)).getOutputs()) {
if (beastObject2 instanceof Operator) {
m_isEstimatedBox.setVisible(true);
// m_editPluginButton.setVisible(true);
break;
}
}
}
box.add(paramBox);
}
}
Aggregations