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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Component;
26 import java.awt.Dimension;
27 import java.awt.FlowLayout;
29 import java.awt.GridLayout;
30 import java.awt.Insets;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.event.FocusEvent;
34 import java.awt.event.FocusListener;
35 import java.awt.event.MouseAdapter;
36 import java.awt.event.MouseEvent;
37 import java.beans.PropertyVetoException;
38 import java.util.ArrayList;
39 import java.util.List;
41 import javax.swing.BorderFactory;
42 import javax.swing.ButtonGroup;
43 import javax.swing.DefaultComboBoxModel;
44 import javax.swing.JButton;
45 import javax.swing.JCheckBox;
46 import javax.swing.JComboBox;
47 import javax.swing.JInternalFrame;
48 import javax.swing.JLabel;
49 import javax.swing.JLayeredPane;
50 import javax.swing.JPanel;
51 import javax.swing.JRadioButton;
52 import javax.swing.event.InternalFrameAdapter;
53 import javax.swing.event.InternalFrameEvent;
55 import jalview.analysis.TreeBuilder;
56 import jalview.analysis.scoremodels.ScoreMatrix;
57 import jalview.analysis.scoremodels.ScoreModels;
58 import jalview.analysis.scoremodels.SimilarityParams;
59 import jalview.api.analysis.ScoreModelI;
60 import jalview.api.analysis.SimilarityParamsI;
61 import jalview.bin.Cache;
62 import jalview.datamodel.SequenceGroup;
63 import jalview.util.MessageManager;
66 * A dialog where a user can choose and action Tree or PCA calculation options
68 public class CalculationChooser extends JPanel
71 * flag for whether gap matches residue in the PID calculation for a Tree
72 * - true gives Jalview 2.10.1 behaviour
73 * - set to false (using Groovy) for a more correct tree
76 private static boolean treeMatchGaps = true;
78 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
81 private static final int MIN_PAIRWISE_SELECTION = 2;
83 private static final int MIN_TREE_SELECTION = 3;
85 private static final int MIN_PCA_SELECTION = 4;
89 JRadioButton pairwise;
93 JRadioButton neighbourJoining;
95 JRadioButton averageDistance;
97 JComboBox<String> modelNames;
101 private JInternalFrame frame;
103 private JCheckBox includeGaps;
105 private JCheckBox matchGaps;
107 private JCheckBox includeGappedColumns;
109 private JCheckBox shorterSequence;
111 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
113 List<String> tips = new ArrayList<>();
116 * the most recently opened PCA results panel
118 private PCAPanel pcaPanel;
125 public CalculationChooser(AlignFrame alignFrame)
127 this.af = alignFrame;
129 af.alignPanel.setCalculationDialog(this);
133 * Lays out the panel and adds it to the desktop
137 setLayout(new BorderLayout());
138 frame = new JInternalFrame();
139 frame.setFrameIcon(null);
140 frame.setContentPane(this);
141 this.setBackground(Color.white);
142 frame.addFocusListener(new FocusListener()
146 public void focusLost(FocusEvent e)
151 public void focusGained(FocusEvent e)
157 * Layout consists of 3 or 4 panels:
158 * - first with choice of PCA or tree method NJ or AV
159 * - second with choice of score model
160 * - third with score model parameter options [suppressed]
161 * - fourth with OK and Cancel
163 pca = new JRadioButton(
164 MessageManager.getString("label.principal_component_analysis"));
165 pca.setOpaque(false);
167 neighbourJoining = new JRadioButton(
168 MessageManager.getString("label.tree_calc_nj"));
169 neighbourJoining.setSelected(true);
170 neighbourJoining.setOpaque(false);
172 averageDistance = new JRadioButton(
173 MessageManager.getString("label.tree_calc_av"));
174 averageDistance.setOpaque(false);
176 pairwise = new JRadioButton(MessageManager.getString("label.pairwise_alignment"));
177 pairwise.setOpaque(false);
179 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
180 calcChoicePanel.setOpaque(false);
182 // first create the Tree calculation's border panel
183 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
184 treePanel.setOpaque(false);
186 JvSwingUtils.createTitledBorder(treePanel,
187 MessageManager.getString("label.tree"), true);
189 // then copy the inset dimensions for the border-less PCA panel
190 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
191 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
192 pcaBorderless.setBorder(
193 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
194 pcaBorderless.setOpaque(false);
196 pcaBorderless.add(pca, FlowLayout.LEFT);
197 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
199 treePanel.add(neighbourJoining);
200 treePanel.add(averageDistance);
202 calcChoicePanel.add(treePanel);
203 calcChoicePanel.add(pairwise,FlowLayout.CENTER);
205 ButtonGroup calcTypes = new ButtonGroup();
207 calcTypes.add(neighbourJoining);
208 calcTypes.add(averageDistance);
209 calcTypes.add(pairwise);
211 ActionListener calcChanged = new ActionListener()
214 public void actionPerformed(ActionEvent e)
219 pca.addActionListener(calcChanged);
220 neighbourJoining.addActionListener(calcChanged);
221 averageDistance.addActionListener(calcChanged);
222 pairwise.addActionListener(calcChanged);
224 * score models drop-down - with added tooltips!
226 modelNames = buildModelOptionsList();
228 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
229 scoreModelPanel.setOpaque(false);
230 scoreModelPanel.add(modelNames);
233 * score model parameters
235 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
236 paramsPanel.setOpaque(false);
237 includeGaps = new JCheckBox("Include gaps");
238 matchGaps = new JCheckBox("Match gaps");
239 includeGappedColumns = new JCheckBox("Include gapped columns");
240 shorterSequence = new JCheckBox("Match on shorter sequence");
241 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
242 paramsPanel.add(includeGaps);
243 paramsPanel.add(matchGaps);
244 paramsPanel.add(includeGappedColumns);
245 paramsPanel.add(shorterSequence);
248 * OK / Cancel buttons
250 calculate = new JButton(MessageManager.getString("action.calculate"));
251 calculate.setFont(VERDANA_11PT);
252 calculate.addActionListener(new java.awt.event.ActionListener()
255 public void actionPerformed(ActionEvent e)
257 calculate_actionPerformed();
260 JButton close = new JButton(MessageManager.getString("action.close"));
261 close.setFont(VERDANA_11PT);
262 close.addActionListener(new java.awt.event.ActionListener()
265 public void actionPerformed(ActionEvent e)
267 close_actionPerformed();
270 JPanel actionPanel = new JPanel();
271 actionPanel.setOpaque(false);
272 actionPanel.add(calculate);
273 actionPanel.add(close);
275 boolean includeParams = false;
276 this.add(calcChoicePanel, BorderLayout.CENTER);
277 calcChoicePanel.add(scoreModelPanel);
280 scoreModelPanel.add(paramsPanel);
282 this.add(actionPanel, BorderLayout.SOUTH);
285 int height = includeParams ? 420 : 240;
287 setMinimumSize(new Dimension(325, height - 10));
288 String title = MessageManager.getString("label.choose_calculation");
289 if (af.getViewport().getViewName() != null)
291 title = title + " (" + af.getViewport().getViewName() + ")";
294 Desktop.addInternalFrame(frame, title, width, height, false);
295 calcChoicePanel.doLayout();
298 * null the AlignmentPanel's reference to the dialog when it is closed
300 frame.addInternalFrameListener(new InternalFrameAdapter()
303 public void internalFrameClosed(InternalFrameEvent evt)
305 af.alignPanel.setCalculationDialog(null);
310 frame.setLayer(JLayeredPane.PALETTE_LAYER);
314 * enable calculations applicable for the current alignment or selection.
316 protected void validateCalcTypes()
318 int size = af.getViewport().getAlignment().getHeight();
319 if (af.getViewport().getSelectionGroup() != null)
321 size = af.getViewport().getSelectionGroup().getSize();
325 * disable calc options for which there is insufficient input data
326 * return value of true means enabled and selected
328 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
329 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
331 boolean checkAverageDistance = checkEnabled(averageDistance, size,
333 boolean checkPairwise = checkEnabled(pairwise, size,
334 MIN_PAIRWISE_SELECTION);
336 if (checkPca || checkNeighbourJoining || checkAverageDistance || checkPairwise)
338 calculate.setToolTipText(null);
339 calculate.setEnabled(true);
343 calculate.setEnabled(false);
345 updateScoreModels(modelNames, tips);
349 * Check the input and disable a calculation's radio button if necessary. A
350 * tooltip is shown for disabled calculations.
353 * - radio button for the calculation being validated
355 * - size of input to calculation
357 * - minimum size for calculation
358 * @return true if size >= minsize and calc.isSelected
360 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
362 String ttip = MessageManager
363 .formatMessage("label.you_need_at_least_n_sequences", minsize);
365 calc.setEnabled(size >= minsize);
366 if (!calc.isEnabled())
368 calc.setToolTipText(ttip);
372 calc.setToolTipText(null);
374 if (calc.isSelected())
376 modelNames.setEnabled(calc.isEnabled());
377 if (calc.isEnabled())
383 calculate.setToolTipText(ttip);
390 * A rather elaborate helper method (blame Swing, not me) that builds a
391 * drop-down list of score models (by name) with descriptions as tooltips.
392 * There is also a tooltip shown for the currently selected item when hovering
393 * over it (without opening the list).
395 protected JComboBox<String> buildModelOptionsList()
397 final JComboBox<String> scoreModelsCombo = new JComboBox<>();
398 scoreModelsCombo.setRenderer(renderer);
401 * show tooltip on mouse over the combobox
402 * note the listener has to be on the components that make up
403 * the combobox, doesn't work if just on the combobox
405 final MouseAdapter mouseListener = new MouseAdapter()
408 public void mouseEntered(MouseEvent e)
410 scoreModelsCombo.setToolTipText(
411 tips.get(scoreModelsCombo.getSelectedIndex()));
415 public void mouseExited(MouseEvent e)
417 scoreModelsCombo.setToolTipText(null);
420 for (Component c : scoreModelsCombo.getComponents())
422 c.addMouseListener(mouseListener);
425 updateScoreModels(scoreModelsCombo, tips);
428 * set the list of tooltips on the combobox's renderer
430 renderer.setTooltips(tips);
432 return scoreModelsCombo;
435 private void updateScoreModels(JComboBox<String> comboBox,
436 List<String> toolTips)
438 Object curSel = comboBox.getSelectedItem();
440 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
443 * select the score models applicable to the alignment type
445 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
446 List<ScoreModelI> models = getApplicableScoreModels(nucleotide,
450 * now we can actually add entries to the combobox,
451 * remembering their descriptions for tooltips
453 boolean selectedIsPresent = false;
454 for (ScoreModelI sm : models)
456 if (curSel != null && sm.getName().equals(curSel))
458 selectedIsPresent = true;
459 curSel = sm.getName();
461 model.addElement(sm.getName());
464 * tooltip is description if provided, else text lookup with
465 * fallback on the model name
467 String tooltip = sm.getDescription();
470 tooltip = MessageManager.getStringOrReturn("label.score_model_",
473 toolTips.add(tooltip);
476 if (selectedIsPresent)
478 model.setSelectedItem(curSel);
480 // finally, update the model
481 comboBox.setModel(model);
485 * Builds a list of score models which are applicable for the alignment and
486 * calculation type (peptide or generic models for protein, nucleotide or
487 * generic models for nucleotide).
489 * As a special case, includes BLOSUM62 as an extra option for nucleotide PCA.
490 * This is for backwards compatibility with Jalview prior to 2.8 when BLOSUM62
491 * was the only score matrix supported. This is included if property
492 * BLOSUM62_PCA_FOR_NUCLEOTIDE is set to true in the Jalview properties file.
498 protected static List<ScoreModelI> getApplicableScoreModels(
499 boolean nucleotide, boolean forPca)
501 List<ScoreModelI> filtered = new ArrayList<>();
503 ScoreModels scoreModels = ScoreModels.getInstance();
504 for (ScoreModelI sm : scoreModels.getModels())
506 if (!nucleotide && sm.isProtein() || nucleotide && sm.isDNA())
513 * special case: add BLOSUM62 as last option for nucleotide PCA,
514 * for backwards compatibility with Jalview < 2.8 (JAL-2962)
516 if (nucleotide && forPca
517 && Cache.getDefault("BLOSUM62_PCA_FOR_NUCLEOTIDE", false))
519 filtered.add(scoreModels.getBlosum62());
526 * Open and calculate the selected tree or PCA on 'OK'
528 protected void calculate_actionPerformed()
530 boolean doPCA = pca.isSelected();
531 String modelName = modelNames.getSelectedItem().toString();
532 SimilarityParamsI params = getSimilarityParameters(doPCA);
536 openPcaPanel(modelName, params);
538 if (pairwise.isSelected())
540 openPairwisePanel(modelName,params);
543 openTreePanel(modelName, params);
549 private void openPairwisePanel(String modelName, SimilarityParamsI params)
551 ScoreModelI sm = ScoreModels.getInstance().getScoreModel(modelName, af.alignPanel);
552 if (sm==null || !(sm instanceof ScoreMatrix))
556 JInternalFrame frame = new JInternalFrame();
557 frame.setFrameIcon(null);
558 frame.setContentPane(new PairwiseAlignPanel(af.getViewport(),(ScoreMatrix)sm));
559 Desktop.addInternalFrame(frame,
560 MessageManager.getString("action.pairwise_alignment"), 600,
565 * Open a new Tree panel on the desktop
570 protected void openTreePanel(String modelName, SimilarityParamsI params)
573 * gui validation shouldn't allow insufficient sequences here, but leave
574 * this check in in case this method gets exposed programmatically in future
576 AlignViewport viewport = af.getViewport();
577 SequenceGroup sg = viewport.getSelectionGroup();
578 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
580 JvOptionPane.showMessageDialog(Desktop.desktop,
581 MessageManager.formatMessage(
582 "label.you_need_at_least_n_sequences",
584 MessageManager.getString("label.not_enough_sequences"),
585 JvOptionPane.WARNING_MESSAGE);
589 String treeType = neighbourJoining.isSelected()
590 ? TreeBuilder.NEIGHBOUR_JOINING
591 : TreeBuilder.AVERAGE_DISTANCE;
592 af.newTreePanel(treeType, modelName, params);
596 * Open a new PCA panel on the desktop
601 protected void openPcaPanel(String modelName, SimilarityParamsI params)
603 AlignViewport viewport = af.getViewport();
606 * gui validation shouldn't allow insufficient sequences here, but leave
607 * this check in in case this method gets exposed programmatically in future
609 if (((viewport.getSelectionGroup() != null)
610 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
611 && (viewport.getSelectionGroup().getSize() > 0))
612 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
614 JvOptionPane.showInternalMessageDialog(this,
615 MessageManager.formatMessage(
616 "label.you_need_at_least_n_sequences",
619 .getString("label.sequence_selection_insufficient"),
620 JvOptionPane.WARNING_MESSAGE);
625 * construct the panel and kick off its calculation thread
627 pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
628 new Thread(pcaPanel).start();
636 protected void closeFrame()
640 frame.setClosed(true);
641 } catch (PropertyVetoException ex)
647 * Returns a data bean holding parameters for similarity (or distance) model
653 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
655 // commented out: parameter choices read from gui widgets
656 // SimilarityParamsI params = new SimilarityParams(
657 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
658 // includeGaps.isSelected(), shorterSequence.isSelected());
660 boolean includeGapGap = true;
661 boolean includeGapResidue = true;
662 boolean matchOnShortestLength = false;
665 * 'matchGaps' flag is only used in the PID calculation
666 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
667 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
668 * - set to false for Tree for a more correct calculation (JAL-374)
670 boolean matchGap = doPCA ? false : treeMatchGaps;
672 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
673 matchOnShortestLength);
677 * Closes dialog on Close button press
679 protected void close_actionPerformed()
683 frame.setClosed(true);
684 } catch (Exception ex)
689 public PCAPanel getPcaPanel()