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;
66 import org.forester.archaeopteryx.Archaeopteryx;
67 import org.forester.phylogeny.Phylogeny;
70 * A dialog where a user can choose and action Tree or PCA calculation options
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 final Font VERDANA_11PT = new Font("Verdana", 0, 11);
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 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
112 List<String> tips = new ArrayList<>();
119 public CalculationChooser(AlignFrame alignFrame)
121 this.af = alignFrame;
123 af.alignPanel.setCalculationDialog(this);
127 * Lays out the panel and adds it to the desktop
131 setLayout(new BorderLayout());
132 frame = new JInternalFrame();
133 frame.setContentPane(this);
134 this.setBackground(Color.white);
135 frame.addFocusListener(new FocusListener()
139 public void focusLost(FocusEvent e)
144 public void focusGained(FocusEvent e)
150 * Layout consists of 3 or 4 panels:
151 * - first with choice of PCA or tree method NJ or AV
152 * - second with choice of score model
153 * - third with score model parameter options [suppressed]
154 * - fourth with OK and Cancel
156 pca = new JRadioButton(
157 MessageManager.getString("label.principal_component_analysis"));
158 pca.setOpaque(false);
159 neighbourJoining = new JRadioButton(
160 MessageManager.getString("label.tree_calc_nj"));
161 neighbourJoining.setSelected(true);
162 averageDistance = new JRadioButton(
163 MessageManager.getString("label.tree_calc_av"));
164 neighbourJoining.setOpaque(false);
166 JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
167 calcChoicePanel.setOpaque(false);
169 // first create the Tree calculation's border panel
170 JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
171 treePanel.setOpaque(false);
173 treePanel.setBorder(BorderFactory
174 .createTitledBorder(MessageManager.getString("label.tree")));
176 // then copy the inset dimensions for the border-less PCA panel
177 JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
178 Insets b = treePanel.getBorder().getBorderInsets(treePanel);
179 pcaBorderless.setBorder(
180 BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
181 pcaBorderless.setOpaque(false);
183 pcaBorderless.add(pca, FlowLayout.LEFT);
184 calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
186 treePanel.add(neighbourJoining);
187 treePanel.add(averageDistance);
189 calcChoicePanel.add(treePanel);
191 ButtonGroup calcTypes = new ButtonGroup();
193 calcTypes.add(neighbourJoining);
194 calcTypes.add(averageDistance);
196 ActionListener calcChanged = new ActionListener()
199 public void actionPerformed(ActionEvent e)
204 pca.addActionListener(calcChanged);
205 neighbourJoining.addActionListener(calcChanged);
206 averageDistance.addActionListener(calcChanged);
209 * score models drop-down - with added tooltips!
211 modelNames = buildModelOptionsList();
213 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
214 scoreModelPanel.setOpaque(false);
215 scoreModelPanel.add(modelNames);
218 * score model parameters
220 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
221 paramsPanel.setOpaque(false);
222 includeGaps = new JCheckBox("Include gaps");
223 matchGaps = new JCheckBox("Match gaps");
224 includeGappedColumns = new JCheckBox("Include gapped columns");
225 shorterSequence = new JCheckBox("Match on shorter sequence");
226 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
227 paramsPanel.add(includeGaps);
228 paramsPanel.add(matchGaps);
229 paramsPanel.add(includeGappedColumns);
230 paramsPanel.add(shorterSequence);
233 * OK / Cancel buttons
235 calculate = new JButton(MessageManager.getString("action.calculate"));
236 calculate.setFont(VERDANA_11PT);
237 calculate.addActionListener(new java.awt.event.ActionListener()
240 public void actionPerformed(ActionEvent e)
242 calculate_actionPerformed();
245 JButton close = new JButton(MessageManager.getString("action.close"));
246 close.setFont(VERDANA_11PT);
247 close.addActionListener(new java.awt.event.ActionListener()
250 public void actionPerformed(ActionEvent e)
252 close_actionPerformed();
255 JPanel actionPanel = new JPanel();
256 actionPanel.setOpaque(false);
257 actionPanel.add(calculate);
258 actionPanel.add(close);
260 boolean includeParams = false;
261 this.add(calcChoicePanel, BorderLayout.CENTER);
262 calcChoicePanel.add(scoreModelPanel);
265 scoreModelPanel.add(paramsPanel);
267 this.add(actionPanel, BorderLayout.SOUTH);
270 int height = includeParams ? 420 : 240;
272 setMinimumSize(new Dimension(325, height - 10));
273 String title = MessageManager.getString("label.choose_calculation");
274 if (af.getViewport().viewName != null)
276 title = title + " (" + af.getViewport().viewName + ")";
279 Desktop.addInternalFrame(frame, title, width, height, false);
280 calcChoicePanel.doLayout();
283 * null the AlignmentPanel's reference to the dialog when it is closed
285 frame.addInternalFrameListener(new InternalFrameAdapter()
288 public void internalFrameClosed(InternalFrameEvent evt)
290 af.alignPanel.setCalculationDialog(null);
294 frame.setLayer(JLayeredPane.PALETTE_LAYER);
298 * enable calculations applicable for the current alignment or selection.
300 protected void validateCalcTypes()
302 int size = af.getViewport().getAlignment().getHeight();
303 if (af.getViewport().getSelectionGroup() != null)
305 size = af.getViewport().getSelectionGroup().getSize();
309 * disable calc options for which there is insufficient input data
310 * return value of true means enabled and selected
312 boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
313 boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
315 boolean checkAverageDistance = checkEnabled(averageDistance, size,
318 if (checkPca || checkNeighbourJoining || checkAverageDistance)
320 calculate.setToolTipText(null);
321 calculate.setEnabled(true);
325 calculate.setEnabled(false);
327 updateScoreModels(modelNames, tips);
331 * Check the input and disable a calculation's radio button if necessary. A
332 * tooltip is shown for disabled calculations.
335 * - radio button for the calculation being validated
337 * - size of input to calculation
339 * - minimum size for calculation
340 * @return true if size >= minsize and calc.isSelected
342 private boolean checkEnabled(JRadioButton calc, int size, int minsize)
344 String ttip = MessageManager
345 .formatMessage("label.you_need_at_least_n_sequences", minsize);
347 calc.setEnabled(size >= minsize);
348 if (!calc.isEnabled())
350 calc.setToolTipText(ttip);
354 calc.setToolTipText(null);
356 if (calc.isSelected())
358 modelNames.setEnabled(calc.isEnabled());
359 if (calc.isEnabled())
365 calculate.setToolTipText(ttip);
372 * A rather elaborate helper method (blame Swing, not me) that builds a
373 * drop-down list of score models (by name) with descriptions as tooltips.
374 * There is also a tooltip shown for the currently selected item when hovering
375 * over it (without opening the list).
377 protected JComboBox<String> buildModelOptionsList()
379 final JComboBox<String> scoreModelsCombo = new JComboBox<>();
380 scoreModelsCombo.setRenderer(renderer);
383 * show tooltip on mouse over the combobox
384 * note the listener has to be on the components that make up
385 * the combobox, doesn't work if just on the combobox
387 final MouseAdapter mouseListener = new MouseAdapter()
390 public void mouseEntered(MouseEvent e)
392 scoreModelsCombo.setToolTipText(
393 tips.get(scoreModelsCombo.getSelectedIndex()));
397 public void mouseExited(MouseEvent e)
399 scoreModelsCombo.setToolTipText(null);
402 for (Component c : scoreModelsCombo.getComponents())
404 c.addMouseListener(mouseListener);
407 updateScoreModels(scoreModelsCombo, tips);
410 * set the list of tooltips on the combobox's renderer
412 renderer.setTooltips(tips);
414 return scoreModelsCombo;
417 private void updateScoreModels(JComboBox<String> comboBox,
418 List<String> toolTips)
420 Object curSel = comboBox.getSelectedItem();
422 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
425 * now we can actually add entries to the combobox,
426 * remembering their descriptions for tooltips
428 ScoreModels scoreModels = ScoreModels.getInstance();
429 boolean selectedIsPresent = false;
430 for (ScoreModelI sm : scoreModels.getModels())
432 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
433 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
435 if (curSel != null && sm.getName().equals(curSel))
437 selectedIsPresent = true;
438 curSel = sm.getName();
440 model.addElement(sm.getName());
443 * tooltip is description if provided, else text lookup with
444 * fallback on the model name
446 String tooltip = sm.getDescription();
449 tooltip = MessageManager.getStringOrReturn("label.score_model_",
452 toolTips.add(tooltip);
455 if (selectedIsPresent)
457 model.setSelectedItem(curSel);
459 // finally, update the model
460 comboBox.setModel(model);
464 * Open and calculate the selected tree or PCA on 'OK'
466 protected void calculate_actionPerformed()
468 boolean doPCA = pca.isSelected();
469 String substitutionMatrix = modelNames.getSelectedItem().toString();
470 SimilarityParamsI params = getSimilarityParameters(doPCA);
474 openPcaPanel(substitutionMatrix, params);
479 String treeAlgo = determineTreeAlgo();
480 TreeCalculator treeCalculator = new TreeCalculator(treeAlgo,
481 substitutionMatrix, params);
482 TreeBuilder calculatedTree = treeCalculator
483 .makeTree(af.getViewport());
484 TreeModel tree = new TreeModel(calculatedTree);
485 openTreePanel(tree, treeAlgo, substitutionMatrix);
487 ArchaeopteryxTreeBuilder aptxTreeBuilder = new ArchaeopteryxTreeBuilder();
488 Phylogeny aptxTree = aptxTreeBuilder.buildAptxTree(calculatedTree);
489 Archaeopteryx.createApplication(aptxTree);
497 protected String determineTreeAlgo() // to be modified & expanded
499 String treeAlgorithm = neighbourJoining.isSelected()
500 ? TreeBuilder.NEIGHBOUR_JOINING
501 : TreeBuilder.AVERAGE_DISTANCE;
503 return treeAlgorithm;
507 protected void checkEnoughSequences(AlignViewport viewport)
509 SequenceGroup sg = viewport.getSelectionGroup();
510 if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
512 JvOptionPane.showMessageDialog(Desktop.desktop,
513 MessageManager.formatMessage(
514 "label.you_need_at_least_n_sequences",
516 MessageManager.getString("label.not_enough_sequences"),
517 JvOptionPane.WARNING_MESSAGE);
523 * Open a new Tree panel on the desktop
529 protected void openTreePanel(TreeModel tree, String treeAlgo,
530 String substitutionMatrix)
533 * gui validation shouldn't allow insufficient sequences here, but leave
534 * this check in in case this method gets exposed programmatically in future
536 checkEnoughSequences(af.getViewport());
538 af.newTreePanel(tree, treeAlgo, substitutionMatrix);
542 * Open a new PCA panel on the desktop
547 protected void openPcaPanel(String modelName, SimilarityParamsI params)
549 AlignViewport viewport = af.getViewport();
552 * gui validation shouldn't allow insufficient sequences here, but leave
553 * this check in in case this method gets exposed programmatically in future
555 if (((viewport.getSelectionGroup() != null)
556 && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
557 && (viewport.getSelectionGroup().getSize() > 0))
558 || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
560 JvOptionPane.showInternalMessageDialog(this,
561 MessageManager.formatMessage(
562 "label.you_need_at_least_n_sequences",
565 .getString("label.sequence_selection_insufficient"),
566 JvOptionPane.WARNING_MESSAGE);
569 new PCAPanel(af.alignPanel, modelName, params);
575 protected void closeFrame()
579 frame.setClosed(true);
580 } catch (PropertyVetoException ex)
586 * Returns a data bean holding parameters for similarity (or distance) model
592 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
594 // commented out: parameter choices read from gui widgets
595 // SimilarityParamsI params = new SimilarityParams(
596 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
597 // includeGaps.isSelected(), shorterSequence.isSelected());
599 boolean includeGapGap = true;
600 boolean includeGapResidue = true;
601 boolean matchOnShortestLength = false;
604 * 'matchGaps' flag is only used in the PID calculation
605 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
606 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
607 * - set to false for Tree for a more correct calculation (JAL-374)
609 boolean matchGap = doPCA ? false : treeMatchGaps;
612 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
613 matchOnShortestLength);
617 * Closes dialog on Close button press
619 protected void close_actionPerformed()
623 frame.setClosed(true);
624 } catch (Exception ex)