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.DefaultComboBoxModel;
51 import javax.swing.JButton;
52 import javax.swing.JCheckBox;
53 import javax.swing.JComboBox;
54 import javax.swing.JInternalFrame;
55 import javax.swing.JLabel;
56 import javax.swing.JLayeredPane;
57 import javax.swing.JPanel;
58 import javax.swing.JRadioButton;
59 import javax.swing.event.InternalFrameAdapter;
60 import javax.swing.event.InternalFrameEvent;
63 * A dialog where a user can choose and action Tree or PCA calculation options
65 public class CalculationChooser extends JPanel
68 * flag for whether gap matches residue in the PID calculation for a Tree
69 * - true gives Jalview 2.10.1 behaviour
70 * - set to false (using Groovy) for a more correct tree
73 private static boolean treeMatchGaps = true;
75 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
81 JRadioButton neighbourJoining;
83 JRadioButton averageDistance;
85 JComboBox<String> modelNames;
89 private JInternalFrame frame;
91 private JCheckBox includeGaps;
93 private JCheckBox matchGaps;
95 private JCheckBox includeGappedColumns;
97 private JCheckBox shorterSequence;
104 public CalculationChooser(AlignFrame alignFrame)
106 this.af = alignFrame;
108 af.alignPanel.setCalculationDialog(this);
112 * Lays out the panel and adds it to the desktop
116 setLayout(new BorderLayout());
117 frame = new JInternalFrame();
118 frame.setContentPane(this);
119 this.setBackground(Color.white);
120 frame.addFocusListener(new FocusListener()
124 public void focusLost(FocusEvent e)
129 public void focusGained(FocusEvent e)
135 * Layout consists of 3 or 4 panels:
136 * - first with choice of PCA or tree method NJ or AV
137 * - second with choice of score model
138 * - third with score model parameter options [suppressed]
139 * - fourth with OK and Cancel
141 pca = new JRadioButton(
142 MessageManager.getString("label.principal_component_analysis"));
143 pca.setOpaque(false);
144 neighbourJoining = new JRadioButton(
145 MessageManager.getString("label.tree_calc_nj"));
146 averageDistance = new JRadioButton(
147 MessageManager.getString("label.tree_calc_av"));
148 neighbourJoining.setOpaque(false);
150 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
151 calcChoicePanel.setOpaque(false);
153 // first create the Tree calculation's border panel
154 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
155 treePanel.setOpaque(false);
157 treePanel.setBorder(BorderFactory.createTitledBorder(MessageManager
158 .getString("label.tree")));
160 // then copy the inset dimensions for the border-less PCA panel
161 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
162 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
163 pcaBorderless.setBorder(BorderFactory.createEmptyBorder(2, b.left, 2,
165 pcaBorderless.setOpaque(false);
167 pcaBorderless.add(pca, FlowLayout.LEFT);
168 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
171 treePanel.add(neighbourJoining);
172 treePanel.add(averageDistance);
174 calcChoicePanel.add(treePanel);
176 ButtonGroup calcTypes = new ButtonGroup();
178 calcTypes.add(neighbourJoining);
179 calcTypes.add(averageDistance);
181 ActionListener calcChanged = new ActionListener()
184 public void actionPerformed(ActionEvent e)
189 pca.addActionListener(calcChanged);
190 neighbourJoining.addActionListener(calcChanged);
191 averageDistance.addActionListener(calcChanged);
193 * score models drop-down - with added tooltips!
195 modelNames = buildModelOptionsList();
197 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
198 scoreModelPanel.setOpaque(false);
199 scoreModelPanel.add(modelNames);
202 * score model parameters
204 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
205 paramsPanel.setOpaque(false);
206 includeGaps = new JCheckBox("Include gaps");
207 matchGaps = new JCheckBox("Match gaps");
208 includeGappedColumns = new JCheckBox("Include gapped columns");
209 shorterSequence = new JCheckBox("Match on shorter sequence");
210 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
211 paramsPanel.add(includeGaps);
212 paramsPanel.add(matchGaps);
213 paramsPanel.add(includeGappedColumns);
214 paramsPanel.add(shorterSequence);
217 * OK / Cancel buttons
219 ok = new JButton(MessageManager.getString("action.calculate"));
220 ok.setFont(VERDANA_11PT);
221 ok.addActionListener(new java.awt.event.ActionListener()
224 public void actionPerformed(ActionEvent e)
226 ok_actionPerformed();
229 JButton cancel = new JButton(MessageManager.getString("action.close"));
230 cancel.setFont(VERDANA_11PT);
231 cancel.addActionListener(new java.awt.event.ActionListener()
234 public void actionPerformed(ActionEvent e)
236 cancel_actionPerformed(e);
239 JPanel actionPanel = new JPanel();
240 actionPanel.setOpaque(false);
242 actionPanel.add(cancel);
244 boolean includeParams = false;
245 this.add(calcChoicePanel, BorderLayout.CENTER);
246 calcChoicePanel.add(scoreModelPanel);
249 scoreModelPanel.add(paramsPanel);
251 this.add(actionPanel, BorderLayout.SOUTH);
254 int height = includeParams ? 420 : 240;
256 setMinimumSize(new Dimension(325, height - 10));
257 String title = MessageManager.getString("label.choose_calculation");
258 if (af.getViewport().viewName != null)
260 title = title + " (" + af.getViewport().viewName + ")";
263 Desktop.addInternalFrame(frame,
266 calcChoicePanel.doLayout();
269 * null the AlignmentPanel's reference to the dialog when it is closed
271 frame.addInternalFrameListener(new InternalFrameAdapter()
274 public void internalFrameClosed(InternalFrameEvent evt)
276 af.alignPanel.setCalculationDialog(null);
280 frame.setLayer(JLayeredPane.PALETTE_LAYER);
284 * enable calculations applicable for the current alignment or selection.
286 protected void validateCalcTypes()
288 int size = af.getViewport().getAlignment().getHeight();
289 if (af.getViewport().getSelectionGroup() != null)
291 size = af.getViewport().getSelectionGroup().getSize();
293 if (!(checkEnabled(pca, size, 4)
294 | checkEnabled(neighbourJoining, size, 3) | checkEnabled(
295 averageDistance, size, 3)))
297 ok.setToolTipText(null);
302 ok.setEnabled(false);
304 updateScoreModels(comboBox, tips);
308 * Check the input and disable a calculation's radio button if necessary. A
309 * tooltip is shown for disabled calculations.
312 * - radio button for the calculation being validated
314 * - size of input to calculation
316 * - minimum size for calculation
317 * @return true if size < minsize *and* calc.isSelected
319 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
321 String ttip = MessageManager.formatMessage(
322 "label.you_need_more_than_n_sequences", minsize);
324 calc.setEnabled(size >= minsize);
325 if (!calc.isEnabled())
327 calc.setToolTipText(ttip);
331 calc.setToolTipText(null);
333 if (calc.isSelected())
335 modelNames.setEnabled(calc.isEnabled());
336 if (!calc.isEnabled())
338 ok.setEnabled(false);
339 ok.setToolTipText(ttip);
346 final JComboBox<String> comboBox = new JComboBox<String>();
348 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
350 List<String> tips = new ArrayList<String>();
353 * A rather elaborate helper method (blame Swing, not me) that builds a
354 * drop-down list of score models (by name) with descriptions as tooltips.
355 * There is also a tooltip shown for the currently selected item when hovering
356 * over it (without opening the list).
358 protected JComboBox<String> buildModelOptionsList()
360 comboBox.setRenderer(renderer);
363 * show tooltip on mouse over the combobox
364 * note the listener has to be on the components that make up
365 * the combobox, doesn't work if just on the combobox
367 final MouseAdapter mouseListener = new MouseAdapter()
370 public void mouseEntered(MouseEvent e)
372 comboBox.setToolTipText(tips.get(comboBox.getSelectedIndex()));
376 public void mouseExited(MouseEvent e)
378 comboBox.setToolTipText(null);
381 for (Component c : comboBox.getComponents())
383 c.addMouseListener(mouseListener);
386 updateScoreModels(comboBox, tips);
389 * set the list of tooltips on the combobox's renderer
391 renderer.setTooltips(tips);
396 private void updateScoreModels(JComboBox comboBox, List<String> tips)
398 Object curSel = comboBox.getSelectedItem();
400 DefaultComboBoxModel model = new DefaultComboBoxModel();
403 * now we can actually add entries to the combobox,
404 * remembering their descriptions for tooltips
406 ScoreModels scoreModels = ScoreModels.getInstance();
407 boolean selectedIsPresent = false;
408 for (ScoreModelI sm : scoreModels.getModels())
410 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
411 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
413 if (curSel != null && sm.getName().equals(curSel))
415 selectedIsPresent = true;
416 curSel = sm.getName();
418 model.addElement(sm.getName());
421 * tooltip is description if provided, else text lookup with
422 * fallback on the model name
424 String tooltip = sm.getDescription();
427 tooltip = MessageManager.getStringOrReturn("label.score_model_",
433 if (selectedIsPresent)
435 model.setSelectedItem(curSel);
437 // finally, update the model
438 comboBox.setModel(model);
442 * Open and calculate the selected tree or PCA on 'OK'
444 protected void ok_actionPerformed()
446 boolean doPCA = pca.isSelected();
447 String modelName = modelNames.getSelectedItem().toString();
448 SimilarityParamsI params = getSimilarityParameters(doPCA);
452 openPcaPanel(modelName, params);
456 openTreePanel(modelName, params);
463 * Open a new Tree panel on the desktop
468 protected void openTreePanel(String modelName, SimilarityParamsI params)
470 String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
471 : TreeBuilder.AVERAGE_DISTANCE;
472 af.newTreePanel(treeType, modelName, params);
476 * Open a new PCA panel on the desktop
481 protected void openPcaPanel(String modelName, SimilarityParamsI params)
483 AlignViewport viewport = af.getViewport();
484 if (((viewport.getSelectionGroup() != null)
485 && (viewport.getSelectionGroup().getSize() < 4) && (viewport
486 .getSelectionGroup().getSize() > 0))
487 || (viewport.getAlignment().getHeight() < 4))
490 .showInternalMessageDialog(
493 .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
495 .getString("label.sequence_selection_insufficient"),
496 JvOptionPane.WARNING_MESSAGE);
499 new PCAPanel(af.alignPanel, modelName, params);
505 protected void closeFrame()
509 frame.setClosed(true);
510 } catch (PropertyVetoException ex)
516 * Returns a data bean holding parameters for similarity (or distance) model
522 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
524 // commented out: parameter choices read from gui widgets
525 // SimilarityParamsI params = new SimilarityParams(
526 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
527 // includeGaps.isSelected(), shorterSequence.isSelected());
529 boolean includeGapGap = true;
530 boolean includeGapResidue = true;
531 boolean matchOnShortestLength = false;
534 * 'matchGaps' flag is only used in the PID calculation
535 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
536 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
537 * - set to false for Tree for a more correct calculation (JAL-374)
539 boolean matchGap = doPCA ? false : treeMatchGaps;
541 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength);
545 * Closes dialog on cancel
549 protected void cancel_actionPerformed(ActionEvent e)
553 frame.setClosed(true);
554 } catch (Exception ex)