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;
55 * A dialog where a user can choose and action Tree or PCA calculation options
57 public class CalculationChooser extends JPanel
60 * flag for whether gap matches residue in the PID calculation for a Tree
61 * - true gives Jalview 2.10.1 behaviour
62 * - set to false (using Groovy) for a more correct tree
65 private static boolean treeMatchGaps = true;
67 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
75 JRadioButton neighbourJoining;
77 JRadioButton averageDistance;
79 JComboBox<String> modelNames;
81 private JInternalFrame frame;
83 private ButtonGroup treeTypes;
85 private JCheckBox includeGaps;
87 private JCheckBox matchGaps;
89 private JCheckBox includeGappedColumns;
91 private JCheckBox shorterSequence;
98 public CalculationChooser(AlignFrame alignFrame)
100 this.af = alignFrame;
105 * Lays out the panel and adds it to the desktop
109 frame = new JInternalFrame();
110 frame.setContentPane(this);
111 this.setBackground(Color.white);
114 * Layout consists of 4 or 5 panels:
115 * - first with choice of Tree or PCA
116 * - second with choice of tree method NJ or AV
117 * - third with choice of score model
118 * - fourth with score model parameter options [suppressed]
119 * - fifth with OK and Cancel
121 tree = new JRadioButton(MessageManager.getString("label.tree"));
122 tree.setOpaque(false);
123 pca = new JRadioButton(
124 MessageManager.getString("label.principal_component_analysis"));
125 pca.setOpaque(false);
126 neighbourJoining = new JRadioButton(
127 MessageManager.getString("label.tree_calc_nj"));
128 averageDistance = new JRadioButton(
129 MessageManager.getString("label.tree_calc_av"));
130 ItemListener listener = new ItemListener()
133 public void itemStateChanged(ItemEvent e)
135 neighbourJoining.setEnabled(tree.isSelected());
136 averageDistance.setEnabled(tree.isSelected());
139 pca.addItemListener(listener);
140 tree.addItemListener(listener);
141 ButtonGroup calcTypes = new ButtonGroup();
144 JPanel calcChoicePanel = new JPanel();
145 calcChoicePanel.setOpaque(false);
146 tree.setSelected(true);
147 calcChoicePanel.add(tree);
148 calcChoicePanel.add(pca);
150 neighbourJoining.setOpaque(false);
151 treeTypes = new ButtonGroup();
152 treeTypes.add(neighbourJoining);
153 treeTypes.add(averageDistance);
154 neighbourJoining.setSelected(true);
155 JPanel treeChoicePanel = new JPanel();
156 treeChoicePanel.setOpaque(false);
157 treeChoicePanel.add(neighbourJoining);
158 treeChoicePanel.add(averageDistance);
161 * score models drop-down - with added tooltips!
163 modelNames = buildModelOptionsList();
165 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
166 scoreModelPanel.setOpaque(false);
167 scoreModelPanel.add(modelNames, FlowLayout.LEFT);
170 * score model parameters
172 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
173 paramsPanel.setOpaque(false);
174 includeGaps = new JCheckBox("Include gaps");
175 matchGaps = new JCheckBox("Match gaps");
176 includeGappedColumns = new JCheckBox("Include gapped columns");
177 shorterSequence = new JCheckBox("Match on shorter sequence");
178 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
179 paramsPanel.add(includeGaps);
180 paramsPanel.add(matchGaps);
181 paramsPanel.add(includeGappedColumns);
182 paramsPanel.add(shorterSequence);
185 * OK / Cancel buttons
187 JButton ok = new JButton(MessageManager.getString("action.ok"));
188 ok.setFont(VERDANA_11PT);
189 ok.addActionListener(new java.awt.event.ActionListener()
192 public void actionPerformed(ActionEvent e)
194 ok_actionPerformed();
197 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
198 cancel.setFont(VERDANA_11PT);
199 cancel.addActionListener(new java.awt.event.ActionListener()
202 public void actionPerformed(ActionEvent e)
204 cancel_actionPerformed(e);
207 JPanel actionPanel = new JPanel();
208 actionPanel.setOpaque(false);
210 actionPanel.add(cancel);
212 boolean includeParams = false;
213 this.add(calcChoicePanel);
214 this.add(treeChoicePanel);
215 this.add(scoreModelPanel);
218 this.add(paramsPanel);
220 this.add(actionPanel);
223 int height = includeParams ? 400 : 220;
224 Desktop.addInternalFrame(frame,
225 MessageManager.getString("label.choose_calculation"), width,
228 frame.setLayer(JLayeredPane.PALETTE_LAYER);
232 * A rather elaborate helper method (blame Swing, not me) that builds a
233 * drop-down list of score models (by name) with descriptions as tooltips.
234 * There is also a tooltip shown for the currently selected item when hovering
235 * over it (without opening the list).
237 protected JComboBox<String> buildModelOptionsList()
239 final JComboBox<String> comboBox = new JComboBox<String>();
240 ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
241 comboBox.setRenderer(renderer);
242 final List<String> tips = new ArrayList<String>();
245 * show tooltip on mouse over the combobox
246 * note the listener has to be on the components that make up
247 * the combobox, doesn't work if just on the combobox
249 MouseAdapter mouseListener = new MouseAdapter()
252 public void mouseEntered(MouseEvent e)
254 comboBox.setToolTipText(tips.get(comboBox.getSelectedIndex()));
258 public void mouseExited(MouseEvent e)
260 comboBox.setToolTipText(null);
263 for (Component c : comboBox.getComponents())
265 c.addMouseListener(mouseListener);
269 * now we can actually add entries to the combobox,
270 * remembering their descriptions for tooltips
272 ScoreModels scoreModels = ScoreModels.getInstance();
273 for (ScoreModelI sm : scoreModels.getModels())
275 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
276 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
278 comboBox.addItem(sm.getName());
281 * tooltip is description if provided, else text lookup with
282 * fallback on the model name
284 String tooltip = sm.getDescription();
287 tooltip = MessageManager.getStringOrReturn("label.score_model_",
294 * set the list of tooltips on the combobox's renderer
296 renderer.setTooltips(tips);
303 * Open and calculate the selected tree on 'OK'
305 protected void ok_actionPerformed()
307 boolean doPCA = pca.isSelected();
308 ScoreModelI sm = ScoreModels.getInstance().forName(
309 modelNames.getSelectedItem().toString());
310 SimilarityParamsI params = getSimilarityParameters(doPCA);
314 openPcaPanel(sm, params);
318 openTreePanel(sm, params);
325 * Open a new Tree panel on the desktop
330 protected void openTreePanel(ScoreModelI sm, SimilarityParamsI params)
332 String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
333 : TreeBuilder.AVERAGE_DISTANCE;
334 af.newTreePanel(treeType, sm, params);
338 * Open a new PCA panel on the desktop
343 protected void openPcaPanel(ScoreModelI sm, SimilarityParamsI params)
345 AlignViewport viewport = af.getViewport();
346 if (((viewport.getSelectionGroup() != null)
347 && (viewport.getSelectionGroup().getSize() < 4) && (viewport
348 .getSelectionGroup().getSize() > 0))
349 || (viewport.getAlignment().getHeight() < 4))
352 .showInternalMessageDialog(
355 .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
357 .getString("label.sequence_selection_insufficient"),
358 JvOptionPane.WARNING_MESSAGE);
361 new PCAPanel(af.alignPanel, sm, params);
367 protected void closeFrame()
371 frame.setClosed(true);
372 } catch (PropertyVetoException ex)
378 * Returns a data bean holding parameters for similarity (or distance) model
384 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
386 // commented out: parameter choices read from gui widgets
387 // SimilarityParamsI params = new SimilarityParams(
388 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
389 // includeGaps.isSelected(), shorterSequence.isSelected());
391 boolean includeGapGap = true;
392 boolean includeGapResidue = true;
393 boolean matchOnShortestLength = false;
396 * 'matchGaps' flag is only used in the PID calculation
397 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
398 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
399 * - set to false for Tree for a more correct calculation (JAL-374)
401 boolean matchGap = doPCA ? false : treeMatchGaps;
403 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength);
407 * Closes dialog on cancel
411 protected void cancel_actionPerformed(ActionEvent e)
415 frame.setClosed(true);
416 } catch (Exception ex)