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.datamodel.SequenceGroup;
29 import jalview.util.MessageManager;
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Component;
34 import java.awt.Dimension;
35 import java.awt.FlowLayout;
37 import java.awt.GridLayout;
38 import java.awt.Insets;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.FocusEvent;
42 import java.awt.event.FocusListener;
43 import java.awt.event.MouseAdapter;
44 import java.awt.event.MouseEvent;
45 import java.beans.PropertyVetoException;
46 import java.util.ArrayList;
47 import java.util.List;
49 import javax.swing.BorderFactory;
50 import javax.swing.ButtonGroup;
51 import javax.swing.DefaultComboBoxModel;
52 import javax.swing.JButton;
53 import javax.swing.JCheckBox;
54 import javax.swing.JComboBox;
55 import javax.swing.JInternalFrame;
56 import javax.swing.JLabel;
57 import javax.swing.JLayeredPane;
58 import javax.swing.JPanel;
59 import javax.swing.JRadioButton;
60 import javax.swing.event.InternalFrameAdapter;
61 import javax.swing.event.InternalFrameEvent;
64 * A dialog where a user can choose and action Tree or PCA calculation options
66 public class CalculationChooser extends JPanel
69 * flag for whether gap matches residue in the PID calculation for a Tree
70 * - true gives Jalview 2.10.1 behaviour
71 * - set to false (using Groovy) for a more correct tree
74 private static boolean treeMatchGaps = true;
76 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
78 private static final int MIN_TREE_SELECTION = 3;
80 private static final int MIN_PCA_SELECTION = 4;
86 JRadioButton neighbourJoining;
88 JRadioButton averageDistance;
90 JComboBox<String> modelNames;
94 private JInternalFrame frame;
96 private JCheckBox includeGaps;
98 private JCheckBox matchGaps;
100 private JCheckBox includeGappedColumns;
102 private JCheckBox shorterSequence;
104 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
106 List<String> tips = new ArrayList<String>();
109 * the most recently opened PCA results panel
111 private PCAPanel pcaPanel;
118 public CalculationChooser(AlignFrame alignFrame)
120 this.af = alignFrame;
122 af.alignPanel.setCalculationDialog(this);
126 * Lays out the panel and adds it to the desktop
130 setLayout(new BorderLayout());
131 frame = new JInternalFrame();
132 frame.setContentPane(this);
133 this.setBackground(Color.white);
134 frame.addFocusListener(new FocusListener()
138 public void focusLost(FocusEvent e)
143 public void focusGained(FocusEvent e)
149 * Layout consists of 3 or 4 panels:
150 * - first with choice of PCA or tree method NJ or AV
151 * - second with choice of score model
152 * - third with score model parameter options [suppressed]
153 * - fourth with OK and Cancel
155 pca = new JRadioButton(
156 MessageManager.getString("label.principal_component_analysis"));
157 pca.setOpaque(false);
159 neighbourJoining = new JRadioButton(
160 MessageManager.getString("label.tree_calc_nj"));
161 neighbourJoining.setSelected(true);
162 neighbourJoining.setOpaque(false);
164 averageDistance = new JRadioButton(
165 MessageManager.getString("label.tree_calc_av"));
166 averageDistance.setOpaque(false);
168 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
169 calcChoicePanel.setOpaque(false);
171 // first create the Tree calculation's border panel
172 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
173 treePanel.setOpaque(false);
175 JvSwingUtils.createTitledBorder(treePanel,
176 MessageManager.getString("label.tree"), true);
178 // then copy the inset dimensions for the border-less PCA panel
179 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
180 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
181 pcaBorderless.setBorder(
182 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
183 pcaBorderless.setOpaque(false);
185 pcaBorderless.add(pca, FlowLayout.LEFT);
186 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
188 treePanel.add(neighbourJoining);
189 treePanel.add(averageDistance);
191 calcChoicePanel.add(treePanel);
193 ButtonGroup calcTypes = new ButtonGroup();
195 calcTypes.add(neighbourJoining);
196 calcTypes.add(averageDistance);
198 ActionListener calcChanged = new ActionListener()
201 public void actionPerformed(ActionEvent e)
206 pca.addActionListener(calcChanged);
207 neighbourJoining.addActionListener(calcChanged);
208 averageDistance.addActionListener(calcChanged);
211 * score models drop-down - with added tooltips!
213 modelNames = buildModelOptionsList();
215 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
216 scoreModelPanel.setOpaque(false);
217 scoreModelPanel.add(modelNames);
220 * score model parameters
222 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
223 paramsPanel.setOpaque(false);
224 includeGaps = new JCheckBox("Include gaps");
225 matchGaps = new JCheckBox("Match gaps");
226 includeGappedColumns = new JCheckBox("Include gapped columns");
227 shorterSequence = new JCheckBox("Match on shorter sequence");
228 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
229 paramsPanel.add(includeGaps);
230 paramsPanel.add(matchGaps);
231 paramsPanel.add(includeGappedColumns);
232 paramsPanel.add(shorterSequence);
235 * OK / Cancel buttons
237 calculate = new JButton(MessageManager.getString("action.calculate"));
238 calculate.setFont(VERDANA_11PT);
239 calculate.addActionListener(new java.awt.event.ActionListener()
242 public void actionPerformed(ActionEvent e)
244 calculate_actionPerformed();
247 JButton close = new JButton(MessageManager.getString("action.close"));
248 close.setFont(VERDANA_11PT);
249 close.addActionListener(new java.awt.event.ActionListener()
252 public void actionPerformed(ActionEvent e)
254 close_actionPerformed();
257 JPanel actionPanel = new JPanel();
258 actionPanel.setOpaque(false);
259 actionPanel.add(calculate);
260 actionPanel.add(close);
262 boolean includeParams = false;
263 this.add(calcChoicePanel, BorderLayout.CENTER);
264 calcChoicePanel.add(scoreModelPanel);
267 scoreModelPanel.add(paramsPanel);
269 this.add(actionPanel, BorderLayout.SOUTH);
272 int height = includeParams ? 420 : 240;
274 setMinimumSize(new Dimension(325, height - 10));
275 String title = MessageManager.getString("label.choose_calculation");
276 if (af.getViewport().getViewName() != null)
278 title = title + " (" + af.getViewport().getViewName() + ")";
281 Desktop.addInternalFrame(frame, title, width, height, false);
282 calcChoicePanel.doLayout();
285 * null the AlignmentPanel's reference to the dialog when it is closed
287 frame.addInternalFrameListener(new InternalFrameAdapter()
290 public void internalFrameClosed(InternalFrameEvent evt)
292 af.alignPanel.setCalculationDialog(null);
297 frame.setLayer(JLayeredPane.PALETTE_LAYER);
301 * enable calculations applicable for the current alignment or selection.
303 protected void validateCalcTypes()
305 int size = af.getViewport().getAlignment().getHeight();
306 if (af.getViewport().getSelectionGroup() != null)
308 size = af.getViewport().getSelectionGroup().getSize();
312 * disable calc options for which there is insufficient input data
313 * return value of true means enabled and selected
315 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
316 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
318 boolean checkAverageDistance = checkEnabled(averageDistance, size,
321 if (checkPca || checkNeighbourJoining || checkAverageDistance)
323 calculate.setToolTipText(null);
324 calculate.setEnabled(true);
328 calculate.setEnabled(false);
330 updateScoreModels(modelNames, tips);
334 * Check the input and disable a calculation's radio button if necessary. A
335 * tooltip is shown for disabled calculations.
338 * - radio button for the calculation being validated
340 * - size of input to calculation
342 * - minimum size for calculation
343 * @return true if size >= minsize and calc.isSelected
345 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
347 String ttip = MessageManager
348 .formatMessage("label.you_need_at_least_n_sequences", minsize);
350 calc.setEnabled(size >= minsize);
351 if (!calc.isEnabled())
353 calc.setToolTipText(ttip);
357 calc.setToolTipText(null);
359 if (calc.isSelected())
361 modelNames.setEnabled(calc.isEnabled());
362 if (calc.isEnabled())
368 calculate.setToolTipText(ttip);
375 * A rather elaborate helper method (blame Swing, not me) that builds a
376 * drop-down list of score models (by name) with descriptions as tooltips.
377 * There is also a tooltip shown for the currently selected item when hovering
378 * over it (without opening the list).
380 protected JComboBox<String> buildModelOptionsList()
382 final JComboBox<String> scoreModelsCombo = new JComboBox<String>();
383 scoreModelsCombo.setRenderer(renderer);
386 * show tooltip on mouse over the combobox
387 * note the listener has to be on the components that make up
388 * the combobox, doesn't work if just on the combobox
390 final MouseAdapter mouseListener = new MouseAdapter()
393 public void mouseEntered(MouseEvent e)
395 scoreModelsCombo.setToolTipText(
396 tips.get(scoreModelsCombo.getSelectedIndex()));
400 public void mouseExited(MouseEvent e)
402 scoreModelsCombo.setToolTipText(null);
405 for (Component c : scoreModelsCombo.getComponents())
407 c.addMouseListener(mouseListener);
410 updateScoreModels(scoreModelsCombo, tips);
413 * set the list of tooltips on the combobox's renderer
415 renderer.setTooltips(tips);
417 return scoreModelsCombo;
420 private void updateScoreModels(JComboBox<String> comboBox,
421 List<String> toolTips)
423 Object curSel = comboBox.getSelectedItem();
425 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
428 * now we can actually add entries to the combobox,
429 * remembering their descriptions for tooltips
431 ScoreModels scoreModels = ScoreModels.getInstance();
432 boolean selectedIsPresent = false;
433 for (ScoreModelI sm : scoreModels.getModels())
435 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
436 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
438 if (curSel != null && sm.getName().equals(curSel))
440 selectedIsPresent = true;
441 curSel = sm.getName();
443 model.addElement(sm.getName());
446 * tooltip is description if provided, else text lookup with
447 * fallback on the model name
449 String tooltip = sm.getDescription();
452 tooltip = MessageManager.getStringOrReturn("label.score_model_",
455 toolTips.add(tooltip);
458 if (selectedIsPresent)
460 model.setSelectedItem(curSel);
462 // finally, update the model
463 comboBox.setModel(model);
467 * Open and calculate the selected tree or PCA on 'OK'
469 protected void calculate_actionPerformed()
471 boolean doPCA = pca.isSelected();
472 String modelName = modelNames.getSelectedItem().toString();
473 SimilarityParamsI params = getSimilarityParameters(doPCA);
477 openPcaPanel(modelName, params);
481 openTreePanel(modelName, params);
488 * Open a new Tree panel on the desktop
493 protected void openTreePanel(String modelName, SimilarityParamsI params)
496 * gui validation shouldn't allow insufficient sequences here, but leave
497 * this check in in case this method gets exposed programmatically in future
499 AlignViewport viewport = af.getViewport();
500 SequenceGroup sg = viewport.getSelectionGroup();
501 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
503 JvOptionPane.showMessageDialog(Desktop.desktop,
504 MessageManager.formatMessage(
505 "label.you_need_at_least_n_sequences",
507 MessageManager.getString("label.not_enough_sequences"),
508 JvOptionPane.WARNING_MESSAGE);
512 String treeType = neighbourJoining.isSelected()
513 ? TreeBuilder.NEIGHBOUR_JOINING
514 : TreeBuilder.AVERAGE_DISTANCE;
515 af.newTreePanel(treeType, modelName, params);
519 * Open a new PCA panel on the desktop
524 protected void openPcaPanel(String modelName, SimilarityParamsI params)
526 AlignViewport viewport = af.getViewport();
529 * gui validation shouldn't allow insufficient sequences here, but leave
530 * this check in in case this method gets exposed programmatically in future
532 if (((viewport.getSelectionGroup() != null)
533 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
534 && (viewport.getSelectionGroup().getSize() > 0))
535 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
537 JvOptionPane.showInternalMessageDialog(this,
538 MessageManager.formatMessage(
539 "label.you_need_at_least_n_sequences",
542 .getString("label.sequence_selection_insufficient"),
543 JvOptionPane.WARNING_MESSAGE);
546 pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
552 protected void closeFrame()
556 frame.setClosed(true);
557 } catch (PropertyVetoException ex)
563 * Returns a data bean holding parameters for similarity (or distance) model
569 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
571 // commented out: parameter choices read from gui widgets
572 // SimilarityParamsI params = new SimilarityParams(
573 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
574 // includeGaps.isSelected(), shorterSequence.isSelected());
576 boolean includeGapGap = true;
577 boolean includeGapResidue = true;
578 boolean matchOnShortestLength = false;
581 * 'matchGaps' flag is only used in the PID calculation
582 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
583 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
584 * - set to false for Tree for a more correct calculation (JAL-374)
586 boolean matchGap = doPCA ? false : treeMatchGaps;
588 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
589 matchOnShortestLength);
593 * Closes dialog on Close button press
595 protected void close_actionPerformed()
599 frame.setClosed(true);
600 } catch (Exception ex)
605 public PCAPanel getPcaPanel()