| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.tempest; |
| 27 |
|
| 28 |
import dr.app.gui.chart.*; |
| 29 |
import dr.app.gui.util.LongTask; |
| 30 |
import dr.evolution.tree.*; |
| 31 |
import dr.evolution.tree.TreeUtils; |
| 32 |
import dr.evolution.util.TaxonList; |
| 33 |
import dr.math.MathUtils; |
| 34 |
import dr.stats.DiscreteStatistics; |
| 35 |
import dr.stats.Regression; |
| 36 |
import dr.stats.Variate; |
| 37 |
import dr.util.NumberFormatter; |
| 38 |
import figtree.panel.FigTreePanel; |
| 39 |
import figtree.treeviewer.TreePaneSelector; |
| 40 |
import figtree.treeviewer.TreeSelectionListener; |
| 41 |
import figtree.treeviewer.TreeViewer; |
| 42 |
import jam.framework.Exportable; |
| 43 |
import jam.panels.SearchPanel; |
| 44 |
import jam.panels.SearchPanelListener; |
| 45 |
import jam.table.TableRenderer; |
| 46 |
import jam.toolbar.Toolbar; |
| 47 |
import jam.toolbar.ToolbarAction; |
| 48 |
import jam.toolbar.ToolbarButton; |
| 49 |
import jam.util.IconUtils; |
| 50 |
import jebl.evolution.graphs.Node; |
| 51 |
import jebl.evolution.taxa.Taxon; |
| 52 |
import jebl.evolution.trees.RootedTree; |
| 53 |
|
| 54 |
import javax.swing.*; |
| 55 |
import javax.swing.plaf.BorderUIResource; |
| 56 |
import javax.swing.table.AbstractTableModel; |
| 57 |
import java.awt.*; |
| 58 |
import java.awt.event.ActionEvent; |
| 59 |
import java.awt.event.ActionListener; |
| 60 |
import java.io.PrintWriter; |
| 61 |
import java.io.Writer; |
| 62 |
import java.text.DecimalFormat; |
| 63 |
import java.util.*; |
| 64 |
import java.util.List; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public class TempestPanel extends JPanel implements Exportable { |
| 71 |
|
| 72 |
public static final String COLOUR = "Colour..."; |
| 73 |
public static final String CLEAR_COLOURING = "Clear Colouring..."; |
| 74 |
|
| 75 |
StatisticsModel statisticsModel; |
| 76 |
JTable statisticsTable = null; |
| 77 |
|
| 78 |
private Tree tree = null; |
| 79 |
private Tree currentTree = null; |
| 80 |
private Tree bestFittingRootTree = null; |
| 81 |
|
| 82 |
private final TempestFrame frame; |
| 83 |
private final JTabbedPane tabbedPane = new JTabbedPane(); |
| 84 |
private final JTextArea textArea = new JTextArea(); |
| 85 |
private final JCheckBox showMRCACheck = new JCheckBox("Show ancestor traces"); |
| 86 |
|
| 87 |
|
| 88 |
private final SamplesPanel samplesPanel; |
| 89 |
private final FigTreePanel treePanel; |
| 90 |
|
| 91 |
private SearchPanel filterPanel; |
| 92 |
private JPopupMenu filterPopup; |
| 93 |
|
| 94 |
JChartPanel rootToTipPanel; |
| 95 |
JChart rootToTipChart; |
| 96 |
ScatterPlot rootToTipPlot; |
| 97 |
|
| 98 |
private static final boolean SHOW_NODE_DENSITY = true; |
| 99 |
JChartPanel nodeDensityPanel; |
| 100 |
JChart nodeDensityChart; |
| 101 |
ScatterPlot nodeDensityPlot; |
| 102 |
|
| 103 |
JChartPanel residualPanel; |
| 104 |
JChart residualChart; |
| 105 |
ScatterPlot residualPlot; |
| 106 |
|
| 107 |
ErrorBarPlot errorBarPlot; |
| 108 |
ParentPlot mrcaPlot; |
| 109 |
|
| 110 |
Map<Node, Integer> pointMap = new HashMap<Node, Integer>(); |
| 111 |
|
| 112 |
Set<Integer> selectedPoints = new HashSet<Integer>(); |
| 113 |
|
| 114 |
private boolean bestFittingRoot; |
| 115 |
private TemporalRooting.RootingFunction rootingFunction; |
| 116 |
private TemporalRooting temporalRooting = null; |
| 117 |
|
| 118 |
public TempestPanel(TempestFrame parent, TaxonList taxa, Tree tree) { |
| 119 |
frame = parent; |
| 120 |
|
| 121 |
samplesPanel = new SamplesPanel(parent, taxa); |
| 122 |
|
| 123 |
tabbedPane.addTab("Sample Dates", samplesPanel); |
| 124 |
|
| 125 |
statisticsModel = new StatisticsModel(); |
| 126 |
statisticsTable = new JTable(statisticsModel); |
| 127 |
|
| 128 |
statisticsTable.getColumnModel().getColumn(0).setCellRenderer( |
| 129 |
new TableRenderer(SwingConstants.RIGHT, new Insets(0, 4, 0, 4))); |
| 130 |
statisticsTable.getColumnModel().getColumn(1).setCellRenderer( |
| 131 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 132 |
|
| 133 |
JScrollPane scrollPane = new JScrollPane(statisticsTable, |
| 134 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 135 |
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); |
| 136 |
|
| 137 |
Box controlPanel1 = new Box(BoxLayout.PAGE_AXIS); |
| 138 |
controlPanel1.setOpaque(false); |
| 139 |
|
| 140 |
JPanel panel3 = new JPanel(new BorderLayout(0, 0)); |
| 141 |
panel3.setOpaque(false); |
| 142 |
rootingCheck = new JCheckBox("Best-fitting root"); |
| 143 |
panel3.add(rootingCheck, BorderLayout.CENTER); |
| 144 |
|
| 145 |
controlPanel1.add(panel3); |
| 146 |
|
| 147 |
final JComboBox rootingFunctionCombo = new JComboBox(TemporalRooting.RootingFunction.values()); |
| 148 |
|
| 149 |
JPanel panel4 = new JPanel(new BorderLayout(0,0)); |
| 150 |
panel4.setOpaque(false); |
| 151 |
panel4.add(new JLabel("Function: "), BorderLayout.WEST); |
| 152 |
panel4.add(rootingFunctionCombo, BorderLayout.CENTER); |
| 153 |
controlPanel1.add(panel4); |
| 154 |
|
| 155 |
JPanel panel1 = new JPanel(new BorderLayout(0, 0)); |
| 156 |
|
| 157 |
panel1.setOpaque(false); |
| 158 |
panel1.add(scrollPane, BorderLayout.CENTER); |
| 159 |
panel1.add(controlPanel1, BorderLayout.NORTH); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
Toolbar toolBar = new Toolbar(); |
| 164 |
toolBar.setOpaque(false); |
| 165 |
toolBar.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.darkGray)); |
| 166 |
|
| 167 |
toolBar.setRollover(true); |
| 168 |
toolBar.setFloatable(false); |
| 169 |
|
| 170 |
Icon colourToolIcon = IconUtils.getIcon(this.getClass(), "images/coloursTool.png"); |
| 171 |
|
| 172 |
final ToolbarAction colourToolbarAction = new ToolbarAction("Colour", COLOUR, colourToolIcon) { |
| 173 |
public void actionPerformed(ActionEvent e){ |
| 174 |
colourSelected(); |
| 175 |
} |
| 176 |
}; |
| 177 |
ToolbarButton colourToolButton = new ToolbarButton(colourToolbarAction, true); |
| 178 |
colourToolButton.setFocusable(false); |
| 179 |
toolBar.addComponent(colourToolButton); |
| 180 |
|
| 181 |
toolBar.addFlexibleSpace(); |
| 182 |
|
| 183 |
filterPopup = new JPopupMenu(); |
| 184 |
|
| 185 |
final ButtonGroup bg = new ButtonGroup(); |
| 186 |
boolean first = true; |
| 187 |
for (TreeViewer.TextSearchType searchType : TreeViewer.TextSearchType.values()) { |
| 188 |
final JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(searchType.toString()); |
| 189 |
if (first) { |
| 190 |
menuItem.setSelected(true); |
| 191 |
first = false; |
| 192 |
} |
| 193 |
filterPopup.add(menuItem); |
| 194 |
bg.add(menuItem); |
| 195 |
} |
| 196 |
filterPanel = new SearchPanel("Filter", filterPopup, true); |
| 197 |
filterPanel.setOpaque(false); |
| 198 |
|
| 199 |
filterPanel.addSearchPanelListener(new SearchPanelListener() { |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public void searchStarted(String searchString) { |
| 210 |
Enumeration e = bg.getElements(); |
| 211 |
String value = null; |
| 212 |
while (e.hasMoreElements()) { |
| 213 |
AbstractButton button = (AbstractButton)e.nextElement(); |
| 214 |
if (button.isSelected()) { |
| 215 |
value = button.getText(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
for (TreeViewer.TextSearchType searchType : TreeViewer.TextSearchType.values()) { |
| 220 |
if (searchType.toString().equals(value)) { |
| 221 |
treePanel.getTreeViewer().selectTaxa("!name", searchType, searchString, false); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public void searchStopped() { |
| 231 |
|
| 232 |
} |
| 233 |
}); |
| 234 |
|
| 235 |
JPanel panel5 = new JPanel(new FlowLayout()); |
| 236 |
panel5.setOpaque(false); |
| 237 |
panel5.add(filterPanel); |
| 238 |
toolBar.addComponent(panel5); |
| 239 |
|
| 240 |
treePanel = new FigTreePanel(FigTreePanel.Style.SIMPLE); |
| 241 |
|
| 242 |
JPanel panel2 = new JPanel(new BorderLayout(0, 0)); |
| 243 |
panel2.setOpaque(false); |
| 244 |
panel2.add(treePanel, BorderLayout.CENTER); |
| 245 |
panel2.add(toolBar, BorderLayout.NORTH); |
| 246 |
|
| 247 |
tabbedPane.add("Tree", panel2); |
| 248 |
|
| 249 |
treePanel.getTreeViewer().setSelectionMode(TreePaneSelector.SelectionMode.TAXA); |
| 250 |
treePanel.getTreeViewer().addTreeSelectionListener(new TreeSelectionListener() { |
| 251 |
public void selectionChanged() { |
| 252 |
treeSelectionChanged(); |
| 253 |
} |
| 254 |
}); |
| 255 |
|
| 256 |
rootToTipChart = new JChart(new LinearAxis(), new LinearAxis(Axis.AT_ZERO, Axis.AT_MINOR_TICK)); |
| 257 |
|
| 258 |
ChartSelector selector1 = new ChartSelector(rootToTipChart); |
| 259 |
|
| 260 |
rootToTipPanel = new JChartPanel(rootToTipChart, "", "time", "divergence"); |
| 261 |
JPanel panel = new JPanel(new BorderLayout()); |
| 262 |
panel.add(rootToTipPanel, BorderLayout.CENTER); |
| 263 |
panel.add(showMRCACheck, BorderLayout.SOUTH); |
| 264 |
panel.setOpaque(false); |
| 265 |
|
| 266 |
tabbedPane.add("Root-to-tip", panel); |
| 267 |
|
| 268 |
residualChart = new JChart(new LinearAxis(), new LinearAxis(Axis.AT_ZERO, Axis.AT_MINOR_TICK)); |
| 269 |
|
| 270 |
ChartSelector selector2 = new ChartSelector(residualChart); |
| 271 |
|
| 272 |
residualPanel = new JChartPanel(residualChart, "", "time", "residual"); |
| 273 |
residualPanel.setOpaque(false); |
| 274 |
|
| 275 |
tabbedPane.add("Residuals", residualPanel); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
JPanel panel6 = new JPanel(new BorderLayout(0, 0)); |
| 280 |
panel6.setOpaque(false); |
| 281 |
panel6.add(tabbedPane, BorderLayout.CENTER); |
| 282 |
|
| 283 |
|
| 284 |
if (SHOW_NODE_DENSITY) { |
| 285 |
nodeDensityChart = new JChart(new LinearAxis(), new LinearAxis(Axis.AT_ZERO, Axis.AT_MINOR_TICK)); |
| 286 |
nodeDensityPanel = new JChartPanel(nodeDensityChart, "", "time", "node density"); |
| 287 |
JPanel panel7 = new JPanel(new BorderLayout()); |
| 288 |
panel7.add(nodeDensityPanel, BorderLayout.CENTER); |
| 289 |
panel7.setOpaque(false); |
| 290 |
|
| 291 |
ChartSelector selector3 = new ChartSelector(nodeDensityChart); |
| 292 |
|
| 293 |
tabbedPane.add("Node density", panel7); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, tabbedPane); |
| 298 |
splitPane.setDividerLocation(220); |
| 299 |
splitPane.setContinuousLayout(true); |
| 300 |
splitPane.setBorder(BorderFactory.createEmptyBorder()); |
| 301 |
splitPane.setOpaque(false); |
| 302 |
|
| 303 |
setOpaque(false); |
| 304 |
setLayout(new BorderLayout(0, 0)); |
| 305 |
setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12))); |
| 306 |
|
| 307 |
add(splitPane, BorderLayout.CENTER); |
| 308 |
|
| 309 |
rootingCheck.addActionListener(new ActionListener() { |
| 310 |
public void actionPerformed(ActionEvent e) { |
| 311 |
setBestFittingRoot(rootingCheck.isSelected(), (TemporalRooting.RootingFunction) rootingFunctionCombo.getSelectedItem()); |
| 312 |
} |
| 313 |
}); |
| 314 |
|
| 315 |
rootingFunctionCombo.addActionListener(new ActionListener() { |
| 316 |
public void actionPerformed(ActionEvent e) { |
| 317 |
setBestFittingRoot(rootingCheck.isSelected(), (TemporalRooting.RootingFunction) rootingFunctionCombo.getSelectedItem()); |
| 318 |
} |
| 319 |
}); |
| 320 |
|
| 321 |
showMRCACheck.addActionListener(new ActionListener() { |
| 322 |
public void actionPerformed(ActionEvent e) { |
| 323 |
setupPanel(); |
| 324 |
} |
| 325 |
}); |
| 326 |
|
| 327 |
|
| 328 |
setTree(tree); |
| 329 |
} |
| 330 |
|
| 331 |
public List<String> getSelectedTips() { |
| 332 |
List<String> tips = new ArrayList<String>(); |
| 333 |
jebl.evolution.trees.Tree tree = treePanel.getTreeViewer().getTrees().get(0); |
| 334 |
|
| 335 |
for (Node node : treePanel.getTreeViewer().getSelectedTips()) { |
| 336 |
tips.add(tree.getTaxon(node).getName()); |
| 337 |
} |
| 338 |
return tips; |
| 339 |
} |
| 340 |
|
| 341 |
private static Color lastColor = Color.GRAY; |
| 342 |
|
| 343 |
private void colourSelected() { |
| 344 |
Color color = JColorChooser.showDialog(this, "Select Colour", lastColor); |
| 345 |
if (color != null) { |
| 346 |
treePanel.getTreeViewer().annotateSelectedTips("!color", color); |
| 347 |
lastColor = color; |
| 348 |
} |
| 349 |
setupPanel(); |
| 350 |
} |
| 351 |
|
| 352 |
private void treeSelectionChanged() { |
| 353 |
Set<Node> selectedTips = treePanel.getTreeViewer().getSelectedTips(); |
| 354 |
frame.getCopyAction().setEnabled(selectedTips != null && selectedTips.size() > 0); |
| 355 |
selectedPoints = new HashSet<Integer>(); |
| 356 |
for (Node node : selectedTips) { |
| 357 |
selectedPoints.add(pointMap.get(node)); |
| 358 |
} |
| 359 |
if (rootToTipPlot != null) { |
| 360 |
rootToTipPlot.setSelectedPoints(selectedPoints); |
| 361 |
} |
| 362 |
if (residualPlot != null) { |
| 363 |
residualPlot.setSelectedPoints(selectedPoints); |
| 364 |
} |
| 365 |
if (SHOW_NODE_DENSITY && nodeDensityPlot != null) { |
| 366 |
nodeDensityPlot.setSelectedPoints(selectedPoints); |
| 367 |
} |
| 368 |
|
| 369 |
selectMRCA(); |
| 370 |
} |
| 371 |
|
| 372 |
private void plotSelectionChanged(final Set<Integer> selectedPoints) { |
| 373 |
this.selectedPoints = selectedPoints; |
| 374 |
Set<String> selectedTaxa = new HashSet<String>(); |
| 375 |
for (Integer i : selectedPoints) { |
| 376 |
selectedTaxa.add(tree.getTaxon(i).toString()); |
| 377 |
} |
| 378 |
|
| 379 |
treePanel.getTreeViewer().selectTaxa(selectedTaxa); |
| 380 |
|
| 381 |
selectMRCA(); |
| 382 |
} |
| 383 |
|
| 384 |
private void selectMRCA() { |
| 385 |
if (mrcaPlot == null) return; |
| 386 |
|
| 387 |
if (selectedPoints != null && selectedPoints.size() > 0) { |
| 388 |
|
| 389 |
Set<String> selectedTaxa = new HashSet<String>(); |
| 390 |
for (Integer i : selectedPoints) { |
| 391 |
selectedTaxa.add(tree.getTaxon(i).toString()); |
| 392 |
} |
| 393 |
|
| 394 |
Regression r = temporalRooting.getRootToTipRegression(currentTree); |
| 395 |
NodeRef mrca = TreeUtils.getCommonAncestorNode(currentTree, selectedTaxa); |
| 396 |
double mrcaDistance1 = temporalRooting.getRootToTipDistance(currentTree, mrca); |
| 397 |
double mrcaTime1 = r.getX(mrcaDistance1); |
| 398 |
if (tree.isExternal(mrca)) { |
| 399 |
mrca = tree.getParent(mrca); |
| 400 |
} |
| 401 |
double mrcaDistance = temporalRooting.getRootToTipDistance(currentTree, mrca); |
| 402 |
double mrcaTime = r.getX(mrcaDistance); |
| 403 |
|
| 404 |
mrcaPlot.setSelectedPoints(selectedPoints, mrcaTime, mrcaDistance); |
| 405 |
} else { |
| 406 |
mrcaPlot.clearSelection(); |
| 407 |
} |
| 408 |
repaint(); |
| 409 |
} |
| 410 |
|
| 411 |
public void timeScaleChanged() { |
| 412 |
bestFittingRootTree = null; |
| 413 |
if (rootingCheck.isSelected()) { |
| 414 |
rootingCheck.setSelected(false); |
| 415 |
} else { |
| 416 |
setupPanel(); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
public JComponent getExportableComponent() { |
| 421 |
return (JComponent) tabbedPane.getSelectedComponent(); |
| 422 |
} |
| 423 |
|
| 424 |
public void setTree(Tree tree) { |
| 425 |
this.tree = tree; |
| 426 |
setupPanel(); |
| 427 |
} |
| 428 |
|
| 429 |
public void setBestFittingRoot(boolean bestFittingRoot, final TemporalRooting.RootingFunction rootingFunction) { |
| 430 |
this.bestFittingRoot = bestFittingRoot; |
| 431 |
if (this.rootingFunction != rootingFunction) { |
| 432 |
bestFittingRootTree = null; |
| 433 |
this.rootingFunction = rootingFunction; |
| 434 |
} |
| 435 |
if (this.bestFittingRoot && bestFittingRootTree == null) { |
| 436 |
findRoot(); |
| 437 |
} |
| 438 |
|
| 439 |
setupPanel(); |
| 440 |
} |
| 441 |
|
| 442 |
public Tree getTree() { |
| 443 |
return tree; |
| 444 |
} |
| 445 |
|
| 446 |
public Tree getTreeAsViewed() { |
| 447 |
return currentTree; |
| 448 |
} |
| 449 |
|
| 450 |
public void writeDataFile(Writer writer) { |
| 451 |
PrintWriter pw = new PrintWriter(writer); |
| 452 |
String labels[] = temporalRooting.getTipLabels(currentTree); |
| 453 |
double yValues[] = temporalRooting.getRootToTipDistances(currentTree); |
| 454 |
|
| 455 |
if (temporalRooting.isContemporaneous()) { |
| 456 |
double meanY = DiscreteStatistics.mean(yValues); |
| 457 |
pw.println("tip\tdistance\tdeviation"); |
| 458 |
for (int i = 0; i < yValues.length; i++) { |
| 459 |
pw.println(labels[i] + "\t" + "\t" + yValues[i] + "\t" + (yValues[i] - meanY)); |
| 460 |
} |
| 461 |
} else { |
| 462 |
double xValues[] = temporalRooting.getTipDates(currentTree); |
| 463 |
Regression r = temporalRooting.getRootToTipRegression(currentTree); |
| 464 |
double[] residuals = temporalRooting.getRootToTipResiduals(currentTree, r); |
| 465 |
pw.println("tip\tdate\tdistance\tresidual"); |
| 466 |
for (int i = 0; i < xValues.length; i++) { |
| 467 |
pw.println(labels[i] + "\t" + xValues[i] + "\t" + yValues[i] + "\t" + residuals[i]); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
public void setupPanel() { |
| 473 |
StringBuilder sb = new StringBuilder(); |
| 474 |
NumberFormatter nf = new NumberFormatter(6); |
| 475 |
|
| 476 |
if (tree != null) { |
| 477 |
temporalRooting = new TemporalRooting(tree); |
| 478 |
currentTree = this.tree; |
| 479 |
|
| 480 |
if (bestFittingRoot && bestFittingRootTree != null) { |
| 481 |
currentTree = bestFittingRootTree; |
| 482 |
sb.append("Best-fitting root"); |
| 483 |
} else { |
| 484 |
sb.append("User root"); |
| 485 |
} |
| 486 |
|
| 487 |
if (temporalRooting.isContemporaneous()) { |
| 488 |
if (tabbedPane.getSelectedIndex() == 2) { |
| 489 |
tabbedPane.setSelectedIndex(1); |
| 490 |
} |
| 491 |
tabbedPane.setEnabledAt(2, false); |
| 492 |
} else { |
| 493 |
tabbedPane.setEnabledAt(2, true); |
| 494 |
} |
| 495 |
|
| 496 |
RootedTree jtree = dr.evolution.tree.TreeUtils.asJeblTree(currentTree); |
| 497 |
|
| 498 |
List<Color> colours = new ArrayList<Color>(); |
| 499 |
for (Node tip : jtree.getExternalNodes()) { |
| 500 |
Taxon taxon = jtree.getTaxon(tip); |
| 501 |
colours.add((Color)taxon.getAttribute("!color")); |
| 502 |
} |
| 503 |
|
| 504 |
if (temporalRooting.isContemporaneous()) { |
| 505 |
double[] dv = temporalRooting.getRootToTipDistances(currentTree); |
| 506 |
|
| 507 |
List<Double> values = new ArrayList<Double>(); |
| 508 |
for (double d : dv) { |
| 509 |
values.add(d); |
| 510 |
} |
| 511 |
|
| 512 |
rootToTipChart.removeAllPlots(); |
| 513 |
NumericalDensityPlot dp = new NumericalDensityPlot(values, 20); |
| 514 |
dp.setLineColor(new Color(9, 70, 15)); |
| 515 |
|
| 516 |
double yOffset = (Double) dp.getYData().getMax() / 2; |
| 517 |
List<Double> dummyValues = new ArrayList<Double>(); |
| 518 |
for (int i = 0; i < values.size(); i++) { |
| 519 |
|
| 520 |
double y = MathUtils.nextGaussian() * ((Double) dp.getYData().getMax() * 0.05); |
| 521 |
dummyValues.add(yOffset + y); |
| 522 |
} |
| 523 |
|
| 524 |
rootToTipPlot = new ScatterPlot(values, dummyValues); |
| 525 |
rootToTipPlot.setColours(colours); |
| 526 |
rootToTipPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149)); |
| 527 |
rootToTipPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground")); |
| 528 |
rootToTipPlot.addListener(new Plot.Adaptor() { |
| 529 |
@Override |
| 530 |
public void markClicked(int index, double x, double y, boolean isShiftDown) { |
| 531 |
rootToTipPlot.selectPoint(index, isShiftDown); |
| 532 |
} |
| 533 |
|
| 534 |
public void selectionChanged(final Set<Integer> selectedPoints) { |
| 535 |
plotSelectionChanged(selectedPoints); |
| 536 |
} |
| 537 |
}); |
| 538 |
|
| 539 |
rootToTipChart.addPlot(rootToTipPlot); |
| 540 |
rootToTipChart.addPlot(dp); |
| 541 |
rootToTipPanel.setXAxisTitle("root-to-tip divergence"); |
| 542 |
rootToTipPanel.setYAxisTitle("proportion"); |
| 543 |
|
| 544 |
residualChart.removeAllPlots(); |
| 545 |
|
| 546 |
sb.append(", contemporaneous tips"); |
| 547 |
sb.append(", mean root-tip distance: " + nf.format(DiscreteStatistics.mean(dv))); |
| 548 |
sb.append(", coefficient of variation: " + nf.format(DiscreteStatistics.stdev(dv) / DiscreteStatistics.mean(dv))); |
| 549 |
sb.append(", stdev: " + nf.format(DiscreteStatistics.stdev(dv))); |
| 550 |
sb.append(", variance: " + nf.format(DiscreteStatistics.variance(dv))); |
| 551 |
|
| 552 |
showMRCACheck.setVisible(false); |
| 553 |
} else { |
| 554 |
Regression r = temporalRooting.getRootToTipRegression(currentTree); |
| 555 |
|
| 556 |
double[] residuals = temporalRooting.getRootToTipResiduals(currentTree, r); |
| 557 |
pointMap.clear(); |
| 558 |
for (int i = 0; i < currentTree.getExternalNodeCount(); i++) { |
| 559 |
NodeRef tip = currentTree.getExternalNode(i); |
| 560 |
Node node = jtree.getNode(Taxon.getTaxon(currentTree.getNodeTaxon(tip).getId())); |
| 561 |
node.setAttribute("residual", residuals[i]); |
| 562 |
|
| 563 |
pointMap.put(node, i); |
| 564 |
} |
| 565 |
|
| 566 |
rootToTipChart.removeAllPlots(); |
| 567 |
|
| 568 |
if (showMRCACheck.isSelected()) { |
| 569 |
double[] dv = temporalRooting.getParentRootToTipDistances(currentTree); |
| 570 |
|
| 571 |
List<Double> parentDistances = new ArrayList<Double>(); |
| 572 |
for (int i = 0; i < dv.length; i++) { |
| 573 |
parentDistances.add(i, dv[i]); |
| 574 |
} |
| 575 |
|
| 576 |
List<Double> parentTimes = new ArrayList<Double>(); |
| 577 |
for (int i = 0; i < parentDistances.size(); i++) { |
| 578 |
parentTimes.add(i, r.getX(parentDistances.get(i))); |
| 579 |
} |
| 580 |
mrcaPlot = new ParentPlot(r.getXData(), r.getYData(), parentTimes, parentDistances); |
| 581 |
mrcaPlot.setLineColor(new Color(105, 202, 105)); |
| 582 |
mrcaPlot.setLineStroke(new BasicStroke(0.5F)); |
| 583 |
|
| 584 |
rootToTipChart.addPlot(mrcaPlot); |
| 585 |
} |
| 586 |
|
| 587 |
if (true) { |
| 588 |
double[] datePrecisions = temporalRooting.getTipDatePrecisions(currentTree); |
| 589 |
|
| 590 |
Variate.D ed = new Variate.D(); |
| 591 |
|
| 592 |
for (int i = 0; i < datePrecisions.length; i++) { |
| 593 |
ed.add(datePrecisions[i]); |
| 594 |
} |
| 595 |
|
| 596 |
errorBarPlot = new ErrorBarPlot(ErrorBarPlot.Orientation.HORIZONTAL, r.getXData(), r.getYData(), ed); |
| 597 |
errorBarPlot.setLineColor(new Color(44, 44, 44)); |
| 598 |
errorBarPlot.setLineStroke(new BasicStroke(1.0F)); |
| 599 |
|
| 600 |
rootToTipChart.addPlot(errorBarPlot); |
| 601 |
} |
| 602 |
|
| 603 |
rootToTipPlot = new ScatterPlot(r.getXData(), r.getYData()); |
| 604 |
rootToTipPlot.addListener(new Plot.Adaptor() { |
| 605 |
public void selectionChanged(final Set<Integer> selectedPoints) { |
| 606 |
plotSelectionChanged(selectedPoints); |
| 607 |
} |
| 608 |
}); |
| 609 |
|
| 610 |
rootToTipPlot.setColours(colours); |
| 611 |
|
| 612 |
rootToTipPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149)); |
| 613 |
rootToTipPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground")); |
| 614 |
|
| 615 |
rootToTipChart.addPlot(rootToTipPlot); |
| 616 |
|
| 617 |
rootToTipChart.addPlot(new RegressionPlot(r)); |
| 618 |
|
| 619 |
rootToTipChart.getXAxis().addRange(r.getXIntercept(), (Double) r.getXData().getMax()); |
| 620 |
rootToTipPanel.setXAxisTitle("time"); |
| 621 |
rootToTipPanel.setYAxisTitle("root-to-tip divergence"); |
| 622 |
|
| 623 |
residualChart.removeAllPlots(); |
| 624 |
Variate.D values = (Variate.D) r.getYResidualData(); |
| 625 |
NumericalDensityPlot dp = new NumericalDensityPlot(values, 20); |
| 626 |
dp.setLineColor(new Color(103, 128, 144)); |
| 627 |
|
| 628 |
double yOffset = (Double) dp.getYData().getMax() / 2; |
| 629 |
Double[] dummyValues = new Double[values.getCount()]; |
| 630 |
for (int i = 0; i < dummyValues.length; i++) { |
| 631 |
|
| 632 |
double y = MathUtils.nextGaussian() * ((Double) dp.getYData().getMax() * 0.05); |
| 633 |
dummyValues[i] = yOffset + y; |
| 634 |
} |
| 635 |
Variate.D yOffsetValues = new Variate.D(dummyValues); |
| 636 |
residualPlot = new ScatterPlot(values, yOffsetValues); |
| 637 |
residualPlot.addListener(new Plot.Adaptor() { |
| 638 |
@Override |
| 639 |
public void markClicked(int index, double x, double y, boolean isShiftDown) { |
| 640 |
rootToTipPlot.selectPoint(index, isShiftDown); |
| 641 |
} |
| 642 |
|
| 643 |
@Override |
| 644 |
public void selectionChanged(final Set<Integer> selectedPoints) { |
| 645 |
plotSelectionChanged(selectedPoints); |
| 646 |
} |
| 647 |
}); |
| 648 |
residualPlot.setColours(colours); |
| 649 |
residualPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149)); |
| 650 |
residualPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground")); |
| 651 |
|
| 652 |
residualChart.addPlot(residualPlot); |
| 653 |
residualChart.addPlot(dp); |
| 654 |
residualPanel.setXAxisTitle("residual"); |
| 655 |
residualPanel.setYAxisTitle("proportion"); |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
if (SHOW_NODE_DENSITY) { |
| 669 |
Regression r2 = temporalRooting.getNodeDensityRegression(currentTree); |
| 670 |
nodeDensityChart.removeAllPlots(); |
| 671 |
nodeDensityPlot = new ScatterPlot(r2.getXData(), r2.getYData()); |
| 672 |
nodeDensityPlot.addListener(new Plot.Adaptor() { |
| 673 |
public void selectionChanged(final Set<Integer> selectedPoints) { |
| 674 |
plotSelectionChanged(selectedPoints); |
| 675 |
} |
| 676 |
}); |
| 677 |
nodeDensityPlot.setColours(colours); |
| 678 |
nodeDensityPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149)); |
| 679 |
nodeDensityPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground")); |
| 680 |
|
| 681 |
nodeDensityChart.addPlot(nodeDensityPlot); |
| 682 |
|
| 683 |
nodeDensityChart.addPlot(new RegressionPlot(r2)); |
| 684 |
|
| 685 |
nodeDensityChart.getXAxis().addRange(r2.getXIntercept(), (Double) r2.getXData().getMax()); |
| 686 |
nodeDensityPanel.setXAxisTitle("time"); |
| 687 |
nodeDensityPanel.setYAxisTitle("node density"); |
| 688 |
} |
| 689 |
|
| 690 |
sb.append(", dated tips"); |
| 691 |
sb.append(", date range: " + nf.format(temporalRooting.getDateRange())); |
| 692 |
sb.append(", slope (rate): " + nf.format(r.getGradient())); |
| 693 |
sb.append(", x-intercept (TMRCA): " + nf.format(r.getXIntercept())); |
| 694 |
sb.append(", corr. coeff: " + nf.format(r.getCorrelationCoefficient())); |
| 695 |
sb.append(", R^2: " + nf.format(r.getRSquared())); |
| 696 |
|
| 697 |
showMRCACheck.setVisible(true); |
| 698 |
} |
| 699 |
|
| 700 |
treePanel.setTree(jtree); |
| 701 |
treePanel.setColourBy("residual"); |
| 702 |
|
| 703 |
} else { |
| 704 |
treePanel.setTree(null); |
| 705 |
rootToTipChart.removeAllPlots(); |
| 706 |
sb.append("No trees loaded"); |
| 707 |
} |
| 708 |
|
| 709 |
textArea.setText(sb.toString()); |
| 710 |
|
| 711 |
statisticsModel.fireTableStructureChanged(); |
| 712 |
repaint(); |
| 713 |
} |
| 714 |
|
| 715 |
private javax.swing.Timer timer = null; |
| 716 |
|
| 717 |
|
| 718 |
private void findRoot() { |
| 719 |
|
| 720 |
|
| 721 |
final FindRootTask analyseTask = new FindRootTask(); |
| 722 |
|
| 723 |
final ProgressMonitor progressMonitor = new ProgressMonitor(frame, |
| 724 |
"Finding best-fit root", |
| 725 |
"", 0, tree.getNodeCount()); |
| 726 |
progressMonitor.setMillisToPopup(0); |
| 727 |
progressMonitor.setMillisToDecideToPopup(0); |
| 728 |
|
| 729 |
timer = new javax.swing.Timer(10, new ActionListener() { |
| 730 |
public void actionPerformed(ActionEvent evt) { |
| 731 |
progressMonitor.setProgress(analyseTask.getCurrent()); |
| 732 |
if (progressMonitor.isCanceled() || analyseTask.done()) { |
| 733 |
progressMonitor.close(); |
| 734 |
analyseTask.stop(); |
| 735 |
timer.stop(); |
| 736 |
} |
| 737 |
} |
| 738 |
}); |
| 739 |
|
| 740 |
analyseTask.go(); |
| 741 |
timer.start(); |
| 742 |
|
| 743 |
} |
| 744 |
|
| 745 |
class FindRootTask extends LongTask { |
| 746 |
|
| 747 |
public FindRootTask() { |
| 748 |
} |
| 749 |
|
| 750 |
public int getCurrent() { |
| 751 |
return temporalRooting.getCurrentRootBranch(); |
| 752 |
} |
| 753 |
|
| 754 |
public int getLengthOfTask() { |
| 755 |
return temporalRooting.getTotalRootBranches(); |
| 756 |
} |
| 757 |
|
| 758 |
public String getDescription() { |
| 759 |
return "Calculating demographic reconstruction..."; |
| 760 |
} |
| 761 |
|
| 762 |
public String getMessage() { |
| 763 |
return null; |
| 764 |
} |
| 765 |
|
| 766 |
public Object doWork() { |
| 767 |
bestFittingRootTree = temporalRooting.findRoot(tree, rootingFunction); |
| 768 |
EventQueue.invokeLater( |
| 769 |
new Runnable() { |
| 770 |
public void run() { |
| 771 |
setupPanel(); |
| 772 |
} |
| 773 |
}); |
| 774 |
|
| 775 |
return null; |
| 776 |
} |
| 777 |
|
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
public TemporalRooting getTemporalRooting() { |
| 782 |
return temporalRooting; |
| 783 |
} |
| 784 |
|
| 785 |
class StatisticsModel extends AbstractTableModel { |
| 786 |
|
| 787 |
String[] rowNamesDatedTips = {"Date range", "Slope (rate)", "X-Intercept (TMRCA)", "Correlation Coefficient", "R squared", "Residual Mean Squared"}; |
| 788 |
String[] rowNamesContemporaneousTips = {"Mean root-tip", "Coefficient of variation", "Stdev", "Variance"}; |
| 789 |
|
| 790 |
private DecimalFormat formatter = new DecimalFormat("0.####E0"); |
| 791 |
private DecimalFormat formatter2 = new DecimalFormat("####0.####"); |
| 792 |
|
| 793 |
public StatisticsModel() { |
| 794 |
} |
| 795 |
|
| 796 |
public int getColumnCount() { |
| 797 |
return 2; |
| 798 |
} |
| 799 |
|
| 800 |
public int getRowCount() { |
| 801 |
if (temporalRooting == null) { |
| 802 |
return 0; |
| 803 |
} else if (temporalRooting.isContemporaneous()) { |
| 804 |
return rowNamesContemporaneousTips.length; |
| 805 |
} else { |
| 806 |
return rowNamesDatedTips.length; |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
public Object getValueAt(int row, int col) { |
| 811 |
|
| 812 |
double value = 0; |
| 813 |
if (temporalRooting.isContemporaneous()) { |
| 814 |
if (col == 0) { |
| 815 |
return rowNamesContemporaneousTips[row]; |
| 816 |
} |
| 817 |
double values[] = temporalRooting.getRootToTipDistances(currentTree); |
| 818 |
|
| 819 |
switch (row) { |
| 820 |
case 0: |
| 821 |
value = DiscreteStatistics.mean(values); |
| 822 |
break; |
| 823 |
case 1: |
| 824 |
value = DiscreteStatistics.stdev(values) / DiscreteStatistics.mean(values); |
| 825 |
break; |
| 826 |
case 2: |
| 827 |
value = DiscreteStatistics.stdev(values); |
| 828 |
break; |
| 829 |
case 3: |
| 830 |
value = DiscreteStatistics.variance(values); |
| 831 |
break; |
| 832 |
} |
| 833 |
} else { |
| 834 |
Regression r = temporalRooting.getRootToTipRegression(currentTree); |
| 835 |
if (col == 0) { |
| 836 |
return rowNamesDatedTips[row]; |
| 837 |
} |
| 838 |
switch (row) { |
| 839 |
case 0: |
| 840 |
value = temporalRooting.getDateRange(); |
| 841 |
break; |
| 842 |
case 1: |
| 843 |
value = r.getGradient(); |
| 844 |
break; |
| 845 |
case 2: |
| 846 |
value = r.getXIntercept(); |
| 847 |
break; |
| 848 |
case 3: |
| 849 |
value = r.getCorrelationCoefficient(); |
| 850 |
break; |
| 851 |
case 4: |
| 852 |
value = r.getRSquared(); |
| 853 |
break; |
| 854 |
case 5: |
| 855 |
value = r.getResidualMeanSquared(); |
| 856 |
break; |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
if (value > 0 && (Math.abs(value) < 0.1 || Math.abs(value) >= 100000.0)) { |
| 861 |
return formatter.format(value); |
| 862 |
} else return formatter2.format(value); |
| 863 |
} |
| 864 |
|
| 865 |
public String getColumnName(int column) { |
| 866 |
if (column > 0) { |
| 867 |
return ""; |
| 868 |
} |
| 869 |
if (temporalRooting == null) { |
| 870 |
return "No tree loaded"; |
| 871 |
} else if (temporalRooting.isContemporaneous()) { |
| 872 |
return "Contemporaneous Tips"; |
| 873 |
} else { |
| 874 |
return "Dated Tips"; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
public Class getColumnClass(int c) { |
| 879 |
return getValueAt(0, c).getClass(); |
| 880 |
} |
| 881 |
|
| 882 |
public String toString() { |
| 883 |
StringBuffer buffer = new StringBuffer(); |
| 884 |
|
| 885 |
buffer.append(getColumnName(0)); |
| 886 |
for (int j = 1; j < getColumnCount(); j++) { |
| 887 |
buffer.append("\t"); |
| 888 |
buffer.append(getColumnName(j)); |
| 889 |
} |
| 890 |
buffer.append("\n"); |
| 891 |
|
| 892 |
for (int i = 0; i < getRowCount(); i++) { |
| 893 |
buffer.append(getValueAt(i, 0)); |
| 894 |
for (int j = 1; j < getColumnCount(); j++) { |
| 895 |
buffer.append("\t"); |
| 896 |
buffer.append(getValueAt(i, j)); |
| 897 |
} |
| 898 |
buffer.append("\n"); |
| 899 |
} |
| 900 |
|
| 901 |
return buffer.toString(); |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
private JCheckBox rootingCheck; |
| 906 |
|
| 907 |
} |