| 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.treestat; |
| 27 |
|
| 28 |
import dr.evolution.tree.TreeUtils; |
| 29 |
import jam.framework.Application; |
| 30 |
import jam.framework.DocumentFrame; |
| 31 |
import jam.util.IconUtils; |
| 32 |
|
| 33 |
import javax.swing.*; |
| 34 |
import javax.swing.plaf.BorderUIResource; |
| 35 |
import java.awt.*; |
| 36 |
import java.io.*; |
| 37 |
|
| 38 |
import dr.evolution.io.Importer; |
| 39 |
import dr.evolution.io.NexusImporter; |
| 40 |
import dr.evolution.io.NewickImporter; |
| 41 |
import dr.evolution.io.TreeImporter; |
| 42 |
import dr.evolution.tree.Tree; |
| 43 |
import dr.app.treestat.statistics.TreeSummaryStatistic; |
| 44 |
|
| 45 |
public class TreeStatFrame extends DocumentFrame { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private static final long serialVersionUID = -1775448072034877658L; |
| 51 |
|
| 52 |
private TreeStatData treeStatData = null; |
| 53 |
|
| 54 |
private JTabbedPane tabbedPane = new JTabbedPane(); |
| 55 |
|
| 56 |
private TaxonSetsPanel taxonSetsPanel; |
| 57 |
private StatisticsPanel statisticsPanel; |
| 58 |
|
| 59 |
private JLabel statusLabel; |
| 60 |
private JLabel progressLabel; |
| 61 |
private JProgressBar progressBar; |
| 62 |
|
| 63 |
final Icon gearIcon = IconUtils.getIcon(this.getClass(), "images/gear.png"); |
| 64 |
|
| 65 |
public TreeStatFrame(Application application, String title) { |
| 66 |
super(); |
| 67 |
|
| 68 |
setTitle(title); |
| 69 |
|
| 70 |
treeStatData = new TreeStatData(); |
| 71 |
|
| 72 |
setImportAction(importTaxaAction); |
| 73 |
setExportAction(processTreeFileAction); |
| 74 |
|
| 75 |
getOpenAction().setEnabled(false); |
| 76 |
getSaveAction().setEnabled(false); |
| 77 |
getSaveAsAction().setEnabled(false); |
| 78 |
} |
| 79 |
|
| 80 |
public void initializeComponents() { |
| 81 |
|
| 82 |
setSize(new java.awt.Dimension(800, 600)); |
| 83 |
|
| 84 |
taxonSetsPanel = new TaxonSetsPanel(this, treeStatData); |
| 85 |
statisticsPanel = new StatisticsPanel(this, treeStatData); |
| 86 |
|
| 87 |
tabbedPane.addTab("Statistics", null, statisticsPanel); |
| 88 |
tabbedPane.addTab("Taxon Sets", null, taxonSetsPanel); |
| 89 |
|
| 90 |
statusLabel = new JLabel("No statistics selected"); |
| 91 |
processTreeFileAction.setEnabled(false); |
| 92 |
|
| 93 |
JPanel progressPanel = new JPanel(new BorderLayout(0,0)); |
| 94 |
progressLabel = new JLabel(""); |
| 95 |
progressBar = new JProgressBar(); |
| 96 |
progressPanel.add(progressLabel, BorderLayout.NORTH); |
| 97 |
progressPanel.add(progressBar, BorderLayout.CENTER); |
| 98 |
|
| 99 |
JPanel panel2 = new JPanel(new FlowLayout()); |
| 100 |
JButton goButton = new JButton(processTreeFileAction); |
| 101 |
goButton.setFocusable(false); |
| 102 |
goButton.putClientProperty("JButton.buttonType", "textured"); |
| 103 |
goButton.setMargin(new Insets(4,4,4,4)); |
| 104 |
panel2.add(goButton); |
| 105 |
panel2.add(progressPanel); |
| 106 |
|
| 107 |
JPanel panel1 = new JPanel(new BorderLayout(0,0)); |
| 108 |
panel1.add(statusLabel, BorderLayout.WEST); |
| 109 |
panel1.add(panel2, BorderLayout.EAST); |
| 110 |
panel1.setBorder(new BorderUIResource.EmptyBorderUIResource(new java.awt.Insets(0, 6, 0, 6))); |
| 111 |
|
| 112 |
JPanel panel = new JPanel(new BorderLayout(0,0)); |
| 113 |
panel.add(tabbedPane, BorderLayout.CENTER); |
| 114 |
panel.add(panel1, BorderLayout.SOUTH); |
| 115 |
panel.setBorder(new BorderUIResource.EmptyBorderUIResource(new java.awt.Insets(12, 12, 12, 12))); |
| 116 |
|
| 117 |
getContentPane().setLayout(new java.awt.BorderLayout(0, 0)); |
| 118 |
getContentPane().add(panel, BorderLayout.CENTER); |
| 119 |
} |
| 120 |
|
| 121 |
public void fireDataChanged() { |
| 122 |
if (treeStatData.statistics.size() > 0) { |
| 123 |
statusLabel.setText("" + treeStatData.statistics.size() + " statistics selected"); |
| 124 |
processTreeFileAction.setEnabled(true); |
| 125 |
} else { |
| 126 |
statusLabel = new JLabel("No statistics selected"); |
| 127 |
processTreeFileAction.setEnabled(false); |
| 128 |
} |
| 129 |
taxonSetsPanel.dataChanged(); |
| 130 |
statisticsPanel.dataChanged(); |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
protected boolean readFromFile(File file) throws IOException { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
protected boolean writeToFile(File file) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
public final void doImport() { |
| 143 |
|
| 144 |
FileDialog dialog = new FileDialog(this, |
| 145 |
"Import Tree File...", |
| 146 |
FileDialog.LOAD); |
| 147 |
|
| 148 |
dialog.setVisible(true); |
| 149 |
if (dialog.getFile() != null) { |
| 150 |
File file = new File(dialog.getDirectory(), dialog.getFile()); |
| 151 |
|
| 152 |
try { |
| 153 |
importFromFile(file); |
| 154 |
|
| 155 |
} catch (Importer.ImportException ie) { |
| 156 |
JOptionPane.showMessageDialog(this, "Unable to read tree file: " + ie, |
| 157 |
"Unable to read tree file", |
| 158 |
JOptionPane.ERROR_MESSAGE); |
| 159 |
} catch (FileNotFoundException fnfe) { |
| 160 |
JOptionPane.showMessageDialog(this, "Unable to open file: File not found", |
| 161 |
"Unable to open file", |
| 162 |
JOptionPane.ERROR_MESSAGE); |
| 163 |
} catch (IOException ioe) { |
| 164 |
JOptionPane.showMessageDialog(this, "Unable to read file: " + ioe, |
| 165 |
"Unable to read file", |
| 166 |
JOptionPane.ERROR_MESSAGE); |
| 167 |
} catch (Exception e) { |
| 168 |
JOptionPane.showMessageDialog(this, "Error: " + e, |
| 169 |
"Error", |
| 170 |
JOptionPane.ERROR_MESSAGE); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
protected void importFromFile(File file) throws IOException, Importer.ImportException { |
| 177 |
|
| 178 |
BufferedReader reader = new BufferedReader(new FileReader(file)); |
| 179 |
|
| 180 |
String line = reader.readLine(); |
| 181 |
Tree tree = null; |
| 182 |
|
| 183 |
if (line.toUpperCase().startsWith("#NEXUS")) { |
| 184 |
NexusImporter importer = new NexusImporter(reader); |
| 185 |
tree = importer.importTree(null); |
| 186 |
} else { |
| 187 |
reader.close(); |
| 188 |
reader = new BufferedReader(new FileReader(file)); |
| 189 |
NewickImporter importer = new NewickImporter(reader); |
| 190 |
tree = importer.importTree(null); |
| 191 |
} |
| 192 |
|
| 193 |
treeStatData.allTaxa = TreeUtils.getLeafSet(tree); |
| 194 |
statusLabel.setText(Integer.toString(treeStatData.allTaxa.size()) + " taxa loaded."); |
| 195 |
reader.close(); |
| 196 |
|
| 197 |
fireDataChanged(); |
| 198 |
} |
| 199 |
|
| 200 |
public final void doExport() { |
| 201 |
|
| 202 |
FileDialog inDialog = new FileDialog(this, |
| 203 |
"Import Tree File...", |
| 204 |
FileDialog.LOAD); |
| 205 |
|
| 206 |
inDialog.setVisible(true); |
| 207 |
if (inDialog.getFile() != null) { |
| 208 |
File inFile = new File(inDialog.getDirectory(), inDialog.getFile()); |
| 209 |
|
| 210 |
|
| 211 |
FileDialog outDialog = new FileDialog(this, |
| 212 |
"Save Log File As...", |
| 213 |
FileDialog.SAVE); |
| 214 |
|
| 215 |
outDialog.setVisible(true); |
| 216 |
if (outDialog.getFile() != null) { |
| 217 |
|
| 218 |
File outFile = new File(outDialog.getDirectory(), outDialog.getFile()); |
| 219 |
|
| 220 |
try { |
| 221 |
processTreeFile(inFile, outFile); |
| 222 |
|
| 223 |
} catch (FileNotFoundException fnfe) { |
| 224 |
JOptionPane.showMessageDialog(this, "Unable to open file: File not found", |
| 225 |
"Unable to open file", |
| 226 |
JOptionPane.ERROR_MESSAGE); |
| 227 |
} catch (IOException ioe) { |
| 228 |
JOptionPane.showMessageDialog(this, "Unable to read/write file: " + ioe, |
| 229 |
"Unable to read/write file", |
| 230 |
JOptionPane.ERROR_MESSAGE); |
| 231 |
} catch (Importer.ImportException ie) { |
| 232 |
JOptionPane.showMessageDialog(this, "Unable to import file: " + ie, |
| 233 |
"Unable to import tree file", |
| 234 |
JOptionPane.ERROR_MESSAGE); |
| 235 |
} catch (Exception e) { |
| 236 |
JOptionPane.showMessageDialog(this, "Error: " + e, |
| 237 |
"Error", |
| 238 |
JOptionPane.ERROR_MESSAGE); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
protected void processTreeFile(File inFile, File outFile) throws IOException, Importer.ImportException { |
| 245 |
processTreeFileAction.setEnabled(false); |
| 246 |
|
| 247 |
BufferedReader r = new BufferedReader(new FileReader(inFile)); |
| 248 |
String line = r.readLine(); |
| 249 |
r.close(); |
| 250 |
|
| 251 |
final ProgressMonitorInputStream in = new ProgressMonitorInputStream( |
| 252 |
this, |
| 253 |
"Reading " + inFile.getName(), |
| 254 |
new FileInputStream(inFile)); |
| 255 |
in.getProgressMonitor().setMillisToDecideToPopup(0); |
| 256 |
in.getProgressMonitor().setMillisToPopup(0); |
| 257 |
|
| 258 |
final Reader reader = new InputStreamReader(new BufferedInputStream(in)); |
| 259 |
|
| 260 |
|
| 261 |
final TreeImporter importer; |
| 262 |
|
| 263 |
if (line.toUpperCase().startsWith("#NEXUS")) { |
| 264 |
importer = new NexusImporter(reader); |
| 265 |
} else { |
| 266 |
reader.close(); |
| 267 |
importer = new NewickImporter(reader); |
| 268 |
} |
| 269 |
|
| 270 |
final Tree firstTree = importer.importNextTree(); |
| 271 |
boolean isUltrametric = TreeUtils.isUltrametric(firstTree); |
| 272 |
boolean isBinary = TreeUtils.isBinary(firstTree); |
| 273 |
boolean stop = false; |
| 274 |
|
| 275 |
|
| 276 |
for (int i = 0; i < treeStatData.statistics.size(); i++) { |
| 277 |
TreeSummaryStatistic tss = (TreeSummaryStatistic)treeStatData.statistics.get(i); |
| 278 |
String label = tss.getSummaryStatisticName(); |
| 279 |
|
| 280 |
if (!isUltrametric && !tss.allowsNonultrametricTrees()) { |
| 281 |
if (JOptionPane.showConfirmDialog( |
| 282 |
this, "Warning: These trees may not be ultrametric and this is\na requirement of the " + |
| 283 |
label + " statistic. Do you wish to continue?", "Warning", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { |
| 284 |
stop = true; |
| 285 |
break; |
| 286 |
} |
| 287 |
|
| 288 |
isUltrametric = true; |
| 289 |
} |
| 290 |
|
| 291 |
if (!isBinary && !tss.allowsPolytomies()) { |
| 292 |
if (JOptionPane.showConfirmDialog( |
| 293 |
this, "Warning: These trees may not be strictly bifurcating and this is\na requirement of the " + |
| 294 |
label + " statistic. Do you wish to continue?", "Warning", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { |
| 295 |
stop = true; |
| 296 |
break; |
| 297 |
} |
| 298 |
|
| 299 |
isBinary = true; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
if (stop) { |
| 304 |
processTreeFileAction.setEnabled(true); |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
final PrintWriter writer = new PrintWriter(new FileWriter(outFile)); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
Tree tree = firstTree; |
| 313 |
|
| 314 |
writer.print("state"); |
| 315 |
for (int i = 0; i < treeStatData.statistics.size(); i++) { |
| 316 |
TreeSummaryStatistic tss = (TreeSummaryStatistic)treeStatData.statistics.get(i); |
| 317 |
|
| 318 |
int dim = tss.getStatisticDimensions(tree); |
| 319 |
for (int j = 0; j < dim; j++) { |
| 320 |
writer.print("\t" + tss.getStatisticLabel(tree, j)); |
| 321 |
} |
| 322 |
|
| 323 |
} |
| 324 |
writer.println(); |
| 325 |
|
| 326 |
state = 0; |
| 327 |
do { |
| 328 |
writer.print(state); |
| 329 |
|
| 330 |
for (int i = 0; i < treeStatData.statistics.size(); i++) { |
| 331 |
TreeSummaryStatistic tss = (TreeSummaryStatistic)treeStatData.statistics.get(i); |
| 332 |
double[] stats = tss.getSummaryStatistic(tree); |
| 333 |
for (int j = 0; j < stats.length; j++) { |
| 334 |
writer.print("\t" + stats[j]); |
| 335 |
} |
| 336 |
} |
| 337 |
writer.println(); |
| 338 |
state += 1; |
| 339 |
|
| 340 |
final int currentState = state; |
| 341 |
|
| 342 |
in.getProgressMonitor().setNote("Processing Tree " + currentState + "..."); |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
tree = importer.importNextTree(); |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
} while (tree != null); |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
reader.close(); |
| 382 |
writer.close(); |
| 383 |
|
| 384 |
progressLabel.setText("" + state + " trees processed."); |
| 385 |
processTreeFileAction.setEnabled(true); |
| 386 |
} |
| 387 |
|
| 388 |
private int state = 0; |
| 389 |
|
| 390 |
public void doCopy() { |
| 391 |
|
| 392 |
} |
| 393 |
|
| 394 |
public JComponent getExportableComponent() { |
| 395 |
|
| 396 |
return statisticsPanel.getExportableComponent(); |
| 397 |
} |
| 398 |
|
| 399 |
protected AbstractAction importTaxaAction = new AbstractAction("Import Taxa...") { |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
private static final long serialVersionUID = -3185667996732228702L; |
| 404 |
|
| 405 |
public void actionPerformed(java.awt.event.ActionEvent ae) { |
| 406 |
doImport(); |
| 407 |
} |
| 408 |
}; |
| 409 |
|
| 410 |
protected AbstractAction processTreeFileAction = new AbstractAction("Process Tree File...", gearIcon) { |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
private static final long serialVersionUID = -8285433136692586532L; |
| 415 |
|
| 416 |
public void actionPerformed(java.awt.event.ActionEvent ae) { |
| 417 |
doExport(); |
| 418 |
} |
| 419 |
}; |
| 420 |
|
| 421 |
TreeImporter treeImporter; |
| 422 |
} |