2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.analysis.TreeBuilder;
24 import jalview.analysis.scoremodels.ScoreModels;
25 import jalview.analysis.scoremodels.SimilarityParams;
26 import jalview.api.analysis.ScoreModelI;
27 import jalview.api.analysis.SimilarityParamsI;
28 import jalview.util.MessageManager;
30 import java.awt.BorderLayout;
31 import java.awt.Color;
32 import java.awt.Component;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
36 import java.awt.GridLayout;
37 import java.awt.Insets;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.FocusEvent;
41 import java.awt.event.FocusListener;
42 import java.awt.event.MouseAdapter;
43 import java.awt.event.MouseEvent;
44 import java.beans.PropertyVetoException;
45 import java.util.ArrayList;
46 import java.util.List;
48 import javax.swing.BorderFactory;
49 import javax.swing.ButtonGroup;
50 import javax.swing.JButton;
51 import javax.swing.JCheckBox;
52 import javax.swing.JComboBox;
53 import javax.swing.JInternalFrame;
54 import javax.swing.JLabel;
55 import javax.swing.JLayeredPane;
56 import javax.swing.JPanel;
57 import javax.swing.JRadioButton;
58 import javax.swing.event.InternalFrameAdapter;
59 import javax.swing.event.InternalFrameEvent;
62 * A dialog where a user can choose and action Tree or PCA calculation options
64 public class CalculationChooser extends JPanel
67 * flag for whether gap matches residue in the PID calculation for a Tree
68 * - true gives Jalview 2.10.1 behaviour
69 * - set to false (using Groovy) for a more correct tree
72 private static boolean treeMatchGaps = true;
74 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
80 JRadioButton neighbourJoining;
82 JRadioButton averageDistance;
84 JComboBox<String> modelNames;
88 private JInternalFrame frame;
90 private JCheckBox includeGaps;
92 private JCheckBox matchGaps;
94 private JCheckBox includeGappedColumns;
96 private JCheckBox shorterSequence;
103 public CalculationChooser(AlignFrame alignFrame)
105 this.af = alignFrame;
107 af.alignPanel.setCalculationDialog(this);
111 * Lays out the panel and adds it to the desktop
115 setLayout(new BorderLayout());
116 frame = new JInternalFrame();
117 frame.setContentPane(this);
118 this.setBackground(Color.white);
121 * Layout consists of 3 or 4 panels:
122 * - first with choice of PCA or tree method NJ or AV
123 * - second with choice of score model
124 * - third with score model parameter options [suppressed]
125 * - fourth with OK and Cancel
127 pca = new JRadioButton(
128 MessageManager.getString("label.principal_component_analysis"));
129 pca.setOpaque(false);
130 neighbourJoining = new JRadioButton(
131 MessageManager.getString("label.tree_calc_nj"));
132 averageDistance = new JRadioButton(
133 MessageManager.getString("label.tree_calc_av"));
134 neighbourJoining.setOpaque(false);
136 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
137 calcChoicePanel.setOpaque(false);
139 // first create the Tree calculation's border panel
140 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
141 treePanel.setOpaque(false);
143 treePanel.setBorder(BorderFactory.createTitledBorder(MessageManager
144 .getString("label.tree")));
146 // then copy the inset dimensions for the border-less PCA panel
147 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
148 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
149 pcaBorderless.setBorder(BorderFactory.createEmptyBorder(2, b.left, 2,
151 pcaBorderless.setOpaque(false);
153 pcaBorderless.add(pca, FlowLayout.LEFT);
154 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
157 treePanel.add(neighbourJoining);
158 treePanel.add(averageDistance);
160 calcChoicePanel.add(treePanel);
162 ButtonGroup calcTypes = new ButtonGroup();
164 calcTypes.add(neighbourJoining);
165 calcTypes.add(averageDistance);
167 ActionListener calcChanged = new ActionListener()
170 public void actionPerformed(ActionEvent e)
175 pca.addActionListener(calcChanged);
176 neighbourJoining.addActionListener(calcChanged);
177 averageDistance.addActionListener(calcChanged);
179 * score models drop-down - with added tooltips!
181 modelNames = buildModelOptionsList();
183 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
184 scoreModelPanel.setOpaque(false);
185 scoreModelPanel.add(modelNames);
188 * score model parameters
190 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
191 paramsPanel.setOpaque(false);
192 includeGaps = new JCheckBox("Include gaps");
193 matchGaps = new JCheckBox("Match gaps");
194 includeGappedColumns = new JCheckBox("Include gapped columns");
195 shorterSequence = new JCheckBox("Match on shorter sequence");
196 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
197 paramsPanel.add(includeGaps);
198 paramsPanel.add(matchGaps);
199 paramsPanel.add(includeGappedColumns);
200 paramsPanel.add(shorterSequence);
203 * OK / Cancel buttons
205 ok = new JButton(MessageManager.getString("action.calculate"));
206 ok.setFont(VERDANA_11PT);
207 ok.addActionListener(new java.awt.event.ActionListener()
210 public void actionPerformed(ActionEvent e)
212 ok_actionPerformed();
215 JButton cancel = new JButton(MessageManager.getString("action.close"));
216 cancel.setFont(VERDANA_11PT);
217 cancel.addActionListener(new java.awt.event.ActionListener()
220 public void actionPerformed(ActionEvent e)
222 cancel_actionPerformed(e);
225 JPanel actionPanel = new JPanel();
226 actionPanel.setOpaque(false);
228 actionPanel.add(cancel);
230 boolean includeParams = false;
231 this.add(calcChoicePanel, BorderLayout.CENTER);
232 calcChoicePanel.add(scoreModelPanel);
235 scoreModelPanel.add(paramsPanel);
237 this.add(actionPanel, BorderLayout.SOUTH);
240 int height = includeParams ? 420 : 240;
242 setMinimumSize(new Dimension(325, height - 10));
243 String title = MessageManager.getString("label.choose_calculation");
244 if (af.getViewport().viewName != null)
246 title = title + " (" + af.getViewport().viewName + ")";
249 Desktop.addInternalFrame(frame,
252 calcChoicePanel.doLayout();
255 * null the AlignmentPanel's reference to the dialog when it is closed
257 frame.addInternalFrameListener(new InternalFrameAdapter()
260 public void internalFrameClosed(InternalFrameEvent evt)
262 af.alignPanel.setCalculationDialog(null);
266 frame.setLayer(JLayeredPane.PALETTE_LAYER);
270 * enable calculations applicable for the current alignment or selection.
272 protected void validateCalcTypes()
274 int size = af.getViewport().getAlignment().getHeight();
275 if (af.getViewport().getSelectionGroup() != null)
277 size = af.getViewport().getSelectionGroup().getSize();
279 if (!(checkEnabled(pca, size, 4)
280 | checkEnabled(neighbourJoining, size, 3) | checkEnabled(
281 averageDistance, size, 3)))
283 ok.setToolTipText(null);
288 ok.setEnabled(false);
293 * Check the input and disable a calculation's radio button if necessary. A
294 * tooltip is shown for disabled calculations.
297 * - radio button for the calculation being validated
299 * - size of input to calculation
301 * - minimum size for calculation
302 * @return true if size < minsize *and* calc.isSelected
304 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
306 String ttip = MessageManager.formatMessage(
307 "label.you_need_more_than_n_sequences", minsize);
309 calc.setEnabled(size >= minsize);
310 if (!calc.isEnabled())
312 calc.setToolTipText(ttip);
316 calc.setToolTipText(null);
318 if (calc.isSelected())
320 modelNames.setEnabled(calc.isEnabled());
321 if (!calc.isEnabled())
323 ok.setEnabled(false);
324 ok.setToolTipText(ttip);
331 * A rather elaborate helper method (blame Swing, not me) that builds a
332 * drop-down list of score models (by name) with descriptions as tooltips.
333 * There is also a tooltip shown for the currently selected item when hovering
334 * over it (without opening the list).
336 protected JComboBox<String> buildModelOptionsList()
338 final JComboBox<String> comboBox = new JComboBox<String>();
339 ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
340 comboBox.setRenderer(renderer);
341 final List<String> tips = new ArrayList<String>();
344 * show tooltip on mouse over the combobox
345 * note the listener has to be on the components that make up
346 * the combobox, doesn't work if just on the combobox
348 MouseAdapter mouseListener = new MouseAdapter()
351 public void mouseEntered(MouseEvent e)
353 comboBox.setToolTipText(tips.get(comboBox.getSelectedIndex()));
357 public void mouseExited(MouseEvent e)
359 comboBox.setToolTipText(null);
362 for (Component c : comboBox.getComponents())
364 c.addMouseListener(mouseListener);
368 * now we can actually add entries to the combobox,
369 * remembering their descriptions for tooltips
371 ScoreModels scoreModels = ScoreModels.getInstance();
372 for (ScoreModelI sm : scoreModels.getModels())
374 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
375 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
377 comboBox.addItem(sm.getName());
380 * tooltip is description if provided, else text lookup with
381 * fallback on the model name
383 String tooltip = sm.getDescription();
386 tooltip = MessageManager.getStringOrReturn("label.score_model_",
393 * set the list of tooltips on the combobox's renderer
395 renderer.setTooltips(tips);
402 * Open and calculate the selected tree or PCA on 'OK'
404 protected void ok_actionPerformed()
406 boolean doPCA = pca.isSelected();
407 String modelName = modelNames.getSelectedItem().toString();
408 SimilarityParamsI params = getSimilarityParameters(doPCA);
412 openPcaPanel(modelName, params);
416 openTreePanel(modelName, params);
423 * Open a new Tree panel on the desktop
428 protected void openTreePanel(String modelName, SimilarityParamsI params)
430 String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
431 : TreeBuilder.AVERAGE_DISTANCE;
432 af.newTreePanel(treeType, modelName, params);
436 * Open a new PCA panel on the desktop
441 protected void openPcaPanel(String modelName, SimilarityParamsI params)
443 AlignViewport viewport = af.getViewport();
444 if (((viewport.getSelectionGroup() != null)
445 && (viewport.getSelectionGroup().getSize() < 4) && (viewport
446 .getSelectionGroup().getSize() > 0))
447 || (viewport.getAlignment().getHeight() < 4))
450 .showInternalMessageDialog(
453 .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
455 .getString("label.sequence_selection_insufficient"),
456 JvOptionPane.WARNING_MESSAGE);
459 new PCAPanel(af.alignPanel, modelName, params);
465 protected void closeFrame()
469 frame.setClosed(true);
470 } catch (PropertyVetoException ex)
476 * Returns a data bean holding parameters for similarity (or distance) model
482 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
484 // commented out: parameter choices read from gui widgets
485 // SimilarityParamsI params = new SimilarityParams(
486 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
487 // includeGaps.isSelected(), shorterSequence.isSelected());
489 boolean includeGapGap = true;
490 boolean includeGapResidue = true;
491 boolean matchOnShortestLength = false;
494 * 'matchGaps' flag is only used in the PID calculation
495 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
496 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
497 * - set to false for Tree for a more correct calculation (JAL-374)
499 boolean matchGap = doPCA ? false : treeMatchGaps;
501 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength);
505 * Closes dialog on cancel
509 protected void cancel_actionPerformed(ActionEvent e)
513 frame.setClosed(true);
514 } catch (Exception ex)