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.TreeCalculator;
25 import jalview.analysis.scoremodels.ScoreModels;
26 import jalview.analysis.scoremodels.SimilarityParams;
27 import jalview.api.analysis.ScoreModelI;
28 import jalview.api.analysis.SimilarityParamsI;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.util.MessageManager;
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.Component;
35 import java.awt.Dimension;
36 import java.awt.FlowLayout;
38 import java.awt.GridLayout;
39 import java.awt.Insets;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.FocusEvent;
43 import java.awt.event.FocusListener;
44 import java.awt.event.MouseAdapter;
45 import java.awt.event.MouseEvent;
46 import java.beans.PropertyVetoException;
47 import java.util.ArrayList;
48 import java.util.List;
50 import javax.swing.BorderFactory;
51 import javax.swing.ButtonGroup;
52 import javax.swing.DefaultComboBoxModel;
53 import javax.swing.JButton;
54 import javax.swing.JCheckBox;
55 import javax.swing.JComboBox;
56 import javax.swing.JInternalFrame;
57 import javax.swing.JLabel;
58 import javax.swing.JLayeredPane;
59 import javax.swing.JPanel;
60 import javax.swing.JRadioButton;
61 import javax.swing.event.InternalFrameAdapter;
62 import javax.swing.event.InternalFrameEvent;
65 * A dialog where a user can choose and action Tree or PCA calculation options
67 public class CalculationChooser extends JPanel
70 * flag for whether gap matches residue in the PID calculation for a Tree
71 * - true gives Jalview 2.10.1 behaviour
72 * - set to false (using Groovy) for a more correct tree
75 private static boolean treeMatchGaps = true;
77 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
79 private static final int MIN_TREE_SELECTION = 3;
81 private static final int MIN_PCA_SELECTION = 4;
87 JRadioButton neighbourJoining;
89 JRadioButton averageDistance;
91 JComboBox<String> modelNames;
95 private JInternalFrame frame;
97 private JCheckBox includeGaps;
99 private JCheckBox matchGaps;
101 private JCheckBox includeGappedColumns;
103 private JCheckBox shorterSequence;
105 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
107 List<String> tips = new ArrayList<>();
114 public CalculationChooser(AlignFrame alignFrame)
116 this.af = alignFrame;
118 af.alignPanel.setCalculationDialog(this);
122 * Lays out the panel and adds it to the desktop
126 setLayout(new BorderLayout());
127 frame = new JInternalFrame();
128 frame.setContentPane(this);
129 this.setBackground(Color.white);
130 frame.addFocusListener(new FocusListener()
134 public void focusLost(FocusEvent e)
139 public void focusGained(FocusEvent e)
145 * Layout consists of 3 or 4 panels:
146 * - first with choice of PCA or tree method NJ or AV
147 * - second with choice of score model
148 * - third with score model parameter options [suppressed]
149 * - fourth with OK and Cancel
151 pca = new JRadioButton(
152 MessageManager.getString("label.principal_component_analysis"));
153 pca.setOpaque(false);
154 neighbourJoining = new JRadioButton(
155 MessageManager.getString("label.tree_calc_nj"));
156 neighbourJoining.setSelected(true);
157 averageDistance = new JRadioButton(
158 MessageManager.getString("label.tree_calc_av"));
159 neighbourJoining.setOpaque(false);
161 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
162 calcChoicePanel.setOpaque(false);
164 // first create the Tree calculation's border panel
165 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
166 treePanel.setOpaque(false);
168 treePanel.setBorder(BorderFactory
169 .createTitledBorder(MessageManager.getString("label.tree")));
171 // then copy the inset dimensions for the border-less PCA panel
172 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
173 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
174 pcaBorderless.setBorder(
175 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
176 pcaBorderless.setOpaque(false);
178 pcaBorderless.add(pca, FlowLayout.LEFT);
179 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
181 treePanel.add(neighbourJoining);
182 treePanel.add(averageDistance);
184 calcChoicePanel.add(treePanel);
186 ButtonGroup calcTypes = new ButtonGroup();
188 calcTypes.add(neighbourJoining);
189 calcTypes.add(averageDistance);
191 ActionListener calcChanged = new ActionListener()
194 public void actionPerformed(ActionEvent e)
199 pca.addActionListener(calcChanged);
200 neighbourJoining.addActionListener(calcChanged);
201 averageDistance.addActionListener(calcChanged);
204 * score models drop-down - with added tooltips!
206 modelNames = buildModelOptionsList();
208 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
209 scoreModelPanel.setOpaque(false);
210 scoreModelPanel.add(modelNames);
213 * score model parameters
215 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
216 paramsPanel.setOpaque(false);
217 includeGaps = new JCheckBox("Include gaps");
218 matchGaps = new JCheckBox("Match gaps");
219 includeGappedColumns = new JCheckBox("Include gapped columns");
220 shorterSequence = new JCheckBox("Match on shorter sequence");
221 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
222 paramsPanel.add(includeGaps);
223 paramsPanel.add(matchGaps);
224 paramsPanel.add(includeGappedColumns);
225 paramsPanel.add(shorterSequence);
228 * OK / Cancel buttons
230 calculate = new JButton(MessageManager.getString("action.calculate"));
231 calculate.setFont(VERDANA_11PT);
232 calculate.addActionListener(new java.awt.event.ActionListener()
235 public void actionPerformed(ActionEvent e)
237 calculate_actionPerformed();
240 JButton close = new JButton(MessageManager.getString("action.close"));
241 close.setFont(VERDANA_11PT);
242 close.addActionListener(new java.awt.event.ActionListener()
245 public void actionPerformed(ActionEvent e)
247 close_actionPerformed();
250 JPanel actionPanel = new JPanel();
251 actionPanel.setOpaque(false);
252 actionPanel.add(calculate);
253 actionPanel.add(close);
255 boolean includeParams = false;
256 this.add(calcChoicePanel, BorderLayout.CENTER);
257 calcChoicePanel.add(scoreModelPanel);
260 scoreModelPanel.add(paramsPanel);
262 this.add(actionPanel, BorderLayout.SOUTH);
265 int height = includeParams ? 420 : 240;
267 setMinimumSize(new Dimension(325, height - 10));
268 String title = MessageManager.getString("label.choose_calculation");
269 if (af.getViewport().viewName != null)
271 title = title + " (" + af.getViewport().viewName + ")";
274 Desktop.addInternalFrame(frame, title, width, height, false);
275 calcChoicePanel.doLayout();
278 * null the AlignmentPanel's reference to the dialog when it is closed
280 frame.addInternalFrameListener(new InternalFrameAdapter()
283 public void internalFrameClosed(InternalFrameEvent evt)
285 af.alignPanel.setCalculationDialog(null);
289 frame.setLayer(JLayeredPane.PALETTE_LAYER);
293 * enable calculations applicable for the current alignment or selection.
295 protected void validateCalcTypes()
297 int size = af.getViewport().getAlignment().getHeight();
298 if (af.getViewport().getSelectionGroup() != null)
300 size = af.getViewport().getSelectionGroup().getSize();
304 * disable calc options for which there is insufficient input data
305 * return value of true means enabled and selected
307 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
308 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
310 boolean checkAverageDistance = checkEnabled(averageDistance, size,
313 if (checkPca || checkNeighbourJoining || checkAverageDistance)
315 calculate.setToolTipText(null);
316 calculate.setEnabled(true);
320 calculate.setEnabled(false);
322 updateScoreModels(modelNames, tips);
326 * Check the input and disable a calculation's radio button if necessary. A
327 * tooltip is shown for disabled calculations.
330 * - radio button for the calculation being validated
332 * - size of input to calculation
334 * - minimum size for calculation
335 * @return true if size >= minsize and calc.isSelected
337 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
339 String ttip = MessageManager
340 .formatMessage("label.you_need_at_least_n_sequences", minsize);
342 calc.setEnabled(size >= minsize);
343 if (!calc.isEnabled())
345 calc.setToolTipText(ttip);
349 calc.setToolTipText(null);
351 if (calc.isSelected())
353 modelNames.setEnabled(calc.isEnabled());
354 if (calc.isEnabled())
360 calculate.setToolTipText(ttip);
367 * A rather elaborate helper method (blame Swing, not me) that builds a
368 * drop-down list of score models (by name) with descriptions as tooltips.
369 * There is also a tooltip shown for the currently selected item when hovering
370 * over it (without opening the list).
372 protected JComboBox<String> buildModelOptionsList()
374 final JComboBox<String> scoreModelsCombo = new JComboBox<>();
375 scoreModelsCombo.setRenderer(renderer);
378 * show tooltip on mouse over the combobox
379 * note the listener has to be on the components that make up
380 * the combobox, doesn't work if just on the combobox
382 final MouseAdapter mouseListener = new MouseAdapter()
385 public void mouseEntered(MouseEvent e)
387 scoreModelsCombo.setToolTipText(
388 tips.get(scoreModelsCombo.getSelectedIndex()));
392 public void mouseExited(MouseEvent e)
394 scoreModelsCombo.setToolTipText(null);
397 for (Component c : scoreModelsCombo.getComponents())
399 c.addMouseListener(mouseListener);
402 updateScoreModels(scoreModelsCombo, tips);
405 * set the list of tooltips on the combobox's renderer
407 renderer.setTooltips(tips);
409 return scoreModelsCombo;
412 private void updateScoreModels(JComboBox<String> comboBox,
413 List<String> toolTips)
415 Object curSel = comboBox.getSelectedItem();
417 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
420 * now we can actually add entries to the combobox,
421 * remembering their descriptions for tooltips
423 ScoreModels scoreModels = ScoreModels.getInstance();
424 boolean selectedIsPresent = false;
425 for (ScoreModelI sm : scoreModels.getModels())
427 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
428 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
430 if (curSel != null && sm.getName().equals(curSel))
432 selectedIsPresent = true;
433 curSel = sm.getName();
435 model.addElement(sm.getName());
438 * tooltip is description if provided, else text lookup with
439 * fallback on the model name
441 String tooltip = sm.getDescription();
444 tooltip = MessageManager.getStringOrReturn("label.score_model_",
447 toolTips.add(tooltip);
450 if (selectedIsPresent)
452 model.setSelectedItem(curSel);
454 // finally, update the model
455 comboBox.setModel(model);
459 * Open and calculate the selected tree or PCA on 'OK'
461 protected void calculate_actionPerformed()
463 boolean doPCA = pca.isSelected();
464 String substitutionMatrix = modelNames.getSelectedItem().toString();
465 SimilarityParamsI params = getSimilarityParameters(doPCA);
469 openPcaPanel(substitutionMatrix, params);
474 String treeAlgo = determineTreeAlgo();
475 TreeCalculator calculateTree = new TreeCalculator(treeAlgo,
479 openTreePanel(treeAlgo, substitutionMatrix, params);
485 protected String determineTreeAlgo() // to be modified & expanded
487 String treeAlgorithm = neighbourJoining.isSelected()
488 ? TreeBuilder.NEIGHBOUR_JOINING
489 : TreeBuilder.AVERAGE_DISTANCE;
491 return treeAlgorithm;
495 protected void checkEnoughSequences(AlignViewport viewport)
497 SequenceGroup sg = viewport.getSelectionGroup();
498 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
500 JvOptionPane.showMessageDialog(Desktop.desktop,
501 MessageManager.formatMessage(
502 "label.you_need_at_least_n_sequences",
504 MessageManager.getString("label.not_enough_sequences"),
505 JvOptionPane.WARNING_MESSAGE);
511 * Open a new Tree panel on the desktop
514 protected void openTreePanel(String treeType, String substitutionMatrix,
515 SimilarityParamsI params)
518 * gui validation shouldn't allow insufficient sequences here, but leave
519 * this check in in case this method gets exposed programmatically in future
521 checkEnoughSequences(af.getViewport());
525 af.newTreePanel(treeType, substitutionMatrix, params);
529 * Open a new PCA panel on the desktop
534 protected void openPcaPanel(String modelName, SimilarityParamsI params)
536 AlignViewport viewport = af.getViewport();
539 * gui validation shouldn't allow insufficient sequences here, but leave
540 * this check in in case this method gets exposed programmatically in future
542 if (((viewport.getSelectionGroup() != null)
543 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
544 && (viewport.getSelectionGroup().getSize() > 0))
545 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
547 JvOptionPane.showInternalMessageDialog(this,
548 MessageManager.formatMessage(
549 "label.you_need_at_least_n_sequences",
552 .getString("label.sequence_selection_insufficient"),
553 JvOptionPane.WARNING_MESSAGE);
556 new PCAPanel(af.alignPanel, modelName, params);
562 protected void closeFrame()
566 frame.setClosed(true);
567 } catch (PropertyVetoException ex)
573 * Returns a data bean holding parameters for similarity (or distance) model
579 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
581 // commented out: parameter choices read from gui widgets
582 // SimilarityParamsI params = new SimilarityParams(
583 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
584 // includeGaps.isSelected(), shorterSequence.isSelected());
586 boolean includeGapGap = true;
587 boolean includeGapResidue = true;
588 boolean matchOnShortestLength = false;
591 * 'matchGaps' flag is only used in the PID calculation
592 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
593 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
594 * - set to false for Tree for a more correct calculation (JAL-374)
596 boolean matchGap = doPCA ? false : treeMatchGaps;
599 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
600 matchOnShortestLength);
604 * Closes dialog on Close button press
606 protected void close_actionPerformed()
610 frame.setClosed(true);
611 } catch (Exception ex)