| 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 |
package dr.app.beauti.priorspanel; |
| 33 |
|
| 34 |
import dr.app.beauti.components.hpm.HierarchicalModelComponentOptions; |
| 35 |
import dr.app.beauti.components.hpm.HierarchicalPhylogeneticModel; |
| 36 |
import dr.app.beauti.options.BeautiOptions; |
| 37 |
import dr.app.beauti.options.Parameter; |
| 38 |
import dr.app.beauti.types.PriorType; |
| 39 |
import dr.app.gui.chart.Axis; |
| 40 |
import dr.app.gui.chart.JChart; |
| 41 |
import dr.app.gui.chart.LinearAxis; |
| 42 |
import dr.app.gui.chart.PDFPlot; |
| 43 |
import dr.app.gui.components.RealNumberField; |
| 44 |
import dr.app.util.OSType; |
| 45 |
import dr.math.distributions.Distribution; |
| 46 |
import dr.math.distributions.GammaDistribution; |
| 47 |
import dr.math.distributions.NormalDistribution; |
| 48 |
import dr.math.distributions.OffsetPositiveDistribution; |
| 49 |
import dr.util.NumberFormatter; |
| 50 |
import jam.panels.OptionsPanel; |
| 51 |
|
| 52 |
import javax.swing.*; |
| 53 |
import javax.swing.border.EmptyBorder; |
| 54 |
import java.awt.*; |
| 55 |
import java.awt.event.ItemEvent; |
| 56 |
import java.awt.event.ItemListener; |
| 57 |
import java.util.HashMap; |
| 58 |
import java.util.Map; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public class HierarchicalPriorDialog { |
| 64 |
|
| 65 |
private JFrame frame; |
| 66 |
|
| 67 |
private Map<PriorType, PriorOptionsPanel> optionsPanels = new HashMap<PriorType, PriorOptionsPanel>(); |
| 68 |
|
| 69 |
private JComboBox priorCombo = new JComboBox(); |
| 70 |
private JCheckBox meanInRealSpaceCheck = new JCheckBox(); |
| 71 |
private RealNumberField initialField = new RealNumberField(); |
| 72 |
private RealNumberField selectedField; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private JTextField nameField; |
| 77 |
private JPanel panel; |
| 78 |
|
| 79 |
|
| 80 |
private JChart[] chart; |
| 81 |
private JPanel[] quantilePanel; |
| 82 |
private JTextArea[] quantileText; |
| 83 |
|
| 84 |
private java.util.List<Parameter> parameterList; |
| 85 |
private Parameter parameter; |
| 86 |
|
| 87 |
final private BeautiOptions options; |
| 88 |
|
| 89 |
private double hpmMeanMean = 0.0; |
| 90 |
private double hpmMeanStDev = 100.0; |
| 91 |
private double hpmMeanInitial = 0.0; |
| 92 |
private double hpmPrecShape = 0.001; |
| 93 |
private double hpmPrecScale = 1000.0; |
| 94 |
private double hpmPrecInitial = 1.0; |
| 95 |
|
| 96 |
public HierarchicalPriorDialog(JFrame frame, BeautiOptions options) { |
| 97 |
this.frame = frame; |
| 98 |
this.options = options; |
| 99 |
|
| 100 |
initialField.setColumns(10); |
| 101 |
|
| 102 |
optionsPanels.put(PriorType.NORMAL_PRIOR, new NormalOptionsPanel()); |
| 103 |
optionsPanels.put(PriorType.GAMMA_PRIOR, new GammaOptionsPanel()); |
| 104 |
|
| 105 |
chart = new JChart[2]; |
| 106 |
quantileText = new JTextArea[2]; |
| 107 |
quantilePanel = new JPanel[2]; |
| 108 |
|
| 109 |
for (int i = 0; i < 2; ++i) { |
| 110 |
chart[i] = new JChart(new LinearAxis(Axis.AT_MINOR_TICK, Axis.AT_MINOR_TICK), |
| 111 |
new LinearAxis(Axis.AT_ZERO, Axis.AT_DATA)); |
| 112 |
|
| 113 |
JLabel quantileLabels = new JLabel(); |
| 114 |
quantileLabels.setFont(quantileLabels.getFont().deriveFont(10.0f)); |
| 115 |
quantileLabels.setOpaque(false); |
| 116 |
quantileLabels.setText("<html><p align=\"right\">Quantiles: 2.5%:<br>5%:<br>Median:<br>95%:<br>97.5%:</p></html>"); |
| 117 |
|
| 118 |
quantileText[i] = new JTextArea(0, 5); |
| 119 |
quantileText[i].setFont(quantileText[i].getFont().deriveFont(10.0f)); |
| 120 |
quantileText[i].setOpaque(false); |
| 121 |
quantileText[i].setEditable(false); |
| 122 |
quantileLabels.setHorizontalAlignment(JLabel.LEFT); |
| 123 |
|
| 124 |
quantilePanel[i] = new JPanel(); |
| 125 |
quantilePanel[i].add(quantileLabels); |
| 126 |
quantilePanel[i].add(quantileText[i]); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
public void addHPM(java.util.List<Parameter> parameterList) { |
| 134 |
HierarchicalModelComponentOptions comp = (HierarchicalModelComponentOptions) |
| 135 |
options.getComponentOptions(HierarchicalModelComponentOptions.class); |
| 136 |
HierarchicalPhylogeneticModel hpm = comp.addHPM(nameField.getText(), parameterList, parameterList.get(0).priorType); |
| 137 |
|
| 138 |
hpm.getConditionalParameterList().get(0).mean = hpmMeanMean; |
| 139 |
hpm.getConditionalParameterList().get(0).stdev = hpmMeanStDev; |
| 140 |
hpm.getConditionalParameterList().get(0).setInitial(hpmMeanInitial); |
| 141 |
|
| 142 |
hpm.getConditionalParameterList().get(1).shape = hpmPrecShape; |
| 143 |
hpm.getConditionalParameterList().get(1).scale = hpmPrecScale; |
| 144 |
hpm.getConditionalParameterList().get(1).setInitial(hpmPrecInitial); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
public boolean validateModelName() { |
| 149 |
return validateModelName(nameField.getText()); |
| 150 |
} |
| 151 |
|
| 152 |
private boolean validateModelName(String modelName) { |
| 153 |
|
| 154 |
|
| 155 |
if (modelName.trim().length() == 0) { |
| 156 |
Toolkit.getDefaultToolkit().beep(); |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
if (modelExists(modelName)) { |
| 162 |
JOptionPane.showMessageDialog(frame, |
| 163 |
"A model with this name already exists.", |
| 164 |
"HPM name error", |
| 165 |
|
| 166 |
JOptionPane.WARNING_MESSAGE); |
| 167 |
|
| 168 |
return false; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
private boolean modelExists(String modelName) { |
| 179 |
|
| 180 |
HierarchicalModelComponentOptions comp = (HierarchicalModelComponentOptions) |
| 181 |
options.getComponentOptions(HierarchicalModelComponentOptions.class); |
| 182 |
return comp.modelExists(modelName); |
| 183 |
} |
| 184 |
|
| 185 |
public int showDialog(final java.util.List<Parameter> parameterList) { |
| 186 |
|
| 187 |
this.parameterList = parameterList; |
| 188 |
this.parameter = parameterList.get(0); |
| 189 |
PriorType priorType = parameter.priorType; |
| 190 |
|
| 191 |
|
| 192 |
priorCombo.removeAllItems(); |
| 193 |
if (parameter.isNonNegative) { |
| 194 |
priorCombo.addItem(PriorType.LOGNORMAL_HPM_PRIOR); |
| 195 |
} |
| 196 |
priorCombo.addItem(PriorType.NORMAL_HPM_PRIOR); |
| 197 |
|
| 198 |
priorCombo.setSelectedItem(priorType); |
| 199 |
|
| 200 |
double lower = Double.NEGATIVE_INFINITY; |
| 201 |
double upper = Double.POSITIVE_INFINITY; |
| 202 |
|
| 203 |
if (parameter.isZeroOne) { |
| 204 |
lower = 0.0; |
| 205 |
upper = 1.0; |
| 206 |
} else if (parameter.isNonNegative) { |
| 207 |
lower = 0.0; |
| 208 |
} |
| 209 |
|
| 210 |
initialField.setRange(lower, upper); |
| 211 |
initialField.setValue(parameter.getInitial()); |
| 212 |
|
| 213 |
panel = new JPanel(new GridBagLayout()); |
| 214 |
|
| 215 |
setupComponents(); |
| 216 |
|
| 217 |
JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 218 |
scrollPane.setBorder(null); |
| 219 |
scrollPane.getViewport().setOpaque(false); |
| 220 |
|
| 221 |
JOptionPane optionPane = new JOptionPane(scrollPane, |
| 222 |
JOptionPane.PLAIN_MESSAGE, |
| 223 |
JOptionPane.OK_CANCEL_OPTION, |
| 224 |
null, |
| 225 |
null, |
| 226 |
null); |
| 227 |
optionPane.setBorder(new EmptyBorder(12, 12, 12, 12)); |
| 228 |
|
| 229 |
final JDialog dialog = optionPane.createDialog(frame, "Phylogenetic Hierarchical Model Setup"); |
| 230 |
|
| 231 |
priorCombo.addItemListener(new ItemListener() { |
| 232 |
public void itemStateChanged(ItemEvent e) { |
| 233 |
setupComponents(); |
| 234 |
dialog.validate(); |
| 235 |
dialog.repaint(); |
| 236 |
dialog.pack(); |
| 237 |
} |
| 238 |
}); |
| 239 |
|
| 240 |
for (PriorOptionsPanel optionsPanel : optionsPanels.values()) { |
| 241 |
optionsPanel.addListener(new PriorOptionsPanel.Listener() { |
| 242 |
public void optionsPanelChanged() { |
| 243 |
setupChart(); |
| 244 |
dialog.validate(); |
| 245 |
dialog.repaint(); |
| 246 |
} |
| 247 |
}); |
| 248 |
} |
| 249 |
|
| 250 |
dialog.pack(); |
| 251 |
if (OSType.isMac()) { |
| 252 |
dialog.setMinimumSize(new Dimension(dialog.getBounds().width, 300)); |
| 253 |
} else { |
| 254 |
Toolkit tk = Toolkit.getDefaultToolkit(); |
| 255 |
Dimension d = tk.getScreenSize(); |
| 256 |
if (d.height < 700 && panel.getHeight() > 450) { |
| 257 |
dialog.setSize(new Dimension(panel.getWidth() + 100, 550)); |
| 258 |
} else { |
| 259 |
|
| 260 |
dialog.setSize(new Dimension(panel.getWidth() + 100, panel.getHeight() + 100)); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
dialog.setResizable(true); |
| 268 |
dialog.setVisible(true); |
| 269 |
|
| 270 |
int result = JOptionPane.CANCEL_OPTION; |
| 271 |
Integer value = (Integer) optionPane.getValue(); |
| 272 |
if (value != null && value != -1) { |
| 273 |
result = value; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
return result; |
| 281 |
} |
| 282 |
|
| 283 |
private void setArguments(PriorType priorType) { |
| 284 |
|
| 285 |
optionsPanels.get(PriorType.NORMAL_PRIOR).setArguments(parameter); |
| 286 |
optionsPanels.get(PriorType.GAMMA_PRIOR).setArguments(parameter); |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
public void getArguments() { |
| 291 |
for (Parameter parameter : parameterList) { |
| 292 |
parameter.priorType = (PriorType) priorCombo.getSelectedItem(); |
| 293 |
} |
| 294 |
|
| 295 |
optionsPanels.get(PriorType.NORMAL_PRIOR).getArguments(parameter); |
| 296 |
optionsPanels.get(PriorType.GAMMA_PRIOR).getArguments(parameter); |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
private void setupComponents() { |
| 301 |
panel.removeAll(); |
| 302 |
|
| 303 |
JPanel mainPanel = new JPanel(new GridBagLayout()); |
| 304 |
panel.add(mainPanel); |
| 305 |
|
| 306 |
OptionsPanel[] optionsPanel = new OptionsPanel[4]; |
| 307 |
JPanel panel[] = new JPanel[4]; |
| 308 |
for (int i = 0; i < 4; ++i) { |
| 309 |
optionsPanel[i] = new OptionsPanel(12, (OSType.isMac() ? 6 : 24)); |
| 310 |
panel[i] = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
| 311 |
if (i > 1) { |
| 312 |
panel[i].setLayout(new BoxLayout(panel[i], BoxLayout.PAGE_AXIS)); |
| 313 |
} |
| 314 |
panel[i].add(optionsPanel[i]); |
| 315 |
|
| 316 |
GridBagConstraints gbc = new GridBagConstraints(); |
| 317 |
gbc.gridx = i % 2; |
| 318 |
gbc.gridy = i / 2; |
| 319 |
mainPanel.add(panel[i], gbc); |
| 320 |
} |
| 321 |
|
| 322 |
String modelName = "untitled"; |
| 323 |
nameField = new JTextField(modelName); |
| 324 |
nameField.setColumns(10); |
| 325 |
optionsPanel[0].addComponentWithLabel("Unique Name: ", nameField); |
| 326 |
|
| 327 |
PriorType modelType; |
| 328 |
optionsPanel[0].addComponentWithLabel("Hierarchical Distribution: ", priorCombo); |
| 329 |
modelType = (PriorType) priorCombo.getSelectedItem(); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
optionsPanel[1].addComponent(new JLabel("Selected parameters: ")); |
| 334 |
|
| 335 |
Object[] parameters = parameterList.toArray(); |
| 336 |
|
| 337 |
JList list = new JList(parameters); |
| 338 |
|
| 339 |
list.setLayoutOrientation(JList.VERTICAL); |
| 340 |
list.setVisibleRowCount(-1); |
| 341 |
list.setEnabled(false); |
| 342 |
JScrollPane scrollPane = new JScrollPane(list); |
| 343 |
scrollPane.setPreferredSize(new Dimension(250, 120)); |
| 344 |
|
| 345 |
optionsPanel[1].addSpanningComponent(scrollPane); |
| 346 |
|
| 347 |
optionsPanel[2].addSeparator(); |
| 348 |
optionsPanel[3].addSeparator(); |
| 349 |
|
| 350 |
optionsPanel[2].addSpanningComponent(new JLabel("Population Mean Hyperprior: Normal")); |
| 351 |
optionsPanel[3].addSpanningComponent(new JLabel("Population Precision Hyperprior: Gamma")); |
| 352 |
|
| 353 |
optionsPanel[2].addSpanningComponent(optionsPanels.get(PriorType.NORMAL_PRIOR)); |
| 354 |
optionsPanel[3].addSpanningComponent(optionsPanels.get(PriorType.GAMMA_PRIOR)); |
| 355 |
|
| 356 |
optionsPanel[2].addSeparator(); |
| 357 |
optionsPanel[3].addSeparator(); |
| 358 |
|
| 359 |
setupChart(); |
| 360 |
for (int i = 0; i < 2; ++i) { |
| 361 |
chart[i].setPreferredSize(new Dimension(300, 200)); |
| 362 |
chart[i].setFontSize(8); |
| 363 |
panel[2 + i].add(chart[i]); |
| 364 |
panel[2 + i].add(quantilePanel[i]); |
| 365 |
} |
| 366 |
|
| 367 |
setArguments(modelType); |
| 368 |
mainPanel.repaint(); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
NumberFormatter formatter = new NumberFormatter(4); |
| 380 |
|
| 381 |
private void setupChart() { |
| 382 |
for (int i = 0; i < 2; ++i) { |
| 383 |
chart[i].removeAllPlots(); |
| 384 |
|
| 385 |
double offset = 0.0; |
| 386 |
Distribution distribution = null; |
| 387 |
switch (i) { |
| 388 |
case 0: |
| 389 |
distribution = optionsPanels.get(PriorType.NORMAL_PRIOR).getDistribution(); |
| 390 |
break; |
| 391 |
case 1: |
| 392 |
distribution = optionsPanels.get(PriorType.GAMMA_PRIOR).getDistribution(); |
| 393 |
break; |
| 394 |
} |
| 395 |
|
| 396 |
chart[i].addPlot(new PDFPlot(distribution, offset)); |
| 397 |
if (distribution != null) { |
| 398 |
quantileText[i].setText(formatter.format(distribution.quantile(0.025)) + |
| 399 |
"\n" + formatter.format(distribution.quantile(0.05)) + |
| 400 |
"\n" + formatter.format(distribution.quantile(0.5)) + |
| 401 |
"\n" + formatter.format(distribution.quantile(0.95)) + |
| 402 |
"\n" + formatter.format(distribution.quantile(0.975))); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
class NormalOptionsPanel extends PriorOptionsPanel { |
| 410 |
|
| 411 |
public NormalOptionsPanel() { |
| 412 |
super(false); |
| 413 |
} |
| 414 |
|
| 415 |
public void setup() { |
| 416 |
addField("Hyperprior Mean", 0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); |
| 417 |
addField("Hyperprior Stdev", 1000.0, 0.0, Double.MAX_VALUE); |
| 418 |
} |
| 419 |
|
| 420 |
public Distribution getDistribution() { |
| 421 |
return new NormalDistribution(getValue(0), getValue(1)); |
| 422 |
} |
| 423 |
|
| 424 |
public void setArguments(Parameter parameter) { |
| 425 |
getInitialField().setValue(hpmMeanInitial); |
| 426 |
getField(0).setValue(hpmMeanMean); |
| 427 |
getField(1).setValue(hpmMeanStDev); |
| 428 |
} |
| 429 |
|
| 430 |
public void getArguments(Parameter parameter) { |
| 431 |
hpmMeanInitial = getInitialField().getValue(); |
| 432 |
hpmMeanMean = getValue(0); |
| 433 |
hpmMeanStDev = getValue(1); |
| 434 |
} |
| 435 |
|
| 436 |
@Override |
| 437 |
boolean isInputValid() { |
| 438 |
return true; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
class GammaOptionsPanel extends PriorOptionsPanel { |
| 443 |
|
| 444 |
public GammaOptionsPanel() { |
| 445 |
super(false); |
| 446 |
} |
| 447 |
|
| 448 |
public void setup() { |
| 449 |
addField("Hyperprior Shape", 0.001, Double.MIN_VALUE, Double.POSITIVE_INFINITY); |
| 450 |
addField("Hyperprior Scale", 1000.0, Double.MIN_VALUE, Double.POSITIVE_INFINITY); |
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
public Distribution getDistribution() { |
| 455 |
return new OffsetPositiveDistribution( |
| 456 |
new GammaDistribution(getValue(0), getValue(1)), 0.0); |
| 457 |
} |
| 458 |
|
| 459 |
public void setArguments(Parameter parameter) { |
| 460 |
getInitialField().setValue(hpmPrecInitial); |
| 461 |
getField(0).setValue(hpmPrecShape); |
| 462 |
getField(1).setValue(hpmPrecScale); |
| 463 |
} |
| 464 |
|
| 465 |
public void getArguments(Parameter parameter) { |
| 466 |
hpmPrecInitial = getInitialField().getValue(); |
| 467 |
parameter.shape = hpmPrecShape = getValue(0); |
| 468 |
parameter.scale = hpmPrecScale = getValue(1); |
| 469 |
} |
| 470 |
|
| 471 |
@Override |
| 472 |
boolean isInputValid() { |
| 473 |
return true; |
| 474 |
} |
| 475 |
} |
| 476 |
} |