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.NJTree;
24 import jalview.analysis.scoremodels.ScoreMatrix;
25 import jalview.analysis.scoremodels.ScoreModels;
26 import jalview.analysis.scoremodels.SimilarityParams;
27 import jalview.api.analysis.DistanceScoreModelI;
28 import jalview.api.analysis.ScoreModelI;
29 import jalview.api.analysis.SimilarityParamsI;
30 import jalview.util.MessageManager;
32 import java.awt.Color;
33 import java.awt.FlowLayout;
35 import java.awt.GridLayout;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ItemEvent;
38 import java.awt.event.ItemListener;
39 import java.beans.PropertyVetoException;
41 import javax.swing.ButtonGroup;
42 import javax.swing.JButton;
43 import javax.swing.JCheckBox;
44 import javax.swing.JComboBox;
45 import javax.swing.JInternalFrame;
46 import javax.swing.JLabel;
47 import javax.swing.JLayeredPane;
48 import javax.swing.JPanel;
49 import javax.swing.JRadioButton;
52 * A dialog to allow a user to select and action Tree calculation options
54 public class TreeChooser extends JPanel
56 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
64 JRadioButton neighbourJoining;
66 JRadioButton averageDistance;
68 JComboBox<String> modelNames;
70 private JInternalFrame frame;
72 private ButtonGroup treeTypes;
74 private JCheckBox includeGaps;
76 private JCheckBox matchGaps;
78 private JCheckBox includeGappedColumns;
80 private JCheckBox shorterSequence;
87 public TreeChooser(AlignFrame alignFrame)
94 * Lays out the panel and adds it to the desktop
98 frame = new JInternalFrame();
99 frame.setContentPane(this);
100 this.setBackground(Color.white);
103 * Layout consists of 5 panels:
104 * - first with choice of Tree or PCA
105 * - second with choice of tree method NJ or AV
106 * - third with choice of score model
107 * - fourth with score model parameter options
108 * - fifth with OK and Cancel
110 tree = new JRadioButton(MessageManager.getString("label.tree"));
111 tree.setOpaque(false);
112 pca = new JRadioButton(
113 MessageManager.getString("label.principal_component_analysis"));
114 pca.setOpaque(false);
115 neighbourJoining = new JRadioButton(
116 MessageManager.getString("label.tree_calc_nj"));
117 averageDistance = new JRadioButton(
118 MessageManager.getString("label.tree_calc_av"));
119 ItemListener listener = new ItemListener()
122 public void itemStateChanged(ItemEvent e)
124 neighbourJoining.setEnabled(tree.isSelected());
125 averageDistance.setEnabled(tree.isSelected());
128 pca.addItemListener(listener);
129 tree.addItemListener(listener);
130 ButtonGroup calcTypes = new ButtonGroup();
133 JPanel calcChoicePanel = new JPanel();
134 calcChoicePanel.setOpaque(false);
135 tree.setSelected(true);
136 calcChoicePanel.add(tree);
137 calcChoicePanel.add(pca);
139 neighbourJoining.setOpaque(false);
140 treeTypes = new ButtonGroup();
141 treeTypes.add(neighbourJoining);
142 treeTypes.add(averageDistance);
143 neighbourJoining.setSelected(true);
144 JPanel treeChoicePanel = new JPanel();
145 treeChoicePanel.setOpaque(false);
146 treeChoicePanel.add(neighbourJoining);
147 treeChoicePanel.add(averageDistance);
150 * score model drop-down
152 modelNames = new JComboBox<String>();
153 ScoreModels scoreModels = ScoreModels.getInstance();
154 for (ScoreModelI sm : scoreModels.getModels())
156 boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
157 if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
159 modelNames.addItem(sm.getName());
162 modelNames.addItemListener(new ItemListener()
166 public void itemStateChanged(ItemEvent e)
168 if (e.getStateChange() == ItemEvent.SELECTED)
170 scoreModelChanged((String) e.getItem());
174 JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
175 scoreModelPanel.setOpaque(false);
176 scoreModelPanel.add(modelNames, FlowLayout.LEFT);
179 * score model parameters
181 JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
182 paramsPanel.setOpaque(false);
183 includeGaps = new JCheckBox("Include gaps");
184 matchGaps = new JCheckBox("Match gaps");
185 includeGappedColumns = new JCheckBox("Include gapped columns");
186 shorterSequence = new JCheckBox("Match on shorter sequence");
187 paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
188 paramsPanel.add(includeGaps);
189 paramsPanel.add(matchGaps);
190 paramsPanel.add(includeGappedColumns);
191 paramsPanel.add(shorterSequence);
192 // configure initial state of options
193 scoreModelChanged(modelNames.getItemAt(0));
196 * OK / Cancel buttons
198 JButton ok = new JButton(MessageManager.getString("action.ok"));
199 ok.setFont(VERDANA_11PT);
200 ok.addActionListener(new java.awt.event.ActionListener()
203 public void actionPerformed(ActionEvent e)
205 ok_actionPerformed(e);
208 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
209 cancel.setFont(VERDANA_11PT);
210 cancel.addActionListener(new java.awt.event.ActionListener()
213 public void actionPerformed(ActionEvent e)
215 cancel_actionPerformed(e);
218 JPanel actionPanel = new JPanel();
219 actionPanel.setOpaque(false);
221 actionPanel.add(cancel);
223 this.add(calcChoicePanel);
224 this.add(treeChoicePanel);
225 this.add(scoreModelPanel);
226 this.add(paramsPanel);
227 this.add(actionPanel);
229 Desktop.addInternalFrame(frame,
230 MessageManager.getString("label.choose_tree"), 400, 400, false);
232 frame.setLayer(JLayeredPane.PALETTE_LAYER);
236 * Action on selection of score model
240 protected void scoreModelChanged(String modelName)
243 * enable/disable options appropriate to score model
244 * NB this is temporary - will get score models to provide
245 * their own parameters
247 includeGaps.setEnabled(true);
248 matchGaps.setEnabled(true);
249 includeGappedColumns.setEnabled(true);
250 shorterSequence.setEnabled(true);
252 ScoreModelI sm = ScoreModels.getInstance().forName(modelName);
253 if (sm instanceof DistanceScoreModelI)
255 matchGaps.setEnabled(false);
256 includeGappedColumns.setEnabled(false);
257 shorterSequence.setEnabled(false);
259 if (sm instanceof ScoreMatrix)
261 matchGaps.setEnabled(false);
266 * Open and calculate the selected tree on 'OK'
270 protected void ok_actionPerformed(ActionEvent e)
272 ScoreModelI sm = ScoreModels.getInstance().forName(
273 modelNames.getSelectedItem().toString());
274 SimilarityParamsI params = getSimilarityParameters();
276 if (pca.isSelected())
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);
298 String treeType = neighbourJoining.isSelected() ? NJTree.NEIGHBOUR_JOINING
299 : NJTree.AVERAGE_DISTANCE;
300 af.newTreePanel(treeType, sm, params);
309 protected void closeFrame()
313 frame.setClosed(true);
314 } catch (PropertyVetoException ex)
319 private SimilarityParamsI getSimilarityParameters()
321 SimilarityParamsI params = new SimilarityParams(
322 includeGappedColumns.isSelected(), matchGaps.isSelected(),
323 includeGaps.isSelected(), shorterSequence.isSelected());
328 * Closes dialog on cancel
332 protected void cancel_actionPerformed(ActionEvent e)
336 frame.setClosed(true);
337 } catch (Exception ex)