Debian Codegraph @

src/dr/app/beauti/mcmcpanel/MCMCPanel.java

raw symbols SCIP
1 /*
2 * MCMCPanel.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.beauti.mcmcpanel;
27
28
29 import dr.app.beauti.BeautiFrame;
30 import dr.app.beauti.BeautiPanel;
31 import dr.app.beauti.components.marginalLikelihoodEstimation.MLEDialog;
32 import dr.app.beauti.components.marginalLikelihoodEstimation.MLEGSSDialog;
33 import dr.app.beauti.components.marginalLikelihoodEstimation.MarginalLikelihoodEstimationOptions;
34 import dr.app.beauti.options.AbstractPartitionData;
35 import dr.app.beauti.options.BeautiOptions;
36 import dr.app.beauti.options.PartitionTreeModel;
37 import dr.app.beauti.util.PanelUtils;
38 import dr.app.gui.components.WholeNumberField;
39 import dr.app.util.OSType;
40 import dr.evolution.datatype.DataType;
41 import dr.evolution.datatype.Microsatellite;
42 import jam.panels.OptionsPanel;
43
44 import javax.swing.*;
45 import javax.swing.event.ChangeEvent;
46 import javax.swing.event.ChangeListener;
47 import java.awt.*;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.KeyEvent;
50 import java.util.List;
51
52 /**
53 * @author Andrew Rambaut
54 * @author Alexei Drummond
55 * @version $Id: MCMCPanel.java,v 1.16 2006/09/05 13:29:34 rambaut Exp $
56 */
57 public class MCMCPanel extends BeautiPanel {
58
59 private static final long serialVersionUID = -3710586474593827540L;
60
61 WholeNumberField chainLengthField = new WholeNumberField(1, Integer.MAX_VALUE);
62 WholeNumberField echoEveryField = new WholeNumberField(1, Integer.MAX_VALUE);
63 WholeNumberField logEveryField = new WholeNumberField(1, Integer.MAX_VALUE);
64
65 JCheckBox samplePriorCheckBox = new JCheckBox("Sample from prior only - create empty alignment");
66 JComboBox performMLECombo = new JComboBox(new String[] {"None", "path sampling/stepping-stone sampling", "generalized stepping-stone sampling"});
67 // JCheckBox performMLE = new JCheckBox("Perform marginal likelihood estimation (MLE) using path sampling/stepping-stone sampling");
68 JButton buttonMLE = new JButton("Settings");
69 // JCheckBox performMLEGSS = new JCheckBox("Perform marginal likelihood estimation (MLE) using generalized stepping-stone sampling");
70 // JButton buttonMLEGSS = new JButton("Settings");
71
72 public static final String DEFAULT_FILE_NAME_STEM = "untitled";
73 JTextField fileNameStemField = new JTextField(DEFAULT_FILE_NAME_STEM);
74
75 private JCheckBox addTxt = new JCheckBox("Add .txt suffix");
76
77 JTextArea logFileNameField = new JTextArea(DEFAULT_FILE_NAME_STEM + ".log");
78 JTextArea treeFileNameField = new JTextArea(DEFAULT_FILE_NAME_STEM + ".trees");
79 // JCheckBox allowOverwriteLogCheck = new JCheckBox("Allow to overwrite the existing log file");
80
81 // JCheckBox mapTreeLogCheck = new JCheckBox("Create tree file containing the MAP tree:");
82 // JTextField mapTreeFileNameField = new JTextField("untitled.MAP.tree");
83
84 JCheckBox substTreeLogCheck = new JCheckBox("Create tree log file with branch length in substitutions:");
85 JTextArea substTreeFileNameField = new JTextArea("untitled(subst).trees");
86
87 JCheckBox operatorAnalysisCheck = new JCheckBox("Create operator analysis file:");
88 JTextArea operatorAnalysisFileNameField = new JTextArea(DEFAULT_FILE_NAME_STEM + ".ops");
89
90 BeautiFrame frame = null;
91 private final OptionsPanel optionsPanel;
92 private BeautiOptions options;
93
94 private MLEDialog mleDialog = null;
95 private MLEGSSDialog mleGssDialog = null;
96 private MarginalLikelihoodEstimationOptions mleOptions = new MarginalLikelihoodEstimationOptions();
97
98 public MCMCPanel(BeautiFrame parent) {
99 setLayout(new BorderLayout());
100
101 // Mac OS X components have more spacing round them already
102 optionsPanel = new OptionsPanel(12, (OSType.isMac() ? 6 : 24));
103
104 this.frame = parent;
105
106 setOpaque(false);
107 optionsPanel.setOpaque(false);
108
109 chainLengthField.setValue(100000);
110 chainLengthField.setColumns(10);
111 optionsPanel.addComponentWithLabel("Length of chain:", chainLengthField);
112 chainLengthField.addKeyListener(new java.awt.event.KeyListener() {
113 public void keyTyped(KeyEvent e) {
114 }
115
116 public void keyPressed(KeyEvent e) {
117 }
118
119 public void keyReleased(KeyEvent e) {
120 options.chainLength = chainLengthField.getValue();
121 frame.setDirty();
122 }
123 });
124
125 optionsPanel.addSeparator();
126
127 echoEveryField.setValue(1000);
128 echoEveryField.setColumns(10);
129 optionsPanel.addComponentWithLabel("Echo state to screen every:", echoEveryField);
130 echoEveryField.addKeyListener(new java.awt.event.KeyListener() {
131 public void keyTyped(KeyEvent e) {
132 }
133
134 public void keyPressed(KeyEvent e) {
135 }
136
137 public void keyReleased(KeyEvent e) {
138 options.echoEvery = echoEveryField.getValue();
139 frame.setDirty();
140 }
141 });
142
143 logEveryField.setValue(100);
144 logEveryField.setColumns(10);
145 optionsPanel.addComponentWithLabel("Log parameters every:", logEveryField);
146 logEveryField.addKeyListener(new java.awt.event.KeyListener() {
147 public void keyTyped(KeyEvent e) {
148 }
149
150 public void keyPressed(KeyEvent e) {
151 }
152
153 public void keyReleased(KeyEvent e) {
154 options.logEvery = logEveryField.getValue();
155 frame.setDirty();
156 }
157 });
158
159 optionsPanel.addSeparator();
160
161 fileNameStemField.setColumns(32);
162 optionsPanel.addComponentWithLabel("File name stem:", fileNameStemField);
163 fileNameStemField.setEditable(true);
164 fileNameStemField.addKeyListener(new java.awt.event.KeyListener() {
165 public void keyTyped(KeyEvent e) {
166 }
167
168 public void keyPressed(KeyEvent e) {
169 }
170
171 public void keyReleased(KeyEvent e) {
172 options.fileNameStem = fileNameStemField.getText();
173 updateOtherFileNames(options);
174 frame.setDirty();
175 }
176 });
177
178 optionsPanel.addComponent(addTxt);
179 if (OSType.isWindows()) {
180 addTxt.setSelected(true);
181 } else {
182 addTxt.setSelected(false);
183 }
184 addTxt.addChangeListener(new ChangeListener() {
185 public void stateChanged(ChangeEvent changeEvent) {
186 setOptions(options);
187 frame.setDirty();
188 }
189 });
190
191 optionsPanel.addSeparator();
192
193 logFileNameField.setColumns(32);
194 optionsPanel.addComponentWithLabel("Log file name:", logFileNameField);
195 logFileNameField.setEditable(false);
196
197 // optionsPanel.addComponent(allowOverwriteLogCheck);
198 // allowOverwriteLogCheck.setSelected(false);
199 // allowOverwriteLogCheck.addChangeListener(new ChangeListener() {
200 // public void stateChanged(ChangeEvent changeEvent) {
201 // options.allowOverwriteLog = allowOverwriteLogCheck.isSelected();
202 // }
203 // });
204
205 treeFileNameField.setColumns(32);
206 optionsPanel.addComponentWithLabel("Trees file name:", treeFileNameField);
207 treeFileNameField.setEditable(false);
208
209
210 // addComponent(mapTreeLogCheck);
211 // mapTreeLogCheck.setOpaque(false);
212 // mapTreeLogCheck.addActionListener(new java.awt.event.ActionListener() {
213 // public void actionPerformed(ActionEvent e) {
214 // mapTreeFileNameField.setEnabled(mapTreeLogCheck.isSelected());
215 // }
216 // });
217 //
218 // mapTreeFileNameField.setColumns(32);
219 // addComponentWithLabel("MAP tree file name:", mapTreeFileNameField);
220
221 optionsPanel.addComponent(substTreeLogCheck);
222 substTreeLogCheck.setOpaque(false);
223 substTreeLogCheck.addActionListener(new java.awt.event.ActionListener() {
224 public void actionPerformed(ActionEvent e) {
225 options.substTreeLog = substTreeLogCheck.isSelected();
226 updateTreeFileNameList();
227 substTreeFileNameField.setEnabled(substTreeLogCheck.isSelected());
228 if (substTreeLogCheck.isSelected()) {
229 substTreeFileNameField.setText(displayTreeList(options.substTreeFileName));
230 } else {
231 substTreeFileNameField.setText("");
232 }
233
234 frame.setDirty();
235 }
236 });
237
238 substTreeFileNameField.setColumns(32);
239 substTreeFileNameField.setEditable(false);
240 substTreeFileNameField.setEnabled(false);
241 optionsPanel.addComponentWithLabel("Substitutions trees file name:", substTreeFileNameField);
242
243 optionsPanel.addComponent(operatorAnalysisCheck);
244 operatorAnalysisCheck.addActionListener(new java.awt.event.ActionListener() {
245 public void actionPerformed(ActionEvent e) {
246 options.operatorAnalysis = operatorAnalysisCheck.isSelected();
247
248 updateOtherFileNames(options);
249
250 frame.setDirty();
251 }
252 });
253
254 operatorAnalysisFileNameField.setColumns(32);
255 operatorAnalysisFileNameField.setEditable(false);
256 operatorAnalysisFileNameField.setEnabled(false);
257 optionsPanel.addComponentWithLabel("Operator analysis file name:", operatorAnalysisFileNameField);
258
259 optionsPanel.addSeparator();
260
261 optionsPanel.addComponent(samplePriorCheckBox);
262 samplePriorCheckBox.setOpaque(false);
263 samplePriorCheckBox.addChangeListener(new ChangeListener() {
264 public void stateChanged(ChangeEvent changeEvent) {
265 frame.setDirty();
266 }
267 });
268
269 optionsPanel.addSeparator();
270
271 JTextArea mleInfo = new JTextArea("Select the option below to perform marginal likelihood " +
272 "estimation (MLE) using path sampling (PS) / stepping-stone sampling (SS) " +
273 "or generalized stepping-stone sampling (GSS) which performs an additional" +
274 "analysis after the standard MCMC chain has finished.");
275 mleInfo.setColumns(50);
276 PanelUtils.setupComponent(mleInfo);
277 optionsPanel.addSpanningComponent(mleInfo);
278
279 //add PS/SS button
280 optionsPanel.addComponentWithLabel("Marginal likelihood estimation (MLE):", performMLECombo);
281
282 optionsPanel.addComponent(buttonMLE);
283 buttonMLE.setEnabled(false);
284 performMLECombo.addActionListener(new java.awt.event.ActionListener() {
285 public void actionPerformed(ActionEvent e) {
286 if (performMLECombo.getSelectedIndex() == 1) {
287 mleOptions.performMLE = true;
288 mleOptions.performMLEGSS = false;
289 options.logCoalescentEventsStatistic = false;
290 buttonMLE.setEnabled(true);
291 updateMLEFileNameStem();
292 } else if (performMLECombo.getSelectedIndex() == 2) {
293 // Generalized stepping-stone sampling
294 for (AbstractPartitionData partition : options.getDataPartitions()) {
295 if (partition.getDataType().getType() != DataType.NUCLEOTIDES) {
296 JOptionPane.showMessageDialog(frame,
297 "Generalized stepping-stone sampling is not currently\n" +
298 "compatible with substitution models other than those\n" +
299 "for nucleotide data. \n\n" +
300 "Use path sampling/stepping-stone sampling instead",
301 "Warning",
302 JOptionPane.WARNING_MESSAGE);
303
304 performMLECombo.setSelectedIndex(1);
305 return;
306
307 }
308 }
309 mleOptions.performMLE = false;
310 mleOptions.performMLEGSS = true;
311 //set to true because product of exponentials is the default option
312 options.logCoalescentEventsStatistic = true;
313 buttonMLE.setEnabled(true);
314 updateMLEFileNameStem();
315 } else {
316 mleOptions.performMLE = false;
317 mleOptions.performMLEGSS = false;
318 mleOptions.printOperatorAnalysis = false;
319 options.logCoalescentEventsStatistic = false;
320 buttonMLE.setEnabled(false);
321 }
322 }
323 });
324 buttonMLE.addActionListener(new java.awt.event.ActionListener() {
325 public void actionPerformed(ActionEvent e) {
326 updateMLEFileNameStem();
327
328 int result = -1;
329
330 if (performMLECombo.getSelectedIndex() == 1) {
331 result = mleDialog.showDialog();
332 } else if (performMLECombo.getSelectedIndex() == 2) {
333 result = mleGssDialog.showDialog();
334 }
335
336 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
337 return;
338 }
339
340 }
341 });
342
343 // JTextArea mleGssInfo = new JTextArea("Select the option below to perform marginal likelihood " +
344 // "estimation (MLE) using generalized stepping-stone sampling (GSS) which " +
345 // "performs an additional analysis after the standard MCMC chain has finished.");
346 // mleGssInfo.setColumns(50);
347 // PanelUtils.setupComponent(mleGssInfo);
348 // optionsPanel.addSpanningComponent(mleGssInfo);
349
350 // //add GSS button
351 // optionsPanel.addComponent(performMLEGSS);
352 // //will be false by default
353 // //options.performMLE = false; ??
354 // optionsPanel.addComponent(buttonMLEGSS);
355 // buttonMLEGSS.setEnabled(false);
356 // performMLEGSS.addActionListener(new java.awt.event.ActionListener() {
357 // public void actionPerformed(ActionEvent e) {
358 // if (performMLEGSS.isSelected()) {
359 // mleOptions.performMLEGSS = true;
360 // //set to true because product of exponentials is the default option
361 // options.logCoalescentEventsStatistic = true;
362 // buttonMLEGSS.setEnabled(true);
363 // buttonMLE.setEnabled(false);
364 // performMLE.setEnabled(false);
365 // updateMLEFileNameStem();
366 // } else {
367 // mleOptions.performMLEGSS = false;
368 // mleOptions.printOperatorAnalysis = false;
369 // options.logCoalescentEventsStatistic = false;
370 // buttonMLE.setEnabled(false);
371 // performMLE.setEnabled(true);
372 // buttonMLEGSS.setEnabled(false);
373 // }
374 // }
375 // });
376 // buttonMLEGSS.addActionListener(new java.awt.event.ActionListener() {
377 // public void actionPerformed(ActionEvent e) {
378 // updateMLEFileNameStem();
379 //
380 // int result = mleGssDialog.showDialog();
381 //
382 // if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
383 // return;
384 // }
385 //
386 // }
387 // });
388
389 // logFileNameField.addKeyListener(listener);
390 // treeFileNameField.addKeyListener(listener);
391 //mapTreeFileNameField.addKeyListener(listener);
392 // substTreeFileNameField.addKeyListener(listener);
393
394 // optionsPanel.setPreferredSize(new java.awt.Dimension(500, 600));
395
396 JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
397 panel.add(optionsPanel, BorderLayout.CENTER);
398 panel.setOpaque(false);
399
400 JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
401 scrollPane.setOpaque(false);
402 scrollPane.setBorder(null);
403 scrollPane.getViewport().setOpaque(false);
404
405 add(scrollPane, BorderLayout.CENTER);
406 }
407
408 private void updateTreeFileNameList() {
409 options.treeFileName.clear();
410 options.substTreeFileName.clear();
411 String treeFileName;
412
413 for (PartitionTreeModel tree : options.getPartitionTreeModels()) {
414 if (options.substTreeLog) {
415 treeFileName = getTreeFileName(tree.getPrefix() + "(time).");
416 } else {
417 treeFileName = getTreeFileName(tree.getPrefix());
418 }
419 if (addTxt.isSelected()) treeFileName = treeFileName + ".txt";
420 options.treeFileName.add(treeFileName);
421
422 if (options.substTreeLog) {
423 treeFileName = getTreeFileName(tree.getPrefix() + "(subst).");
424 if (addTxt.isSelected()) treeFileName = treeFileName + ".txt";
425 options.substTreeFileName.add(treeFileName);
426 }
427 }
428 }
429
430 private String getTreeFileName(String treeName) {
431 return options.fileNameStem + "." + treeName + "trees";
432 }
433
434 private String displayTreeList(List<String> treeList) {
435 if (treeList.size() > 1) {
436 return getTreeFileName("[tree name].");
437 } else {
438 return getTreeFileName("");
439 }
440 }
441
442 public void setOptions(BeautiOptions options) {
443 this.options = options;
444
445 /*System.err.println("mleOptions: " + mleOptions);
446 System.err.println("options.pathSteps: " + mleOptions.pathSteps);
447 System.err.println("options.mleChainLength: " + mleOptions.mleChainLength);
448 System.err.println("options.mleLogEvery: " + mleOptions.mleLogEvery);*/
449
450 // get the MLE options
451 mleOptions = (MarginalLikelihoodEstimationOptions)options.getComponentOptions(MarginalLikelihoodEstimationOptions.class);
452 /*if (mleOptions == null) {
453 mleOptions = new MarginalLikelihoodEstimationOptions();
454 }*/
455
456 /*System.err.println("mleOptions: " + mleOptions);
457 System.err.println("options.pathSteps: " + mleOptions.pathSteps);
458 System.err.println("options.mleChainLength: " + mleOptions.mleChainLength);
459 System.err.println("options.mleLogEvery: " + mleOptions.mleLogEvery);*/
460
461 if (mleDialog != null) {
462 //mleDialog.setOptions(mleOptions);
463 }
464 if (mleGssDialog != null) {
465 //mleGssDialog.setOptions(mleOptions);
466 }
467
468 chainLengthField.setValue(options.chainLength);
469 echoEveryField.setValue(options.echoEvery);
470 logEveryField.setValue(options.logEvery);
471
472 if (options.fileNameStem != null) {
473 fileNameStemField.setText(options.fileNameStem);
474 } else {
475 fileNameStemField.setText(DEFAULT_FILE_NAME_STEM);
476 fileNameStemField.setEnabled(false);
477 }
478
479 operatorAnalysisCheck.setSelected(options.operatorAnalysis);
480
481 updateOtherFileNames(options);
482
483 if (options.contains(Microsatellite.INSTANCE)) {
484 samplePriorCheckBox.setSelected(false);
485 samplePriorCheckBox.setVisible(false);
486 } else {
487 samplePriorCheckBox.setVisible(true);
488 samplePriorCheckBox.setSelected(options.samplePriorOnly);
489 }
490
491 optionsPanel.validate();
492 optionsPanel.repaint();
493 }
494
495 private void updateOtherFileNames(BeautiOptions options) {
496 if (options.fileNameStem != null) {
497 // fileNameStemField.setText(options.fileNameStem);
498
499 options.logFileName = options.fileNameStem + ".log";
500 if (addTxt.isSelected()) options.logFileName = options.logFileName + ".txt";
501 logFileNameField.setText(options.logFileName);
502
503 // if (options.mapTreeFileName == null) {
504 // mapTreeFileNameField.setText(options.fileNameStem + ".MAP.tree");
505 // } else {
506 // mapTreeFileNameField.setText(options.mapTreeFileName);
507 // }
508
509 updateTreeFileNameList();
510 treeFileNameField.setText(displayTreeList(options.treeFileName));
511
512 if (options.substTreeLog) {
513 substTreeFileNameField.setText(displayTreeList(options.substTreeFileName));
514 } else {
515 substTreeFileNameField.setText("");
516 }
517
518 options.operatorAnalysisFileName = options.fileNameStem + ".ops";
519 if (addTxt.isSelected()) {
520 options.operatorAnalysisFileName = options.operatorAnalysisFileName + ".txt";
521 }
522 operatorAnalysisFileNameField.setEnabled(options.operatorAnalysis);
523 if (options.operatorAnalysis) {
524 operatorAnalysisFileNameField.setText(options.operatorAnalysisFileName);
525 } else {
526 operatorAnalysisFileNameField.setText("");
527 }
528
529 // mapTreeLogCheck.setEnabled(true);
530 // mapTreeLogCheck.setSelected(options.mapTreeLog);
531 // mapTreeFileNameField.setEnabled(options.mapTreeLog);
532
533 substTreeLogCheck.setEnabled(true);
534 substTreeLogCheck.setSelected(options.substTreeLog);
535
536 updateMLEFileNameStem();
537 } else {
538 // fileNameStemField.setText(fileNameStem);
539 // fileNameStemField.setEnabled(false);
540 logFileNameField.setText(DEFAULT_FILE_NAME_STEM + ".log");
541 treeFileNameField.setText(DEFAULT_FILE_NAME_STEM + ".trees");
542 // mapTreeLogCheck.setEnabled(false);
543 // mapTreeFileNameField.setEnabled(false);
544 // mapTreeFileNameField.setText("untitled");
545 substTreeLogCheck.setSelected(false);
546 substTreeFileNameField.setEnabled(false);
547 substTreeFileNameField.setText("");
548 operatorAnalysisCheck.setSelected(false);
549 operatorAnalysisFileNameField.setText("");
550 }
551 }
552
553 private void updateMLEFileNameStem() {
554 if (mleDialog == null) {
555 mleDialog = new MLEDialog(frame, mleOptions);
556 }
557 if (mleGssDialog == null) {
558 mleGssDialog = new MLEGSSDialog(frame, mleOptions, options);
559 }
560 mleDialog.setFilenameStem(options.fileNameStem, addTxt.isSelected());
561 mleGssDialog.setFilenameStem(options.fileNameStem, addTxt.isSelected());
562 }
563
564 public void getOptions(BeautiOptions options) {
565 options.fileNameStem = fileNameStemField.getText();
566 options.logFileName = logFileNameField.getText();
567
568 // options.mapTreeLog = mapTreeLogCheck.isSelected();
569 // options.mapTreeFileName = mapTreeFileNameField.getText();
570
571 options.substTreeLog = substTreeLogCheck.isSelected();
572 updateTreeFileNameList();
573
574 options.operatorAnalysis = operatorAnalysisCheck.isSelected();
575 options.operatorAnalysisFileName = operatorAnalysisFileNameField.getText();
576
577 options.samplePriorOnly = samplePriorCheckBox.isSelected();
578
579 if (mleDialog != null) {
580 //mleDialog.getOptions(mleOptions);
581 }
582
583 if (mleGssDialog != null) {
584 //mleGssDialog.getOptions(mleOptions);
585 }
586
587 }
588
589 public JComponent getExportableComponent() {
590 return optionsPanel;
591 }
592
593 }