Debian Codegraph @

src/dr/app/tempest/TempestFrame.java

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