use of beast.math.distributions.MRCAPrior in project beast2 by CompEvol.
the class Beauti method addAlignmentProviderMenus.
private void addAlignmentProviderMenus(JMenu fileMenu) {
List<BeautiAlignmentProvider> providers = doc.beautiConfig.alignmentProvider;
for (BeautiAlignmentProvider provider : providers) {
AbstractAction action = new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
try {
setCursor(new Cursor(Cursor.WAIT_CURSOR));
// get user-specified alignments
List<BEASTInterface> beastObjects = provider.getAlignments(doc);
if (beastObjects != null) {
for (BEASTInterface o : beastObjects) {
if (o instanceof Alignment) {
try {
BeautiDoc.createTaxonSet((Alignment) o, doc);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
doc.connectModel();
doc.fireDocHasChanged();
if (beastObjects != null) {
for (BEASTInterface o : beastObjects) {
if (o instanceof MRCAPrior) {
doc.addMRCAPrior((MRCAPrior) o);
}
}
}
a_save.setEnabled(true);
a_saveas.setEnabled(true);
} catch (Exception exx) {
exx.printStackTrace();
String text = "Something went wrong importing the alignment:\n";
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.append(exx.getMessage());
textArea.setSize(textArea.getPreferredSize().width, 1);
textArea.setOpaque(false);
JOptionPane.showMessageDialog(null, textArea, "Error importing alignment", JOptionPane.WARNING_MESSAGE);
}
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
};
String providerInfo = provider.toString().replaceAll("Add ", "Add partition for ");
action.putValue(Action.SHORT_DESCRIPTION, providerInfo);
action.putValue(Action.LONG_DESCRIPTION, providerInfo);
action.putValue(Action.NAME, provider.toString());
fileMenu.add(action);
}
}
use of beast.math.distributions.MRCAPrior in project beast2 by CompEvol.
the class BeautiConfig method selectAlignments.
/**
* if fileArray == null, then use getAlignments(doc)
* @param doc
* @param parent
* @param fileArray
* @return a list of alignments based on the user selected alignment provider
*/
public List<BEASTInterface> selectAlignments(BeautiDoc doc, JComponent parent, File[] fileArray) {
List<BeautiAlignmentProvider> providers = alignmentProvider;
BeautiAlignmentProvider selectedProvider = null;
if (providers.size() == 1) {
selectedProvider = providers.get(0);
} else {
selectedProvider = (BeautiAlignmentProvider) JOptionPane.showInputDialog(parent, "Select what to add", "Add partition", JOptionPane.QUESTION_MESSAGE, null, providers.toArray(), providers.get(0));
if (selectedProvider == null) {
return null;
}
}
List<BEASTInterface> beastObjects = selectedProvider.getAlignments(doc, fileArray);
// create taxon sets, if any
if (beastObjects != null) {
for (BEASTInterface o : beastObjects) {
if (o instanceof Alignment) {
try {
BeautiDoc.createTaxonSet((Alignment) o, doc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
doc.connectModel();
if (beastObjects != null) {
for (BEASTInterface o : beastObjects) {
if (o instanceof MRCAPrior) {
doc.addMRCAPrior((MRCAPrior) o);
}
}
}
return beastObjects;
}
use of beast.math.distributions.MRCAPrior in project beast2 by CompEvol.
the class CalibratedBirthDeathModel method initAndValidate.
@Override
public void initAndValidate() {
super.initAndValidate();
type = correctionTypeInput.get();
final TreeInterface tree = treeInput.get();
// shallow copy. we shall change cals later
final List<CalibrationPoint> cals = new ArrayList<>(calibrationsInput.get());
int calCount = cals.size();
final List<TaxonSet> taxaSets = new ArrayList<>(calCount);
if (cals.size() > 0) {
xclades = new int[calCount][];
// convenience
for (final CalibrationPoint cal : cals) {
taxaSets.add(cal.taxa());
}
} else {
// find calibration points from prior
for (final Object beastObject : getOutputs()) {
if (beastObject instanceof CompoundDistribution) {
final CompoundDistribution prior = (CompoundDistribution) beastObject;
for (final Distribution distr : prior.pDistributions.get()) {
if (distr instanceof MRCAPrior) {
final MRCAPrior _MRCAPrior = (MRCAPrior) distr;
// make sure MRCAPrior is monophyletic
if (_MRCAPrior.distInput.get() != null) {
// make sure MRCAPrior is monophyletic
if (!_MRCAPrior.isMonophyleticInput.get()) {
throw new IllegalArgumentException("MRCAPriors must be monophyletic for Calibrated Yule prior");
}
// create CalibrationPoint from MRCAPrior
final CalibrationPoint cal = new CalibrationPoint();
cal.distInput.setValue(_MRCAPrior.distInput.get(), cal);
cal.taxonsetInput.setValue(_MRCAPrior.taxonsetInput.get(), cal);
cal.initAndValidate();
cals.add(cal);
taxaSets.add(cal.taxa());
cal.taxa().initAndValidate();
calCount++;
calcCalibrations = false;
} else {
if (_MRCAPrior.isMonophyleticInput.get()) {
Log.warning.println("WARNING: MRCAPriors must have a distribution when monophyletic for Calibrated Yule prior");
}
}
}
}
}
}
xclades = new int[calCount][];
}
if (calCount == 0) {
// assume we are in beauti, back off for now
return;
}
for (int k = 0; k < calCount; ++k) {
final TaxonSet tk = taxaSets.get(k);
for (int i = k + 1; i < calCount; ++i) {
final TaxonSet ti = taxaSets.get(i);
if (ti.containsAny(tk)) {
if (!(ti.containsAll(tk) || tk.containsAll(ti))) {
throw new IllegalArgumentException("Overlapping taxaSets??");
}
}
}
}
orderedCalibrations = new CalibrationPoint[calCount];
{
int loc = taxaSets.size() - 1;
while (loc >= 0) {
assert loc == taxaSets.size() - 1;
// place maximal taxaSets at end one at a time
int k = 0;
for (; /**/
k < taxaSets.size(); ++k) {
if (isMaximal(taxaSets, k)) {
break;
}
}
final List<String> tk = taxaSets.get(k).asStringList();
final int tkcount = tk.size();
this.xclades[loc] = new int[tkcount];
for (int nt = 0; nt < tkcount; ++nt) {
final int taxonIndex = getTaxonIndex(tree, tk.get(nt));
this.xclades[loc][nt] = taxonIndex;
if (taxonIndex < 0) {
throw new IllegalArgumentException("Taxon not found in tree: " + tk.get(nt));
}
}
orderedCalibrations[loc] = cals.remove(k);
taxaSets.remove(k);
// cals and taxaSets should match
--loc;
}
}
// tio[i] will contain all taxaSets contained in the i'th clade, in the form of thier index into orderedCalibrations
@SuppressWarnings("unchecked") final List<Integer>[] tio = new List[orderedCalibrations.length];
for (int k = 0; k < orderedCalibrations.length; ++k) {
tio[k] = new ArrayList<>();
}
for (int k = 0; k < orderedCalibrations.length; ++k) {
final TaxonSet txk = orderedCalibrations[k].taxa();
for (int i = k + 1; i < orderedCalibrations.length; ++i) {
if (orderedCalibrations[i].taxa().containsAll(txk)) {
tio[i].add(k);
break;
}
}
}
this.taxaPartialOrder = new int[orderedCalibrations.length][];
for (int k = 0; k < orderedCalibrations.length; ++k) {
final List<Integer> tiok = tio[k];
this.taxaPartialOrder[k] = new int[tiok.size()];
for (int j = 0; j < tiok.size(); ++j) {
this.taxaPartialOrder[k][j] = tiok.get(j);
}
}
// true if clade is not contained in any other clade
final boolean[] maximal = new boolean[calCount];
for (int k = 0; k < calCount; ++k) {
maximal[k] = true;
}
for (int k = 0; k < calCount; ++k) {
for (final int i : this.taxaPartialOrder[k]) {
maximal[i] = false;
}
}
isYule = deathToBirthRatioInput.get() == null && sampleProbabilityInput.get() == null;
userPDF = userMarInput.get();
if (userPDF == null) {
boolean needTables = false;
if (type == Type.OVER_ALL_TOPOS) {
if (calCount == 1 && isYule) {
// closed form formula
} else {
boolean anyParent = false;
for (final CalibrationPoint c : orderedCalibrations) {
if (c.forParentInput.get()) {
anyParent = true;
}
}
if (anyParent) {
throw new IllegalArgumentException("Sorry, not implemented: calibration on parent for more than one clade.");
}
if (isYule && calCount == 2 && orderedCalibrations[1].taxa().containsAll(orderedCalibrations[0].taxa())) {
// closed form formulas
} else {
needTables = true;
lastHeights = new double[calCount];
}
}
} else if (type == Type.OVER_RANKED_COUNTS) {
// setUpTables(tree.getLeafNodeCount() + 1);
needTables = true;
}
if (needTables) {
setUpTables(tree.getLeafNodeCount() + 1);
linsIter = new CalibrationLineagesIterator(this.xclades, this.taxaPartialOrder, maximal, tree.getLeafNodeCount());
}
}
final List<Node> leafs = tree.getExternalNodes();
final double height = leafs.get(0).getHeight();
for (final Node leaf : leafs) {
if (Math.abs(leaf.getHeight() - height) > 1e-8) {
Log.warning.println("WARNING: Calibrated Birth-Death Model does not handle dated tips correctly. " + "Consider using a coalescent prior instead.");
break;
}
}
}
use of beast.math.distributions.MRCAPrior in project beast2 by CompEvol.
the class StarBeastStartState method initWithMRCACalibrations.
private void initWithMRCACalibrations(List<MRCAPrior> calibrations) {
final Tree spTree = speciesTreeInput.get();
final RandomTree rnd = new RandomTree();
rnd.setInputValue("taxonset", spTree.getTaxonset());
for (final MRCAPrior cal : calibrations) {
rnd.setInputValue("constraint", cal);
}
ConstantPopulation pf = new ConstantPopulation();
pf.setInputValue("popSize", new RealParameter("1.0"));
rnd.setInputValue("populationModel", pf);
rnd.initAndValidate();
spTree.assignFromWithoutID((Tree) rnd);
final double rootHeight = spTree.getRoot().getHeight();
randomInitGeneTrees(rootHeight);
}
Aggregations