Debian Codegraph @

src/dr/app/beauti/ancestralstatespanel/AncestralStatesOptionsPanel.java

raw symbols SCIP
1 /*
2 * AncestralStatesOptionsPanel.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.ancestralstatespanel;
27
28 import dr.app.beauti.components.ancestralstates.AncestralStatesComponentOptions;
29 import dr.app.beauti.components.sequenceerror.SequenceErrorModelComponentOptions;
30 import dr.app.beauti.options.AbstractPartitionData;
31 import dr.app.beauti.options.BeautiOptions;
32 import dr.app.beauti.types.SequenceErrorType;
33 import dr.app.beauti.util.PanelUtils;
34 import dr.app.util.OSType;
35 import dr.evolution.datatype.DataType;
36 import dr.evolution.util.Taxa;
37 import jam.panels.OptionsPanel;
38
39 import javax.swing.*;
40 import java.awt.*;
41 import java.awt.event.ItemEvent;
42 import java.awt.event.ItemListener;
43
44 /**
45 * @author Alexei Drummond
46 * @author Andrew Rambaut
47 * @author Walter Xie
48 */
49 public class AncestralStatesOptionsPanel extends OptionsPanel {
50
51 private static final String ROBUST_COUNTING_TOOL_TIP = "<html>"
52 + "Enable counting of reconstructed number of substitutions as described in<br>"
53 + "Minin & Suchard (2008). These will be annotated directly in the<br>"
54 + "logged trees.</html>";
55
56 private static final String DNDS_ROBUST_COUNTING_TOOL_TIP = "<html>"
57 + "Enable counting of synonymous and non-synonymous substitution as described in<br>"
58 + "O'Brien, Minin & Suchard (2009) and Lemey, Minin, Bielejec, Kosakovsky-Pond &<br>"
59 + "Suchard (2012). This model requires a 3-partition codon model to be<br>"
60 + "selected in the Site model for this partition and NO Site Heterogeneity Model.</html>";
61
62 private static final String COMPLETE_HISTORY_LOGGING_TOOL_TIP = "<html>"
63 + "Log a complete history of realised state changes to the tree log file.<br>"
64 + "This can make the files very large but can be useful for post hoc analysis.</html>";
65
66 // Components
67 private static final long serialVersionUID = -1645661616353099424L;
68
69 private final AbstractPartitionData partition;
70
71 private JCheckBox ancestralReconstructionCheck = new JCheckBox(
72 "Reconstruct states at all ancestors");
73 private JCheckBox mrcaReconstructionCheck = new JCheckBox(
74 "Reconstruct states at ancestor:");
75 private JComboBox mrcaReconstructionCombo = new JComboBox();
76 private JCheckBox countingCheck = new JCheckBox(
77 "Reconstruct state change counts");
78 private JCheckBox dNdSRobustCountingCheck = new JCheckBox(
79 "Reconstruct synonymous/non-synonymous change counts");
80 private JCheckBox completeHistoryLoggingCheck = new JCheckBox(
81 "Reconstruct complete change history on tree");
82
83
84 private JTextArea dNnSText = new JTextArea(
85 "This model requires a 3-partition codon model to be selected in the Site model " +
86 "for this partition and NO Site Heterogeneity Model before it can be selected.");
87
88 // dNdS robust counting is automatic if RC is turned on for a codon
89 // partitioned data set.
90 // private JCheckBox dNdSCountingCheck = new JCheckBox(
91 // "Reconstruct synonymous/non-synonymous counts");
92
93 final BeautiOptions options;
94
95 // JComboBox errorModelCombo = new JComboBox(EnumSet.range(SequenceErrorType.NO_ERROR, SequenceErrorType.BASE_ALL).toArray());
96 JComboBox errorModelCombo = new JComboBox(SequenceErrorType.values());
97
98 AncestralStatesComponentOptions ancestralStatesComponent;
99 SequenceErrorModelComponentOptions sequenceErrorComponent;
100
101 public AncestralStatesOptionsPanel(final AncestralStatesPanel ancestralStatesPanel, final BeautiOptions options, final AbstractPartitionData partition) {
102
103 super(12, (OSType.isMac() ? 6 : 24));
104 setOpaque(false);
105
106 this.partition = partition;
107 this.options = options;
108
109 PanelUtils.setupComponent(ancestralReconstructionCheck);
110 ancestralReconstructionCheck
111 .setToolTipText("<html>"
112 + "Reconstruct posterior realizations of the states at ancestral nodes.<br>" +
113 "These will be annotated directly in the logged trees.</html>");
114
115 PanelUtils.setupComponent(mrcaReconstructionCheck);
116 mrcaReconstructionCheck
117 .setToolTipText("<html>"
118 + "Reconstruct posterior realizations of the states at a specific common<br>" +
119 "ancestor defined by a taxon set. This will be recorded in the log file.</html>");
120 PanelUtils.setupComponent(mrcaReconstructionCombo);
121 mrcaReconstructionCombo
122 .setToolTipText("<html>"
123 + "Reconstruct posterior realizations of the states at a specific common.<br>" +
124 "ancestor defined by a taxon set. This will be recorded in the log file.</html>");
125
126 PanelUtils.setupComponent(countingCheck);
127 countingCheck.setToolTipText(ROBUST_COUNTING_TOOL_TIP);
128
129 PanelUtils.setupComponent(dNdSRobustCountingCheck);
130 dNdSRobustCountingCheck.setToolTipText(DNDS_ROBUST_COUNTING_TOOL_TIP);
131
132 PanelUtils.setupComponent(completeHistoryLoggingCheck);
133 completeHistoryLoggingCheck.setToolTipText(COMPLETE_HISTORY_LOGGING_TOOL_TIP);
134
135 // ////////////////////////
136 PanelUtils.setupComponent(errorModelCombo);
137 errorModelCombo.setToolTipText("<html>Select how to model sequence error or<br>"
138 + "post-mortem DNA damage.</html>");
139
140
141 // Set the initial options
142 ancestralStatesComponent = (AncestralStatesComponentOptions)options.getComponentOptions(AncestralStatesComponentOptions.class);
143 // ancestralStatesComponent.createParameters(options);
144 ancestralReconstructionCheck.setSelected(ancestralStatesComponent.reconstructAtNodes(partition));
145 mrcaReconstructionCheck.setSelected(ancestralStatesComponent.reconstructAtMRCA(partition));
146 mrcaReconstructionCombo.setSelectedItem(ancestralStatesComponent.getMRCATaxonSet(partition));
147 countingCheck.setSelected(ancestralStatesComponent.isCountingStates(partition));
148 dNdSRobustCountingCheck.setSelected(ancestralStatesComponent.dNdSRobustCounting(partition));
149 completeHistoryLoggingCheck.setSelected(ancestralStatesComponent.isCompleteHistoryLogging(partition));
150
151 sequenceErrorComponent = (SequenceErrorModelComponentOptions)options.getComponentOptions(SequenceErrorModelComponentOptions.class);
152 // sequenceErrorComponent.createParameters(options); // this cannot create correct param here, because of improper design
153 errorModelCombo.setSelectedItem(sequenceErrorComponent.getSequenceErrorType(partition));
154
155 setupPanel();
156
157 ItemListener listener = new ItemListener() {
158 public void itemStateChanged(final ItemEvent itemEvent) {
159 optionsChanged();
160 ancestralStatesPanel.fireModelChanged();
161
162 // The following is only necessary is simpleCounting XOR robustCounting
163 // if (itemEvent.getItem() == countingCheck) {
164 // boolean enableRC = !countingCheck.isSelected() && ancestralStatesComponent.dNdSRobustCountingAvailable(partition);
165 // dNdSRobustCountingCheck.setEnabled(enableRC);
166 // dNnSText.setEnabled(enableRC);
167 // }
168 //
169 // if (itemEvent.getItem() == dNdSRobustCountingCheck) {
170 // boolean enableSimpleCounting = !dNdSRobustCountingCheck.isSelected();
171 // countingCheck.setEnabled(enableSimpleCounting);
172 // }
173 completeHistoryLoggingCheck.setEnabled(countingCheck.isSelected() || dNdSRobustCountingCheck.isSelected());
174 }
175 };
176
177 ancestralReconstructionCheck.addItemListener(listener);
178 mrcaReconstructionCheck.addItemListener(listener);
179 mrcaReconstructionCombo.addItemListener(listener);
180 countingCheck.addItemListener(listener);
181 dNdSRobustCountingCheck.addItemListener(listener);
182 completeHistoryLoggingCheck.addItemListener(listener);
183
184 errorModelCombo.addItemListener(listener);
185 }
186
187 private void optionsChanged() {
188 if (isUpdating) return;
189
190 ancestralStatesComponent.setReconstructAtNodes(partition, ancestralReconstructionCheck.isSelected());
191 ancestralStatesComponent.setReconstructAtMRCA(partition, mrcaReconstructionCheck.isSelected());
192 mrcaReconstructionCombo.setEnabled(mrcaReconstructionCheck.isSelected());
193 if (mrcaReconstructionCombo.getSelectedIndex() == 0) {
194 // root node
195 ancestralStatesComponent.setMRCATaxonSet(partition, null);
196 } else {
197 String text = (String) mrcaReconstructionCombo.getSelectedItem();
198 String taxonSetId = text.substring(5,text.length() - 1);
199
200 ancestralStatesComponent.setMRCATaxonSet(partition, taxonSetId);
201 }
202 ancestralStatesComponent.setCountingStates(partition, countingCheck.isSelected());
203 // ancestralStatesComponent.setDNdSRobustCounting(partition, robustCountingCheck.isSelected());
204 ancestralStatesComponent.setDNdSRobustCounting(partition, dNdSRobustCountingCheck.isSelected());
205 ancestralStatesComponent.setCompleteHistoryLogging(partition, completeHistoryLoggingCheck.isSelected());
206
207 sequenceErrorComponent.setSequenceErrorType(partition, (SequenceErrorType)errorModelCombo.getSelectedItem());
208 sequenceErrorComponent.createParameters(options);
209 }
210
211 /**
212 * Lays out the appropriate components in the panel for this partition
213 * model.
214 */
215 void setupPanel() {
216
217 isUpdating = true;
218
219 String selectedItem = (String)mrcaReconstructionCombo.getSelectedItem();
220
221 if (mrcaReconstructionCombo.getItemCount() > 0) {
222 mrcaReconstructionCombo.removeAllItems();
223 }
224 mrcaReconstructionCombo.addItem("Tree Root");
225 if (options.taxonSets.size() > 0) {
226 for (Taxa taxonSet : options.taxonSets) {
227 mrcaReconstructionCombo.addItem("MRCA("+ taxonSet.getId() + ")");
228 }
229 if (selectedItem != null) {
230 mrcaReconstructionCombo.setSelectedItem(selectedItem);
231 }
232 }
233 mrcaReconstructionCombo.setEnabled(mrcaReconstructionCheck.isSelected());
234
235 boolean ancestralReconstructionAvailable = true;
236 boolean countingAvailable = true;
237 boolean dNdSRobustCountingAvailable = false;
238 boolean errorModelAvailable = false;
239
240 switch (partition.getDataType().getType()) {
241 case DataType.NUCLEOTIDES:
242 errorModelAvailable = true;
243 dNdSRobustCountingAvailable = true; // but will be disabled if not codon partitioned
244 break;
245 case DataType.AMINO_ACIDS:
246 case DataType.GENERAL:
247 case DataType.TWO_STATES:
248 break;
249 case DataType.CONTINUOUS:
250 countingAvailable = false;
251 break;
252 case DataType.MICRO_SAT:
253 ancestralReconstructionAvailable = false;
254 countingAvailable = false;
255 break;
256 default:
257 throw new IllegalArgumentException("Unsupported data type");
258
259 }
260
261 removeAll();
262
263 if (ancestralReconstructionAvailable) {
264 if (partition.getPartitionSubstitutionModel().getCodonPartitionCount() == 2) {
265 // mergedPatterns for codon positions 1&2 will always be compressed...
266 // so cannot do any of this stuff. Disable it and provide an explanation.
267 addSpanningComponent(new JLabel("<html>Unable to provide these options with the 1+2,3 codon<br>" +
268 "position model. Use a 1,2,3 codon position model instead.<br><html>"));
269 }
270
271
272 JLabel label1 = new JLabel("Ancestral State Reconstruction:");
273 addSpanningComponent(label1);
274
275 addComponent(ancestralReconstructionCheck);
276
277 FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
278 layout.setHgap(0);
279 JPanel panel = new JPanel(layout);
280 panel.setOpaque(false);
281 panel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
282 panel.add(mrcaReconstructionCheck);
283 panel.add(mrcaReconstructionCombo);
284 addComponent(panel);
285
286 boolean enabled = true;
287
288 if (partition.getPartitionSubstitutionModel().getCodonPartitionCount() == 2) {
289 // mergedPatterns for codon positions 1&2 will always be compressed...
290 // so cannot do any of this stuff. Disable it and provide an explanation.
291 ancestralReconstructionCheck.setEnabled(false);
292 enabled = false;
293 }
294
295 label1.setEnabled(enabled);
296 panel.setEnabled(enabled);
297 ancestralReconstructionCheck.setEnabled(enabled);
298 mrcaReconstructionCheck.setEnabled(enabled);
299 mrcaReconstructionCombo.setEnabled(enabled);
300 }
301
302 if (countingAvailable) {
303 if (ancestralReconstructionAvailable) {
304 addSeparator();
305 }
306
307 JLabel label2 = new JLabel("State Change Count Reconstruction:");
308 addSpanningComponent(label2);
309
310 JTextArea text1 = new JTextArea(
311 "Select this option to reconstruct counts of state changes using " +
312 "Markov Jumps. This approach is described in Minin & Suchard (2008).");
313 text1.setColumns(40);
314 PanelUtils.setupComponent(text1);
315 addComponent(text1);
316
317 addComponent(countingCheck);
318
319 boolean enableSimpleCounting = true;
320
321 // TODO Simple counting is currently not available for codon partitioned models due to BEAUti limitation
322 if (ancestralStatesComponent.dNdSRobustCountingAvailable(partition) || partition.getPartitionSubstitutionModel().getCodonPartitionCount() == 2) {
323 enableSimpleCounting = false;
324 countingCheck.setSelected(false);
325 }
326
327 countingCheck.setEnabled(enableSimpleCounting);
328 label2.setEnabled(enableSimpleCounting);
329 text1.setEnabled(enableSimpleCounting);
330
331 JTextArea text2 = null;
332
333 if (dNdSRobustCountingAvailable) {
334 // addSeparator();
335 text2 = new JTextArea(
336 "Renaissance counting: select this option to reconstruct counts of synonymous and nonsynonymous " +
337 "changes using Robust Counting. This approach is described in O'Brien, Minin " +
338 "& Suchard (2009) and Lemey, Minin, Bielejec, Kosakovsky-Pond & Suchard " +
339 "(2012):");
340 text2.setColumns(40);
341 PanelUtils.setupComponent(text2);
342 addComponent(text2);
343
344 addComponent(dNdSRobustCountingCheck);
345
346 dNnSText.setColumns(40);
347 dNnSText.setBorder(BorderFactory.createEmptyBorder(0, 32, 0, 0));
348 PanelUtils.setupComponent(dNnSText);
349 addComponent(dNnSText);
350
351 boolean enableRC = ancestralStatesComponent.dNdSRobustCountingAvailable(partition);
352 // && !ancestralStatesComponent.isCountingStates(partition);
353 dNdSRobustCountingCheck.setEnabled(enableRC);
354
355 ancestralStatesComponent.setDNdSRobustCounting(partition, enableRC && dNdSRobustCountingCheck.isSelected());
356
357 text2.setEnabled(enableRC);
358 dNnSText.setEnabled(enableRC);
359 if (!enableRC) {
360 dNdSRobustCountingCheck.setSelected(false);
361 }
362 }
363
364 addComponent(completeHistoryLoggingCheck);
365 completeHistoryLoggingCheck.setEnabled(countingCheck.isSelected() || dNdSRobustCountingCheck.isSelected());
366
367 }
368
369 if (errorModelAvailable) {
370 if (ancestralReconstructionAvailable || countingAvailable) {
371 addSeparator();
372 }
373 JLabel label3 = new JLabel("Sequence error model:");
374 addSpanningComponent(label3);
375 JLabel label4 = addComponentWithLabel("Error Model:", errorModelCombo);
376
377 boolean enabled = (partition.getPartitionSubstitutionModel().getCodonPartitionCount() != 2);
378
379 label3.setEnabled(enabled);
380 label4.setEnabled(enabled);
381 errorModelCombo.setEnabled(enabled);
382
383 }
384
385 isUpdating = false;
386
387 }
388
389 private boolean isUpdating = false;
390
391 }