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;
33 import java.awt.BorderLayout;
34 import java.awt.Color;
35 import java.awt.Component;
36 import java.awt.Dimension;
37 import java.awt.FlowLayout;
39 import java.awt.GridLayout;
40 import java.awt.Insets;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.FocusEvent;
44 import java.awt.event.FocusListener;
45 import java.awt.event.MouseAdapter;
46 import java.awt.event.MouseEvent;
47 import java.beans.PropertyVetoException;
48 import java.util.ArrayList;
49 import java.util.List;
51 import javax.swing.BorderFactory;
52 import javax.swing.ButtonGroup;
53 import javax.swing.DefaultComboBoxModel;
54 import javax.swing.JButton;
55 import javax.swing.JCheckBox;
56 import javax.swing.JComboBox;
57 import javax.swing.JInternalFrame;
58 import javax.swing.JLabel;
59 import javax.swing.JLayeredPane;
60 import javax.swing.JPanel;
61 import javax.swing.JRadioButton;
62 import javax.swing.event.InternalFrameAdapter;
63 import javax.swing.event.InternalFrameEvent;
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);
80 private static final int MIN_TREE_SELECTION = 3;
82 private static final int MIN_PCA_SELECTION = 4;
84 private static final int MIN_PASIMAP_SELECTION = 4; //&! <++>!! chekc how many
90 JRadioButton pasimap; //&! initialize JRadioButton object for pasimap
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 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
112 List<String> tips = new ArrayList<>();
115 * the most recently opened PCA results panel
117 private PCAPanel pcaPanel;
120 private PaSiMapPanel pasimapPanel;
127 public CalculationChooser(AlignFrame alignFrame)
129 this.af = alignFrame;
131 af.alignPanel.setCalculationDialog(this);
135 * Lays out the panel and adds it to the desktop
139 setLayout(new BorderLayout());
140 frame = new JInternalFrame();
141 frame.setContentPane(this);
142 this.setBackground(Color.white);
143 frame.addFocusListener(new FocusListener()
147 public void focusLost(FocusEvent e)
152 public void focusGained(FocusEvent e)
158 * Layout consists of 3 or 4 panels:
159 * - first with choice of PCA or tree method NJ or AV
160 * - second with choice of score model
161 * - third with score model parameter options [suppressed]
162 * - fourth with OK and Cancel
164 pca = new JRadioButton(
165 MessageManager.getString("label.principal_component_analysis"));
166 pca.setOpaque(false);
168 pasimap = new JRadioButton( // create the JRadioButton for pasimap with label.pasimap as its text
169 MessageManager.getString("label.pasimap"));
170 pasimap.setOpaque(false);
172 neighbourJoining = new JRadioButton(
173 MessageManager.getString("label.tree_calc_nj"));
174 neighbourJoining.setSelected(true);
175 neighbourJoining.setOpaque(false);
177 averageDistance = new JRadioButton(
178 MessageManager.getString("label.tree_calc_av"));
179 averageDistance.setOpaque(false);
181 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
182 calcChoicePanel.setOpaque(false);
184 // first create the Tree calculation's border panel
185 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
186 treePanel.setOpaque(false);
188 JvSwingUtils.createTitledBorder(treePanel,
189 MessageManager.getString("label.tree"), true);
191 // then copy the inset dimensions for the border-less PCA panel
192 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
193 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
194 pcaBorderless.setBorder(
195 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
196 pcaBorderless.setOpaque(false);
198 pcaBorderless.add(pca, FlowLayout.LEFT);
199 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
201 //&! create pasimap panel
202 JPanel pasimapBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT)); // create new JPanel (button) for pasimap
203 pasimapBorderless.setBorder(
204 BorderFactory.createEmptyBorder(2, b.left, 2, b.right)); // set border (margin) for button (same as treePanel and pca)
205 pasimapBorderless.setOpaque(false); // false -> stops every pixel inside border from being painted
206 pasimapBorderless.add(pasimap, FlowLayout.LEFT); // add pasimap button to the JPanel
207 calcChoicePanel.add(pasimapBorderless, FlowLayout.LEFT); // add button with border and everything to the overall ChoicePanel
209 treePanel.add(neighbourJoining);
210 treePanel.add(averageDistance);
212 calcChoicePanel.add(treePanel);
214 ButtonGroup calcTypes = new ButtonGroup();
216 calcTypes.add(pasimap); //&! add pasimap to the calculation types
217 calcTypes.add(neighbourJoining);
218 calcTypes.add(averageDistance);
220 ActionListener calcChanged = new ActionListener()
223 public void actionPerformed(ActionEvent e)
228 pca.addActionListener(calcChanged);
229 pasimap.addActionListener(calcChanged); // add the calcChanged ActionListener to pasimap --> <++> idk
230 neighbourJoining.addActionListener(calcChanged);
231 averageDistance.addActionListener(calcChanged);
234 * score models drop-down - with added tooltips!
236 modelNames = buildModelOptionsList();
238 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
239 scoreModelPanel.setOpaque(false);
240 scoreModelPanel.add(modelNames);
243 * score model parameters
245 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
246 paramsPanel.setOpaque(false);
247 includeGaps = new JCheckBox("Include gaps");
248 matchGaps = new JCheckBox("Match gaps");
249 includeGappedColumns = new JCheckBox("Include gapped columns");
250 shorterSequence = new JCheckBox("Match on shorter sequence");
251 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
252 paramsPanel.add(includeGaps);
253 paramsPanel.add(matchGaps);
254 paramsPanel.add(includeGappedColumns);
255 paramsPanel.add(shorterSequence);
258 * OK / Cancel buttons
260 calculate = new JButton(MessageManager.getString("action.calculate"));
261 calculate.setFont(VERDANA_11PT);
262 calculate.addActionListener(new java.awt.event.ActionListener()
265 public void actionPerformed(ActionEvent e)
267 calculate_actionPerformed();
270 JButton close = new JButton(MessageManager.getString("action.close"));
271 close.setFont(VERDANA_11PT);
272 close.addActionListener(new java.awt.event.ActionListener()
275 public void actionPerformed(ActionEvent e)
277 close_actionPerformed();
280 JPanel actionPanel = new JPanel();
281 actionPanel.setOpaque(false);
282 actionPanel.add(calculate);
283 actionPanel.add(close);
285 boolean includeParams = false;
286 this.add(calcChoicePanel, BorderLayout.CENTER);
287 calcChoicePanel.add(scoreModelPanel);
290 scoreModelPanel.add(paramsPanel);
292 this.add(actionPanel, BorderLayout.SOUTH);
295 int height = includeParams ? 420 : 240;
297 setMinimumSize(new Dimension(325, height - 10));
298 String title = MessageManager.getString("label.choose_calculation");
299 if (af.getViewport().getViewName() != null)
301 title = title + " (" + af.getViewport().getViewName() + ")";
304 Desktop.addInternalFrame(frame, title, width, height, false);
305 calcChoicePanel.doLayout();
308 * null the AlignmentPanel's reference to the dialog when it is closed
310 frame.addInternalFrameListener(new InternalFrameAdapter()
313 public void internalFrameClosed(InternalFrameEvent evt)
315 af.alignPanel.setCalculationDialog(null);
320 frame.setLayer(JLayeredPane.PALETTE_LAYER);
324 * enable calculations applicable for the current alignment or selection.
326 protected void validateCalcTypes()
328 int size = af.getViewport().getAlignment().getHeight();
329 if (af.getViewport().getSelectionGroup() != null)
331 size = af.getViewport().getSelectionGroup().getSize();
335 * disable calc options for which there is insufficient input data
336 * return value of true means enabled and selected
338 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
339 boolean checkPasimap = checkEnabled(pasimap, size, MIN_PASIMAP_SELECTION); // check if pasimap is enabled and min_size is fulfilled
340 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
342 boolean checkAverageDistance = checkEnabled(averageDistance, size,
345 if (checkPca || checkPasimap || checkNeighbourJoining || checkAverageDistance)
347 calculate.setToolTipText(null);
348 calculate.setEnabled(true);
352 calculate.setEnabled(false);
354 updateScoreModels(modelNames, tips);
358 * Check the input and disable a calculation's radio button if necessary. A
359 * tooltip is shown for disabled calculations.
362 * - radio button for the calculation being validated
364 * - size of input to calculation
366 * - minimum size for calculation
367 * @return true if size >= minsize and calc.isSelected
369 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
371 String ttip = MessageManager
372 .formatMessage("label.you_need_at_least_n_sequences", minsize);
374 calc.setEnabled(size >= minsize);
375 if (!calc.isEnabled())
377 calc.setToolTipText(ttip);
381 calc.setToolTipText(null);
383 if (calc.isSelected())
385 modelNames.setEnabled(calc.isEnabled());
386 if (calc.isEnabled())
392 calculate.setToolTipText(ttip);
399 * A rather elaborate helper method (blame Swing, not me) that builds a
400 * drop-down list of score models (by name) with descriptions as tooltips.
401 * There is also a tooltip shown for the currently selected item when hovering
402 * over it (without opening the list).
404 protected JComboBox<String> buildModelOptionsList()
406 final JComboBox<String> scoreModelsCombo = new JComboBox<>();
407 scoreModelsCombo.setRenderer(renderer);
410 * show tooltip on mouse over the combobox
411 * note the listener has to be on the components that make up
412 * the combobox, doesn't work if just on the combobox
414 final MouseAdapter mouseListener = new MouseAdapter()
417 public void mouseEntered(MouseEvent e)
419 scoreModelsCombo.setToolTipText(
420 tips.get(scoreModelsCombo.getSelectedIndex()));
424 public void mouseExited(MouseEvent e)
426 scoreModelsCombo.setToolTipText(null);
429 for (Component c : scoreModelsCombo.getComponents())
431 c.addMouseListener(mouseListener);
434 updateScoreModels(scoreModelsCombo, tips);
437 * set the list of tooltips on the combobox's renderer
439 renderer.setTooltips(tips);
441 return scoreModelsCombo;
444 private void updateScoreModels(JComboBox<String> comboBox,
445 List<String> toolTips)
447 Object curSel = comboBox.getSelectedItem();
449 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
452 * select the score models applicable to the alignment type
454 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
455 List<ScoreModelI> models = getApplicableScoreModels(nucleotide,
459 * now we can actually add entries to the combobox,
460 * remembering their descriptions for tooltips
462 boolean selectedIsPresent = false;
463 for (ScoreModelI sm : models)
465 if (curSel != null && sm.getName().equals(curSel))
467 selectedIsPresent = true;
468 curSel = sm.getName();
470 model.addElement(sm.getName());
473 * tooltip is description if provided, else text lookup with
474 * fallback on the model name
476 String tooltip = sm.getDescription();
479 tooltip = MessageManager.getStringOrReturn("label.score_model_",
482 toolTips.add(tooltip);
485 if (selectedIsPresent)
487 model.setSelectedItem(curSel);
489 // finally, update the model
490 comboBox.setModel(model);
494 * Builds a list of score models which are applicable for the alignment and
495 * calculation type (peptide or generic models for protein, nucleotide or
496 * generic models for nucleotide).
498 * As a special case, includes BLOSUM62 as an extra option for nucleotide PCA.
499 * This is for backwards compatibility with Jalview prior to 2.8 when BLOSUM62
500 * was the only score matrix supported. This is included if property
501 * BLOSUM62_PCA_FOR_NUCLEOTIDE is set to true in the Jalview properties file.
507 protected static List<ScoreModelI> getApplicableScoreModels(
508 boolean nucleotide, boolean forPca)
510 List<ScoreModelI> filtered = new ArrayList<>();
512 ScoreModels scoreModels = ScoreModels.getInstance();
513 for (ScoreModelI sm : scoreModels.getModels())
515 if (!nucleotide && sm.isProtein() || nucleotide && sm.isDNA())
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 boolean doPaSiMap = pasimap.isSelected();
541 String modelName = modelNames.getSelectedItem().toString();
542 SimilarityParamsI params = getSimilarityParameters(doPCA);
544 if (doPCA && !doPaSiMap)
546 openPcaPanel(modelName, params);
548 else if (doPaSiMap && !doPCA)
550 openPasimapPanel(modelName, params);
554 openTreePanel(modelName, params);
561 * Open a new Tree panel on the desktop
566 protected void openTreePanel(String modelName, SimilarityParamsI params)
569 * gui validation shouldn't allow insufficient sequences here, but leave
570 * this check in in case this method gets exposed programmatically in future
572 AlignViewport viewport = af.getViewport();
573 SequenceGroup sg = viewport.getSelectionGroup();
574 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
576 JvOptionPane.showMessageDialog(Desktop.desktop,
577 MessageManager.formatMessage(
578 "label.you_need_at_least_n_sequences",
580 MessageManager.getString("label.not_enough_sequences"),
581 JvOptionPane.WARNING_MESSAGE);
585 String treeType = neighbourJoining.isSelected()
586 ? TreeBuilder.NEIGHBOUR_JOINING
587 : TreeBuilder.AVERAGE_DISTANCE;
588 af.newTreePanel(treeType, modelName, params);
592 * Open a new PCA panel on the desktop
597 protected void openPcaPanel(String modelName, SimilarityParamsI params)
599 AlignViewport viewport = af.getViewport();
602 * gui validation shouldn't allow insufficient sequences here, but leave
603 * this check in in case this method gets exposed programmatically in future
605 if (((viewport.getSelectionGroup() != null)
606 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
607 && (viewport.getSelectionGroup().getSize() > 0))
608 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
610 JvOptionPane.showInternalMessageDialog(this,
611 MessageManager.formatMessage(
612 "label.you_need_at_least_n_sequences",
615 .getString("label.sequence_selection_insufficient"),
616 JvOptionPane.WARNING_MESSAGE);
621 * construct the panel and kick off its calculation thread
623 pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
624 new Thread(pcaPanel).start();
629 * Open a new PaSiMap panel on the desktop
634 protected void openPasimapPanel(String modelName, SimilarityParamsI params)
636 AlignViewport viewport = af.getViewport();
639 * gui validation shouldn't allow insufficient sequences here, but leave
640 * this check in in case this method gets exposed programmatically in future
642 if (((viewport.getSelectionGroup() != null)
643 && (viewport.getSelectionGroup().getSize() < MIN_PASIMAP_SELECTION)
644 && (viewport.getSelectionGroup().getSize() > 0))
645 || (viewport.getAlignment().getHeight() < MIN_PASIMAP_SELECTION))
647 JvOptionPane.showInternalMessageDialog(this,
648 MessageManager.formatMessage(
649 "label.you_need_at_least_n_sequences",
650 MIN_PASIMAP_SELECTION),
652 .getString("label.sequence_selection_insufficient"),
653 JvOptionPane.WARNING_MESSAGE);
658 * construct the panel and kick off its calculation thread
660 pasimapPanel = new PaSiMapPanel(af.alignPanel, modelName, params);
661 new Thread(pasimapPanel).start();
668 protected void closeFrame()
672 frame.setClosed(true);
673 } catch (PropertyVetoException ex)
679 * Returns a data bean holding parameters for similarity (or distance) model
685 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
687 // commented out: parameter choices read from gui widgets
688 // SimilarityParamsI params = new SimilarityParams(
689 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
690 // includeGaps.isSelected(), shorterSequence.isSelected());
692 boolean includeGapGap = true;
693 boolean includeGapResidue = true;
694 boolean matchOnShortestLength = false;
697 * 'matchGaps' flag is only used in the PID calculation
698 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
699 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
700 * - set to false for Tree for a more correct calculation (JAL-374)
702 boolean matchGap = doPCA ? false : treeMatchGaps;
704 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
705 matchOnShortestLength);
709 * Closes dialog on Close button press
711 protected void close_actionPerformed()
715 frame.setClosed(true);
716 } catch (Exception ex)
721 public PCAPanel getPcaPanel()