use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.
the class StatisticsPanel method createStatistic.
public TreeSummaryStatistic createStatistic(TreeSummaryStatistic.Factory factory) {
if (!factory.allowsTaxonList() && !factory.allowsDouble() && !factory.allowsInteger() && !factory.allowsString()) {
return factory.createStatistic();
}
OptionsPanel optionPanel = new OptionsPanel();
optionPanel.addSpanningComponent(new JLabel(factory.getSummaryStatisticDescription()));
final JRadioButton wholeTreeRadio = new JRadioButton("For the whole tree", false);
final JRadioButton taxonSetRadio = new JRadioButton("Using a given taxon set", false);
final JComboBox taxonSetCombo = new JComboBox();
final JTextField valueField;
if (factory.allowsTaxonList()) {
for (Object taxonSet : treeStatData.taxonSets) {
taxonSetCombo.addItem(taxonSet);
}
ButtonGroup group = new ButtonGroup();
ItemListener listener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
taxonSetCombo.setEnabled(taxonSetRadio.isSelected());
}
};
if (factory.allowsWholeTree()) {
group.add(wholeTreeRadio);
wholeTreeRadio.addItemListener(listener);
optionPanel.addSpanningComponent(wholeTreeRadio);
optionPanel.addSeparator();
}
if (factory.allowsTaxonList()) {
group.add(taxonSetRadio);
taxonSetRadio.addItemListener(listener);
optionPanel.addSpanningComponent(taxonSetRadio);
optionPanel.addComponentWithLabel("Taxon Set: ", taxonSetCombo);
optionPanel.addSeparator();
}
if (factory.allowsTaxonList()) {
taxonSetRadio.setSelected(true);
}
if (factory.allowsWholeTree()) {
wholeTreeRadio.setSelected(true);
}
}
if (factory.allowsDouble() || factory.allowsInteger() || factory.allowsString()) {
if (factory.allowsDouble()) {
valueField = new RealNumberField();
valueField.setColumns(12);
optionPanel.addComponentWithLabel(factory.getValueName(), valueField);
} else if (factory.allowsInteger()) {
valueField = new WholeNumberField();
valueField.setColumns(12);
optionPanel.addComponentWithLabel(factory.getValueName(), valueField);
} else {
// allowsString
valueField = new JTextField();
valueField.setColumns(24);
optionPanel.addComponentWithLabel(factory.getValueName(), valueField);
}
} else {
valueField = null;
}
JOptionPane optionPane = new JOptionPane(optionPanel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, null);
optionPane.setBorder(new EmptyBorder(12, 12, 12, 12));
JDialog dialog = optionPane.createDialog(frame, factory.getSummaryStatisticName());
// dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
dialog.setVisible(true);
if (optionPane.getValue() == null) {
return null;
}
int result = (Integer) optionPane.getValue();
if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
return null;
}
TreeSummaryStatistic statistic = factory.createStatistic();
if (wholeTreeRadio.isSelected()) {
statistic = factory.createStatistic();
} else if (taxonSetRadio.isSelected()) {
TreeStatData.TaxonSet t = (TreeStatData.TaxonSet) taxonSetCombo.getSelectedItem();
Taxa taxa = new Taxa();
taxa.setId(t.name);
//Iterator iter = t.taxa.iterator();
for (Object aTaxa : t.taxa) {
String id = (String) aTaxa;
Taxon taxon = new Taxon(id);
taxa.addTaxon(taxon);
}
statistic.setTaxonList(taxa);
} else {
return null;
}
if (factory.allowsDouble()) {
assert valueField instanceof RealNumberField;
Double value = ((RealNumberField) valueField).getValue();
statistic.setDouble(value);
} else if (factory.allowsInteger()) {
assert valueField instanceof WholeNumberField;
Integer value = ((WholeNumberField) valueField).getValue();
statistic.setInteger(value);
} else if (factory.allowsString()) {
String value = valueField.getText();
statistic.setString(value);
}
return statistic;
}
use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.
the class BetaTreeDiversityStatistic method main.
public static void main(String[] arg) throws IOException, Importer.ImportException {
Tree tree = (new NewickImporter("((A:1,B:1):1,(C:1, D:1):1);")).importNextTree();
BetaTreeDiversityStatistic statistic = (BetaTreeDiversityStatistic) BetaTreeDiversityStatistic.FACTORY.createStatistic();
Taxa taxa = new Taxa();
taxa.addTaxon(new Taxon("A"));
taxa.addTaxon(new Taxon("C"));
statistic.setTaxonList(taxa);
System.out.println(statistic.getSummaryStatistic(tree)[0]);
}
use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.
the class SpeciesTreeModel method compatibleUninformedSpeciesTree.
private SimpleTree compatibleUninformedSpeciesTree(Tree startTree) {
double rootHeight = Double.MAX_VALUE;
for (SpeciesBindings.GeneTreeInfo t : species.getGeneTrees()) {
rootHeight = Math.min(rootHeight, t.getCoalInfo()[0].ctime);
}
final SpeciesBindings.SPinfo[] spp = species.species;
if (startTree != null) {
if (startTree.getExternalNodeCount() != spp.length) {
throw new Error("Start tree error - different number of tips");
}
final FlexibleTree tree = new FlexibleTree(startTree, true);
tree.resolveTree();
final double treeHeight = tree.getRootHeight();
if (treeHeight <= 0) {
tree.setRootHeight(1.0);
MutableTree.Utils.correctHeightsForTips(tree);
SimpleTree.Utils.scaleNodeHeights(tree, rootHeight / tree.getRootHeight());
}
SimpleTree sTree = new SimpleTree(tree);
for (int ns = 0; ns < spp.length; ns++) {
SpeciesBindings.SPinfo sp = spp[ns];
final int i = sTree.getTaxonIndex(sp.name);
if (i < 0) {
throw new Error(sp.name + " is not present in the start tree");
}
final SimpleNode node = sTree.getExternalNode(i);
node.setAttribute(spIndexAttrName, ns);
// set for possible pops
tree.setNodeAttribute(tree.getNode(tree.getTaxonIndex(sp.name)), spIndexAttrName, ns);
}
if (treeHeight > 0) {
sTree.setAttribute("check", new Double(rootHeight));
}
{
//assert ! constantPopulation; // not implemented yet
int[] pos = { 0 };
setInitialSplitPopulations(tree, tree.getRoot(), pos);
}
return sTree;
}
final double delta = rootHeight / (spp.length + 1);
double cTime = delta;
List<SimpleNode> subs = new ArrayList<SimpleNode>(spp.length);
for (int ns = 0; ns < spp.length; ns++) {
SpeciesBindings.SPinfo sp = spp[ns];
final SimpleNode node = new SimpleNode();
node.setTaxon(new Taxon(sp.name));
subs.add(node);
node.setAttribute(spIndexAttrName, ns);
}
while (subs.size() > 1) {
final SimpleNode node = new SimpleNode();
int i = 0, j = 1;
node.addChild(subs.get(i));
node.addChild(subs.get(j));
node.setHeight(cTime);
cTime += delta;
subs.set(j, node);
subs.remove(i);
}
return new SimpleTree(subs.get(0));
}
use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.
the class TraitGibbsOperator method doOperation.
public double doOperation() {
NodeRef node = null;
final NodeRef root = treeModel.getRoot();
while (node == null) {
if (onlyInternalNodes)
node = treeModel.getInternalNode(MathUtils.nextInt(treeModel.getInternalNodeCount()));
else {
node = treeModel.getNode(MathUtils.nextInt(treeModel.getNodeCount()));
if (onlyTipsWithPriors && // Is a tip
(treeModel.getChildCount(node) == 0) && !nodeGeoSpatialPriorExists(node)) {
// Does not have a prior
node = null;
}
}
if (!sampleRoot && node == root)
node = null;
}
// select any internal (or internal/external) node
final double[] initialValue = treeModel.getMultivariateNodeTrait(node, traitName);
MeanPrecision mp;
if (node != root)
mp = operateNotRoot(node);
else
mp = operateRoot(node);
final Taxon taxon = treeModel.getNodeTaxon(node);
// final boolean nodePriorExists = nodeGeoSpatialPrior != null && nodeGeoSpatialPrior.containsKey(taxon);
final boolean nodePriorExists = nodeGeoSpatialPriorExists(node);
// if (!onlyInternalNodes) {
// final boolean isTip = (treeModel.getChildCount(node) == 0);
// if (!nodePriorExists && isTip)
// System.err.println("Warning: sampling taxon '"+treeModel.getNodeTaxon(node).getId()
// +"' tip trait without a prior!!!");
// }
int count = 0;
final boolean parameterPriorExists = parameterPrior != null;
double[] draw;
do {
do {
if (count > maxTries) {
// TODO Add to MTT interface
treeModel.setMultivariateTrait(node, traitName, initialValue);
throw new RuntimeException("Truncated Gibbs is stuck!");
}
draw = MultivariateNormalDistribution.nextMultivariateNormalPrecision(mp.mean, mp.precision);
count++;
} while (// There is a prior for this node
nodePriorExists && // And draw is invalid under prior
(nodeGeoSpatialPrior.get(taxon)).logPdf(draw) == Double.NEGATIVE_INFINITY);
// TODO Currently only works for flat/truncated priors, make work for MVN
treeModel.setMultivariateTrait(node, traitName, draw);
} while (parameterPriorExists && (parameterPrior.getLogLikelihood() == Double.NEGATIVE_INFINITY));
return 0;
}
use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.
the class TransmissionHistoryModel method createTreeColouring.
private Taxon createTreeColouring(Tree tree, NodeRef node, DefaultTreeColouring treeColouring) {
Taxon parentHost = null;
Taxon childHost;
if (tree.isExternal(node)) {
childHost = (Taxon) tree.getNodeTaxon(node).getAttribute("host");
if (childHost == null) {
throw new RuntimeException("One or more of the viruses tree's taxa are missing the 'host' attribute");
}
} else {
Taxon h1 = createTreeColouring(tree, tree.getChild(node, 0), treeColouring);
Taxon h2 = createTreeColouring(tree, tree.getChild(node, 1), treeColouring);
if (h1 != h2) {
throw new RuntimeException("Two children have different hosts at coalescent event");
}
childHost = h1;
}
NodeRef parent = tree.getParent(node);
if (parent != null) {
double height0 = tree.getNodeHeight(node);
double height1 = tree.getNodeHeight(parent);
TransmissionEvent event = getTransmissionEventToHost(childHost);
DefaultBranchColouring branchColouring;
if (event != null && event.getTransmissionTime() < height1) {
if (event.getTransmissionTime() < height0) {
throw new RuntimeException("Transmission event is before the node");
}
List<Taxon> hosts = new ArrayList<Taxon>();
List<Double> times = new ArrayList<Double>();
parentHost = childHost;
while (event != null && event.getTransmissionTime() < height1) {
hosts.add(parentHost);
times.add(event.getTransmissionTime());
parentHost = event.donor;
event = getTransmissionEventToHost(parentHost);
}
int host1 = getHostIndex(childHost);
int host2 = getHostIndex(parentHost);
branchColouring = new DefaultBranchColouring(host2, host1);
for (int i = hosts.size() - 1; i >= 0; i--) {
int host = getHostIndex(hosts.get(i));
double time = times.get(i);
branchColouring.addEvent(host, time);
}
} else {
int host = getHostIndex(childHost);
branchColouring = new DefaultBranchColouring(host, host);
parentHost = childHost;
}
treeColouring.setBranchColouring(node, branchColouring);
}
return parentHost;
}
Aggregations