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>();
113 public CalculationChooser(AlignFrame alignFrame)
115 this.af = alignFrame;
117 af.alignPanel.setCalculationDialog(this);
121 * Lays out the panel and adds it to the desktop
125 setLayout(new BorderLayout());
126 frame = new JInternalFrame();
127 frame.setContentPane(this);
128 this.setBackground(Color.white);
129 frame.addFocusListener(new FocusListener()
133 public void focusLost(FocusEvent e)
138 public void focusGained(FocusEvent e)
144 * Layout consists of 3 or 4 panels:
145 * - first with choice of PCA or tree method NJ or AV
146 * - second with choice of score model
147 * - third with score model parameter options [suppressed]
148 * - fourth with OK and Cancel
150 pca = new JRadioButton(
151 MessageManager.getString("label.principal_component_analysis"));
152 pca.setOpaque(false);
153 neighbourJoining = new JRadioButton(
154 MessageManager.getString("label.tree_calc_nj"));
155 neighbourJoining.setSelected(true);
156 averageDistance = new JRadioButton(
157 MessageManager.getString("label.tree_calc_av"));
158 neighbourJoining.setOpaque(false);
160 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
161 calcChoicePanel.setOpaque(false);
163 // first create the Tree calculation's border panel
164 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
165 treePanel.setOpaque(false);
167 treePanel.setBorder(BorderFactory.createTitledBorder(MessageManager
168 .getString("label.tree")));
170 // then copy the inset dimensions for the border-less PCA panel
171 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
172 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
173 pcaBorderless.setBorder(BorderFactory.createEmptyBorder(2, b.left, 2,
175 pcaBorderless.setOpaque(false);
177 pcaBorderless.add(pca, FlowLayout.LEFT);
178 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
180 treePanel.add(neighbourJoining);
181 treePanel.add(averageDistance);
183 calcChoicePanel.add(treePanel);
185 ButtonGroup calcTypes = new ButtonGroup();
187 calcTypes.add(neighbourJoining);
188 calcTypes.add(averageDistance);
190 ActionListener calcChanged = new ActionListener()
193 public void actionPerformed(ActionEvent e)
198 pca.addActionListener(calcChanged);
199 neighbourJoining.addActionListener(calcChanged);
200 averageDistance.addActionListener(calcChanged);
203 * score models drop-down - with added tooltips!
205 modelNames = buildModelOptionsList();
207 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
208 scoreModelPanel.setOpaque(false);
209 scoreModelPanel.add(modelNames);
212 * score model parameters
214 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
215 paramsPanel.setOpaque(false);
216 includeGaps = new JCheckBox("Include gaps");
217 matchGaps = new JCheckBox("Match gaps");
218 includeGappedColumns = new JCheckBox("Include gapped columns");
219 shorterSequence = new JCheckBox("Match on shorter sequence");
220 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
221 paramsPanel.add(includeGaps);
222 paramsPanel.add(matchGaps);
223 paramsPanel.add(includeGappedColumns);
224 paramsPanel.add(shorterSequence);
227 * OK / Cancel buttons
229 calculate = new JButton(MessageManager.getString("action.calculate"));
230 calculate.setFont(VERDANA_11PT);
231 calculate.addActionListener(new java.awt.event.ActionListener()
234 public void actionPerformed(ActionEvent e)
236 calculate_actionPerformed();
239 JButton close = new JButton(MessageManager.getString("action.close"));
240 close.setFont(VERDANA_11PT);
241 close.addActionListener(new java.awt.event.ActionListener()
244 public void actionPerformed(ActionEvent e)
246 close_actionPerformed();
249 JPanel actionPanel = new JPanel();
250 actionPanel.setOpaque(false);
251 actionPanel.add(calculate);
252 actionPanel.add(close);
254 boolean includeParams = false;
255 this.add(calcChoicePanel, BorderLayout.CENTER);
256 calcChoicePanel.add(scoreModelPanel);
259 scoreModelPanel.add(paramsPanel);
261 this.add(actionPanel, BorderLayout.SOUTH);
264 int height = includeParams ? 420 : 240;
266 setMinimumSize(new Dimension(325, height - 10));
267 String title = MessageManager.getString("label.choose_calculation");
268 if (af.getViewport().viewName != null)
270 title = title + " (" + af.getViewport().viewName + ")";
273 Desktop.addInternalFrame(frame, title, width, height, false);
274 calcChoicePanel.doLayout();
277 * null the AlignmentPanel's reference to the dialog when it is closed
279 frame.addInternalFrameListener(new InternalFrameAdapter()
282 public void internalFrameClosed(InternalFrameEvent evt)
284 af.alignPanel.setCalculationDialog(null);
288 frame.setLayer(JLayeredPane.PALETTE_LAYER);
292 * enable calculations applicable for the current alignment or selection.
294 protected void validateCalcTypes()
296 int size = af.getViewport().getAlignment().getHeight();
297 if (af.getViewport().getSelectionGroup() != null)
299 size = af.getViewport().getSelectionGroup().getSize();
303 * disable calc options for which there is insufficient input data
304 * return value of true means enabled and selected
306 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
307 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
309 boolean checkAverageDistance = checkEnabled(averageDistance, size,
312 if (checkPca || checkNeighbourJoining || checkAverageDistance)
314 calculate.setToolTipText(null);
315 calculate.setEnabled(true);
319 calculate.setEnabled(false);
321 updateScoreModels(modelNames, tips);
325 * Check the input and disable a calculation's radio button if necessary. A
326 * tooltip is shown for disabled calculations.
329 * - radio button for the calculation being validated
331 * - size of input to calculation
333 * - minimum size for calculation
334 * @return true if size >= minsize and calc.isSelected
336 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
338 String ttip = MessageManager.formatMessage(
339 "label.you_need_at_least_n_sequences", minsize);
341 calc.setEnabled(size >= minsize);
342 if (!calc.isEnabled())
344 calc.setToolTipText(ttip);
348 calc.setToolTipText(null);
350 if (calc.isSelected())
352 modelNames.setEnabled(calc.isEnabled());
353 if (calc.isEnabled())
359 calculate.setToolTipText(ttip);
366 * A rather elaborate helper method (blame Swing, not me) that builds a
367 * drop-down list of score models (by name) with descriptions as tooltips.
368 * There is also a tooltip shown for the currently selected item when hovering
369 * over it (without opening the list).
371 protected JComboBox<String> buildModelOptionsList()
373 final JComboBox<String> scoreModelsCombo = new JComboBox<String>();
374 scoreModelsCombo.setRenderer(renderer);
377 * show tooltip on mouse over the combobox
378 * note the listener has to be on the components that make up
379 * the combobox, doesn't work if just on the combobox
381 final MouseAdapter mouseListener = new MouseAdapter()
384 public void mouseEntered(MouseEvent e)
386 scoreModelsCombo.setToolTipText(tips.get(scoreModelsCombo.getSelectedIndex()));
390 public void mouseExited(MouseEvent e)
392 scoreModelsCombo.setToolTipText(null);
395 for (Component c : scoreModelsCombo.getComponents())
397 c.addMouseListener(mouseListener);
400 updateScoreModels(scoreModelsCombo, tips);
403 * set the list of tooltips on the combobox's renderer
405 renderer.setTooltips(tips);
407 return scoreModelsCombo;
410 private void updateScoreModels(JComboBox<String> comboBox,
411 List<String> toolTips)
413 Object curSel = comboBox.getSelectedItem();
415 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
418 * now we can actually add entries to the combobox,
419 * remembering their descriptions for tooltips
421 ScoreModels scoreModels = ScoreModels.getInstance();
422 boolean selectedIsPresent = false;
423 for (ScoreModelI sm : scoreModels.getModels())
425 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
426 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
428 if (curSel != null && sm.getName().equals(curSel))
430 selectedIsPresent = true;
431 curSel = sm.getName();
433 model.addElement(sm.getName());
436 * tooltip is description if provided, else text lookup with
437 * fallback on the model name
439 String tooltip = sm.getDescription();
442 tooltip = MessageManager.getStringOrReturn("label.score_model_",
445 toolTips.add(tooltip);
448 if (selectedIsPresent)
450 model.setSelectedItem(curSel);
452 // finally, update the model
453 comboBox.setModel(model);
457 * Open and calculate the selected tree or PCA on 'OK'
459 protected void calculate_actionPerformed()
461 boolean doPCA = pca.isSelected();
462 String modelName = modelNames.getSelectedItem().toString();
463 SimilarityParamsI params = getSimilarityParameters(doPCA);
467 openPcaPanel(modelName, params);
471 openTreePanel(modelName, params);
478 * Open a new Tree panel on the desktop
483 protected void openTreePanel(String modelName, SimilarityParamsI params)
486 * gui validation shouldn't allow insufficient sequences here, but leave
487 * this check in in case this method gets exposed programmatically in future
489 AlignViewport viewport = af.getViewport();
490 SequenceGroup sg = viewport.getSelectionGroup();
491 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
497 .formatMessage("label.you_need_at_least_n_sequences",
500 .getString("label.not_enough_sequences"),
501 JvOptionPane.WARNING_MESSAGE);
505 String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
506 : TreeBuilder.AVERAGE_DISTANCE;
507 af.newTreePanel(treeType, modelName, params);
511 * Open a new PCA panel on the desktop
516 protected void openPcaPanel(String modelName, SimilarityParamsI params)
518 AlignViewport viewport = af.getViewport();
521 * gui validation shouldn't allow insufficient sequences here, but leave
522 * this check in in case this method gets exposed programmatically in future
524 if (((viewport.getSelectionGroup() != null)
525 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION) && (viewport
526 .getSelectionGroup().getSize() > 0))
527 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
529 JvOptionPane.showInternalMessageDialog(this, MessageManager
530 .formatMessage("label.you_need_at_least_n_sequences",
531 MIN_PCA_SELECTION), MessageManager
532 .getString("label.sequence_selection_insufficient"),
533 JvOptionPane.WARNING_MESSAGE);
536 new PCAPanel(af.alignPanel, modelName, params);
542 protected void closeFrame()
546 frame.setClosed(true);
547 } catch (PropertyVetoException ex)
553 * Returns a data bean holding parameters for similarity (or distance) model
559 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
561 // commented out: parameter choices read from gui widgets
562 // SimilarityParamsI params = new SimilarityParams(
563 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
564 // includeGaps.isSelected(), shorterSequence.isSelected());
566 boolean includeGapGap = true;
567 boolean includeGapResidue = true;
568 boolean matchOnShortestLength = false;
571 * 'matchGaps' flag is only used in the PID calculation
572 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
573 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
574 * - set to false for Tree for a more correct calculation (JAL-374)
576 boolean matchGap = doPCA ? false : treeMatchGaps;
578 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength);
582 * Closes dialog on Close button press
584 protected void close_actionPerformed()
588 frame.setClosed(true);
589 } catch (Exception ex)