| 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.beauti.components.marginalLikelihoodEstimation; |
| 27 |
|
| 28 |
import dr.app.beauti.options.BeautiOptions; |
| 29 |
import dr.app.beauti.types.TreePriorType; |
| 30 |
import dr.app.beauti.util.PanelUtils; |
| 31 |
import dr.app.gui.components.WholeNumberField; |
| 32 |
import jam.panels.OptionsPanel; |
| 33 |
|
| 34 |
import javax.swing.*; |
| 35 |
import javax.swing.border.EmptyBorder; |
| 36 |
import java.awt.event.ActionEvent; |
| 37 |
import java.awt.event.KeyEvent; |
| 38 |
import java.awt.event.WindowAdapter; |
| 39 |
import java.awt.event.WindowEvent; |
| 40 |
import java.beans.PropertyChangeEvent; |
| 41 |
import java.beans.PropertyChangeListener; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class MLEGSSDialog { |
| 48 |
|
| 49 |
private JFrame frame; |
| 50 |
|
| 51 |
private final OptionsPanel optionsPanel; |
| 52 |
|
| 53 |
private JLabel labelPathSteps, labelChainLength, labelLogEvery, labelLogFileName, labelResultFileName; |
| 54 |
private JLabel labelStepDistribution, labelTreeWorkingPrior, labelParameterWorkingPrior; |
| 55 |
|
| 56 |
private WholeNumberField pathStepsField = new WholeNumberField(1, Integer.MAX_VALUE); |
| 57 |
private WholeNumberField chainLengthField = new WholeNumberField(1, Integer.MAX_VALUE); |
| 58 |
private WholeNumberField logEveryField = new WholeNumberField(1, Integer.MAX_VALUE); |
| 59 |
|
| 60 |
private JTextArea logFileNameField = new JTextArea("mle.log"); |
| 61 |
private JTextArea resultFileNameField = new JTextArea("mle.result.log"); |
| 62 |
|
| 63 |
JCheckBox operatorAnalysis = new JCheckBox("Print operator analysis"); |
| 64 |
|
| 65 |
private JComboBox stepDistribution = new JComboBox(); |
| 66 |
private JComboBox treeWorkingPrior = new JComboBox(); |
| 67 |
|
| 68 |
private MarginalLikelihoodEstimationOptions options; |
| 69 |
private BeautiOptions beautiOptions; |
| 70 |
|
| 71 |
private String description = "Settings for marginal likelihood estimation using GSS"; |
| 72 |
|
| 73 |
public MLEGSSDialog(final JFrame frame, final MarginalLikelihoodEstimationOptions options, final BeautiOptions beautiOptions) { |
| 74 |
this.frame = frame; |
| 75 |
this.options = options; |
| 76 |
this.beautiOptions = beautiOptions; |
| 77 |
|
| 78 |
optionsPanel = new OptionsPanel(12, 12); |
| 79 |
|
| 80 |
optionsPanel.setOpaque(false); |
| 81 |
|
| 82 |
JTextArea mleInfo = new JTextArea("Set the options to perform marginal likelihood " + |
| 83 |
"estimation (MLE) using generalized stepping-stone sampling (GSS)."); |
| 84 |
mleInfo.setColumns(56); |
| 85 |
PanelUtils.setupComponent(mleInfo); |
| 86 |
optionsPanel.addSpanningComponent(mleInfo); |
| 87 |
|
| 88 |
pathStepsField.setValue(100); |
| 89 |
pathStepsField.setColumns(16); |
| 90 |
pathStepsField.setMinimumSize(pathStepsField.getPreferredSize()); |
| 91 |
labelPathSteps = optionsPanel.addComponentWithLabel("Number of stepping stones:", pathStepsField); |
| 92 |
pathStepsField.addKeyListener(new java.awt.event.KeyListener() { |
| 93 |
public void keyTyped(KeyEvent e) { |
| 94 |
} |
| 95 |
|
| 96 |
public void keyPressed(KeyEvent e) { |
| 97 |
} |
| 98 |
|
| 99 |
public void keyReleased(KeyEvent e) { |
| 100 |
options.pathSteps = pathStepsField.getValue(); |
| 101 |
} |
| 102 |
}); |
| 103 |
|
| 104 |
chainLengthField.setValue(1000000); |
| 105 |
chainLengthField.setColumns(16); |
| 106 |
chainLengthField.setMinimumSize(chainLengthField.getPreferredSize()); |
| 107 |
labelChainLength = optionsPanel.addComponentWithLabel("Length of chains:", chainLengthField); |
| 108 |
chainLengthField.addKeyListener(new java.awt.event.KeyListener() { |
| 109 |
public void keyTyped(KeyEvent e) { |
| 110 |
} |
| 111 |
|
| 112 |
public void keyPressed(KeyEvent e) { |
| 113 |
} |
| 114 |
|
| 115 |
public void keyReleased(KeyEvent e) { |
| 116 |
options.mleChainLength = chainLengthField.getValue(); |
| 117 |
} |
| 118 |
}); |
| 119 |
|
| 120 |
optionsPanel.addSeparator(); |
| 121 |
|
| 122 |
logEveryField.setValue(1000); |
| 123 |
logEveryField.setColumns(16); |
| 124 |
logEveryField.setMinimumSize(logEveryField.getPreferredSize()); |
| 125 |
labelLogEvery = optionsPanel.addComponentWithLabel("Log likelihood every:", logEveryField); |
| 126 |
logEveryField.addKeyListener(new java.awt.event.KeyListener() { |
| 127 |
public void keyTyped(KeyEvent e) { |
| 128 |
} |
| 129 |
|
| 130 |
public void keyPressed(KeyEvent e) { |
| 131 |
} |
| 132 |
|
| 133 |
public void keyReleased(KeyEvent e) { |
| 134 |
options.mleLogEvery = logEveryField.getValue(); |
| 135 |
} |
| 136 |
}); |
| 137 |
|
| 138 |
optionsPanel.addSeparator(); |
| 139 |
|
| 140 |
logFileNameField.setColumns(32); |
| 141 |
logFileNameField.setEditable(false); |
| 142 |
logFileNameField.setMinimumSize(logFileNameField.getPreferredSize()); |
| 143 |
labelLogFileName = optionsPanel.addComponentWithLabel("Log file name:", logFileNameField); |
| 144 |
logFileNameField.addKeyListener(new java.awt.event.KeyListener() { |
| 145 |
public void keyTyped(KeyEvent e) { |
| 146 |
} |
| 147 |
|
| 148 |
public void keyPressed(KeyEvent e) { |
| 149 |
} |
| 150 |
|
| 151 |
public void keyReleased(KeyEvent e) { |
| 152 |
options.mleFileName = logFileNameField.getText(); |
| 153 |
} |
| 154 |
}); |
| 155 |
|
| 156 |
optionsPanel.addSeparator(); |
| 157 |
|
| 158 |
resultFileNameField.setColumns(32); |
| 159 |
resultFileNameField.setEditable(false); |
| 160 |
resultFileNameField.setMinimumSize(resultFileNameField.getPreferredSize()); |
| 161 |
labelResultFileName = optionsPanel.addComponentWithLabel("Results file name:", resultFileNameField); |
| 162 |
resultFileNameField.addKeyListener(new java.awt.event.KeyListener() { |
| 163 |
public void keyTyped(KeyEvent e) { |
| 164 |
} |
| 165 |
|
| 166 |
public void keyPressed(KeyEvent e) { |
| 167 |
} |
| 168 |
|
| 169 |
public void keyReleased(KeyEvent e) { |
| 170 |
options.mleResultFileName = resultFileNameField.getText(); |
| 171 |
} |
| 172 |
}); |
| 173 |
|
| 174 |
optionsPanel.addSeparator(); |
| 175 |
|
| 176 |
JTextArea betaInfo = new JTextArea("By default, the power posteriors are determined according to " + |
| 177 |
"evenly spaced quantiles of a Beta(0.3, 1.0) distribution, thereby estimating " + |
| 178 |
"more power posteriors close to the prior."); |
| 179 |
betaInfo.setColumns(56); |
| 180 |
PanelUtils.setupComponent(betaInfo); |
| 181 |
optionsPanel.addSpanningComponent(betaInfo); |
| 182 |
|
| 183 |
treeWorkingPrior.addItem("None"); |
| 184 |
options.choiceTreeWorkingPrior = "None"; |
| 185 |
treeWorkingPrior.addItem("Product of exponential distributions"); |
| 186 |
treeWorkingPrior.addItem("Matching coalescent model"); |
| 187 |
treeWorkingPrior.addItem("Matching speciation model"); |
| 188 |
|
| 189 |
treeWorkingPrior.addActionListener(new java.awt.event.ActionListener() { |
| 190 |
public void actionPerformed(ActionEvent e) { |
| 191 |
String selection = (String)((JComboBox)e.getSource()).getSelectedItem(); |
| 192 |
TreePriorType treePrior = beautiOptions.getPartitionTreePriors().get(0).getNodeHeightPrior(); |
| 193 |
|
| 194 |
if (selection.equals("None")) { |
| 195 |
JOptionPane.showMessageDialog(frame, |
| 196 |
"A suitable tree working prior needs to be selected. \n" + |
| 197 |
"Trying to determine a tree working prior based on selected models.", |
| 198 |
"Tree working prior selection warning", |
| 199 |
JOptionPane.WARNING_MESSAGE); |
| 200 |
if (treePrior.equals(TreePriorType.YULE)) { |
| 201 |
treeWorkingPrior.setSelectedItem("Matching speciation model"); |
| 202 |
options.choiceTreeWorkingPrior = "Matching speciation model"; |
| 203 |
} else if (treePrior.equals(TreePriorType.CONSTANT) || treePrior.equals(TreePriorType.EXPONENTIAL) |
| 204 |
|| treePrior.equals(TreePriorType.EXPANSION) || treePrior.equals(TreePriorType.LOGISTIC)) { |
| 205 |
treeWorkingPrior.setSelectedItem("Matching coalescent model"); |
| 206 |
options.choiceTreeWorkingPrior = "Matching coalescent model"; |
| 207 |
} else { |
| 208 |
treeWorkingPrior.setSelectedItem("Product of exponential distributions"); |
| 209 |
options.choiceTreeWorkingPrior = "Product of exponential distributions"; |
| 210 |
} |
| 211 |
} |
| 212 |
if (selection.equals("Matching coalescent model") || (selection.equals("Matching speciation model"))) { |
| 213 |
beautiOptions.logCoalescentEventsStatistic = false; |
| 214 |
} else { |
| 215 |
beautiOptions.logCoalescentEventsStatistic = true; |
| 216 |
} |
| 217 |
boolean mcmAllowed = false; |
| 218 |
boolean msmAllowed = false; |
| 219 |
if (treePrior.equals(TreePriorType.CONSTANT) || treePrior.equals(TreePriorType.EXPONENTIAL) |
| 220 |
|| treePrior.equals(TreePriorType.EXPANSION) || treePrior.equals(TreePriorType.LOGISTIC)) { |
| 221 |
mcmAllowed = true; |
| 222 |
} else if (treePrior.equals(TreePriorType.YULE)) { |
| 223 |
msmAllowed = true; |
| 224 |
} |
| 225 |
if (selection.equals("Matching coalescent model")) { |
| 226 |
if (mcmAllowed) { |
| 227 |
options.choiceTreeWorkingPrior = "Matching coalescent model"; |
| 228 |
} else { |
| 229 |
JOptionPane.showMessageDialog(frame, |
| 230 |
"The selected speciation model can't be equipped with a \n" + |
| 231 |
"matching coalescent model. Reverting back to using an \n" + |
| 232 |
"appropriate tree working prior.", |
| 233 |
"Matching coalescent model warning", |
| 234 |
JOptionPane.WARNING_MESSAGE); |
| 235 |
if (msmAllowed) { |
| 236 |
treeWorkingPrior.setSelectedItem("Matching speciation model"); |
| 237 |
options.choiceTreeWorkingPrior = "Matching speciation model"; |
| 238 |
} else { |
| 239 |
treeWorkingPrior.setSelectedItem("Product of exponential distributions"); |
| 240 |
options.choiceTreeWorkingPrior = "Product of exponential distributions"; |
| 241 |
} |
| 242 |
} |
| 243 |
} else if (selection.equals("Matching speciation model") && !msmAllowed) { |
| 244 |
if (msmAllowed) { |
| 245 |
options.choiceTreeWorkingPrior = "Matching speciation model"; |
| 246 |
} else { |
| 247 |
JOptionPane.showMessageDialog(frame, |
| 248 |
"The selected coalescent model can't be equipped with a \n" + |
| 249 |
"matching speciation model. Please use an appropriate working \n" + |
| 250 |
"prior for your selected coalescent model.", |
| 251 |
"Matching speciation model warning", |
| 252 |
JOptionPane.WARNING_MESSAGE); |
| 253 |
if (mcmAllowed) { |
| 254 |
treeWorkingPrior.setSelectedItem("Matching coalescent model"); |
| 255 |
options.choiceTreeWorkingPrior = "Matching coalescent model"; |
| 256 |
} else { |
| 257 |
treeWorkingPrior.setSelectedItem("Product of exponential distributions"); |
| 258 |
options.choiceTreeWorkingPrior = "Product of exponential distributions"; |
| 259 |
} |
| 260 |
} |
| 261 |
} else { |
| 262 |
options.choiceTreeWorkingPrior = selection; |
| 263 |
} |
| 264 |
|
| 265 |
} |
| 266 |
}); |
| 267 |
labelTreeWorkingPrior = optionsPanel.addComponentWithLabel("Tree working prior", treeWorkingPrior); |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
stepDistribution.addItem("Beta"); |
| 274 |
labelStepDistribution = optionsPanel.addComponentWithLabel("Stepping stone distribution:", stepDistribution); |
| 275 |
|
| 276 |
optionsPanel.addSeparator(); |
| 277 |
|
| 278 |
optionsPanel.addComponent(operatorAnalysis); |
| 279 |
|
| 280 |
operatorAnalysis.addActionListener(new java.awt.event.ActionListener() { |
| 281 |
public void actionPerformed(ActionEvent e) { |
| 282 |
if (operatorAnalysis.isSelected()) { |
| 283 |
options.printOperatorAnalysis = true; |
| 284 |
} else { |
| 285 |
options.printOperatorAnalysis = false; |
| 286 |
} |
| 287 |
} |
| 288 |
}); |
| 289 |
|
| 290 |
optionsPanel.addSeparator(); |
| 291 |
|
| 292 |
JTextArea mleTutorial = new JTextArea("Additional information on marginal likelihood estimation in BEAST " + |
| 293 |
"can be found on http://beast.community/model_selection"); |
| 294 |
mleTutorial.setColumns(56); |
| 295 |
PanelUtils.setupComponent(mleTutorial); |
| 296 |
optionsPanel.addSpanningComponent(mleTutorial); |
| 297 |
|
| 298 |
JTextArea citationText = new JTextArea("Baele G, Lemey P, Suchard MA (2016) Genealogical working " + |
| 299 |
"distributions for Bayesian \nmodel testing with phylogenetic uncertainty. Syst. Biol. 65(2), 250-264 [GSS Paper]."); |
| 300 |
citationText.setColumns(45); |
| 301 |
optionsPanel.addComponentWithLabel("Citation:", citationText); |
| 302 |
|
| 303 |
optionsPanel.addSeparator(); |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
} |
| 317 |
|
| 318 |
public int showDialog() { |
| 319 |
|
| 320 |
final JOptionPane optionPane = new JOptionPane(optionsPanel, |
| 321 |
JOptionPane.QUESTION_MESSAGE, |
| 322 |
JOptionPane.OK_CANCEL_OPTION, |
| 323 |
null, |
| 324 |
null, |
| 325 |
null); |
| 326 |
optionPane.setBorder(new EmptyBorder(12, 12, 12, 12)); |
| 327 |
|
| 328 |
final JDialog dialog = optionPane.createDialog(frame, description); |
| 329 |
|
| 330 |
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
| 331 |
dialog.addWindowListener(new WindowAdapter() { |
| 332 |
public void windowClosing(WindowEvent we) { |
| 333 |
|
| 334 |
} |
| 335 |
}); |
| 336 |
|
| 337 |
optionPane.addPropertyChangeListener( |
| 338 |
new PropertyChangeListener() { |
| 339 |
public void propertyChange(PropertyChangeEvent e) { |
| 340 |
String prop = e.getPropertyName(); |
| 341 |
|
| 342 |
if (!dialog.isVisible() && (e.getSource() == optionPane) && (prop.equals(JOptionPane.VALUE_PROPERTY))) { |
| 343 |
|
| 344 |
if (((String)treeWorkingPrior.getSelectedItem()).equals("None")) { |
| 345 |
JOptionPane.showMessageDialog(frame, |
| 346 |
"Please select a suitable tree working prior.", |
| 347 |
"Tree working prior selection warning", |
| 348 |
JOptionPane.WARNING_MESSAGE); |
| 349 |
dialog.setVisible(true); |
| 350 |
} else { |
| 351 |
TreePriorType treePrior = beautiOptions.getPartitionTreePriors().get(0).getNodeHeightPrior(); |
| 352 |
if (((String)treeWorkingPrior.getSelectedItem()).equals("Matching coalescent model")) { |
| 353 |
if (!(treePrior.equals(TreePriorType.CONSTANT) || treePrior.equals(TreePriorType.EXPONENTIAL) |
| 354 |
|| treePrior.equals(TreePriorType.EXPANSION) || treePrior.equals(TreePriorType.LOGISTIC))) { |
| 355 |
JOptionPane.showMessageDialog(frame, |
| 356 |
"Please select a suitable tree working prior.", |
| 357 |
"Tree working prior selection warning", |
| 358 |
JOptionPane.WARNING_MESSAGE); |
| 359 |
dialog.setVisible(true); |
| 360 |
} |
| 361 |
} else if (((String)treeWorkingPrior.getSelectedItem()).equals("Matching speciation model")) { |
| 362 |
if (!treePrior.equals(TreePriorType.YULE)) { |
| 363 |
JOptionPane.showMessageDialog(frame, |
| 364 |
"Please select a suitable tree working prior.", |
| 365 |
"Tree working prior selection warning", |
| 366 |
JOptionPane.WARNING_MESSAGE); |
| 367 |
dialog.setVisible(true); |
| 368 |
} |
| 369 |
} else if (((String)treeWorkingPrior.getSelectedItem()).equals("Product of exponential distributions")) { |
| 370 |
if (treePrior.equals(TreePriorType.YULE)) { |
| 371 |
JOptionPane.showMessageDialog(frame, |
| 372 |
"Please select a suitable tree working prior.", |
| 373 |
"Tree working prior selection warning", |
| 374 |
JOptionPane.WARNING_MESSAGE); |
| 375 |
dialog.setVisible(true); |
| 376 |
} |
| 377 |
} else { |
| 378 |
dialog.setVisible(false); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
); |
| 385 |
|
| 386 |
dialog.pack(); |
| 387 |
dialog.setVisible(true); |
| 388 |
|
| 389 |
int result = JOptionPane.CANCEL_OPTION; |
| 390 |
Integer value = (Integer) optionPane.getValue(); |
| 391 |
if (value != null && value != -1) { |
| 392 |
result = value; |
| 393 |
} |
| 394 |
|
| 395 |
return result; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public void setFilenameStem(String fileNameStem, boolean addTxt) { |
| 403 |
logFileNameField.setText(fileNameStem + ".mle.log" + (addTxt ? ".txt" : "")); |
| 404 |
resultFileNameField.setText(fileNameStem + ".mle.result.log" + (addTxt ? ".txt" : "")); |
| 405 |
options.mleFileName = logFileNameField.getText(); |
| 406 |
options.mleResultFileName = resultFileNameField.getText(); |
| 407 |
} |
| 408 |
|
| 409 |
public void setOptions(MarginalLikelihoodEstimationOptions options) { |
| 410 |
this.options = options; |
| 411 |
|
| 412 |
pathStepsField.setValue(options.pathSteps); |
| 413 |
chainLengthField.setValue(options.mleChainLength); |
| 414 |
logEveryField.setValue(options.mleLogEvery); |
| 415 |
|
| 416 |
logFileNameField.setText(options.mleFileName); |
| 417 |
resultFileNameField.setText(options.mleResultFileName); |
| 418 |
|
| 419 |
treeWorkingPrior.setSelectedItem(options.choiceTreeWorkingPrior); |
| 420 |
if (options.choiceTreeWorkingPrior.equals("Product of exponential distributions") && options.performMLEGSS) { |
| 421 |
beautiOptions.logCoalescentEventsStatistic = true; |
| 422 |
} else { |
| 423 |
beautiOptions.logCoalescentEventsStatistic = false; |
| 424 |
} |
| 425 |
|
| 426 |
optionsPanel.validate(); |
| 427 |
optionsPanel.repaint(); |
| 428 |
} |
| 429 |
|
| 430 |
public void getOptions(MarginalLikelihoodEstimationOptions options) { |
| 431 |
options.pathSteps = pathStepsField.getValue(); |
| 432 |
options.mleChainLength = chainLengthField.getValue(); |
| 433 |
options.mleLogEvery = logEveryField.getValue(); |
| 434 |
|
| 435 |
options.mleFileName = logFileNameField.getText(); |
| 436 |
options.mleResultFileName = resultFileNameField.getText(); |
| 437 |
options.choiceTreeWorkingPrior = treeWorkingPrior.getSelectedItem().toString(); |
| 438 |
if (options.choiceTreeWorkingPrior.equals("Product of exponential distributions") && options.performMLEGSS) { |
| 439 |
beautiOptions.logCoalescentEventsStatistic = true; |
| 440 |
} else { |
| 441 |
beautiOptions.logCoalescentEventsStatistic = false; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
} |