Debian Codegraph @

src/dr/app/coalgen/CoalGenFrame.java

raw symbols SCIP
1 /*
2 * CoalGenFrame.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.coalgen;
27
28 import dr.evolution.coalescent.DemographicFunction;
29 import dr.evolution.io.Importer;
30 import dr.evolution.io.NewickImporter;
31 import dr.evolution.io.NexusImporter;
32 import dr.evolution.tree.Tree;
33 import dr.evolution.tree.TreeUtils;
34 import dr.inference.trace.LogFileTraces;
35 import dr.inference.trace.TraceException;
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.io.*;
43
44 /**
45 * @author Andrew Rambaut
46 * @author Alexei Drummond
47 * @version $Id$
48 */
49 public class CoalGenFrame extends DocumentFrame {
50
51 private CoalGenData data = null;
52
53 private final JTabbedPane tabbedPane = new JTabbedPane();
54 private InputsPanel inputsPanel;
55 private DataPanel dataPanel;
56 private ModelPanel modelPanel;
57 private SimulationsPanel simulationsPanel;
58
59 private JLabel statusLabel;
60
61 public CoalGenFrame(String title) {
62 super();
63
64 setTitle(title);
65
66 data = new CoalGenData();
67 }
68
69 public void initializeComponents() {
70
71 setSize(new java.awt.Dimension(800, 600));
72
73 inputsPanel = new InputsPanel(this, data);
74 dataPanel = new DataPanel(this, data);
75 modelPanel = new ModelPanel(this, data);
76 simulationsPanel = new SimulationsPanel(this, data);
77
78 tabbedPane.addTab("Inputs", null, inputsPanel);
79 tabbedPane.addTab("Taxa", null, dataPanel);
80 tabbedPane.addTab("Model", null, modelPanel);
81 tabbedPane.addTab("Simulations", null, simulationsPanel);
82
83 statusLabel = new JLabel("No taxa loaded");
84
85 JPanel progressPanel = new JPanel(new BorderLayout(0, 0));
86 JLabel progressLabel = new JLabel("");
87 JProgressBar progressBar = new JProgressBar();
88 progressPanel.add(progressLabel, BorderLayout.NORTH);
89 progressPanel.add(progressBar, BorderLayout.CENTER);
90
91 JPanel panel1 = new JPanel(new BorderLayout(0, 0));
92 panel1.add(statusLabel, BorderLayout.CENTER);
93 panel1.add(progressPanel, BorderLayout.EAST);
94 panel1.setBorder(new BorderUIResource.EmptyBorderUIResource(new java.awt.Insets(0, 6, 0, 6)));
95
96 JPanel panel = new JPanel(new BorderLayout(0, 0));
97 panel.add(tabbedPane, BorderLayout.CENTER);
98 panel.add(panel1, BorderLayout.SOUTH);
99 panel.setBorder(new BorderUIResource.EmptyBorderUIResource(new java.awt.Insets(12, 12, 12, 12)));
100
101 getContentPane().setLayout(new java.awt.BorderLayout(0, 0));
102 getContentPane().add(panel, BorderLayout.CENTER);
103 }
104
105 public void fireTaxaChanged() {
106 }
107
108 public void fireModelChanged() {
109 modelPanel.collectSettings();
110 }
111
112
113 public void fireTracesChanged() {
114 inputsPanel.tracesChanged();
115 modelPanel.tracesChanged();
116 simulationsPanel.tracesChanged();
117 }
118
119 public final void dataSelectionChanged(boolean isSelected) {
120 if (isSelected) {
121 getDeleteAction().setEnabled(true);
122 } else {
123 getDeleteAction().setEnabled(false);
124 }
125 }
126
127 public void doDelete() {
128 // if (tabbedPane.getSelectedComponent() == dataPanel) {
129 // dataPanel.deleteSelection();
130 // } else {
131 // throw new RuntimeException("Delete should only be accessable from the Data panel");
132 // }
133 }
134
135 protected boolean writeToFile(File file) {
136 inputsPanel.collectSettings();
137 modelPanel.collectSettings();
138 simulationsPanel.collectSettings();
139 return false;
140 }
141
142 protected boolean readFromFile(final File file) throws IOException {
143
144 try {
145 final String fileName = file.getName();
146 final ProgressMonitorInputStream in = new ProgressMonitorInputStream(
147 this,
148 "Reading " + fileName,
149 new FileInputStream(file));
150
151 // final Reader reader = new InputStreamReader(in);
152 final JFrame frame = this;
153
154 // the monitored activity must be in a new thread.
155 Thread readThread = new Thread() {
156 public void run() {
157 try {
158 final File file1 = new File(fileName);
159 final LogFileTraces traces = new LogFileTraces(fileName, file1);
160 traces.loadTraces(in);
161
162 EventQueue.invokeLater(
163 new Runnable() {
164 public void run() {
165 data.logFile = file;
166 data.traces = traces;
167 fireTracesChanged();
168 }
169 });
170
171 } catch (final TraceException tex) {
172 EventQueue.invokeLater(
173 new Runnable() {
174 public void run() {
175 JOptionPane.showMessageDialog(frame, "Error reading trace file: " + tex,
176 "Error reading trace file",
177 JOptionPane.ERROR_MESSAGE);
178 }
179 });
180 } catch (final InterruptedIOException iioex) {
181 // The cancel dialog button was pressed - do nothing
182 } catch (final IOException ioex) {
183 EventQueue.invokeLater(
184 new Runnable() {
185 public void run() {
186 JOptionPane.showMessageDialog(frame, "File I/O Error: " + ioex,
187 "File I/O Error",
188 JOptionPane.ERROR_MESSAGE);
189 }
190 });
191 } catch (final Exception ex) {
192 EventQueue.invokeLater(
193 new Runnable() {
194 public void run() {
195 JOptionPane.showMessageDialog(frame, "Fatal exception: " + ex,
196 "Error reading file",
197 JOptionPane.ERROR_MESSAGE);
198 }
199 });
200 }
201
202 }
203 };
204 readThread.start();
205
206 } catch (IOException ioex) {
207 JOptionPane.showMessageDialog(this, "File I/O Error: " + ioex,
208 "File I/O Error",
209 JOptionPane.ERROR_MESSAGE);
210 return false;
211 } catch (Exception ex) {
212 JOptionPane.showMessageDialog(this, "Fatal exception: " + ex,
213 "Error reading file",
214 JOptionPane.ERROR_MESSAGE);
215 return false;
216 }
217 return true;
218 }
219
220 public final void doImport() {
221
222 FileDialog dialog = new FileDialog(this,
223 "Import Tree or Alignment...",
224 FileDialog.LOAD);
225
226 dialog.setVisible(true);
227 if (dialog.getFile() != null) {
228 File file = new File(dialog.getDirectory(), dialog.getFile());
229
230 try {
231 importFromFile(file);
232
233
234 } catch (Importer.ImportException ie) {
235 JOptionPane.showMessageDialog(this, "Unable to read file: " + ie,
236 "Unable to read file",
237 JOptionPane.ERROR_MESSAGE);
238 } catch (FileNotFoundException fnfe) {
239 JOptionPane.showMessageDialog(this, "Unable to open file: File not found",
240 "Unable to open file",
241 JOptionPane.ERROR_MESSAGE);
242 } catch (IOException ioe) {
243 JOptionPane.showMessageDialog(this, "Unable to read file: " + ioe,
244 "Unable to read file",
245 JOptionPane.ERROR_MESSAGE);
246 }
247 }
248
249 }
250
251 protected void importFromFile(File file) throws IOException, Importer.ImportException {
252
253 BufferedReader reader = new BufferedReader(new FileReader(file));
254
255 String line = reader.readLine();
256 Tree tree;
257
258 if (line.toUpperCase().startsWith("#NEXUS")) {
259 NexusImporter importer = new NexusImporter(reader);
260 tree = importer.importTree(null);
261 } else {
262 NewickImporter importer = new NewickImporter(reader);
263 tree = importer.importTree(null);
264 }
265
266 data.taxonList = tree;
267 statusLabel.setText(Integer.toString(data.taxonList.getTaxonCount()) + " taxa loaded.");
268 reader.close();
269
270 fireTaxaChanged();
271 }
272
273 public final void doExport() {
274
275 FileDialog outDialog = new FileDialog(this,
276 "Save Log File As...",
277 FileDialog.SAVE);
278
279 outDialog.setVisible(true);
280 if (outDialog.getFile() != null) {
281
282 File outFile = new File(outDialog.getDirectory(), outDialog.getFile());
283
284 try {
285 generateFile(outFile);
286
287 } catch (IOException ioe) {
288 JOptionPane.showMessageDialog(this, "Unable to write file: " + ioe,
289 "Unable to read/write file",
290 JOptionPane.ERROR_MESSAGE);
291 }
292 }
293 }
294
295 protected void generateFile(File outFile) throws IOException {
296
297 PrintWriter writer = new PrintWriter(new FileWriter(outFile));
298
299 dr.evolution.coalescent.CoalescentSimulator simulator = new dr.evolution.coalescent.CoalescentSimulator();
300
301 int count = 0;
302 while (data.hasNext()) {
303 DemographicFunction demo = data.nextDemographic();
304
305 Tree tree = simulator.simulateTree(data.taxonList, demo);
306
307 writer.println(count + "\t" + TreeUtils.newick(tree));
308 count += 1;
309 }
310
311 writer.close();
312 }
313
314 public void doCopy() {
315 }
316
317 public JComponent getExportableComponent() {
318
319 JComponent exportable = null;
320 Component comp = tabbedPane.getSelectedComponent();
321
322 if (comp instanceof Exportable) {
323 exportable = ((Exportable) comp).getExportableComponent();
324 } else if (comp instanceof JComponent) {
325 exportable = (JComponent) comp;
326 }
327
328 return exportable;
329 }
330
331 public boolean useImportAction() {
332 return true;
333 }
334
335 public Action getImportAction() {
336 return importTaxaAction;
337 }
338
339 protected AbstractAction importTaxaAction = new AbstractAction("Import Taxa...") {
340 public void actionPerformed(java.awt.event.ActionEvent ae) {
341 doImport();
342 }
343 };
344
345 public boolean useExportAction() {
346 return true;
347 }
348
349 public Action getExportAction() {
350 return generateSimulationsAction;
351 }
352
353 protected AbstractAction generateSimulationsAction = new AbstractAction("Generate Simulations...") {
354 public void actionPerformed(java.awt.event.ActionEvent ae) {
355 doExport();
356 }
357 };
358 }