JAL-1632 dialog to calculate either Tree or PCA
[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.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 to allow a user to select and action Tree calculation options
51  */
52 public class TreeChooser extends JPanel
53 {
54   private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
55
56   AlignFrame af;
57
58   JRadioButton pca;
59
60   JRadioButton tree;
61
62   JRadioButton neighbourJoining;
63
64   JRadioButton averageDistance;
65
66   JComboBox<String> matrixNames;
67
68   private JInternalFrame frame;
69
70   private ButtonGroup treeTypes;
71
72   private JCheckBox includeGaps;
73
74   private JCheckBox matchGaps;
75
76   private JCheckBox includeGappedColumns;
77
78   private JCheckBox shorterSequence;
79
80   /**
81    * Constructor
82    * 
83    * @param af
84    */
85   public TreeChooser(AlignFrame alignFrame)
86   {
87     this.af = alignFrame;
88     init();
89   }
90
91   /**
92    * Lays out the panel and adds it to the desktop
93    */
94   void init()
95   {
96     frame = new JInternalFrame();
97     frame.setContentPane(this);
98     this.setBackground(Color.white);
99
100     /*
101      * Layout consists of 5 panels:
102      * - first with choice of Tree or PCA
103      * - second with choice of tree method NJ or AV
104      * - third with choice of score model
105      * - fourth with score model parameter options
106      * - fifth with OK and Cancel
107      */
108     tree = new JRadioButton(MessageManager.getString("label.tree"));
109     tree.setOpaque(false);
110     pca = new JRadioButton(
111             MessageManager.getString("label.principal_component_analysis"));
112     pca.setOpaque(false);
113     neighbourJoining = new JRadioButton(
114             MessageManager.getString("label.tree_calc_nj"));
115     averageDistance = new JRadioButton(
116             MessageManager.getString("label.tree_calc_av"));
117     ItemListener listener = new ItemListener()
118     {
119       @Override
120       public void itemStateChanged(ItemEvent e)
121       {
122         neighbourJoining.setEnabled(tree.isSelected());
123         averageDistance.setEnabled(tree.isSelected());
124       }
125     };
126     pca.addItemListener(listener);
127     tree.addItemListener(listener);
128     ButtonGroup calcTypes = new ButtonGroup();
129     calcTypes.add(pca);
130     calcTypes.add(tree);
131     JPanel calcChoicePanel = new JPanel();
132     calcChoicePanel.setOpaque(false);
133     tree.setSelected(true);
134     calcChoicePanel.add(tree);
135     calcChoicePanel.add(pca);
136
137     neighbourJoining.setOpaque(false);
138     treeTypes = new ButtonGroup();
139     treeTypes.add(neighbourJoining);
140     treeTypes.add(averageDistance);
141     neighbourJoining.setSelected(true);
142     JPanel treeChoicePanel = new JPanel();
143     treeChoicePanel.setOpaque(false);
144     treeChoicePanel.add(neighbourJoining);
145     treeChoicePanel.add(averageDistance);
146
147     /*
148      * score model drop-down
149      */
150     matrixNames = new JComboBox<String>();
151     ScoreModels scoreModels = ScoreModels.getInstance();
152     for (ScoreModelI sm : scoreModels.getModels())
153     {
154       boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
155       if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
156       {
157         matrixNames.addItem(sm.getName());
158       }
159     }
160     JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
161     scoreModelPanel.setOpaque(false);
162     scoreModelPanel.add(matrixNames, FlowLayout.LEFT);
163
164     /*
165      * score model parameters
166      */
167     JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
168     paramsPanel.setOpaque(false);
169     includeGaps = new JCheckBox("Include gaps");
170     matchGaps = new JCheckBox("Match gaps");
171     includeGappedColumns = new JCheckBox("Include gapped columns");
172     shorterSequence = new JCheckBox("Match on shorter sequence");
173     paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
174     paramsPanel.add(includeGaps);
175     paramsPanel.add(matchGaps);
176     paramsPanel.add(includeGappedColumns);
177     paramsPanel.add(shorterSequence);
178
179     /*
180      * OK / Cancel buttons
181      */
182     JButton ok = new JButton(MessageManager.getString("action.ok"));
183     ok.setFont(VERDANA_11PT);
184     ok.addActionListener(new java.awt.event.ActionListener()
185     {
186       @Override
187       public void actionPerformed(ActionEvent e)
188       {
189         ok_actionPerformed(e);
190       }
191     });
192     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
193     cancel.setFont(VERDANA_11PT);
194     cancel.addActionListener(new java.awt.event.ActionListener()
195     {
196       @Override
197       public void actionPerformed(ActionEvent e)
198       {
199         cancel_actionPerformed(e);
200       }
201     });
202     JPanel actionPanel = new JPanel();
203     actionPanel.setOpaque(false);
204     actionPanel.add(ok);
205     actionPanel.add(cancel);
206
207     this.add(calcChoicePanel);
208     this.add(treeChoicePanel);
209     this.add(scoreModelPanel);
210     this.add(paramsPanel);
211     this.add(actionPanel);
212
213     Desktop.addInternalFrame(frame,
214             MessageManager.getString("label.choose_tree"), 400, 400, false);
215
216     frame.setLayer(JLayeredPane.PALETTE_LAYER);
217   }
218
219   /**
220    * Open and calculate the selected tree on 'OK'
221    * 
222    * @param e
223    */
224   protected void ok_actionPerformed(ActionEvent e)
225   {
226     ScoreModelI sm = ScoreModels.getInstance().forName(
227             matrixNames.getSelectedItem().toString());
228     SimilarityParamsI params = getSimilarityParameters();
229
230     if (pca.isSelected())
231     {
232       AlignViewport viewport = af.getViewport();
233       if (((viewport.getSelectionGroup() != null)
234               && (viewport.getSelectionGroup().getSize() < 4) && (viewport
235               .getSelectionGroup().getSize() > 0))
236               || (viewport.getAlignment().getHeight() < 4))
237       {
238         JvOptionPane
239                 .showInternalMessageDialog(
240                         this,
241                         MessageManager
242                                 .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
243                         MessageManager
244                                 .getString("label.sequence_selection_insufficient"),
245                         JvOptionPane.WARNING_MESSAGE);
246         return;
247       }
248       new PCAPanel(af.alignPanel, sm, params);
249     }
250     else
251     {
252       String treeType = neighbourJoining.isSelected() ? NJTree.NEIGHBOUR_JOINING
253               : NJTree.AVERAGE_DISTANCE;
254       af.newTreePanel(treeType, sm, params);
255     }
256
257     // closeFrame();
258   }
259
260   /**
261    * 
262    */
263   protected void closeFrame()
264   {
265     try
266     {
267       frame.setClosed(true);
268     } catch (PropertyVetoException ex)
269     {
270     }
271   }
272
273   private SimilarityParamsI getSimilarityParameters()
274   {
275     SimilarityParamsI params = new SimilarityParams(
276             includeGappedColumns.isSelected(), matchGaps.isSelected(),
277             includeGaps.isSelected(), shorterSequence.isSelected());
278     return params;
279   }
280
281   /**
282    * Closes dialog on cancel
283    * 
284    * @param e
285    */
286   protected void cancel_actionPerformed(ActionEvent e)
287   {
288     try
289     {
290       frame.setClosed(true);
291     } catch (Exception ex)
292     {
293     }
294   }
295 }