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.ArchaeopteryxInit;
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<>();
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.setContentPane(this);
136 this.setBackground(Color.white);
137 frame.addFocusListener(new FocusListener()
141 public void focusLost(FocusEvent e)
146 public void focusGained(FocusEvent e)
152 * Layout consists of 3 or 4 panels:
153 * - first with choice of PCA or tree method NJ or AV
154 * - second with choice of score model
155 * - third with score model parameter options [suppressed]
156 * - fourth with OK and Cancel
158 pca = new JRadioButton(
159 MessageManager.getString("label.principal_component_analysis"));
160 pca.setOpaque(false);
161 neighbourJoining = new JRadioButton(
162 MessageManager.getString("label.tree_calc_nj"));
163 neighbourJoining.setSelected(true);
164 averageDistance = new JRadioButton(
165 MessageManager.getString("label.tree_calc_av"));
166 neighbourJoining.setOpaque(false);
168 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
169 calcChoicePanel.setOpaque(false);
171 // first create the Tree calculation's border panel
172 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
173 treePanel.setOpaque(false);
175 treePanel.setBorder(BorderFactory
176 .createTitledBorder(MessageManager.getString("label.tree")));
178 // then copy the inset dimensions for the border-less PCA panel
179 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
180 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
181 pcaBorderless.setBorder(
182 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
183 pcaBorderless.setOpaque(false);
185 pcaBorderless.add(pca, FlowLayout.LEFT);
186 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
188 treePanel.add(neighbourJoining);
189 treePanel.add(averageDistance);
191 calcChoicePanel.add(treePanel);
193 ButtonGroup calcTypes = new ButtonGroup();
195 calcTypes.add(neighbourJoining);
196 calcTypes.add(averageDistance);
198 ActionListener calcChanged = new ActionListener()
201 public void actionPerformed(ActionEvent e)
206 pca.addActionListener(calcChanged);
207 neighbourJoining.addActionListener(calcChanged);
208 averageDistance.addActionListener(calcChanged);
211 * score models drop-down - with added tooltips!
213 modelNames = buildModelOptionsList();
215 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
216 scoreModelPanel.setOpaque(false);
217 scoreModelPanel.add(modelNames);
220 * score model parameters
222 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
223 paramsPanel.setOpaque(false);
224 includeGaps = new JCheckBox("Include gaps");
225 matchGaps = new JCheckBox("Match gaps");
226 includeGappedColumns = new JCheckBox("Include gapped columns");
227 shorterSequence = new JCheckBox("Match on shorter sequence");
228 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
229 paramsPanel.add(includeGaps);
230 paramsPanel.add(matchGaps);
231 paramsPanel.add(includeGappedColumns);
232 paramsPanel.add(shorterSequence);
235 * OK / Cancel buttons
237 calculate = new JButton(MessageManager.getString("action.calculate"));
238 calculate.setFont(VERDANA_11PT);
239 calculate.addActionListener(new java.awt.event.ActionListener()
242 public void actionPerformed(ActionEvent e)
244 calculate_actionPerformed();
247 JButton close = new JButton(MessageManager.getString("action.close"));
248 close.setFont(VERDANA_11PT);
249 close.addActionListener(new java.awt.event.ActionListener()
252 public void actionPerformed(ActionEvent e)
254 close_actionPerformed();
257 JPanel actionPanel = new JPanel();
258 actionPanel.setOpaque(false);
259 actionPanel.add(calculate);
260 actionPanel.add(close);
262 boolean includeParams = false;
263 this.add(calcChoicePanel, BorderLayout.CENTER);
264 calcChoicePanel.add(scoreModelPanel);
267 scoreModelPanel.add(paramsPanel);
269 this.add(actionPanel, BorderLayout.SOUTH);
272 int height = includeParams ? 420 : 240;
274 setMinimumSize(new Dimension(325, height - 10));
275 String title = MessageManager.getString("label.choose_calculation");
276 if (af.getViewport().viewName != null)
278 title = title + " (" + af.getViewport().viewName + ")";
281 Desktop.addInternalFrame(frame, title, width, height, false);
282 calcChoicePanel.doLayout();
285 * null the AlignmentPanel's reference to the dialog when it is closed
287 frame.addInternalFrameListener(new InternalFrameAdapter()
290 public void internalFrameClosed(InternalFrameEvent evt)
292 af.alignPanel.setCalculationDialog(null);
296 frame.setLayer(JLayeredPane.PALETTE_LAYER);
300 * enable calculations applicable for the current alignment or selection.
302 protected void validateCalcTypes()
304 int size = af.getViewport().getAlignment().getHeight();
305 if (af.getViewport().getSelectionGroup() != null)
307 size = af.getViewport().getSelectionGroup().getSize();
311 * disable calc options for which there is insufficient input data
312 * return value of true means enabled and selected
314 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
315 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
317 boolean checkAverageDistance = checkEnabled(averageDistance, size,
320 if (checkPca || checkNeighbourJoining || checkAverageDistance)
322 calculate.setToolTipText(null);
323 calculate.setEnabled(true);
327 calculate.setEnabled(false);
329 updateScoreModels(modelNames, tips);
333 * Check the input and disable a calculation's radio button if necessary. A
334 * tooltip is shown for disabled calculations.
337 * - radio button for the calculation being validated
339 * - size of input to calculation
341 * - minimum size for calculation
342 * @return true if size >= minsize and calc.isSelected
344 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
346 String ttip = MessageManager
347 .formatMessage("label.you_need_at_least_n_sequences", minsize);
349 calc.setEnabled(size >= minsize);
350 if (!calc.isEnabled())
352 calc.setToolTipText(ttip);
356 calc.setToolTipText(null);
358 if (calc.isSelected())
360 modelNames.setEnabled(calc.isEnabled());
361 if (calc.isEnabled())
367 calculate.setToolTipText(ttip);
374 * A rather elaborate helper method (blame Swing, not me) that builds a
375 * drop-down list of score models (by name) with descriptions as tooltips.
376 * There is also a tooltip shown for the currently selected item when hovering
377 * over it (without opening the list).
379 protected JComboBox<String> buildModelOptionsList()
381 final JComboBox<String> scoreModelsCombo = new JComboBox<>();
382 scoreModelsCombo.setRenderer(renderer);
385 * show tooltip on mouse over the combobox
386 * note the listener has to be on the components that make up
387 * the combobox, doesn't work if just on the combobox
389 final MouseAdapter mouseListener = new MouseAdapter()
392 public void mouseEntered(MouseEvent e)
394 scoreModelsCombo.setToolTipText(
395 tips.get(scoreModelsCombo.getSelectedIndex()));
399 public void mouseExited(MouseEvent e)
401 scoreModelsCombo.setToolTipText(null);
404 for (Component c : scoreModelsCombo.getComponents())
406 c.addMouseListener(mouseListener);
409 updateScoreModels(scoreModelsCombo, tips);
412 * set the list of tooltips on the combobox's renderer
414 renderer.setTooltips(tips);
416 return scoreModelsCombo;
419 private void updateScoreModels(JComboBox<String> comboBox,
420 List<String> toolTips)
422 Object curSel = comboBox.getSelectedItem();
424 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
427 * now we can actually add entries to the combobox,
428 * remembering their descriptions for tooltips
430 ScoreModels scoreModels = ScoreModels.getInstance();
431 boolean selectedIsPresent = false;
432 for (ScoreModelI sm : scoreModels.getModels())
434 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
435 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
437 if (curSel != null && sm.getName().equals(curSel))
439 selectedIsPresent = true;
440 curSel = sm.getName();
442 model.addElement(sm.getName());
445 * tooltip is description if provided, else text lookup with
446 * fallback on the model name
448 String tooltip = sm.getDescription();
451 tooltip = MessageManager.getStringOrReturn("label.score_model_",
454 toolTips.add(tooltip);
457 if (selectedIsPresent)
459 model.setSelectedItem(curSel);
461 // finally, update the model
462 comboBox.setModel(model);
466 * Open and calculate the selected tree or PCA on 'OK'
468 protected void calculate_actionPerformed()
470 boolean doPCA = pca.isSelected();
471 String substitutionMatrix = modelNames.getSelectedItem().toString();
472 SimilarityParamsI params = getSimilarityParameters(doPCA);
476 openPcaPanel(substitutionMatrix, params);
481 String treeAlgo = determineTreeAlgo();
482 TreeCalculator treeCalculator = new TreeCalculator(treeAlgo,
483 substitutionMatrix, params);
484 TreeBuilder calculatedTree = treeCalculator
485 .makeTree(af.getViewport());
486 TreeModel tree = new TreeModel(calculatedTree);
488 openTreePanel(tree, treeAlgo, substitutionMatrix);
489 ArchaeopteryxInit.createInstance(calculatedTree);
501 protected String determineTreeAlgo() // to be modified & expanded
503 String treeAlgorithm = neighbourJoining.isSelected()
504 ? TreeBuilder.NEIGHBOUR_JOINING
505 : TreeBuilder.AVERAGE_DISTANCE;
507 return treeAlgorithm;
511 protected void checkEnoughSequences(AlignViewport viewport)
513 SequenceGroup sg = viewport.getSelectionGroup();
514 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
516 JvOptionPane.showMessageDialog(Desktop.desktop,
517 MessageManager.formatMessage(
518 "label.you_need_at_least_n_sequences",
520 MessageManager.getString("label.not_enough_sequences"),
521 JvOptionPane.WARNING_MESSAGE);
527 * Open a new Tree panel on the desktop
533 protected void openTreePanel(TreeModel tree, String treeAlgo,
534 String substitutionMatrix)
537 * gui validation shouldn't allow insufficient sequences here, but leave
538 * this check in in case this method gets exposed programmatically in future
540 checkEnoughSequences(af.getViewport());
542 af.newTreePanel(tree, treeAlgo, substitutionMatrix);
546 * Open a new PCA panel on the desktop
551 protected void openPcaPanel(String modelName, SimilarityParamsI params)
553 AlignViewport viewport = af.getViewport();
556 * gui validation shouldn't allow insufficient sequences here, but leave
557 * this check in in case this method gets exposed programmatically in future
559 if (((viewport.getSelectionGroup() != null)
560 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
561 && (viewport.getSelectionGroup().getSize() > 0))
562 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
564 JvOptionPane.showInternalMessageDialog(this,
565 MessageManager.formatMessage(
566 "label.you_need_at_least_n_sequences",
569 .getString("label.sequence_selection_insufficient"),
570 JvOptionPane.WARNING_MESSAGE);
573 pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
579 protected void closeFrame()
583 frame.setClosed(true);
584 } catch (PropertyVetoException ex)
590 * Returns a data bean holding parameters for similarity (or distance) model
596 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
598 // commented out: parameter choices read from gui widgets
599 // SimilarityParamsI params = new SimilarityParams(
600 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
601 // includeGaps.isSelected(), shorterSequence.isSelected());
603 boolean includeGapGap = true;
604 boolean includeGapResidue = true;
605 boolean matchOnShortestLength = false;
608 * 'matchGaps' flag is only used in the PID calculation
609 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
610 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
611 * - set to false for Tree for a more correct calculation (JAL-374)
613 boolean matchGap = doPCA ? false : treeMatchGaps;
616 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
617 matchOnShortestLength);
621 * Closes dialog on Close button press
623 protected void close_actionPerformed()
627 frame.setClosed(true);
628 } catch (Exception ex)
633 public PCAPanel getPcaPanel()