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.ScoreModels;
57 import jalview.analysis.scoremodels.SimilarityParams;
58 import jalview.api.analysis.ScoreModelI;
59 import jalview.api.analysis.SimilarityParamsI;
60 import jalview.bin.Cache;
61 import jalview.datamodel.AlignmentAnnotation;
62 import jalview.datamodel.SequenceGroup;
63 import jalview.datamodel.SequenceI;
64 import jalview.util.MessageManager;
67 * A dialog where a user can choose and action Tree or PCA calculation options
69 public class CalculationChooser extends JPanel
72 * flag for whether gap matches residue in the PID calculation for a Tree
73 * - true gives Jalview 2.10.1 behaviour
74 * - set to false (using Groovy) for a more correct tree
77 private static boolean treeMatchGaps = true;
79 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
81 private static final int MIN_TREE_SELECTION = 3;
83 private static final int MIN_PCA_SELECTION = 4;
89 JRadioButton neighbourJoining;
91 JRadioButton averageDistance;
93 JComboBox<String> modelNames;
97 private JInternalFrame frame;
99 private JCheckBox includeGaps;
101 private JCheckBox matchGaps;
103 private JCheckBox includeGappedColumns;
105 private JCheckBox shorterSequence;
107 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
109 List<String> tips = new ArrayList<>();
112 * the most recently opened PCA results panel
114 private PCAPanel pcaPanel;
121 public CalculationChooser(AlignFrame alignFrame)
123 this.af = alignFrame;
125 af.alignPanel.setCalculationDialog(this);
129 * Lays out the panel and adds it to the desktop
133 setLayout(new BorderLayout());
134 frame = new JInternalFrame();
135 frame.setFrameIcon(null);
136 frame.setContentPane(this);
137 this.setBackground(Color.white);
138 frame.addFocusListener(new FocusListener()
142 public void focusLost(FocusEvent e)
147 public void focusGained(FocusEvent e)
153 * Layout consists of 3 or 4 panels:
154 * - first with choice of PCA or tree method NJ or AV
155 * - second with choice of score model
156 * - third with score model parameter options [suppressed]
157 * - fourth with OK and Cancel
159 pca = new JRadioButton(
160 MessageManager.getString("label.principal_component_analysis"));
161 pca.setOpaque(false);
163 neighbourJoining = new JRadioButton(
164 MessageManager.getString("label.tree_calc_nj"));
165 neighbourJoining.setSelected(true);
166 neighbourJoining.setOpaque(false);
168 averageDistance = new JRadioButton(
169 MessageManager.getString("label.tree_calc_av"));
170 averageDistance.setOpaque(false);
172 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
173 calcChoicePanel.setOpaque(false);
175 // first create the Tree calculation's border panel
176 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
177 treePanel.setOpaque(false);
179 JvSwingUtils.createTitledBorder(treePanel,
180 MessageManager.getString("label.tree"), true);
182 // then copy the inset dimensions for the border-less PCA panel
183 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
184 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
185 pcaBorderless.setBorder(
186 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
187 pcaBorderless.setOpaque(false);
189 pcaBorderless.add(pca, FlowLayout.LEFT);
190 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
192 treePanel.add(neighbourJoining);
193 treePanel.add(averageDistance);
195 calcChoicePanel.add(treePanel);
197 ButtonGroup calcTypes = new ButtonGroup();
199 calcTypes.add(neighbourJoining);
200 calcTypes.add(averageDistance);
202 ActionListener calcChanged = new ActionListener()
205 public void actionPerformed(ActionEvent e)
210 pca.addActionListener(calcChanged);
211 neighbourJoining.addActionListener(calcChanged);
212 averageDistance.addActionListener(calcChanged);
215 * score models drop-down - with added tooltips!
217 modelNames = buildModelOptionsList();
219 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
220 scoreModelPanel.setOpaque(false);
221 scoreModelPanel.add(modelNames);
224 * score model parameters
226 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
227 paramsPanel.setOpaque(false);
228 includeGaps = new JCheckBox("Include gaps");
229 matchGaps = new JCheckBox("Match gaps");
230 includeGappedColumns = new JCheckBox("Include gapped columns");
231 shorterSequence = new JCheckBox("Match on shorter sequence");
232 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
233 paramsPanel.add(includeGaps);
234 paramsPanel.add(matchGaps);
235 paramsPanel.add(includeGappedColumns);
236 paramsPanel.add(shorterSequence);
239 * OK / Cancel buttons
241 calculate = new JButton(MessageManager.getString("action.calculate"));
242 calculate.setFont(VERDANA_11PT);
243 calculate.addActionListener(new java.awt.event.ActionListener()
246 public void actionPerformed(ActionEvent e)
248 calculate_actionPerformed();
251 JButton close = new JButton(MessageManager.getString("action.close"));
252 close.setFont(VERDANA_11PT);
253 close.addActionListener(new java.awt.event.ActionListener()
256 public void actionPerformed(ActionEvent e)
258 close_actionPerformed();
261 JPanel actionPanel = new JPanel();
262 actionPanel.setOpaque(false);
263 actionPanel.add(calculate);
264 actionPanel.add(close);
266 boolean includeParams = false;
267 this.add(calcChoicePanel, BorderLayout.CENTER);
268 calcChoicePanel.add(scoreModelPanel);
271 scoreModelPanel.add(paramsPanel);
273 this.add(actionPanel, BorderLayout.SOUTH);
276 int height = includeParams ? 420 : 240;
278 setMinimumSize(new Dimension(325, height - 10));
279 String title = MessageManager.getString("label.choose_calculation");
280 if (af.getViewport().getViewName() != null)
282 title = title + " (" + af.getViewport().getViewName() + ")";
285 Desktop.addInternalFrame(frame, title, width, height, false);
286 calcChoicePanel.doLayout();
289 * null the AlignmentPanel's reference to the dialog when it is closed
291 frame.addInternalFrameListener(new InternalFrameAdapter()
294 public void internalFrameClosed(InternalFrameEvent evt)
296 af.alignPanel.setCalculationDialog(null);
301 frame.setLayer(JLayeredPane.PALETTE_LAYER);
305 * enable calculations applicable for the current alignment or selection.
307 protected void validateCalcTypes()
309 int size = af.getViewport().getAlignment().getHeight();
310 if (af.getViewport().getSelectionGroup() != null)
312 size = af.getViewport().getSelectionGroup().getSize();
316 * disable calc options for which there is insufficient input data
317 * return value of true means enabled and selected
319 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
320 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
322 boolean checkAverageDistance = checkEnabled(averageDistance, size,
325 if (checkPca || checkNeighbourJoining || checkAverageDistance)
327 calculate.setToolTipText(null);
328 calculate.setEnabled(true);
332 calculate.setEnabled(false);
334 updateScoreModels(modelNames, tips);
338 * Check the input and disable a calculation's radio button if necessary. A
339 * tooltip is shown for disabled calculations.
342 * - radio button for the calculation being validated
344 * - size of input to calculation
346 * - minimum size for calculation
347 * @return true if size >= minsize and calc.isSelected
349 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
351 String ttip = MessageManager
352 .formatMessage("label.you_need_at_least_n_sequences", minsize);
354 calc.setEnabled(size >= minsize);
355 if (!calc.isEnabled())
357 calc.setToolTipText(ttip);
361 calc.setToolTipText(null);
363 if (calc.isSelected())
365 modelNames.setEnabled(calc.isEnabled());
366 if (calc.isEnabled())
372 calculate.setToolTipText(ttip);
379 * A rather elaborate helper method (blame Swing, not me) that builds a
380 * drop-down list of score models (by name) with descriptions as tooltips.
381 * There is also a tooltip shown for the currently selected item when hovering
382 * over it (without opening the list).
384 protected JComboBox<String> buildModelOptionsList()
386 final JComboBox<String> scoreModelsCombo = new JComboBox<>();
387 scoreModelsCombo.setRenderer(renderer);
390 * show tooltip on mouse over the combobox
391 * note the listener has to be on the components that make up
392 * the combobox, doesn't work if just on the combobox
394 final MouseAdapter mouseListener = new MouseAdapter()
397 public void mouseEntered(MouseEvent e)
399 scoreModelsCombo.setToolTipText(
400 tips.get(scoreModelsCombo.getSelectedIndex()));
404 public void mouseExited(MouseEvent e)
406 scoreModelsCombo.setToolTipText(null);
409 for (Component c : scoreModelsCombo.getComponents())
411 c.addMouseListener(mouseListener);
414 updateScoreModels(scoreModelsCombo, tips);
417 * set the list of tooltips on the combobox's renderer
419 renderer.setTooltips(tips);
421 return scoreModelsCombo;
424 private void updateScoreModels(JComboBox<String> comboBox,
425 List<String> toolTips)
427 Object curSel = comboBox.getSelectedItem();
429 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
432 * select the score models applicable to the alignment type
434 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
435 AlignmentAnnotation[] alignmentAnnotation = af.getViewport().getAlignment().getAlignmentAnnotation();
437 boolean ssPresent = false;
439 for (AlignmentAnnotation aa : alignmentAnnotation)
446 if (aa.label.equals("Secondary Structure") || aa.label.equals("jnetpred"))
454 List<ScoreModelI> models = getApplicableScoreModels(nucleotide, pca.isSelected(),
458 * now we can actually add entries to the combobox,
459 * remembering their descriptions for tooltips
461 boolean selectedIsPresent = false;
462 for (ScoreModelI sm : models)
464 if (curSel != null && sm.getName().equals(curSel))
466 selectedIsPresent = true;
467 curSel = sm.getName();
469 model.addElement(sm.getName());
472 * tooltip is description if provided, else text lookup with
473 * fallback on the model name
475 String tooltip = sm.getDescription();
478 tooltip = MessageManager.getStringOrReturn("label.score_model_",
481 toolTips.add(tooltip);
484 if (selectedIsPresent)
486 model.setSelectedItem(curSel);
488 // finally, update the model
489 comboBox.setModel(model);
493 * Builds a list of score models which are applicable for the alignment and
494 * calculation type (peptide or generic models for protein, nucleotide or
495 * generic models for nucleotide).
497 * As a special case, includes BLOSUM62 as an extra option for nucleotide PCA.
498 * This is for backwards compatibility with Jalview prior to 2.8 when BLOSUM62
499 * was the only score matrix supported. This is included if property
500 * BLOSUM62_PCA_FOR_NUCLEOTIDE is set to true in the Jalview properties file.
506 protected static List<ScoreModelI> getApplicableScoreModels(
507 boolean nucleotide, boolean forPca, boolean ssPresent)
509 List<ScoreModelI> filtered = new ArrayList<>();
511 ScoreModels scoreModels = ScoreModels.getInstance();
512 for (ScoreModelI sm : scoreModels.getModels())
514 if (!nucleotide && sm.isProtein() || nucleotide && sm.isDNA()
515 || ssPresent && sm.isSecondaryStructure())
522 * special case: add BLOSUM62 as last option for nucleotide PCA,
523 * for backwards compatibility with Jalview < 2.8 (JAL-2962)
525 if (nucleotide && forPca
526 && Cache.getDefault("BLOSUM62_PCA_FOR_NUCLEOTIDE", false))
528 filtered.add(scoreModels.getBlosum62());
535 * Open and calculate the selected tree or PCA on 'OK'
537 protected void calculate_actionPerformed()
539 boolean doPCA = pca.isSelected();
540 String modelName = modelNames.getSelectedItem().toString();
541 SimilarityParamsI params = getSimilarityParameters(doPCA);
545 openPcaPanel(modelName, params);
549 openTreePanel(modelName, params);
556 * Open a new Tree panel on the desktop
561 protected void openTreePanel(String modelName, SimilarityParamsI params)
564 * gui validation shouldn't allow insufficient sequences here, but leave
565 * this check in in case this method gets exposed programmatically in future
567 AlignViewport viewport = af.getViewport();
568 SequenceGroup sg = viewport.getSelectionGroup();
569 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
571 JvOptionPane.showMessageDialog(Desktop.desktop,
572 MessageManager.formatMessage(
573 "label.you_need_at_least_n_sequences",
575 MessageManager.getString("label.not_enough_sequences"),
576 JvOptionPane.WARNING_MESSAGE);
580 String treeType = neighbourJoining.isSelected()
581 ? TreeBuilder.NEIGHBOUR_JOINING
582 : TreeBuilder.AVERAGE_DISTANCE;
583 af.newTreePanel(treeType, modelName, params);
587 * Open a new PCA panel on the desktop
592 protected void openPcaPanel(String modelName, SimilarityParamsI params)
594 AlignViewport viewport = af.getViewport();
597 * gui validation shouldn't allow insufficient sequences here, but leave
598 * this check in in case this method gets exposed programmatically in future
600 if (((viewport.getSelectionGroup() != null)
601 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
602 && (viewport.getSelectionGroup().getSize() > 0))
603 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
605 JvOptionPane.showInternalMessageDialog(this,
606 MessageManager.formatMessage(
607 "label.you_need_at_least_n_sequences",
610 .getString("label.sequence_selection_insufficient"),
611 JvOptionPane.WARNING_MESSAGE);
616 * construct the panel and kick off its calculation thread
618 pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
619 new Thread(pcaPanel).start();
626 protected void closeFrame()
630 frame.setClosed(true);
631 } catch (PropertyVetoException ex)
637 * Returns a data bean holding parameters for similarity (or distance) model
643 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
645 // commented out: parameter choices read from gui widgets
646 // SimilarityParamsI params = new SimilarityParams(
647 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
648 // includeGaps.isSelected(), shorterSequence.isSelected());
650 boolean includeGapGap = true;
651 boolean includeGapResidue = true;
652 boolean matchOnShortestLength = false;
655 * 'matchGaps' flag is only used in the PID calculation
656 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
657 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
658 * - set to false for Tree for a more correct calculation (JAL-374)
660 boolean matchGap = doPCA ? false : treeMatchGaps;
662 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
663 matchOnShortestLength);
667 * Closes dialog on Close button press
669 protected void close_actionPerformed()
673 frame.setClosed(true);
674 } catch (Exception ex)
679 public PCAPanel getPcaPanel()