| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
package dr.app.pathogen; |
| 35 |
|
| 36 |
import dr.evolution.io.*; |
| 37 |
import dr.evolution.tree.NodeRef; |
| 38 |
import dr.evolution.tree.Tree; |
| 39 |
import dr.evolution.tree.FlexibleTree; |
| 40 |
import dr.evolution.util.TaxonList; |
| 41 |
import dr.app.tools.NexusExporter; |
| 42 |
import dr.stats.Regression; |
| 43 |
import dr.util.NumberFormatter; |
| 44 |
import figtree.treeviewer.TreeViewer; |
| 45 |
import jam.framework.DocumentFrame; |
| 46 |
import jam.framework.Exportable; |
| 47 |
import jam.panels.SearchPanel; |
| 48 |
import jam.panels.SearchPanelListener; |
| 49 |
import jam.toolbar.Toolbar; |
| 50 |
import jam.toolbar.ToolbarAction; |
| 51 |
import jam.toolbar.ToolbarButton; |
| 52 |
import jam.util.IconUtils; |
| 53 |
|
| 54 |
import javax.swing.*; |
| 55 |
import javax.swing.plaf.BorderUIResource; |
| 56 |
import java.awt.*; |
| 57 |
import java.awt.datatransfer.Clipboard; |
| 58 |
import java.awt.datatransfer.StringSelection; |
| 59 |
import java.awt.event.ActionEvent; |
| 60 |
import java.io.*; |
| 61 |
import java.util.*; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public class PathogenFrame extends DocumentFrame { |
| 69 |
|
| 70 |
private static final long serialVersionUID = 2114148696789612509L; |
| 71 |
|
| 72 |
private JTabbedPane tabbedPane = new JTabbedPane(); |
| 73 |
private JLabel statusLabel = new JLabel("No data loaded"); |
| 74 |
|
| 75 |
private PathogenPanel pathogenPanel; |
| 76 |
|
| 77 |
TaxonList taxa = null; |
| 78 |
java.util.List<Tree> trees = new ArrayList<Tree>(); |
| 79 |
|
| 80 |
public PathogenFrame(String title) { |
| 81 |
super(); |
| 82 |
|
| 83 |
setTitle(title); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); |
| 88 |
|
| 89 |
getOpenAction().setEnabled(true); |
| 90 |
getSaveAction().setEnabled(false); |
| 91 |
getSaveAsAction().setEnabled(false); |
| 92 |
|
| 93 |
getFindAction().setEnabled(false); |
| 94 |
|
| 95 |
getCutAction().setEnabled(false); |
| 96 |
getPasteAction().setEnabled(false); |
| 97 |
getDeleteAction().setEnabled(false); |
| 98 |
getSelectAllAction().setEnabled(false); |
| 99 |
|
| 100 |
getCopyAction().setEnabled(false); |
| 101 |
|
| 102 |
getZoomWindowAction().setEnabled(false); |
| 103 |
} |
| 104 |
|
| 105 |
public void initializeComponents() { |
| 106 |
|
| 107 |
pathogenPanel = new PathogenPanel(this, taxa, trees.get(0)); |
| 108 |
|
| 109 |
JPanel panel = new JPanel(new BorderLayout(0, 0)); |
| 110 |
panel.add(pathogenPanel, BorderLayout.CENTER); |
| 111 |
|
| 112 |
statusLabel.setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(0, 12, 6, 12))); |
| 113 |
panel.add(statusLabel, BorderLayout.SOUTH); |
| 114 |
|
| 115 |
getContentPane().setLayout(new BorderLayout(0, 0)); |
| 116 |
getContentPane().add(panel, BorderLayout.CENTER); |
| 117 |
|
| 118 |
setSize(new Dimension(1024, 768)); |
| 119 |
|
| 120 |
setStatusMessage(); |
| 121 |
} |
| 122 |
|
| 123 |
public void timeScaleChanged() { |
| 124 |
pathogenPanel.timeScaleChanged(); |
| 125 |
setStatusMessage(); |
| 126 |
} |
| 127 |
|
| 128 |
protected boolean readFromFile(File file) throws IOException { |
| 129 |
Reader reader = new FileReader(file); |
| 130 |
|
| 131 |
BufferedReader bufferedReader = new BufferedReader(reader); |
| 132 |
String line = bufferedReader.readLine(); |
| 133 |
while (line != null && line.length() == 0) { |
| 134 |
line = bufferedReader.readLine(); |
| 135 |
} |
| 136 |
|
| 137 |
boolean isNexus = (line != null && line.toUpperCase().contains("#NEXUS")); |
| 138 |
|
| 139 |
reader = new FileReader(file); |
| 140 |
|
| 141 |
Tree tree = null; |
| 142 |
try { |
| 143 |
if (isNexus) { |
| 144 |
NexusImporter importer = new NexusImporter(reader); |
| 145 |
tree = importer.importTree(taxa); |
| 146 |
} else { |
| 147 |
NewickImporter importer = new NewickImporter(reader); |
| 148 |
tree = importer.importTree(taxa); |
| 149 |
} |
| 150 |
|
| 151 |
} catch (Importer.ImportException ime) { |
| 152 |
JOptionPane.showMessageDialog(this, "Error parsing imported file: " + ime, |
| 153 |
"Error reading file", |
| 154 |
JOptionPane.ERROR_MESSAGE); |
| 155 |
ime.printStackTrace(); |
| 156 |
return false; |
| 157 |
} catch (IOException ioex) { |
| 158 |
JOptionPane.showMessageDialog(this, "File I/O Error: " + ioex, |
| 159 |
"File I/O Error", |
| 160 |
JOptionPane.ERROR_MESSAGE); |
| 161 |
ioex.printStackTrace(); |
| 162 |
return false; |
| 163 |
} catch (Exception ex) { |
| 164 |
JOptionPane.showMessageDialog(this, "Fatal exception: " + ex, |
| 165 |
"Error reading file", |
| 166 |
JOptionPane.ERROR_MESSAGE); |
| 167 |
ex.printStackTrace(); |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
if (tree == null) { |
| 173 |
JOptionPane.showMessageDialog(this, "The file is not in a suitable format or contains no trees.", |
| 174 |
"Error reading file", |
| 175 |
JOptionPane.ERROR_MESSAGE); |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
FlexibleTree binaryTree = new FlexibleTree(tree, true); |
| 180 |
binaryTree.resolveTree(); |
| 181 |
trees.add(binaryTree); |
| 182 |
if (taxa == null) { |
| 183 |
taxa = binaryTree; |
| 184 |
} |
| 185 |
|
| 186 |
getExportTreeAction().setEnabled(true); |
| 187 |
getExportDataAction().setEnabled(true); |
| 188 |
|
| 189 |
return true; |
| 190 |
} |
| 191 |
|
| 192 |
protected boolean writeToFile(File file) throws IOException { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
protected void doExportTree() { |
| 197 |
FileDialog dialog = new FileDialog(this, |
| 198 |
"Export Tree File...", |
| 199 |
FileDialog.SAVE); |
| 200 |
|
| 201 |
dialog.setVisible(true); |
| 202 |
if (dialog.getFile() != null) { |
| 203 |
File file = new File(dialog.getDirectory(), dialog.getFile()); |
| 204 |
|
| 205 |
PrintStream ps = null; |
| 206 |
try { |
| 207 |
ps = new PrintStream(file); |
| 208 |
writeTreeFile(ps, false); |
| 209 |
ps.close(); |
| 210 |
} catch (IOException ioe) { |
| 211 |
JOptionPane.showMessageDialog(this, "Error writing tree file: " + ioe.getMessage(), |
| 212 |
"Export Error", |
| 213 |
JOptionPane.ERROR_MESSAGE); |
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
private void doExportTimeTree() { |
| 220 |
FileDialog dialog = new FileDialog(this, |
| 221 |
"Export Time Tree File...", |
| 222 |
FileDialog.SAVE); |
| 223 |
|
| 224 |
dialog.setVisible(true); |
| 225 |
if (dialog.getFile() != null) { |
| 226 |
File file = new File(dialog.getDirectory(), dialog.getFile()); |
| 227 |
|
| 228 |
PrintStream ps = null; |
| 229 |
try { |
| 230 |
ps = new PrintStream(file); |
| 231 |
writeTimeTreeFile(ps); |
| 232 |
ps.close(); |
| 233 |
} catch (IOException ioe) { |
| 234 |
JOptionPane.showMessageDialog(this, "Error writing tree file: " + ioe.getMessage(), |
| 235 |
"Export Error", |
| 236 |
JOptionPane.ERROR_MESSAGE); |
| 237 |
} |
| 238 |
|
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
protected void writeTimeTreeFile(PrintStream ps) throws IOException { |
| 243 |
|
| 244 |
FlexibleTree tree = new FlexibleTree(pathogenPanel.getTreeAsViewed()); |
| 245 |
|
| 246 |
Regression r = pathogenPanel.getTemporalRooting().getRootToTipRegression(pathogenPanel.getTreeAsViewed()); |
| 247 |
|
| 248 |
for (int i = 0; i < tree.getInternalNodeCount(); i++) { |
| 249 |
NodeRef node = tree.getInternalNode(i); |
| 250 |
double height = tree.getNodeHeight(node); |
| 251 |
tree.setNodeHeight(node, height/r.getGradient()); |
| 252 |
} |
| 253 |
|
| 254 |
TreeUtils.setHeightsFromDates(tree); |
| 255 |
|
| 256 |
NexusExporter nexusExporter = new NexusExporter(new PrintStream(ps)); |
| 257 |
nexusExporter.exportTree(tree); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
protected void writeTreeFile(PrintStream ps, boolean newickFormat) throws IOException { |
| 262 |
|
| 263 |
Tree tree = pathogenPanel.getTreeAsViewed(); |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
NexusExporter nexusExporter = new NexusExporter(new PrintStream(ps)); |
| 270 |
nexusExporter.exportTree(tree); |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
protected void doExportData() { |
| 280 |
FileDialog dialog = new FileDialog(this, |
| 281 |
"Export Data File...", |
| 282 |
FileDialog.SAVE); |
| 283 |
|
| 284 |
dialog.setVisible(true); |
| 285 |
if (dialog.getFile() != null) { |
| 286 |
File file = new File(dialog.getDirectory(), dialog.getFile()); |
| 287 |
|
| 288 |
Writer writer = null; |
| 289 |
try { |
| 290 |
writer = new PrintWriter(file); |
| 291 |
pathogenPanel.writeDataFile(writer); |
| 292 |
writer.close(); |
| 293 |
} catch (IOException ioe) { |
| 294 |
JOptionPane.showMessageDialog(this, "Error writing data file: " + ioe.getMessage(), |
| 295 |
"Export Error", |
| 296 |
JOptionPane.ERROR_MESSAGE); |
| 297 |
} |
| 298 |
|
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
private void setStatusMessage() { |
| 303 |
Tree tree = pathogenPanel.getTree(); |
| 304 |
if (tree != null) { |
| 305 |
String message = ""; |
| 306 |
message += "Tree loaded, " + tree.getTaxonCount() + " taxa"; |
| 307 |
|
| 308 |
TemporalRooting tr = pathogenPanel.getTemporalRooting(); |
| 309 |
if (tr.isContemporaneous()) { |
| 310 |
message += ", contemporaneous tips"; |
| 311 |
} else { |
| 312 |
NumberFormatter nf = new NumberFormatter(3); |
| 313 |
message += ", dated tips with range " + nf.format(tr.getDateRange()); |
| 314 |
} |
| 315 |
statusLabel.setText(message); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
public JComponent getExportableComponent() { |
| 320 |
|
| 321 |
JComponent exportable = null; |
| 322 |
Component comp = tabbedPane.getSelectedComponent(); |
| 323 |
|
| 324 |
if (comp instanceof Exportable) { |
| 325 |
exportable = ((Exportable) comp).getExportableComponent(); |
| 326 |
} else if (comp instanceof JComponent) { |
| 327 |
exportable = (JComponent) comp; |
| 328 |
} |
| 329 |
|
| 330 |
return exportable; |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
public void doCopy() { |
| 335 |
StringWriter writer = new StringWriter(); |
| 336 |
PrintWriter pwriter = new PrintWriter(writer); |
| 337 |
|
| 338 |
for (String tip : pathogenPanel.getSelectedTips()) { |
| 339 |
pwriter.println(tip); |
| 340 |
} |
| 341 |
|
| 342 |
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
| 343 |
StringSelection selection = new StringSelection(writer.toString()); |
| 344 |
clipboard.setContents(selection, selection); |
| 345 |
} |
| 346 |
|
| 347 |
public Action getExportTreeAction() { |
| 348 |
return exportTreeAction; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public Action getExportDataAction() { |
| 356 |
return exportDataAction; |
| 357 |
} |
| 358 |
|
| 359 |
protected AbstractAction exportTreeAction = new AbstractAction("Export Tree...") { |
| 360 |
public void actionPerformed(ActionEvent ae) { |
| 361 |
doExportTree(); |
| 362 |
} |
| 363 |
}; |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
protected AbstractAction exportDataAction = new AbstractAction("Export Data...") { |
| 372 |
public void actionPerformed(ActionEvent ae) { |
| 373 |
doExportData(); |
| 374 |
} |
| 375 |
}; |
| 376 |
|
| 377 |
public Action getExportTimeTreeAction() { |
| 378 |
return exportTimeTreeAction; |
| 379 |
} |
| 380 |
|
| 381 |
protected AbstractAction exportTimeTreeAction = new AbstractAction("Export Time Tree...") { |
| 382 |
public void actionPerformed(ActionEvent ae) { |
| 383 |
doExportTimeTree(); |
| 384 |
} |
| 385 |
}; |
| 386 |
|
| 387 |
|
| 388 |
} |