Debian Codegraph @

src/dr/app/treestat/StatisticsPanel.java

raw symbols SCIP
1 /*
2 * StatisticsPanel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.treestat;
27
28 import dr.app.gui.components.RealNumberField;
29 import dr.app.gui.components.WholeNumberField;
30 import dr.app.treestat.statistics.*;
31 import dr.evolution.util.Taxa;
32 import dr.evolution.util.Taxon;
33 import jam.framework.Exportable;
34 import jam.panels.OptionsPanel;
35 import jam.table.TableRenderer;
36 import jam.util.IconUtils;
37
38 import javax.swing.*;
39 import javax.swing.border.EmptyBorder;
40 import javax.swing.event.ListSelectionEvent;
41 import javax.swing.event.ListSelectionListener;
42 import javax.swing.table.AbstractTableModel;
43 import java.awt.*;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ItemEvent;
46 import java.awt.event.ItemListener;
47 import java.util.ArrayList;
48
49
50 public class StatisticsPanel extends OptionsPanel implements Exportable {
51
52
53 /**
54 *
55 */
56 private static final long serialVersionUID = -8026203872020056264L;
57 TreeStatFrame frame;
58 ArrayList<TreeSummaryStatistic.Factory> availableStatistics = new ArrayList<TreeSummaryStatistic.Factory>();
59 TreeStatData treeStatData = null;
60
61 JScrollPane scrollPane1 = null;
62 JScrollPane scrollPane2 = null;
63 JTable includedStatisticsTable = null;
64 JTable availableStatisticsTable = null;
65 AvailableStatisticsTableModel availableStatisticsTableModel = null;
66 IncludedStatisticsTableModel includedStatisticsTableModel = null;
67 TreeSummaryStatisticLabel statisticLabel = new TreeSummaryStatisticLabel(null);
68
69 public StatisticsPanel(TreeStatFrame frame, TreeStatData treeStatData) {
70
71 this.frame = frame;
72 this.treeStatData = treeStatData;
73
74 // default
75 //treeStatDatastics.add(TreeSummaryStatistic.Utils.createTMRCAStatistic());
76
77 // add generic tree statistics here
78 availableStatistics.add(TreeLength.FACTORY);
79 availableStatistics.add(TreeHeight.FACTORY);
80 availableStatistics.add(NodeHeights.FACTORY);
81 availableStatistics.add(InternalBranchLengths.FACTORY);
82 availableStatistics.add(InternalBranchRates.FACTORY);
83 availableStatistics.add(ExternalBranchRates.FACTORY);
84 availableStatistics.add(InternalNodeAttribute.FACTORY);
85 availableStatistics.add(RootToTipLengths.FACTORY);
86 availableStatistics.add(TMRCASummaryStatistic.FACTORY);
87 availableStatistics.add(CladeMRCAAttributeStatistic.FACTORY);
88 availableStatistics.add(CladeMeanAttributeStatistic.FACTORY);
89 availableStatistics.add(BetaTreeDiversityStatistic.FACTORY);
90 // availableStatistics.add(MeanRootToTipLength.FACTORY);
91 // availableStatistics.add(MedianRootToTipLength.FACTORY);
92
93 availableStatistics.add(B1Statistic.FACTORY);
94 availableStatistics.add(CollessIndex.FACTORY);
95 availableStatistics.add(CherryStatistic.FACTORY);
96 availableStatistics.add(SingleChildCountStatistic.FACTORY);
97 availableStatistics.add(Nbar.FACTORY);
98 availableStatistics.add(TreenessStatistic.FACTORY);
99 availableStatistics.add(GammaStatistic.FACTORY);
100 availableStatistics.add(DeltaStatistic.FACTORY);
101 // availableStatistics.add(MonophylySummaryStatistic.FACTORY);
102 // availableStatistics.add(ParsimonySummaryStatistic.FACTORY);
103 availableStatistics.add(ExternalInternalRatio.FACTORY);
104 availableStatistics.add(FuLiD.FACTORY);
105 availableStatistics.add(RankProportionStatistic.FACTORY);
106 availableStatistics.add(IntervalKStatistic.FACTORY);
107 availableStatistics.add(LineageCountStatistic.FACTORY);
108 availableStatistics.add(LineageProportionStatistic.FACTORY);
109 availableStatistics.add(MRCAOlderThanStatistic.FACTORY);
110
111
112 // if (treeStatDataeeStatDatanull) {
113 // for (int i = 0; i < treeStatDataSets.size(); i++) {
114 // availableStatistics.add(TreeSummaryStatistic.Utils.createTMRCAStatistic((TaxonList)treeStatDataSets.get(i)));
115 // }
116 // }
117
118 setOpaque(false);
119
120 Icon includeIcon = IconUtils.getIcon(this.getClass(), "images/include.png");
121 Icon excludeIcon = IconUtils.getIcon(this.getClass(), "images/exclude.png");
122
123 // Available statistics
124 availableStatisticsTableModel = new AvailableStatisticsTableModel();
125 TableSorter sorter = new TableSorter(availableStatisticsTableModel);
126 availableStatisticsTable = new JTable(sorter);
127
128 availableStatisticsTable.getColumnModel().getColumn(0).setCellRenderer(
129 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
130
131 availableStatisticsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
132 public void valueChanged(ListSelectionEvent evt) {
133 statisticsTableSelectionChanged(false);
134 }
135 });
136
137 scrollPane1 = new JScrollPane(availableStatisticsTable,
138 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
139 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
140
141 JPanel buttonPanel1 = createAddRemoveButtonPanel(
142 includeStatisticAction, includeIcon, null,
143 excludeStatisticAction, excludeIcon, null,
144 javax.swing.BoxLayout.Y_AXIS);
145
146 // Included statistics
147 includedStatisticsTableModel = new IncludedStatisticsTableModel();
148 sorter = new TableSorter(includedStatisticsTableModel);
149 includedStatisticsTable = new JTable(sorter);
150
151 includedStatisticsTable.getColumnModel().getColumn(0).setCellRenderer(
152 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
153
154 includedStatisticsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
155 public void valueChanged(ListSelectionEvent evt) {
156 statisticsTableSelectionChanged(true);
157 }
158 });
159
160 scrollPane2 = new JScrollPane(includedStatisticsTable,
161 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
162 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
163
164
165 setLayout(new GridBagLayout());
166 GridBagConstraints c = new GridBagConstraints();
167
168 c.weightx = 0.5;
169 c.weighty = 0.75;
170 c.fill = GridBagConstraints.BOTH;
171 c.anchor = GridBagConstraints.CENTER;
172 c.insets = new Insets(6, 6, 6, 6);
173 c.gridx = 0;
174 c.gridy = 0;
175 add(scrollPane1, c);
176
177 c.weightx = 0;
178 c.weighty = 0.75;
179 c.fill = GridBagConstraints.NONE;
180 c.anchor = GridBagConstraints.CENTER;
181 c.insets = new Insets(6, 0, 6, 0);
182 c.gridx = 1;
183 c.gridy = 0;
184 add(buttonPanel1, c);
185
186 c.weightx = 0.5;
187 c.weighty = 0.75;
188 c.fill = GridBagConstraints.BOTH;
189 c.anchor = GridBagConstraints.CENTER;
190 c.insets = new Insets(6, 6, 6, 6);
191 c.gridx = 2;
192 c.gridy = 0;
193 add(scrollPane2, c);
194
195 c.weightx = 1.0;
196 c.weighty = 0.25;
197 c.fill = GridBagConstraints.BOTH;
198 c.anchor = GridBagConstraints.CENTER;
199 c.insets = new Insets(6, 6, 6, 6);
200 c.gridx = 0;
201 c.gridy = 1;
202 c.gridwidth = 3;
203 add(statisticLabel, c);
204
205 setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
206 }
207
208 JPanel createAddRemoveButtonPanel(Action addAction, Icon addIcon, String addToolTip,
209 Action removeAction, Icon removeIcon, String removeToolTip, int axis) {
210
211 JPanel buttonPanel = new JPanel();
212 buttonPanel.setLayout(new BoxLayout(buttonPanel, axis));
213 buttonPanel.setOpaque(false);
214 JButton addButton = new JButton(addAction);
215 if (addIcon != null) {
216 addButton.setIcon(addIcon);
217 addButton.setText(null);
218 }
219 addButton.setToolTipText(addToolTip);
220 addButton.putClientProperty("JButton.buttonType", "textured");
221 addButton.setOpaque(false);
222 addAction.setEnabled(false);
223
224 JButton removeButton = new JButton(removeAction);
225 if (removeIcon != null) {
226 removeButton.setIcon(removeIcon);
227 removeButton.setText(null);
228 }
229 removeButton.setToolTipText(removeToolTip);
230 removeButton.putClientProperty("JButton.buttonType", "textured");
231 removeButton.setOpaque(false);
232 removeAction.setEnabled(false);
233
234 buttonPanel.add(addButton);
235 buttonPanel.add(new JToolBar.Separator(new Dimension(6, 6)));
236 buttonPanel.add(removeButton);
237
238 return buttonPanel;
239 }
240
241 public JComponent getExportableComponent() {
242 return this;
243 }
244
245 public void dataChanged() {
246 availableStatisticsTableModel.fireTableDataChanged();
247 includedStatisticsTableModel.fireTableDataChanged();
248 }
249
250 private void statisticsTableSelectionChanged(boolean includedTable) {
251 if (includedTable) {
252 int index = includedStatisticsTable.getSelectedRow();
253 if (index != -1) {
254 availableStatisticsTable.clearSelection();
255 SummaryStatisticDescription ssd = treeStatData.statistics.get(index);
256 statisticLabel.setSummaryStatisticDescription(ssd);
257 excludeStatisticAction.setEnabled(true);
258 } else {
259 excludeStatisticAction.setEnabled(false);
260 }
261 } else {
262 int index = availableStatisticsTable.getSelectedRow();
263 if (index != -1) {
264 includedStatisticsTable.clearSelection();
265 SummaryStatisticDescription ssd = availableStatistics.get(index);
266 statisticLabel.setSummaryStatisticDescription(ssd);
267 includeStatisticAction.setEnabled(true);
268 } else {
269 includeStatisticAction.setEnabled(false);
270 }
271 }
272 }
273
274 Action includeStatisticAction = new AbstractAction("->") {
275 /**
276 *
277 */
278 private static final long serialVersionUID = -7179224487959650620L;
279
280 public void actionPerformed(ActionEvent ae) {
281 int[] indices = availableStatisticsTable.getSelectedRows();
282 for (int i = indices.length - 1; i >= 0; i--) {
283 TreeSummaryStatistic.Factory ssd = availableStatistics.get(indices[i]);
284 TreeSummaryStatistic tss = createStatistic(ssd);
285 if (tss != null) {
286 treeStatData.statistics.add(tss);
287 }
288 }
289 frame.fireDataChanged();
290 dataChanged();
291 }
292 };
293
294 Action excludeStatisticAction = new AbstractAction("<-") {
295 /**
296 *
297 */
298 private static final long serialVersionUID = -3904236403703620633L;
299
300 public void actionPerformed(ActionEvent ae) {
301 int[] indices = includedStatisticsTable.getSelectedRows();
302 for (int i = indices.length - 1; i >= 0; i--) {
303 treeStatData.statistics.remove(indices[i]);
304 }
305
306 frame.fireDataChanged();
307 dataChanged();
308 }
309 };
310
311 public TreeSummaryStatistic createStatistic(TreeSummaryStatistic.Factory factory) {
312
313 if (!factory.allowsTaxonList() &&
314 !factory.allowsDouble() && !factory.allowsInteger() && !factory.allowsString()) {
315 return factory.createStatistic();
316 }
317
318 OptionsPanel optionPanel = new OptionsPanel();
319
320 optionPanel.addSpanningComponent(new JLabel(factory.getSummaryStatisticDescription()));
321
322 final JRadioButton wholeTreeRadio = new JRadioButton("For the whole tree", false);
323 final JRadioButton taxonSetRadio = new JRadioButton("Using a given taxon set", false);
324 final JComboBox taxonSetCombo = new JComboBox();
325 final JTextField valueField;
326
327 if (factory.allowsTaxonList()) {
328
329 for (Object taxonSet : treeStatData.taxonSets) {
330 taxonSetCombo.addItem(taxonSet);
331 }
332
333 ButtonGroup group = new ButtonGroup();
334
335 ItemListener listener = new ItemListener() {
336 public void itemStateChanged(ItemEvent e) {
337 taxonSetCombo.setEnabled(taxonSetRadio.isSelected());
338 }
339 };
340
341 if (factory.allowsWholeTree()) {
342 group.add(wholeTreeRadio);
343 wholeTreeRadio.addItemListener(listener);
344
345 optionPanel.addSpanningComponent(wholeTreeRadio);
346 optionPanel.addSeparator();
347 }
348
349 if (factory.allowsTaxonList()) {
350 group.add(taxonSetRadio);
351 taxonSetRadio.addItemListener(listener);
352
353 optionPanel.addSpanningComponent(taxonSetRadio);
354 optionPanel.addComponentWithLabel("Taxon Set: ", taxonSetCombo);
355 optionPanel.addSeparator();
356 }
357
358 if (factory.allowsTaxonList()) {
359 taxonSetRadio.setSelected(true);
360 }
361 if (factory.allowsWholeTree()) {
362 wholeTreeRadio.setSelected(true);
363 }
364 }
365
366 if (factory.allowsDouble() || factory.allowsInteger() || factory.allowsString()) {
367 if (factory.allowsDouble()) {
368 valueField = new RealNumberField();
369 valueField.setColumns(12);
370 optionPanel.addComponentWithLabel(factory.getValueName(), valueField);
371 } else if (factory.allowsInteger()) {
372 valueField = new WholeNumberField();
373 valueField.setColumns(12);
374 optionPanel.addComponentWithLabel(factory.getValueName(), valueField);
375 } else { // allowsString
376 valueField = new JTextField();
377 valueField.setColumns(24);
378 optionPanel.addComponentWithLabel(factory.getValueName(), valueField);
379 }
380 } else {
381 valueField = null;
382 }
383
384 JOptionPane optionPane = new JOptionPane(optionPanel,
385 JOptionPane.QUESTION_MESSAGE,
386 JOptionPane.OK_CANCEL_OPTION,
387 null,
388 null,
389 null);
390 optionPane.setBorder(new EmptyBorder(12, 12, 12, 12));
391
392 JDialog dialog = optionPane.createDialog(frame, factory.getSummaryStatisticName());
393 // dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
394 dialog.pack();
395 dialog.setVisible(true);
396
397 if (optionPane.getValue() == null) {
398 return null;
399 }
400
401 int result = (Integer) optionPane.getValue();
402 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
403 return null;
404 }
405
406 TreeSummaryStatistic statistic = factory.createStatistic();
407
408 if (wholeTreeRadio.isSelected()) {
409 statistic = factory.createStatistic();
410 } else if (taxonSetRadio.isSelected()) {
411 TreeStatData.TaxonSet t = (TreeStatData.TaxonSet) taxonSetCombo.getSelectedItem();
412 Taxa taxa = new Taxa();
413 taxa.setId(t.name);
414 //Iterator iter = t.taxa.iterator();
415 for (Object aTaxa : t.taxa) {
416 String id = (String) aTaxa;
417 Taxon taxon = new Taxon(id);
418 taxa.addTaxon(taxon);
419 }
420 statistic.setTaxonList(taxa);
421 } else {
422 return null;
423 }
424
425 if (factory.allowsDouble()) {
426 assert valueField instanceof RealNumberField;
427
428 Double value = ((RealNumberField) valueField).getValue();
429 statistic.setDouble(value);
430 } else if (factory.allowsInteger()) {
431 assert valueField instanceof WholeNumberField;
432
433 Integer value = ((WholeNumberField) valueField).getValue();
434 statistic.setInteger(value);
435 } else if (factory.allowsString()) {
436 String value = valueField.getText();
437 statistic.setString(value);
438 }
439
440 return statistic;
441 }
442
443 class AvailableStatisticsTableModel extends AbstractTableModel {
444
445 /**
446 *
447 */
448 private static final long serialVersionUID = 86401307035717809L;
449
450 public AvailableStatisticsTableModel() {
451 }
452
453 public int getColumnCount() {
454 return 2;
455 }
456
457 public int getRowCount() {
458
459 return availableStatistics.size();
460 }
461
462 public Object getValueAt(int row, int col) {
463 if (col == 0) return availableStatistics.get(row).getSummaryStatisticName();
464 return availableStatistics.get(row).getCategory();
465 }
466
467 public boolean isCellEditable(int row, int col) {
468 return false;
469 }
470
471 public String getColumnName(int column) {
472 if (column == 0) return "Statistic Name";
473 return "Category";
474 }
475
476 public Class getColumnClass(int c) {
477 return getValueAt(0, c).getClass();
478 }
479 }
480
481 class IncludedStatisticsTableModel extends AbstractTableModel {
482
483 /**
484 *
485 */
486 private static final long serialVersionUID = -7280629792388705376L;
487
488 public IncludedStatisticsTableModel() {
489 }
490
491 public int getColumnCount() {
492 return 1;
493 }
494
495 public int getRowCount() {
496 if (treeStatData == null || treeStatData.statistics == null) return 0;
497
498 return treeStatData.statistics.size();
499 }
500
501 public Object getValueAt(int row, int col) {
502 if (treeStatData == null || treeStatData.statistics == null) return null;
503 if (col == 0) return treeStatData.statistics.get(row).getSummaryStatisticName();
504 return treeStatData.statistics.get(row).getSummaryStatisticDescription();
505 }
506
507 public boolean isCellEditable(int row, int col) {
508 return false;
509 }
510
511 public String getColumnName(int column) {
512 if (column == 0) return "Statistic Name";
513 return "Description";
514 }
515
516 public Class getColumnClass(int c) {
517 return getValueAt(0, c).getClass();
518 }
519 }
520
521 class TreeSummaryStatisticLabel extends JLabel {
522
523 /**
524 *
525 */
526 private static final long serialVersionUID = -5204925491148650874L;
527
528 public TreeSummaryStatisticLabel(SummaryStatisticDescription statistic) {
529 setSummaryStatisticDescription(statistic);
530 setVerticalAlignment(JLabel.TOP);
531 setHorizontalAlignment(JLabel.LEFT);
532 }
533
534 public void setSummaryStatisticDescription(SummaryStatisticDescription statistic) {
535 String html = "";
536 if (statistic != null) {
537 html = "<html><body><h3>" + statistic.getSummaryStatisticName() + "</h3>" +
538 "<em>" + statistic.getSummaryStatisticDescription() + "</em>";
539
540 html += "<ul>";
541 if (!statistic.allowsNonultrametricTrees()) {
542 html += "<li>Trees must be ultrametric.</li>";
543 } else if (!statistic.allowsUnrootedTrees()) {
544 html += "<li>Trees must be rooted.</li>";
545 }
546 if (!statistic.allowsPolytomies()) {
547 html += "<li>Trees must be strictly bifurcating.</li>";
548 }
549 html += "</ul>";
550 String ref = statistic.getSummaryStatisticReference();
551 if (ref != null && !ref.equals("") && !ref.equals("-")) {
552 html += "Reference: " + ref;
553 }
554 html += "</body></html>";
555 }
556
557 setText(html);
558 }
559 }
560
561 }