JAL-1632 similarity options on TreeChooser panel affect tree by PID
[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.beans.PropertyVetoException;
36
37 import javax.swing.ButtonGroup;
38 import javax.swing.JButton;
39 import javax.swing.JCheckBox;
40 import javax.swing.JComboBox;
41 import javax.swing.JInternalFrame;
42 import javax.swing.JLabel;
43 import javax.swing.JLayeredPane;
44 import javax.swing.JPanel;
45 import javax.swing.JRadioButton;
46
47 /**
48  * A dialog to allow a user to select and action Tree calculation options
49  */
50 public class TreeChooser extends JPanel
51 {
52   private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
53
54   AlignFrame af;
55
56   JRadioButton neighbourJoining;
57
58   JRadioButton averageDistance;
59
60   JComboBox<String> matrixNames;
61
62   private JInternalFrame frame;
63
64   private ButtonGroup treeTypes;
65
66   private JCheckBox includeGaps;
67
68   private JCheckBox matchGaps;
69
70   private JCheckBox includeGappedColumns;
71
72   private JCheckBox shorterSequence;
73
74   /**
75    * Constructor
76    * 
77    * @param af
78    */
79   public TreeChooser(AlignFrame alignFrame)
80   {
81     this.af = alignFrame;
82     init();
83   }
84
85   /**
86    * Lays out the panel and adds it to the desktop
87    */
88   void init()
89   {
90     frame = new JInternalFrame();
91     frame.setContentPane(this);
92     this.setBackground(Color.white);
93
94     /*
95      * Layout consists of 4 panels:
96      * - first with choice of tree method NJ or AV
97      * - second with choice of score model
98      * - third with score model parameter options
99      * - fourth with OK and Cancel
100      */
101     neighbourJoining = new JRadioButton(
102             MessageManager.getString("label.tree_calc_nj"));
103     neighbourJoining.setOpaque(false);
104     averageDistance = new JRadioButton(
105             MessageManager.getString("label.tree_calc_av"));
106     treeTypes = new ButtonGroup();
107     treeTypes.add(neighbourJoining);
108     treeTypes.add(averageDistance);
109     neighbourJoining.setSelected(true);
110     JPanel treeChoicePanel = new JPanel();
111     treeChoicePanel.setOpaque(false);
112     treeChoicePanel.add(neighbourJoining);
113     treeChoicePanel.add(averageDistance);
114
115     /*
116      * score model drop-down
117      */
118     matrixNames = new JComboBox<String>();
119     ScoreModels scoreModels = ScoreModels.getInstance();
120     for (ScoreModelI sm : scoreModels.getModels())
121     {
122       boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
123       if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
124       {
125         matrixNames.addItem(sm.getName());
126       }
127     }
128     JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
129     scoreModelPanel.setOpaque(false);
130     scoreModelPanel.add(matrixNames, FlowLayout.LEFT);
131
132     /*
133      * score model parameters
134      */
135     JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
136     paramsPanel.setOpaque(false);
137     includeGaps = new JCheckBox("Include gaps");
138     matchGaps = new JCheckBox("Match gaps");
139     includeGappedColumns = new JCheckBox("Include gapped columns");
140     shorterSequence = new JCheckBox("Match on shorter sequence");
141     paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
142     paramsPanel.add(includeGaps);
143     paramsPanel.add(matchGaps);
144     paramsPanel.add(includeGappedColumns);
145     paramsPanel.add(shorterSequence);
146
147     /*
148      * OK / Cancel buttons
149      */
150     JButton ok = new JButton(MessageManager.getString("action.ok"));
151     ok.setFont(VERDANA_11PT);
152     ok.addActionListener(new java.awt.event.ActionListener()
153     {
154       @Override
155       public void actionPerformed(ActionEvent e)
156       {
157         ok_actionPerformed(e);
158       }
159     });
160     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
161     cancel.setFont(VERDANA_11PT);
162     cancel.addActionListener(new java.awt.event.ActionListener()
163     {
164       @Override
165       public void actionPerformed(ActionEvent e)
166       {
167         cancel_actionPerformed(e);
168       }
169     });
170     JPanel actionPanel = new JPanel();
171     actionPanel.setOpaque(false);
172     actionPanel.add(ok);
173     actionPanel.add(cancel);
174
175     this.add(treeChoicePanel);
176     this.add(scoreModelPanel);
177     this.add(paramsPanel);
178     this.add(actionPanel);
179
180     Desktop.addInternalFrame(frame,
181             MessageManager.getString("label.choose_tree"), 400, 400, false);
182
183     frame.setLayer(JLayeredPane.PALETTE_LAYER);
184   }
185
186   /**
187    * Open and calculate the selected tree on 'OK'
188    * 
189    * @param e
190    */
191   protected void ok_actionPerformed(ActionEvent e)
192   {
193     String treeType = neighbourJoining.isSelected() ? NJTree.NEIGHBOUR_JOINING
194             : NJTree.AVERAGE_DISTANCE;
195     ScoreModelI sm = ScoreModels.getInstance().forName(
196             matrixNames.getSelectedItem().toString());
197     SimilarityParamsI params = getSimilarityParameters();
198     af.newTreePanel(treeType, sm, params);
199
200     try
201     {
202       frame.setClosed(true);
203     } catch (PropertyVetoException ex)
204     {
205     }
206   }
207
208   private SimilarityParamsI getSimilarityParameters()
209   {
210     SimilarityParamsI params = new SimilarityParams(
211             includeGappedColumns.isSelected(), matchGaps.isSelected(),
212             includeGaps.isSelected(), shorterSequence.isSelected());
213     return params;
214   }
215
216   /**
217    * Closes dialog on cancel
218    * 
219    * @param e
220    */
221   protected void cancel_actionPerformed(ActionEvent e)
222   {
223     try
224     {
225       frame.setClosed(true);
226     } catch (Exception ex)
227     {
228     }
229   }
230 }