fa1195cea9ade91ab11475d8c7332a8e0d49a75b
[jalview.git] / src / jalview / gui / TreeChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import jalview.analysis.NJTree;
24 import jalview.analysis.scoremodels.PIDModel;
25 import jalview.analysis.scoremodels.ScoreModels;
26 import jalview.analysis.scoremodels.SimilarityParams;
27 import jalview.api.analysis.ScoreModelI;
28 import jalview.api.analysis.SimilarityParamsI;
29 import jalview.util.MessageManager;
30
31 import java.awt.Color;
32 import java.awt.FlowLayout;
33 import java.awt.Font;
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.beans.PropertyVetoException;
39
40 import javax.swing.ButtonGroup;
41 import javax.swing.JButton;
42 import javax.swing.JCheckBox;
43 import javax.swing.JComboBox;
44 import javax.swing.JInternalFrame;
45 import javax.swing.JLabel;
46 import javax.swing.JLayeredPane;
47 import javax.swing.JPanel;
48 import javax.swing.JRadioButton;
49
50 /**
51  * A dialog to allow a user to select and action Tree calculation options
52  */
53 public class TreeChooser extends JPanel
54 {
55   private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
56
57   AlignFrame af;
58
59   JRadioButton pca;
60
61   JRadioButton tree;
62
63   JRadioButton neighbourJoining;
64
65   JRadioButton averageDistance;
66
67   JComboBox<String> modelNames;
68
69   private JInternalFrame frame;
70
71   private ButtonGroup treeTypes;
72
73   private JCheckBox includeGaps;
74
75   private JCheckBox matchGaps;
76
77   private JCheckBox includeGappedColumns;
78
79   private JCheckBox shorterSequence;
80
81   /**
82    * Constructor
83    * 
84    * @param af
85    */
86   public TreeChooser(AlignFrame alignFrame)
87   {
88     this.af = alignFrame;
89     init();
90   }
91
92   /**
93    * Lays out the panel and adds it to the desktop
94    */
95   void init()
96   {
97     frame = new JInternalFrame();
98     frame.setContentPane(this);
99     this.setBackground(Color.white);
100
101     /*
102      * Layout consists of 4 or 5 panels:
103      * - first with choice of Tree or PCA
104      * - second with choice of tree method NJ or AV
105      * - third with choice of score model
106      * - fourth with score model parameter options [suppressed]
107      * - fifth with OK and Cancel
108      */
109     tree = new JRadioButton(MessageManager.getString("label.tree"));
110     tree.setOpaque(false);
111     pca = new JRadioButton(
112             MessageManager.getString("label.principal_component_analysis"));
113     pca.setOpaque(false);
114     neighbourJoining = new JRadioButton(
115             MessageManager.getString("label.tree_calc_nj"));
116     averageDistance = new JRadioButton(
117             MessageManager.getString("label.tree_calc_av"));
118     ItemListener listener = new ItemListener()
119     {
120       @Override
121       public void itemStateChanged(ItemEvent e)
122       {
123         neighbourJoining.setEnabled(tree.isSelected());
124         averageDistance.setEnabled(tree.isSelected());
125       }
126     };
127     pca.addItemListener(listener);
128     tree.addItemListener(listener);
129     ButtonGroup calcTypes = new ButtonGroup();
130     calcTypes.add(pca);
131     calcTypes.add(tree);
132     JPanel calcChoicePanel = new JPanel();
133     calcChoicePanel.setOpaque(false);
134     tree.setSelected(true);
135     calcChoicePanel.add(tree);
136     calcChoicePanel.add(pca);
137
138     neighbourJoining.setOpaque(false);
139     treeTypes = new ButtonGroup();
140     treeTypes.add(neighbourJoining);
141     treeTypes.add(averageDistance);
142     neighbourJoining.setSelected(true);
143     JPanel treeChoicePanel = new JPanel();
144     treeChoicePanel.setOpaque(false);
145     treeChoicePanel.add(neighbourJoining);
146     treeChoicePanel.add(averageDistance);
147
148     /*
149      * score model drop-down
150      */
151     modelNames = new JComboBox<String>();
152     ScoreModels scoreModels = ScoreModels.getInstance();
153     for (ScoreModelI sm : scoreModels.getModels())
154     {
155       boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
156       if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
157       {
158         modelNames.addItem(sm.getName());
159       }
160     }
161
162     JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
163     scoreModelPanel.setOpaque(false);
164     scoreModelPanel.add(modelNames, FlowLayout.LEFT);
165
166     /*
167      * score model parameters
168      */
169     JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
170     paramsPanel.setOpaque(false);
171     includeGaps = new JCheckBox("Include gaps");
172     matchGaps = new JCheckBox("Match gaps");
173     includeGappedColumns = new JCheckBox("Include gapped columns");
174     shorterSequence = new JCheckBox("Match on shorter sequence");
175     paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
176     paramsPanel.add(includeGaps);
177     paramsPanel.add(matchGaps);
178     paramsPanel.add(includeGappedColumns);
179     paramsPanel.add(shorterSequence);
180
181     /*
182      * OK / Cancel buttons
183      */
184     JButton ok = new JButton(MessageManager.getString("action.ok"));
185     ok.setFont(VERDANA_11PT);
186     ok.addActionListener(new java.awt.event.ActionListener()
187     {
188       @Override
189       public void actionPerformed(ActionEvent e)
190       {
191         ok_actionPerformed();
192       }
193     });
194     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
195     cancel.setFont(VERDANA_11PT);
196     cancel.addActionListener(new java.awt.event.ActionListener()
197     {
198       @Override
199       public void actionPerformed(ActionEvent e)
200       {
201         cancel_actionPerformed(e);
202       }
203     });
204     JPanel actionPanel = new JPanel();
205     actionPanel.setOpaque(false);
206     actionPanel.add(ok);
207     actionPanel.add(cancel);
208
209     boolean includeParams = false;
210     this.add(calcChoicePanel);
211     this.add(treeChoicePanel);
212     this.add(scoreModelPanel);
213     if (includeParams)
214     {
215       this.add(paramsPanel);
216     }
217     this.add(actionPanel);
218
219     int width = 380;
220     int height = includeParams ? 400 : 220;
221     Desktop.addInternalFrame(frame,
222             MessageManager.getString("label.choose_tree"), width, height,
223             false);
224
225     frame.setLayer(JLayeredPane.PALETTE_LAYER);
226   }
227
228   /**
229    * Open and calculate the selected tree on 'OK'
230    */
231   protected void ok_actionPerformed()
232   {
233     boolean doPCA = pca.isSelected();
234     ScoreModelI sm = ScoreModels.getInstance().forName(
235             modelNames.getSelectedItem().toString());
236     SimilarityParamsI params = getSimilarityParameters(doPCA, sm);
237
238     if (doPCA)
239     {
240       AlignViewport viewport = af.getViewport();
241       if (((viewport.getSelectionGroup() != null)
242               && (viewport.getSelectionGroup().getSize() < 4) && (viewport
243               .getSelectionGroup().getSize() > 0))
244               || (viewport.getAlignment().getHeight() < 4))
245       {
246         JvOptionPane
247                 .showInternalMessageDialog(
248                         this,
249                         MessageManager
250                                 .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
251                         MessageManager
252                                 .getString("label.sequence_selection_insufficient"),
253                         JvOptionPane.WARNING_MESSAGE);
254         return;
255       }
256       new PCAPanel(af.alignPanel, sm, params);
257     }
258     else
259     {
260       String treeType = neighbourJoining.isSelected() ? NJTree.NEIGHBOUR_JOINING
261               : NJTree.AVERAGE_DISTANCE;
262       af.newTreePanel(treeType, sm, params);
263     }
264
265     // closeFrame();
266   }
267
268   /**
269    * 
270    */
271   protected void closeFrame()
272   {
273     try
274     {
275       frame.setClosed(true);
276     } catch (PropertyVetoException ex)
277     {
278     }
279   }
280
281   /**
282    * Returns a data bean holding parameters for similarity (or distance) model
283    * calculation
284    * 
285    * @param doPCA
286    * @param sm
287    * @return
288    */
289   protected SimilarityParamsI getSimilarityParameters(boolean doPCA,
290           ScoreModelI sm)
291   {
292     // commented out: parameter choices read from gui widgets
293     // SimilarityParamsI params = new SimilarityParams(
294     // includeGappedColumns.isSelected(), matchGaps.isSelected(),
295     // includeGaps.isSelected(), shorterSequence.isSelected());
296
297     /*
298      * for now we want
299      * includeGappedColumns = true
300      * includeGaps = true
301      * matchOnShortestSequence = false
302      * matchGaps = true except false for PCA by PID (to match SeqSpace)
303      */
304     boolean includeGapGap = true;
305     boolean includeGapResidue = true;
306     boolean matchOnShortestLength = false;
307     boolean matchGap = true;
308     if (doPCA && (sm instanceof PIDModel))
309     {
310       matchGap = false;
311     }
312     return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength);
313   }
314
315   /**
316    * Closes dialog on cancel
317    * 
318    * @param e
319    */
320   protected void cancel_actionPerformed(ActionEvent e)
321   {
322     try
323     {
324       frame.setClosed(true);
325     } catch (Exception ex)
326     {
327     }
328   }
329 }