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.AlignViewportI;
27 import jalview.api.analysis.ScoreModelI;
28 import jalview.api.analysis.SimilarityParamsI;
29 import jalview.bin.Cache;
30 import jalview.datamodel.SequenceGroup;
31 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 * Allows also for dialog-free static methods openPCAPanel(...) and
68 * openTreePanel(...) for scripted use.
71 @SuppressWarnings("serial")
72 public class CalculationChooser extends JPanel
75 * flag for whether gap matches residue in the PID calculation for a Tree
76 * - true gives Jalview 2.10.1 behaviour
77 * - set to false (using Groovy) for a more correct tree
80 private static boolean treeMatchGaps = true;
82 private static Font VERDANA_11PT;
84 private static final int MIN_TREE_SELECTION = 3;
86 private static final int MIN_PCA_SELECTION = 4;
92 JRadioButton neighbourJoining;
94 JRadioButton averageDistance;
96 JComboBox<String> modelNames;
100 private JInternalFrame frame;
102 private JCheckBox includeGaps;
104 private JCheckBox matchGaps;
106 private JCheckBox includeGappedColumns;
108 private JCheckBox shorterSequence;
110 private static ComboBoxTooltipRenderer renderer; // BH was not static
112 List<String> tips = new ArrayList<>();
115 * the most recently opened PCA results panel
117 private PCAPanel pcaPanel;
120 * Open a new Tree panel on the desktop statically. Params are standard (not
121 * set by Groovy). No dialog is opened.
126 * @return null if successful; the string
127 * "label.you_need_at_least_n_sequences" if number of sequences
128 * selected is inappropriate
130 public static Object openTreePanel(AlignFrame af, String treeType,
133 return openTreePanel(af, treeType, modelName, null);
137 * public static method for JalviewJS API to open a PCAPanel without
138 * necessarily using a dialog.
142 * @return the PCAPanel, or the string "label.you_need_at_least_n_sequences"
143 * if number of sequences selected is inappropriate
145 public static Object openPcaPanel(AlignFrame af, String modelName)
147 return openPcaPanel(af, modelName, null);
155 public CalculationChooser(AlignFrame alignFrame)
157 this.af = alignFrame;
159 af.alignPanel.setCalculationDialog(this);
163 * Lays out the panel and adds it to the desktop
167 setLayout(new BorderLayout());
168 frame = new JInternalFrame();
169 frame.setContentPane(this);
170 this.setBackground(Color.white);
171 frame.addFocusListener(new FocusListener()
175 public void focusLost(FocusEvent e)
180 public void focusGained(FocusEvent e)
186 * Layout consists of 3 or 4 panels:
187 * - first with choice of PCA or tree method NJ or AV
188 * - second with choice of score model
189 * - third with score model parameter options [suppressed]
190 * - fourth with OK and Cancel
192 pca = new JRadioButton(
193 MessageManager.getString("label.principal_component_analysis"));
194 pca.setOpaque(false);
196 neighbourJoining = new JRadioButton(
197 MessageManager.getString("label.tree_calc_nj"));
198 neighbourJoining.setSelected(true);
199 neighbourJoining.setOpaque(false);
201 averageDistance = new JRadioButton(
202 MessageManager.getString("label.tree_calc_av"));
203 averageDistance.setOpaque(false);
205 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
206 calcChoicePanel.setOpaque(false);
208 // first create the Tree calculation's border panel
209 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
210 treePanel.setOpaque(false);
212 JvSwingUtils.createTitledBorder(treePanel,
213 MessageManager.getString("label.tree"), true);
215 // then copy the inset dimensions for the border-less PCA panel
216 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
217 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
218 pcaBorderless.setBorder(
219 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
220 pcaBorderless.setOpaque(false);
222 pcaBorderless.add(pca, FlowLayout.LEFT);
223 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
225 treePanel.add(neighbourJoining);
226 treePanel.add(averageDistance);
228 calcChoicePanel.add(treePanel);
230 ButtonGroup calcTypes = new ButtonGroup();
232 calcTypes.add(neighbourJoining);
233 calcTypes.add(averageDistance);
235 ActionListener calcChanged = new ActionListener()
238 public void actionPerformed(ActionEvent e)
243 pca.addActionListener(calcChanged);
244 neighbourJoining.addActionListener(calcChanged);
245 averageDistance.addActionListener(calcChanged);
248 * score models drop-down - with added tooltips!
250 modelNames = buildModelOptionsList();
252 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
253 scoreModelPanel.setOpaque(false);
254 scoreModelPanel.add(modelNames);
257 * score model parameters
259 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
260 paramsPanel.setOpaque(false);
261 includeGaps = new JCheckBox("Include gaps");
262 matchGaps = new JCheckBox("Match gaps");
263 includeGappedColumns = new JCheckBox("Include gapped columns");
264 shorterSequence = new JCheckBox("Match on shorter sequence");
265 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
266 paramsPanel.add(includeGaps);
267 paramsPanel.add(matchGaps);
268 paramsPanel.add(includeGappedColumns);
269 paramsPanel.add(shorterSequence);
271 if (VERDANA_11PT == null)
273 VERDANA_11PT = new Font("Verdana", 0, 11);
276 * OK / Cancel buttons
278 calculate = new JButton(MessageManager.getString("action.calculate"));
279 calculate.setFont(VERDANA_11PT);
280 calculate.addActionListener(new java.awt.event.ActionListener()
283 public void actionPerformed(ActionEvent e)
285 calculate_actionPerformed();
288 JButton close = new JButton(MessageManager.getString("action.close"));
289 close.setFont(VERDANA_11PT);
290 close.addActionListener(new java.awt.event.ActionListener()
293 public void actionPerformed(ActionEvent e)
295 close_actionPerformed();
298 JPanel actionPanel = new JPanel();
299 actionPanel.setOpaque(false);
300 actionPanel.add(calculate);
301 actionPanel.add(close);
303 boolean includeParams = false;
304 this.add(calcChoicePanel, BorderLayout.CENTER);
305 calcChoicePanel.add(scoreModelPanel);
308 scoreModelPanel.add(paramsPanel);
310 this.add(actionPanel, BorderLayout.SOUTH);
313 int height = includeParams ? 420 : 240;
315 setMinimumSize(new Dimension(325, height - 10));
316 String title = MessageManager.getString("label.choose_calculation");
317 if (af.getViewport().getViewName() != null)
319 title = title + " (" + af.getViewport().getViewName() + ")";
322 Desktop.addInternalFrame(frame, title, Desktop.FRAME_MAKE_VISIBLE, width, height, Desktop.FRAME_NOT_RESIZABLE, Desktop.FRAME_SET_MIN_SIZE_300);
323 calcChoicePanel.doLayout();
326 * null the AlignmentPanel's reference to the dialog when it is closed
328 frame.addInternalFrameListener(new InternalFrameAdapter()
331 public void internalFrameClosed(InternalFrameEvent evt)
333 af.alignPanel.setCalculationDialog(null);
338 frame.setLayer(JLayeredPane.PALETTE_LAYER);
342 * enable calculations applicable for the current alignment or selection.
344 protected void validateCalcTypes()
346 int size = af.getViewport().getAlignment().getHeight();
347 if (af.getViewport().getSelectionGroup() != null)
349 size = af.getViewport().getSelectionGroup().getSize();
353 * disable calc options for which there is insufficient input data
354 * return value of true means enabled and selected
356 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
357 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
359 boolean checkAverageDistance = checkEnabled(averageDistance, size,
362 if (checkPca || checkNeighbourJoining || checkAverageDistance)
364 calculate.setToolTipText(null);
365 calculate.setEnabled(true);
369 calculate.setEnabled(false);
371 updateScoreModels(modelNames, tips);
375 * Check the input and disable a calculation's radio button if necessary. A
376 * tooltip is shown for disabled calculations.
379 * - radio button for the calculation being validated
381 * - size of input to calculation
383 * - minimum size for calculation
384 * @return true if size >= minsize and calc.isSelected
386 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
388 String ttip = MessageManager
389 .formatMessage("label.you_need_at_least_n_sequences", minsize);
391 calc.setEnabled(size >= minsize);
392 if (!calc.isEnabled())
394 calc.setToolTipText(ttip);
398 calc.setToolTipText(null);
400 if (calc.isSelected())
402 modelNames.setEnabled(calc.isEnabled());
403 if (calc.isEnabled())
409 calculate.setToolTipText(ttip);
416 * A rather elaborate helper method (blame Swing, not me) that builds a
417 * drop-down list of score models (by name) with descriptions as tooltips.
418 * There is also a tooltip shown for the currently selected item when hovering
419 * over it (without opening the list).
421 protected JComboBox<String> buildModelOptionsList()
423 JComboBox<String> scoreModelsCombo = new JComboBox<>();
424 if (renderer == null)
426 renderer = new ComboBoxTooltipRenderer();
428 scoreModelsCombo.setRenderer(renderer);
431 * show tooltip on mouse over the combobox
432 * note the listener has to be on the components that make up
433 * the combobox, doesn't work if just on the combobox
435 final MouseAdapter mouseListener = new MouseAdapter()
438 public void mouseEntered(MouseEvent e)
440 scoreModelsCombo.setToolTipText(
441 tips.get(scoreModelsCombo.getSelectedIndex()));
445 public void mouseExited(MouseEvent e)
447 scoreModelsCombo.setToolTipText(null);
450 for (Component c : scoreModelsCombo.getComponents())
452 c.addMouseListener(mouseListener);
455 updateScoreModels(scoreModelsCombo, tips);
458 * set the list of tooltips on the combobox's renderer
460 renderer.setTooltips(tips);
462 return scoreModelsCombo;
465 private void updateScoreModels(JComboBox<String> comboBox,
466 List<String> toolTips)
468 Object curSel = comboBox.getSelectedItem();
470 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
473 * select the score models applicable to the alignment type
475 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
476 List<ScoreModelI> models = getApplicableScoreModels(nucleotide,
480 * now we can actually add entries to the combobox,
481 * remembering their descriptions for tooltips
483 boolean selectedIsPresent = false;
484 for (ScoreModelI sm : models)
486 if (curSel != null && sm.getName().equals(curSel))
488 selectedIsPresent = true;
489 curSel = sm.getName();
491 model.addElement(sm.getName());
494 * tooltip is description if provided, else text lookup with
495 * fallback on the model name
497 String tooltip = sm.getDescription();
500 tooltip = MessageManager.getStringOrReturn("label.score_model_",
503 toolTips.add(tooltip);
506 if (selectedIsPresent)
508 model.setSelectedItem(curSel);
510 // finally, update the model
511 comboBox.setModel(model);
515 * Builds a list of score models which are applicable for the alignment and
516 * calculation type (peptide or generic models for protein, nucleotide or
517 * generic models for nucleotide).
519 * As a special case, includes BLOSUM62 as an extra option for nucleotide PCA.
520 * This is for backwards compatibility with Jalview prior to 2.8 when BLOSUM62
521 * was the only score matrix supported. This is included if property
522 * BLOSUM62_PCA_FOR_NUCLEOTIDE is set to true in the Jalview properties file.
528 protected static List<ScoreModelI> getApplicableScoreModels(
529 boolean nucleotide, boolean forPca)
531 List<ScoreModelI> filtered = new ArrayList<>();
533 ScoreModels scoreModels = ScoreModels.getInstance();
534 for (ScoreModelI sm : scoreModels.getModels())
536 if (!nucleotide && sm.isProtein() || nucleotide && sm.isDNA())
543 * special case: add BLOSUM62 as last option for nucleotide PCA,
544 * for backwards compatibility with Jalview < 2.8 (JAL-2962)
546 if (nucleotide && forPca
547 && Cache.getDefault(Preferences.BLOSUM62_PCA_FOR_NUCLEOTIDE,
550 filtered.add(scoreModels.getBlosum62());
557 * Open and calculate the selected tree or PCA on 'OK'
559 protected void calculate_actionPerformed()
561 boolean doPCA = pca.isSelected();
562 String modelName = modelNames.getSelectedItem().toString();
563 SimilarityParamsI params = getSimilarityParameters(doPCA);
567 openPcaPanel(modelName, params);
571 openTreePanel(modelName, params);
578 * Open a new Tree panel on the desktop
583 protected void openTreePanel(String modelName, SimilarityParamsI params)
585 Object ret = openTreePanel(af,
586 neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
587 : TreeBuilder.AVERAGE_DISTANCE,
589 if (ret instanceof String)
591 JvOptionPane.showMessageDialog(this, // was opening on Desktop?
592 MessageManager.formatMessage(
595 MessageManager.getString("label.not_enough_sequences"),
596 JvOptionPane.WARNING_MESSAGE);
602 * Open a new PCA panel on the desktop
607 protected void openPcaPanel(String modelName, SimilarityParamsI params)
609 Object ret = openPcaPanel(af, modelName, params);
610 if (ret instanceof String)
612 JvOptionPane.showInternalMessageDialog(this,
613 MessageManager.formatMessage(
617 .getString("label.sequence_selection_insufficient"),
618 JvOptionPane.WARNING_MESSAGE);
622 // only used for test suite
623 pcaPanel = (PCAPanel) ret;
629 * Open a new Tree panel on the desktop statically
635 * @return null, or the string "label.you_need_at_least_n_sequences" if number
636 * of sequences selected is inappropriate
638 public static Object openTreePanel(AlignFrame af, String treeType,
639 String modelName, SimilarityParamsI params)
643 * gui validation shouldn't allow insufficient sequences here, but leave
644 * this check in in case this method gets exposed programmatically in future
646 AlignViewportI viewport = af.getViewport();
647 SequenceGroup sg = viewport.getSelectionGroup();
648 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
650 return "label.you_need_at_least_n_sequences";
655 params = getSimilarityParameters(false);
658 af.newTreePanel(treeType, modelName, params);
663 * public static method for JalviewJS API
668 * @return the PCAPanel, or null if number of sequences selected is
671 public static Object openPcaPanel(AlignFrame af, String modelName,
672 SimilarityParamsI params)
674 AlignViewportI viewport = af.getViewport();
677 * gui validation shouldn't allow insufficient sequences here, but leave
678 * this check in in case this method gets exposed programmatically in future
682 if (((viewport.getSelectionGroup() != null)
683 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
684 && (viewport.getSelectionGroup().getSize() > 0))
685 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
687 return "label.you_need_at_least_n_sequences";
692 params = getSimilarityParameters(true);
696 * construct the panel and kick off its calculation thread
698 PCAPanel pcap = new PCAPanel(af.alignPanel, modelName, params);
699 new Thread(pcap).start();
706 protected void closeFrame()
710 frame.setClosed(true);
711 } catch (PropertyVetoException ex)
718 * Returns a data bean holding parameters for similarity (or distance) model
724 public static SimilarityParamsI getSimilarityParameters(
727 // commented out: parameter choices read from gui widgets
728 // SimilarityParamsI params = new SimilarityParams(
729 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
730 // includeGaps.isSelected(), shorterSequence.isSelected());
732 boolean includeGapGap = true;
733 boolean includeGapResidue = true;
734 boolean matchOnShortestLength = false;
737 * 'matchGaps' flag is only used in the PID calculation
738 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
739 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
740 * - set to false for Tree for a more correct calculation (JAL-374)
742 boolean matchGap = doPCA ? false : treeMatchGaps;
744 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
745 matchOnShortestLength);
750 * Closes dialog on Close button press
752 protected void close_actionPerformed()
756 frame.setClosed(true);
757 } catch (Exception ex)
762 public PCAPanel getPcaPanel()