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.TreeCalculator;
25 import jalview.analysis.TreeModel;
26 import jalview.analysis.scoremodels.ScoreModels;
27 import jalview.analysis.scoremodels.SimilarityParams;
28 import jalview.api.analysis.ScoreModelI;
29 import jalview.api.analysis.SimilarityParamsI;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.ext.archaeopteryx.ArchaeopteryxTreeBuilder;
32 import jalview.util.MessageManager;
34 import java.awt.BorderLayout;
35 import java.awt.Color;
36 import java.awt.Component;
37 import java.awt.Dimension;
38 import java.awt.FlowLayout;
40 import java.awt.GridLayout;
41 import java.awt.Insets;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.awt.event.FocusEvent;
45 import java.awt.event.FocusListener;
46 import java.awt.event.MouseAdapter;
47 import java.awt.event.MouseEvent;
48 import java.beans.PropertyVetoException;
49 import java.util.ArrayList;
50 import java.util.List;
52 import javax.swing.BorderFactory;
53 import javax.swing.ButtonGroup;
54 import javax.swing.DefaultComboBoxModel;
55 import javax.swing.JButton;
56 import javax.swing.JCheckBox;
57 import javax.swing.JComboBox;
58 import javax.swing.JInternalFrame;
59 import javax.swing.JLabel;
60 import javax.swing.JLayeredPane;
61 import javax.swing.JPanel;
62 import javax.swing.JRadioButton;
63 import javax.swing.event.InternalFrameAdapter;
64 import javax.swing.event.InternalFrameEvent;
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<>();
116 public CalculationChooser(AlignFrame alignFrame)
118 this.af = alignFrame;
120 af.alignPanel.setCalculationDialog(this);
124 * Lays out the panel and adds it to the desktop
128 setLayout(new BorderLayout());
129 frame = new JInternalFrame();
130 frame.setContentPane(this);
131 this.setBackground(Color.white);
132 frame.addFocusListener(new FocusListener()
136 public void focusLost(FocusEvent e)
141 public void focusGained(FocusEvent e)
147 * Layout consists of 3 or 4 panels:
148 * - first with choice of PCA or tree method NJ or AV
149 * - second with choice of score model
150 * - third with score model parameter options [suppressed]
151 * - fourth with OK and Cancel
153 pca = new JRadioButton(
154 MessageManager.getString("label.principal_component_analysis"));
155 pca.setOpaque(false);
156 neighbourJoining = new JRadioButton(
157 MessageManager.getString("label.tree_calc_nj"));
158 neighbourJoining.setSelected(true);
159 averageDistance = new JRadioButton(
160 MessageManager.getString("label.tree_calc_av"));
161 neighbourJoining.setOpaque(false);
163 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
164 calcChoicePanel.setOpaque(false);
166 // first create the Tree calculation's border panel
167 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
168 treePanel.setOpaque(false);
170 treePanel.setBorder(BorderFactory
171 .createTitledBorder(MessageManager.getString("label.tree")));
173 // then copy the inset dimensions for the border-less PCA panel
174 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
175 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
176 pcaBorderless.setBorder(
177 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
178 pcaBorderless.setOpaque(false);
180 pcaBorderless.add(pca, FlowLayout.LEFT);
181 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
183 treePanel.add(neighbourJoining);
184 treePanel.add(averageDistance);
186 calcChoicePanel.add(treePanel);
188 ButtonGroup calcTypes = new ButtonGroup();
190 calcTypes.add(neighbourJoining);
191 calcTypes.add(averageDistance);
193 ActionListener calcChanged = new ActionListener()
196 public void actionPerformed(ActionEvent e)
201 pca.addActionListener(calcChanged);
202 neighbourJoining.addActionListener(calcChanged);
203 averageDistance.addActionListener(calcChanged);
206 * score models drop-down - with added tooltips!
208 modelNames = buildModelOptionsList();
210 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
211 scoreModelPanel.setOpaque(false);
212 scoreModelPanel.add(modelNames);
215 * score model parameters
217 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
218 paramsPanel.setOpaque(false);
219 includeGaps = new JCheckBox("Include gaps");
220 matchGaps = new JCheckBox("Match gaps");
221 includeGappedColumns = new JCheckBox("Include gapped columns");
222 shorterSequence = new JCheckBox("Match on shorter sequence");
223 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
224 paramsPanel.add(includeGaps);
225 paramsPanel.add(matchGaps);
226 paramsPanel.add(includeGappedColumns);
227 paramsPanel.add(shorterSequence);
230 * OK / Cancel buttons
232 calculate = new JButton(MessageManager.getString("action.calculate"));
233 calculate.setFont(VERDANA_11PT);
234 calculate.addActionListener(new java.awt.event.ActionListener()
237 public void actionPerformed(ActionEvent e)
239 calculate_actionPerformed();
242 JButton close = new JButton(MessageManager.getString("action.close"));
243 close.setFont(VERDANA_11PT);
244 close.addActionListener(new java.awt.event.ActionListener()
247 public void actionPerformed(ActionEvent e)
249 close_actionPerformed();
252 JPanel actionPanel = new JPanel();
253 actionPanel.setOpaque(false);
254 actionPanel.add(calculate);
255 actionPanel.add(close);
257 boolean includeParams = false;
258 this.add(calcChoicePanel, BorderLayout.CENTER);
259 calcChoicePanel.add(scoreModelPanel);
262 scoreModelPanel.add(paramsPanel);
264 this.add(actionPanel, BorderLayout.SOUTH);
267 int height = includeParams ? 420 : 240;
269 setMinimumSize(new Dimension(325, height - 10));
270 String title = MessageManager.getString("label.choose_calculation");
271 if (af.getViewport().viewName != null)
273 title = title + " (" + af.getViewport().viewName + ")";
276 Desktop.addInternalFrame(frame, title, width, height, false);
277 calcChoicePanel.doLayout();
280 * null the AlignmentPanel's reference to the dialog when it is closed
282 frame.addInternalFrameListener(new InternalFrameAdapter()
285 public void internalFrameClosed(InternalFrameEvent evt)
287 af.alignPanel.setCalculationDialog(null);
291 frame.setLayer(JLayeredPane.PALETTE_LAYER);
295 * enable calculations applicable for the current alignment or selection.
297 protected void validateCalcTypes()
299 int size = af.getViewport().getAlignment().getHeight();
300 if (af.getViewport().getSelectionGroup() != null)
302 size = af.getViewport().getSelectionGroup().getSize();
306 * disable calc options for which there is insufficient input data
307 * return value of true means enabled and selected
309 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
310 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
312 boolean checkAverageDistance = checkEnabled(averageDistance, size,
315 if (checkPca || checkNeighbourJoining || checkAverageDistance)
317 calculate.setToolTipText(null);
318 calculate.setEnabled(true);
322 calculate.setEnabled(false);
324 updateScoreModels(modelNames, tips);
328 * Check the input and disable a calculation's radio button if necessary. A
329 * tooltip is shown for disabled calculations.
332 * - radio button for the calculation being validated
334 * - size of input to calculation
336 * - minimum size for calculation
337 * @return true if size >= minsize and calc.isSelected
339 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
341 String ttip = MessageManager
342 .formatMessage("label.you_need_at_least_n_sequences", minsize);
344 calc.setEnabled(size >= minsize);
345 if (!calc.isEnabled())
347 calc.setToolTipText(ttip);
351 calc.setToolTipText(null);
353 if (calc.isSelected())
355 modelNames.setEnabled(calc.isEnabled());
356 if (calc.isEnabled())
362 calculate.setToolTipText(ttip);
369 * A rather elaborate helper method (blame Swing, not me) that builds a
370 * drop-down list of score models (by name) with descriptions as tooltips.
371 * There is also a tooltip shown for the currently selected item when hovering
372 * over it (without opening the list).
374 protected JComboBox<String> buildModelOptionsList()
376 final JComboBox<String> scoreModelsCombo = new JComboBox<>();
377 scoreModelsCombo.setRenderer(renderer);
380 * show tooltip on mouse over the combobox
381 * note the listener has to be on the components that make up
382 * the combobox, doesn't work if just on the combobox
384 final MouseAdapter mouseListener = new MouseAdapter()
387 public void mouseEntered(MouseEvent e)
389 scoreModelsCombo.setToolTipText(
390 tips.get(scoreModelsCombo.getSelectedIndex()));
394 public void mouseExited(MouseEvent e)
396 scoreModelsCombo.setToolTipText(null);
399 for (Component c : scoreModelsCombo.getComponents())
401 c.addMouseListener(mouseListener);
404 updateScoreModels(scoreModelsCombo, tips);
407 * set the list of tooltips on the combobox's renderer
409 renderer.setTooltips(tips);
411 return scoreModelsCombo;
414 private void updateScoreModels(JComboBox<String> comboBox,
415 List<String> toolTips)
417 Object curSel = comboBox.getSelectedItem();
419 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
422 * now we can actually add entries to the combobox,
423 * remembering their descriptions for tooltips
425 ScoreModels scoreModels = ScoreModels.getInstance();
426 boolean selectedIsPresent = false;
427 for (ScoreModelI sm : scoreModels.getModels())
429 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
430 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
432 if (curSel != null && sm.getName().equals(curSel))
434 selectedIsPresent = true;
435 curSel = sm.getName();
437 model.addElement(sm.getName());
440 * tooltip is description if provided, else text lookup with
441 * fallback on the model name
443 String tooltip = sm.getDescription();
446 tooltip = MessageManager.getStringOrReturn("label.score_model_",
449 toolTips.add(tooltip);
452 if (selectedIsPresent)
454 model.setSelectedItem(curSel);
456 // finally, update the model
457 comboBox.setModel(model);
461 * Open and calculate the selected tree or PCA on 'OK'
463 protected void calculate_actionPerformed()
465 boolean doPCA = pca.isSelected();
466 String substitutionMatrix = modelNames.getSelectedItem().toString();
467 SimilarityParamsI params = getSimilarityParameters(doPCA);
471 openPcaPanel(substitutionMatrix, params);
476 String treeAlgo = determineTreeAlgo();
477 TreeCalculator treeCalculator = new TreeCalculator(treeAlgo,
478 substitutionMatrix, params);
479 TreeBuilder calculatedTree = treeCalculator
480 .makeTree(af.getViewport());
481 TreeModel tree = new TreeModel(calculatedTree);
482 openTreePanel(tree, treeAlgo, substitutionMatrix);
484 ArchaeopteryxTreeBuilder aptxTree = new ArchaeopteryxTreeBuilder();
492 protected String determineTreeAlgo() // to be modified & expanded
494 String treeAlgorithm = neighbourJoining.isSelected()
495 ? TreeBuilder.NEIGHBOUR_JOINING
496 : TreeBuilder.AVERAGE_DISTANCE;
498 return treeAlgorithm;
502 protected void checkEnoughSequences(AlignViewport viewport)
504 SequenceGroup sg = viewport.getSelectionGroup();
505 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
507 JvOptionPane.showMessageDialog(Desktop.desktop,
508 MessageManager.formatMessage(
509 "label.you_need_at_least_n_sequences",
511 MessageManager.getString("label.not_enough_sequences"),
512 JvOptionPane.WARNING_MESSAGE);
518 * Open a new Tree panel on the desktop
524 protected void openTreePanel(TreeModel tree, String treeAlgo,
525 String substitutionMatrix)
528 * gui validation shouldn't allow insufficient sequences here, but leave
529 * this check in in case this method gets exposed programmatically in future
531 checkEnoughSequences(af.getViewport());
533 af.newTreePanel(tree, treeAlgo, substitutionMatrix);
537 * Open a new PCA panel on the desktop
542 protected void openPcaPanel(String modelName, SimilarityParamsI params)
544 AlignViewport viewport = af.getViewport();
547 * gui validation shouldn't allow insufficient sequences here, but leave
548 * this check in in case this method gets exposed programmatically in future
550 if (((viewport.getSelectionGroup() != null)
551 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
552 && (viewport.getSelectionGroup().getSize() > 0))
553 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
555 JvOptionPane.showInternalMessageDialog(this,
556 MessageManager.formatMessage(
557 "label.you_need_at_least_n_sequences",
560 .getString("label.sequence_selection_insufficient"),
561 JvOptionPane.WARNING_MESSAGE);
564 new PCAPanel(af.alignPanel, modelName, params);
570 protected void closeFrame()
574 frame.setClosed(true);
575 } catch (PropertyVetoException ex)
581 * Returns a data bean holding parameters for similarity (or distance) model
587 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
589 // commented out: parameter choices read from gui widgets
590 // SimilarityParamsI params = new SimilarityParams(
591 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
592 // includeGaps.isSelected(), shorterSequence.isSelected());
594 boolean includeGapGap = true;
595 boolean includeGapResidue = true;
596 boolean matchOnShortestLength = false;
599 * 'matchGaps' flag is only used in the PID calculation
600 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
601 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
602 * - set to false for Tree for a more correct calculation (JAL-374)
604 boolean matchGap = doPCA ? false : treeMatchGaps;
607 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
608 matchOnShortestLength);
612 * Closes dialog on Close button press
614 protected void close_actionPerformed()
618 frame.setClosed(true);
619 } catch (Exception ex)