JAL-1632 TreeChooser renamed CalculationChooser, text improved, i18n
[jalview.git] / src / jalview / gui / CalculationChooser.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.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;
29
30 import java.awt.Color;
31 import java.awt.FlowLayout;
32 import java.awt.Font;
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;
38
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;
48
49 /**
50  * A dialog where a user can choose and action Tree or PCA calculation options
51  */
52 public class CalculationChooser extends JPanel
53 {
54   /*
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
58    * (JAL-374)
59    */
60   private static boolean treeMatchGaps = true;
61
62   private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
63
64   AlignFrame af;
65
66   JRadioButton pca;
67
68   JRadioButton tree;
69
70   JRadioButton neighbourJoining;
71
72   JRadioButton averageDistance;
73
74   JComboBox<String> modelNames;
75
76   private JInternalFrame frame;
77
78   private ButtonGroup treeTypes;
79
80   private JCheckBox includeGaps;
81
82   private JCheckBox matchGaps;
83
84   private JCheckBox includeGappedColumns;
85
86   private JCheckBox shorterSequence;
87
88   /**
89    * Constructor
90    * 
91    * @param af
92    */
93   public CalculationChooser(AlignFrame alignFrame)
94   {
95     this.af = alignFrame;
96     init();
97   }
98
99   /**
100    * Lays out the panel and adds it to the desktop
101    */
102   void init()
103   {
104     frame = new JInternalFrame();
105     frame.setContentPane(this);
106     this.setBackground(Color.white);
107
108     /*
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
115      */
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()
126     {
127       @Override
128       public void itemStateChanged(ItemEvent e)
129       {
130         neighbourJoining.setEnabled(tree.isSelected());
131         averageDistance.setEnabled(tree.isSelected());
132       }
133     };
134     pca.addItemListener(listener);
135     tree.addItemListener(listener);
136     ButtonGroup calcTypes = new ButtonGroup();
137     calcTypes.add(pca);
138     calcTypes.add(tree);
139     JPanel calcChoicePanel = new JPanel();
140     calcChoicePanel.setOpaque(false);
141     tree.setSelected(true);
142     calcChoicePanel.add(tree);
143     calcChoicePanel.add(pca);
144
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);
154
155     /*
156      * score model drop-down
157      */
158     modelNames = new JComboBox<String>();
159     ScoreModels scoreModels = ScoreModels.getInstance();
160     for (ScoreModelI sm : scoreModels.getModels())
161     {
162       boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
163       if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
164       {
165         modelNames.addItem(sm.getName());
166       }
167     }
168
169     JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
170     scoreModelPanel.setOpaque(false);
171     scoreModelPanel.add(modelNames, FlowLayout.LEFT);
172
173     /*
174      * score model parameters
175      */
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);
187
188     /*
189      * OK / Cancel buttons
190      */
191     JButton ok = new JButton(MessageManager.getString("action.ok"));
192     ok.setFont(VERDANA_11PT);
193     ok.addActionListener(new java.awt.event.ActionListener()
194     {
195       @Override
196       public void actionPerformed(ActionEvent e)
197       {
198         ok_actionPerformed();
199       }
200     });
201     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
202     cancel.setFont(VERDANA_11PT);
203     cancel.addActionListener(new java.awt.event.ActionListener()
204     {
205       @Override
206       public void actionPerformed(ActionEvent e)
207       {
208         cancel_actionPerformed(e);
209       }
210     });
211     JPanel actionPanel = new JPanel();
212     actionPanel.setOpaque(false);
213     actionPanel.add(ok);
214     actionPanel.add(cancel);
215
216     boolean includeParams = false;
217     this.add(calcChoicePanel);
218     this.add(treeChoicePanel);
219     this.add(scoreModelPanel);
220     if (includeParams)
221     {
222       this.add(paramsPanel);
223     }
224     this.add(actionPanel);
225
226     int width = 350;
227     int height = includeParams ? 400 : 220;
228     Desktop.addInternalFrame(frame,
229             MessageManager.getString("label.choose_calculation"), width,
230             height, false);
231
232     frame.setLayer(JLayeredPane.PALETTE_LAYER);
233   }
234
235   /**
236    * Open and calculate the selected tree on 'OK'
237    */
238   protected void ok_actionPerformed()
239   {
240     boolean doPCA = pca.isSelected();
241     ScoreModelI sm = ScoreModels.getInstance().forName(
242             modelNames.getSelectedItem().toString());
243     SimilarityParamsI params = getSimilarityParameters(doPCA);
244
245     if (doPCA)
246     {
247       openPcaPanel(sm, params);
248     }
249     else
250     {
251       openTreePanel(sm, params);
252     }
253
254     // closeFrame();
255   }
256
257   /**
258    * Open a new Tree panel on the desktop
259    * 
260    * @param sm
261    * @param params
262    */
263   protected void openTreePanel(ScoreModelI sm, SimilarityParamsI params)
264   {
265     String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
266             : TreeBuilder.AVERAGE_DISTANCE;
267     af.newTreePanel(treeType, sm, params);
268   }
269
270   /**
271    * Open a new PCA panel on the desktop
272    * 
273    * @param sm
274    * @param params
275    */
276   protected void openPcaPanel(ScoreModelI sm, SimilarityParamsI params)
277   {
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))
283     {
284       JvOptionPane
285               .showInternalMessageDialog(
286                       this,
287                       MessageManager
288                               .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
289                       MessageManager
290                               .getString("label.sequence_selection_insufficient"),
291                       JvOptionPane.WARNING_MESSAGE);
292       return;
293     }
294     new PCAPanel(af.alignPanel, sm, params);
295   }
296
297   /**
298    * 
299    */
300   protected void closeFrame()
301   {
302     try
303     {
304       frame.setClosed(true);
305     } catch (PropertyVetoException ex)
306     {
307     }
308   }
309
310   /**
311    * Returns a data bean holding parameters for similarity (or distance) model
312    * calculation
313    * 
314    * @param doPCA
315    * @return
316    */
317   protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
318   {
319     // commented out: parameter choices read from gui widgets
320     // SimilarityParamsI params = new SimilarityParams(
321     // includeGappedColumns.isSelected(), matchGaps.isSelected(),
322     // includeGaps.isSelected(), shorterSequence.isSelected());
323
324     boolean includeGapGap = true;
325     boolean includeGapResidue = true;
326     boolean matchOnShortestLength = false;
327
328     /*
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)
333      */
334     boolean matchGap = doPCA ? false : treeMatchGaps;
335
336     return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength);
337   }
338
339   /**
340    * Closes dialog on cancel
341    * 
342    * @param e
343    */
344   protected void cancel_actionPerformed(ActionEvent e)
345   {
346     try
347     {
348       frame.setClosed(true);
349     } catch (Exception ex)
350     {
351     }
352   }
353 }