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.util.MessageManager;
30 import java.awt.Color;
31 import java.awt.Component;
32 import java.awt.FlowLayout;
34 import java.awt.GridLayout;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ItemEvent;
37 import java.awt.event.ItemListener;
38 import java.awt.event.MouseAdapter;
39 import java.awt.event.MouseEvent;
40 import java.beans.PropertyVetoException;
41 import java.util.ArrayList;
42 import java.util.List;
44 import javax.swing.ButtonGroup;
45 import javax.swing.JButton;
46 import javax.swing.JCheckBox;
47 import javax.swing.JComboBox;
48 import javax.swing.JInternalFrame;
49 import javax.swing.JLabel;
50 import javax.swing.JLayeredPane;
51 import javax.swing.JPanel;
52 import javax.swing.JRadioButton;
53 import javax.swing.event.InternalFrameAdapter;
54 import javax.swing.event.InternalFrameEvent;
57 * A dialog where a user can choose and action Tree or PCA calculation options
59 public class CalculationChooser extends JPanel
62 * flag for whether gap matches residue in the PID calculation for a Tree
63 * - true gives Jalview 2.10.1 behaviour
64 * - set to false (using Groovy) for a more correct tree
67 private static boolean treeMatchGaps = true;
69 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
77 JRadioButton neighbourJoining;
79 JRadioButton averageDistance;
81 JComboBox<String> modelNames;
83 private JInternalFrame frame;
85 private ButtonGroup treeTypes;
87 private JCheckBox includeGaps;
89 private JCheckBox matchGaps;
91 private JCheckBox includeGappedColumns;
93 private JCheckBox shorterSequence;
100 public CalculationChooser(AlignFrame alignFrame)
102 this.af = alignFrame;
104 af.alignPanel.setCalculationDialog(this);
108 * Lays out the panel and adds it to the desktop
112 frame = new JInternalFrame();
113 frame.setContentPane(this);
114 this.setBackground(Color.white);
117 * Layout consists of 4 or 5 panels:
118 * - first with choice of Tree or PCA
119 * - second with choice of tree method NJ or AV
120 * - third with choice of score model
121 * - fourth with score model parameter options [suppressed]
122 * - fifth with OK and Cancel
124 tree = new JRadioButton(MessageManager.getString("label.tree"));
125 tree.setOpaque(false);
126 pca = new JRadioButton(
127 MessageManager.getString("label.principal_component_analysis"));
128 pca.setOpaque(false);
129 neighbourJoining = new JRadioButton(
130 MessageManager.getString("label.tree_calc_nj"));
131 averageDistance = new JRadioButton(
132 MessageManager.getString("label.tree_calc_av"));
133 ItemListener listener = new ItemListener()
136 public void itemStateChanged(ItemEvent e)
138 neighbourJoining.setEnabled(tree.isSelected());
139 averageDistance.setEnabled(tree.isSelected());
142 pca.addItemListener(listener);
143 tree.addItemListener(listener);
144 ButtonGroup calcTypes = new ButtonGroup();
147 JPanel calcChoicePanel = new JPanel();
148 calcChoicePanel.setOpaque(false);
149 tree.setSelected(true);
150 calcChoicePanel.add(tree);
151 calcChoicePanel.add(pca);
153 neighbourJoining.setOpaque(false);
154 treeTypes = new ButtonGroup();
155 treeTypes.add(neighbourJoining);
156 treeTypes.add(averageDistance);
157 neighbourJoining.setSelected(true);
158 JPanel treeChoicePanel = new JPanel();
159 treeChoicePanel.setOpaque(false);
160 treeChoicePanel.add(neighbourJoining);
161 treeChoicePanel.add(averageDistance);
164 * score models drop-down - with added tooltips!
166 modelNames = buildModelOptionsList();
168 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
169 scoreModelPanel.setOpaque(false);
170 scoreModelPanel.add(modelNames, FlowLayout.LEFT);
173 * score model parameters
175 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
176 paramsPanel.setOpaque(false);
177 includeGaps = new JCheckBox("Include gaps");
178 matchGaps = new JCheckBox("Match gaps");
179 includeGappedColumns = new JCheckBox("Include gapped columns");
180 shorterSequence = new JCheckBox("Match on shorter sequence");
181 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
182 paramsPanel.add(includeGaps);
183 paramsPanel.add(matchGaps);
184 paramsPanel.add(includeGappedColumns);
185 paramsPanel.add(shorterSequence);
188 * OK / Cancel buttons
190 JButton ok = new JButton(MessageManager.getString("action.ok"));
191 ok.setFont(VERDANA_11PT);
192 ok.addActionListener(new java.awt.event.ActionListener()
195 public void actionPerformed(ActionEvent e)
197 ok_actionPerformed();
200 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
201 cancel.setFont(VERDANA_11PT);
202 cancel.addActionListener(new java.awt.event.ActionListener()
205 public void actionPerformed(ActionEvent e)
207 cancel_actionPerformed(e);
210 JPanel actionPanel = new JPanel();
211 actionPanel.setOpaque(false);
213 actionPanel.add(cancel);
215 boolean includeParams = false;
216 this.add(calcChoicePanel);
217 this.add(treeChoicePanel);
218 this.add(scoreModelPanel);
221 this.add(paramsPanel);
223 this.add(actionPanel);
226 int height = includeParams ? 400 : 220;
227 String title = MessageManager.getString("label.choose_calculation");
228 if (af.getViewport().viewName != null)
230 title = title + " (" + af.getViewport().viewName + ")";
233 Desktop.addInternalFrame(frame,
238 * null the AlignmentPanel's reference to the dialog when it is closed
240 frame.addInternalFrameListener(new InternalFrameAdapter()
243 public void internalFrameClosed(InternalFrameEvent evt)
245 af.alignPanel.setCalculationDialog(null);
249 frame.setLayer(JLayeredPane.PALETTE_LAYER);
253 * A rather elaborate helper method (blame Swing, not me) that builds a
254 * drop-down list of score models (by name) with descriptions as tooltips.
255 * There is also a tooltip shown for the currently selected item when hovering
256 * over it (without opening the list).
258 protected JComboBox<String> buildModelOptionsList()
260 final JComboBox<String> comboBox = new JComboBox<String>();
261 ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
262 comboBox.setRenderer(renderer);
263 final List<String> tips = new ArrayList<String>();
266 * show tooltip on mouse over the combobox
267 * note the listener has to be on the components that make up
268 * the combobox, doesn't work if just on the combobox
270 MouseAdapter mouseListener = new MouseAdapter()
273 public void mouseEntered(MouseEvent e)
275 comboBox.setToolTipText(tips.get(comboBox.getSelectedIndex()));
279 public void mouseExited(MouseEvent e)
281 comboBox.setToolTipText(null);
284 for (Component c : comboBox.getComponents())
286 c.addMouseListener(mouseListener);
290 * now we can actually add entries to the combobox,
291 * remembering their descriptions for tooltips
293 ScoreModels scoreModels = ScoreModels.getInstance();
294 for (ScoreModelI sm : scoreModels.getModels())
296 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
297 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
299 comboBox.addItem(sm.getName());
302 * tooltip is description if provided, else text lookup with
303 * fallback on the model name
305 String tooltip = sm.getDescription();
308 tooltip = MessageManager.getStringOrReturn("label.score_model_",
315 * set the list of tooltips on the combobox's renderer
317 renderer.setTooltips(tips);
324 * Open and calculate the selected tree or PCA on 'OK'
326 protected void ok_actionPerformed()
328 boolean doPCA = pca.isSelected();
329 String modelName = modelNames.getSelectedItem().toString();
330 SimilarityParamsI params = getSimilarityParameters(doPCA);
334 openPcaPanel(modelName, params);
338 openTreePanel(modelName, params);
345 * Open a new Tree panel on the desktop
350 protected void openTreePanel(String modelName, SimilarityParamsI params)
352 String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
353 : TreeBuilder.AVERAGE_DISTANCE;
354 af.newTreePanel(treeType, modelName, params);
358 * Open a new PCA panel on the desktop
363 protected void openPcaPanel(String modelName, SimilarityParamsI params)
365 AlignViewport viewport = af.getViewport();
366 if (((viewport.getSelectionGroup() != null)
367 && (viewport.getSelectionGroup().getSize() < 4) && (viewport
368 .getSelectionGroup().getSize() > 0))
369 || (viewport.getAlignment().getHeight() < 4))
372 .showInternalMessageDialog(
375 .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
377 .getString("label.sequence_selection_insufficient"),
378 JvOptionPane.WARNING_MESSAGE);
381 new PCAPanel(af.alignPanel, modelName, params);
387 protected void closeFrame()
391 frame.setClosed(true);
392 } catch (PropertyVetoException ex)
398 * Returns a data bean holding parameters for similarity (or distance) model
404 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
406 // commented out: parameter choices read from gui widgets
407 // SimilarityParamsI params = new SimilarityParams(
408 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
409 // includeGaps.isSelected(), shorterSequence.isSelected());
411 boolean includeGapGap = true;
412 boolean includeGapResidue = true;
413 boolean matchOnShortestLength = false;
416 * 'matchGaps' flag is only used in the PID calculation
417 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
418 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
419 * - set to false for Tree for a more correct calculation (JAL-374)
421 boolean matchGap = doPCA ? false : treeMatchGaps;
423 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength);
427 * Closes dialog on cancel
431 protected void cancel_actionPerformed(ActionEvent e)
435 frame.setClosed(true);
436 } catch (Exception ex)