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.bin.Cache;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.util.MessageManager;
31 import jalview.viewmodel.AlignmentViewport;
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;
64 import jalview.analysis.TreeBuilder;
65 import jalview.analysis.scoremodels.ScoreModels;
66 import jalview.analysis.scoremodels.SimilarityParams;
67 import jalview.api.analysis.ScoreModelI;
68 import jalview.api.analysis.SimilarityParamsI;
69 import jalview.bin.Cache;
70 import jalview.datamodel.SequenceGroup;
71 import jalview.util.MessageManager;
74 * A dialog where a user can choose and action Tree or PCA calculation options
76 public class CalculationChooser extends JPanel
79 * flag for whether gap matches residue in the PID calculation for a Tree
80 * - true gives Jalview 2.10.1 behaviour
81 * - set to false (using Groovy) for a more correct tree
84 private static boolean treeMatchGaps = true;
86 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
88 private static final int MIN_TREE_SELECTION = 3;
90 private static final int MIN_PCA_SELECTION = 4;
92 private static final int MIN_PASIMAP_SELECTION = 8;
100 JRadioButton neighbourJoining;
102 JRadioButton averageDistance;
104 JComboBox<String> modelNames;
108 private JInternalFrame frame;
110 private JCheckBox includeGaps;
112 private JCheckBox matchGaps;
114 private JCheckBox includeGappedColumns;
116 private JCheckBox shorterSequence;
118 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
120 List<String> tips = new ArrayList<>();
123 * the most recently opened PCA results panel
125 private PCAPanel pcaPanel;
127 private PaSiMapPanel pasimapPanel;
134 public CalculationChooser(AlignFrame alignFrame)
136 this.af = alignFrame;
138 af.alignPanel.setCalculationDialog(this);
142 * Lays out the panel and adds it to the desktop
146 setLayout(new BorderLayout());
147 frame = new JInternalFrame();
148 frame.setFrameIcon(null);
149 frame.setContentPane(this);
150 this.setBackground(Color.white);
151 frame.addFocusListener(new FocusListener()
155 public void focusLost(FocusEvent e)
160 public void focusGained(FocusEvent e)
166 * Layout consists of 3 or 4 panels:
167 * - first with choice of PCA or tree method NJ or AV
168 * - second with choice of score model
169 * - third with score model parameter options [suppressed]
170 * - fourth with OK and Cancel
172 pca = new JRadioButton(
173 MessageManager.getString("label.principal_component_analysis"));
174 pca.setOpaque(false);
176 pasimap = new JRadioButton( // create the JRadioButton for pasimap with label.pasimap as its text
177 MessageManager.getString("label.pasimap"));
178 pasimap.setOpaque(false);
180 neighbourJoining = new JRadioButton(
181 MessageManager.getString("label.tree_calc_nj"));
182 neighbourJoining.setSelected(true);
183 neighbourJoining.setOpaque(false);
185 averageDistance = new JRadioButton(
186 MessageManager.getString("label.tree_calc_av"));
187 averageDistance.setOpaque(false);
189 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
190 calcChoicePanel.setOpaque(false);
192 // first create the Tree calculation's border panel
193 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
194 treePanel.setOpaque(false);
196 JvSwingUtils.createTitledBorder(treePanel,
197 MessageManager.getString("label.tree"), true);
199 // then copy the inset dimensions for the border-less PCA panel
200 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
201 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
202 pcaBorderless.setBorder(
203 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
204 pcaBorderless.setOpaque(false);
206 pcaBorderless.add(pca, FlowLayout.LEFT);
207 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
209 // create pasimap panel
210 JPanel pasimapBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT)); // create new JPanel (button) for pasimap
211 pasimapBorderless.setBorder(
212 BorderFactory.createEmptyBorder(2, b.left, 2, b.right)); // set border (margin) for button (same as treePanel and pca)
213 pasimapBorderless.setOpaque(false); // false -> stops every pixel inside border from being painted
214 pasimapBorderless.add(pasimap, FlowLayout.LEFT); // add pasimap button to the JPanel
215 calcChoicePanel.add(pasimapBorderless, FlowLayout.LEFT); // add button with border and everything to the overall ChoicePanel
217 treePanel.add(neighbourJoining);
218 treePanel.add(averageDistance);
220 calcChoicePanel.add(treePanel);
222 ButtonGroup calcTypes = new ButtonGroup();
224 calcTypes.add(pasimap);
225 calcTypes.add(neighbourJoining);
226 calcTypes.add(averageDistance);
228 ActionListener calcChanged = new ActionListener()
231 public void actionPerformed(ActionEvent e)
236 pca.addActionListener(calcChanged);
237 pasimap.addActionListener(calcChanged); // add the calcChanged ActionListener to pasimap --> <++> idk
238 neighbourJoining.addActionListener(calcChanged);
239 averageDistance.addActionListener(calcChanged);
242 * score models drop-down - with added tooltips!
244 modelNames = buildModelOptionsList();
246 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
247 scoreModelPanel.setOpaque(false);
248 scoreModelPanel.add(modelNames);
251 * score model parameters
253 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
254 paramsPanel.setOpaque(false);
255 includeGaps = new JCheckBox("Include gaps");
256 matchGaps = new JCheckBox("Match gaps");
257 includeGappedColumns = new JCheckBox("Include gapped columns");
258 shorterSequence = new JCheckBox("Match on shorter sequence");
259 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
260 paramsPanel.add(includeGaps);
261 paramsPanel.add(matchGaps);
262 paramsPanel.add(includeGappedColumns);
263 paramsPanel.add(shorterSequence);
266 * OK / Cancel buttons
268 calculate = new JButton(MessageManager.getString("action.calculate"));
269 calculate.setFont(VERDANA_11PT);
270 calculate.addActionListener(new java.awt.event.ActionListener()
273 public void actionPerformed(ActionEvent e)
275 calculate_actionPerformed();
278 JButton close = new JButton(MessageManager.getString("action.close"));
279 close.setFont(VERDANA_11PT);
280 close.addActionListener(new java.awt.event.ActionListener()
283 public void actionPerformed(ActionEvent e)
285 close_actionPerformed();
288 JPanel actionPanel = new JPanel();
289 actionPanel.setOpaque(false);
290 actionPanel.add(calculate);
291 actionPanel.add(close);
293 boolean includeParams = false;
294 this.add(calcChoicePanel, BorderLayout.CENTER);
295 calcChoicePanel.add(scoreModelPanel);
298 scoreModelPanel.add(paramsPanel);
300 this.add(actionPanel, BorderLayout.SOUTH);
303 int height = includeParams ? 420 : 240;
305 setMinimumSize(new Dimension(325, height - 10));
306 String title = MessageManager.getString("label.choose_calculation");
307 if (af.getViewport().getViewName() != null)
309 title = title + " (" + af.getViewport().getViewName() + ")";
312 Desktop.addInternalFrame(frame, title, width, height, false);
313 calcChoicePanel.doLayout();
316 * null the AlignmentPanel's reference to the dialog when it is closed
318 frame.addInternalFrameListener(new InternalFrameAdapter()
321 public void internalFrameClosed(InternalFrameEvent evt)
323 af.alignPanel.setCalculationDialog(null);
328 frame.setLayer(JLayeredPane.PALETTE_LAYER);
332 * enable calculations applicable for the current alignment or selection.
334 protected void validateCalcTypes()
336 int size = af.getViewport().getAlignment().getHeight();
337 if (af.getViewport().getSelectionGroup() != null)
339 size = af.getViewport().getSelectionGroup().getSize();
343 * disable calc options for which there is insufficient input data
344 * return value of true means enabled and selected
346 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
347 boolean checkPasimap = checkEnabled(pasimap, size, MIN_PASIMAP_SELECTION); // check if pasimap is enabled and min_size is fulfilled
348 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
350 boolean checkAverageDistance = checkEnabled(averageDistance, size,
353 if (checkPca || checkPasimap || checkNeighbourJoining || checkAverageDistance)
355 calculate.setToolTipText(null);
356 calculate.setEnabled(true);
360 calculate.setEnabled(false);
362 updateScoreModels(modelNames, tips);
366 * Check the input and disable a calculation's radio button if necessary. A
367 * tooltip is shown for disabled calculations.
370 * - radio button for the calculation being validated
372 * - size of input to calculation
374 * - minimum size for calculation
375 * @return true if size >= minsize and calc.isSelected
377 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
379 String ttip = MessageManager
380 .formatMessage("label.you_need_at_least_n_sequences", minsize);
382 calc.setEnabled(size >= minsize);
383 if (!calc.isEnabled())
385 calc.setToolTipText(ttip);
389 calc.setToolTipText(null);
391 if (calc.isSelected())
393 modelNames.setEnabled(calc.isEnabled());
394 if (calc.isEnabled())
400 calculate.setToolTipText(ttip);
407 * A rather elaborate helper method (blame Swing, not me) that builds a
408 * drop-down list of score models (by name) with descriptions as tooltips.
409 * There is also a tooltip shown for the currently selected item when hovering
410 * over it (without opening the list).
412 protected JComboBox<String> buildModelOptionsList()
414 final JComboBox<String> scoreModelsCombo = new JComboBox<>();
415 scoreModelsCombo.setRenderer(renderer);
418 * show tooltip on mouse over the combobox
419 * note the listener has to be on the components that make up
420 * the combobox, doesn't work if just on the combobox
422 final MouseAdapter mouseListener = new MouseAdapter()
425 public void mouseEntered(MouseEvent e)
427 scoreModelsCombo.setToolTipText(
428 tips.get(scoreModelsCombo.getSelectedIndex()));
432 public void mouseExited(MouseEvent e)
434 scoreModelsCombo.setToolTipText(null);
437 for (Component c : scoreModelsCombo.getComponents())
439 c.addMouseListener(mouseListener);
442 updateScoreModels(scoreModelsCombo, tips);
445 * set the list of tooltips on the combobox's renderer
447 renderer.setTooltips(tips);
449 return scoreModelsCombo;
452 private void updateScoreModels(JComboBox<String> comboBox,
453 List<String> toolTips)
455 Object curSel = comboBox.getSelectedItem();
457 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
460 * select the score models applicable to the alignment type
462 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
463 List<ScoreModelI> models = getApplicableScoreModels(nucleotide,
467 * now we can actually add entries to the combobox,
468 * remembering their descriptions for tooltips
470 boolean selectedIsPresent = false;
471 for (ScoreModelI sm : models)
473 if (curSel != null && sm.getName().equals(curSel))
475 selectedIsPresent = true;
476 curSel = sm.getName();
478 model.addElement(sm.getName());
481 * tooltip is description if provided, else text lookup with
482 * fallback on the model name
484 String tooltip = sm.getDescription();
487 tooltip = MessageManager.getStringOrReturn("label.score_model_",
490 toolTips.add(tooltip);
493 if (selectedIsPresent)
495 model.setSelectedItem(curSel);
497 // finally, update the model
498 comboBox.setModel(model);
502 * Builds a list of score models which are applicable for the alignment and
503 * calculation type (peptide or generic models for protein, nucleotide or
504 * generic models for nucleotide).
506 * As a special case, includes BLOSUM62 as an extra option for nucleotide PCA.
507 * This is for backwards compatibility with Jalview prior to 2.8 when BLOSUM62
508 * was the only score matrix supported. This is included if property
509 * BLOSUM62_PCA_FOR_NUCLEOTIDE is set to true in the Jalview properties file.
515 protected static List<ScoreModelI> getApplicableScoreModels(
516 boolean nucleotide, boolean forPca)
518 List<ScoreModelI> filtered = new ArrayList<>();
520 ScoreModels scoreModels = ScoreModels.getInstance();
521 for (ScoreModelI sm : scoreModels.getModels())
523 if (!nucleotide && sm.isProtein() || nucleotide && sm.isDNA())
530 * special case: add BLOSUM62 as last option for nucleotide PCA,
531 * for backwards compatibility with Jalview < 2.8 (JAL-2962)
533 if (nucleotide && forPca
534 && Cache.getDefault("BLOSUM62_PCA_FOR_NUCLEOTIDE", false))
536 filtered.add(scoreModels.getBlosum62());
543 * Open and calculate the selected tree or PCA on 'OK'
545 protected void calculate_actionPerformed()
547 boolean doPCA = pca.isSelected();
548 boolean doPaSiMap = pasimap.isSelected();
549 String modelName = modelNames.getSelectedItem().toString();
550 SimilarityParamsI params = getSimilarityParameters(doPCA);
552 if (doPCA && !doPaSiMap)
554 openPcaPanel(modelName, params);
556 else if (doPaSiMap && !doPCA)
558 openPasimapPanel(modelName, params);
562 openTreePanel(modelName, params);
569 * Open a new Tree panel on the desktop
574 protected void openTreePanel(String modelName, SimilarityParamsI params)
577 * gui validation shouldn't allow insufficient sequences here, but leave
578 * this check in in case this method gets exposed programmatically in future
580 AlignViewport viewport = af.getViewport();
581 SequenceGroup sg = viewport.getSelectionGroup();
582 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
584 JvOptionPane.showMessageDialog(Desktop.desktop,
585 MessageManager.formatMessage(
586 "label.you_need_at_least_n_sequences",
588 MessageManager.getString("label.not_enough_sequences"),
589 JvOptionPane.WARNING_MESSAGE);
593 String treeType = neighbourJoining.isSelected()
594 ? TreeBuilder.NEIGHBOUR_JOINING
595 : TreeBuilder.AVERAGE_DISTANCE;
596 af.newTreePanel(treeType, modelName, params);
600 * Open a new PCA panel on the desktop
605 protected void openPcaPanel(String modelName, SimilarityParamsI params)
607 AlignViewport viewport = af.getViewport();
610 * gui validation shouldn't allow insufficient sequences here, but leave
611 * this check in in case this method gets exposed programmatically in future
613 if (((viewport.getSelectionGroup() != null)
614 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
615 && (viewport.getSelectionGroup().getSize() > 0))
616 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
618 JvOptionPane.showInternalMessageDialog(this,
619 MessageManager.formatMessage(
620 "label.you_need_at_least_n_sequences",
623 .getString("label.sequence_selection_insufficient"),
624 JvOptionPane.WARNING_MESSAGE);
629 * construct the panel and kick off its calculation thread
631 pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
632 new Thread(pcaPanel).start();
637 * Open a new PaSiMap panel on the desktop
642 protected void openPasimapPanel(String modelName, SimilarityParamsI params)
644 AlignViewport viewport = af.getViewport();
647 * gui validation shouldn't allow insufficient sequences here, but leave
648 * this check in in case this method gets exposed programmatically in future
650 if (((viewport.getSelectionGroup() != null)
651 && (viewport.getSelectionGroup().getSize() < MIN_PASIMAP_SELECTION)
652 && (viewport.getSelectionGroup().getSize() > 0))
653 || (viewport.getAlignment().getHeight() < MIN_PASIMAP_SELECTION))
655 JvOptionPane.showInternalMessageDialog(this,
656 MessageManager.formatMessage(
657 "label.you_need_at_least_n_sequences",
658 MIN_PASIMAP_SELECTION),
660 .getString("label.sequence_selection_insufficient"),
661 JvOptionPane.WARNING_MESSAGE);
666 * construct the panel and kick off its calculation thread
668 pasimapPanel = new PaSiMapPanel(af.alignPanel, modelName, params);
669 new Thread(pasimapPanel).start();
676 protected void closeFrame()
680 frame.setClosed(true);
681 } catch (PropertyVetoException ex)
687 * Returns a data bean holding parameters for similarity (or distance) model
693 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
695 // commented out: parameter choices read from gui widgets
696 // SimilarityParamsI params = new SimilarityParams(
697 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
698 // includeGaps.isSelected(), shorterSequence.isSelected());
700 boolean includeGapGap = true;
701 boolean includeGapResidue = true;
702 boolean matchOnShortestLength = false;
705 * 'matchGaps' flag is only used in the PID calculation
706 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
707 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
708 * - set to false for Tree for a more correct calculation (JAL-374)
710 boolean matchGap = doPCA ? false : treeMatchGaps;
712 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
713 matchOnShortestLength);
717 * Closes dialog on Close button press
719 protected void close_actionPerformed()
723 frame.setClosed(true);
724 } catch (Exception ex)
729 public PCAPanel getPcaPanel()