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.FlowLayout;
33 import java.awt.GridLayout;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ItemEvent;
36 import java.awt.event.ItemListener;
37 import java.beans.PropertyVetoException;
39 import javax.swing.ButtonGroup;
40 import javax.swing.JButton;
41 import javax.swing.JCheckBox;
42 import javax.swing.JComboBox;
43 import javax.swing.JInternalFrame;
44 import javax.swing.JLabel;
45 import javax.swing.JLayeredPane;
46 import javax.swing.JPanel;
47 import javax.swing.JRadioButton;
50 * A dialog where a user can choose and action Tree or PCA calculation options
52 public class CalculationChooser extends JPanel
55 * flag for whether gap matches residue in the PID calculation for a Tree
56 * - true gives Jalview 2.10.1 behaviour
57 * - set to false (using Groovy) for a more correct tree
60 private static boolean treeMatchGaps = true;
62 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
70 JRadioButton neighbourJoining;
72 JRadioButton averageDistance;
74 JComboBox<String> modelNames;
76 private JInternalFrame frame;
78 private ButtonGroup treeTypes;
80 private JCheckBox includeGaps;
82 private JCheckBox matchGaps;
84 private JCheckBox includeGappedColumns;
86 private JCheckBox shorterSequence;
93 public CalculationChooser(AlignFrame alignFrame)
100 * Lays out the panel and adds it to the desktop
104 frame = new JInternalFrame();
105 frame.setContentPane(this);
106 this.setBackground(Color.white);
109 * Layout consists of 4 or 5 panels:
110 * - first with choice of Tree or PCA
111 * - second with choice of tree method NJ or AV
112 * - third with choice of score model
113 * - fourth with score model parameter options [suppressed]
114 * - fifth with OK and Cancel
116 tree = new JRadioButton(MessageManager.getString("label.tree"));
117 tree.setOpaque(false);
118 pca = new JRadioButton(
119 MessageManager.getString("label.principal_component_analysis"));
120 pca.setOpaque(false);
121 neighbourJoining = new JRadioButton(
122 MessageManager.getString("label.tree_calc_nj"));
123 averageDistance = new JRadioButton(
124 MessageManager.getString("label.tree_calc_av"));
125 ItemListener listener = new ItemListener()
128 public void itemStateChanged(ItemEvent e)
130 neighbourJoining.setEnabled(tree.isSelected());
131 averageDistance.setEnabled(tree.isSelected());
134 pca.addItemListener(listener);
135 tree.addItemListener(listener);
136 ButtonGroup calcTypes = new ButtonGroup();
139 JPanel calcChoicePanel = new JPanel();
140 calcChoicePanel.setOpaque(false);
141 tree.setSelected(true);
142 calcChoicePanel.add(tree);
143 calcChoicePanel.add(pca);
145 neighbourJoining.setOpaque(false);
146 treeTypes = new ButtonGroup();
147 treeTypes.add(neighbourJoining);
148 treeTypes.add(averageDistance);
149 neighbourJoining.setSelected(true);
150 JPanel treeChoicePanel = new JPanel();
151 treeChoicePanel.setOpaque(false);
152 treeChoicePanel.add(neighbourJoining);
153 treeChoicePanel.add(averageDistance);
156 * score model drop-down
158 modelNames = new JComboBox<String>();
159 ScoreModels scoreModels = ScoreModels.getInstance();
160 for (ScoreModelI sm : scoreModels.getModels())
162 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
163 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
165 modelNames.addItem(sm.getName());
169 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
170 scoreModelPanel.setOpaque(false);
171 scoreModelPanel.add(modelNames, FlowLayout.LEFT);
174 * score model parameters
176 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
177 paramsPanel.setOpaque(false);
178 includeGaps = new JCheckBox("Include gaps");
179 matchGaps = new JCheckBox("Match gaps");
180 includeGappedColumns = new JCheckBox("Include gapped columns");
181 shorterSequence = new JCheckBox("Match on shorter sequence");
182 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
183 paramsPanel.add(includeGaps);
184 paramsPanel.add(matchGaps);
185 paramsPanel.add(includeGappedColumns);
186 paramsPanel.add(shorterSequence);
189 * OK / Cancel buttons
191 JButton ok = new JButton(MessageManager.getString("action.ok"));
192 ok.setFont(VERDANA_11PT);
193 ok.addActionListener(new java.awt.event.ActionListener()
196 public void actionPerformed(ActionEvent e)
198 ok_actionPerformed();
201 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
202 cancel.setFont(VERDANA_11PT);
203 cancel.addActionListener(new java.awt.event.ActionListener()
206 public void actionPerformed(ActionEvent e)
208 cancel_actionPerformed(e);
211 JPanel actionPanel = new JPanel();
212 actionPanel.setOpaque(false);
214 actionPanel.add(cancel);
216 boolean includeParams = false;
217 this.add(calcChoicePanel);
218 this.add(treeChoicePanel);
219 this.add(scoreModelPanel);
222 this.add(paramsPanel);
224 this.add(actionPanel);
227 int height = includeParams ? 400 : 220;
228 Desktop.addInternalFrame(frame,
229 MessageManager.getString("label.choose_calculation"), width,
232 frame.setLayer(JLayeredPane.PALETTE_LAYER);
236 * Open and calculate the selected tree on 'OK'
238 protected void ok_actionPerformed()
240 boolean doPCA = pca.isSelected();
241 ScoreModelI sm = ScoreModels.getInstance().forName(
242 modelNames.getSelectedItem().toString());
243 SimilarityParamsI params = getSimilarityParameters(doPCA);
247 openPcaPanel(sm, params);
251 openTreePanel(sm, params);
258 * Open a new Tree panel on the desktop
263 protected void openTreePanel(ScoreModelI sm, SimilarityParamsI params)
265 String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
266 : TreeBuilder.AVERAGE_DISTANCE;
267 af.newTreePanel(treeType, sm, params);
271 * Open a new PCA panel on the desktop
276 protected void openPcaPanel(ScoreModelI sm, SimilarityParamsI params)
278 AlignViewport viewport = af.getViewport();
279 if (((viewport.getSelectionGroup() != null)
280 && (viewport.getSelectionGroup().getSize() < 4) && (viewport
281 .getSelectionGroup().getSize() > 0))
282 || (viewport.getAlignment().getHeight() < 4))
285 .showInternalMessageDialog(
288 .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
290 .getString("label.sequence_selection_insufficient"),
291 JvOptionPane.WARNING_MESSAGE);
294 new PCAPanel(af.alignPanel, sm, params);
300 protected void closeFrame()
304 frame.setClosed(true);
305 } catch (PropertyVetoException ex)
311 * Returns a data bean holding parameters for similarity (or distance) model
317 protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
319 // commented out: parameter choices read from gui widgets
320 // SimilarityParamsI params = new SimilarityParams(
321 // includeGappedColumns.isSelected(), matchGaps.isSelected(),
322 // includeGaps.isSelected(), shorterSequence.isSelected());
324 boolean includeGapGap = true;
325 boolean includeGapResidue = true;
326 boolean matchOnShortestLength = false;
329 * 'matchGaps' flag is only used in the PID calculation
330 * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
331 * - set to true for Tree to reproduce Jalview 2.10.1 calculation
332 * - set to false for Tree for a more correct calculation (JAL-374)
334 boolean matchGap = doPCA ? false : treeMatchGaps;
336 return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength);
340 * Closes dialog on cancel
344 protected void cancel_actionPerformed(ActionEvent e)
348 frame.setClosed(true);
349 } catch (Exception ex)