097a5a0f3e7fef8a64bc082bcf77a353543e9b4e
[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.bin.Cache;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.util.MessageManager;
31
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.Component;
35 import java.awt.Dimension;
36 import java.awt.FlowLayout;
37 import java.awt.Font;
38 import java.awt.GridLayout;
39 import java.awt.Insets;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.FocusEvent;
43 import java.awt.event.FocusListener;
44 import java.awt.event.MouseAdapter;
45 import java.awt.event.MouseEvent;
46 import java.beans.PropertyVetoException;
47 import java.util.ArrayList;
48 import java.util.List;
49
50 import javax.swing.BorderFactory;
51 import javax.swing.ButtonGroup;
52 import javax.swing.DefaultComboBoxModel;
53 import javax.swing.JButton;
54 import javax.swing.JCheckBox;
55 import javax.swing.JComboBox;
56 import javax.swing.JInternalFrame;
57 import javax.swing.JLabel;
58 import javax.swing.JLayeredPane;
59 import javax.swing.JPanel;
60 import javax.swing.JRadioButton;
61 import javax.swing.event.InternalFrameAdapter;
62 import javax.swing.event.InternalFrameEvent;
63
64 /**
65  * A dialog where a user can choose and action Tree or PCA calculation options
66  */
67 public class CalculationChooser extends JPanel
68 {
69   /*
70    * flag for whether gap matches residue in the PID calculation for a Tree
71    * - true gives Jalview 2.10.1 behaviour
72    * - set to false (using Groovy) for a more correct tree
73    * (JAL-374)
74    */
75   private static boolean treeMatchGaps = true;
76
77   private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
78
79   private static final int MIN_TREE_SELECTION = 3;
80
81   private static final int MIN_PCA_SELECTION = 4;
82
83   AlignFrame af;
84
85   JRadioButton pca;
86
87   JRadioButton neighbourJoining;
88
89   JRadioButton averageDistance;
90
91   JComboBox<String> modelNames;
92
93   JButton calculate;
94
95   private JInternalFrame frame;
96
97   private JCheckBox includeGaps;
98
99   private JCheckBox matchGaps;
100
101   private JCheckBox includeGappedColumns;
102
103   private JCheckBox shorterSequence;
104
105   final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
106
107   List<String> tips = new ArrayList<>();
108
109   /*
110    * the most recently opened PCA results panel
111    */
112   private PCAPanel pcaPanel;
113
114   /**
115    * Constructor
116    * 
117    * @param af
118    */
119   public CalculationChooser(AlignFrame alignFrame)
120   {
121     this.af = alignFrame;
122     init();
123     af.alignPanel.setCalculationDialog(this);
124   }
125
126   /**
127    * Lays out the panel and adds it to the desktop
128    */
129   void init()
130   {
131     setLayout(new BorderLayout());
132     frame = new JInternalFrame();
133     frame.setContentPane(this);
134     this.setBackground(Color.white);
135     frame.addFocusListener(new FocusListener()
136     {
137
138       @Override
139       public void focusLost(FocusEvent e)
140       {
141       }
142
143       @Override
144       public void focusGained(FocusEvent e)
145       {
146         validateCalcTypes();
147       }
148     });
149     /*
150      * Layout consists of 3 or 4 panels:
151      * - first with choice of PCA or tree method NJ or AV
152      * - second with choice of score model
153      * - third with score model parameter options [suppressed]
154      * - fourth with OK and Cancel
155      */
156     pca = new JRadioButton(
157             MessageManager.getString("label.principal_component_analysis"));
158     pca.setOpaque(false);
159     
160     neighbourJoining = new JRadioButton(
161             MessageManager.getString("label.tree_calc_nj"));
162     neighbourJoining.setSelected(true);
163     neighbourJoining.setOpaque(false);
164
165     averageDistance = new JRadioButton(
166             MessageManager.getString("label.tree_calc_av"));
167     averageDistance.setOpaque(false);
168
169     JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
170     calcChoicePanel.setOpaque(false);
171
172     // first create the Tree calculation's border panel
173     JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
174     treePanel.setOpaque(false);
175
176     JvSwingUtils.createTitledBorder(treePanel,
177             MessageManager.getString("label.tree"), true);
178
179     // then copy the inset dimensions for the border-less PCA panel
180     JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
181     Insets b = treePanel.getBorder().getBorderInsets(treePanel);
182     pcaBorderless.setBorder(
183             BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
184     pcaBorderless.setOpaque(false);
185
186     pcaBorderless.add(pca, FlowLayout.LEFT);
187     calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT);
188
189     treePanel.add(neighbourJoining);
190     treePanel.add(averageDistance);
191
192     calcChoicePanel.add(treePanel);
193
194     ButtonGroup calcTypes = new ButtonGroup();
195     calcTypes.add(pca);
196     calcTypes.add(neighbourJoining);
197     calcTypes.add(averageDistance);
198
199     ActionListener calcChanged = new ActionListener()
200     {
201       @Override
202       public void actionPerformed(ActionEvent e)
203       {
204         validateCalcTypes();
205       }
206     };
207     pca.addActionListener(calcChanged);
208     neighbourJoining.addActionListener(calcChanged);
209     averageDistance.addActionListener(calcChanged);
210
211     /*
212      * score models drop-down - with added tooltips!
213      */
214     modelNames = buildModelOptionsList();
215
216     JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
217     scoreModelPanel.setOpaque(false);
218     scoreModelPanel.add(modelNames);
219
220     /*
221      * score model parameters
222      */
223     JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
224     paramsPanel.setOpaque(false);
225     includeGaps = new JCheckBox("Include gaps");
226     matchGaps = new JCheckBox("Match gaps");
227     includeGappedColumns = new JCheckBox("Include gapped columns");
228     shorterSequence = new JCheckBox("Match on shorter sequence");
229     paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
230     paramsPanel.add(includeGaps);
231     paramsPanel.add(matchGaps);
232     paramsPanel.add(includeGappedColumns);
233     paramsPanel.add(shorterSequence);
234
235     /*
236      * OK / Cancel buttons
237      */
238     calculate = new JButton(MessageManager.getString("action.calculate"));
239     calculate.setFont(VERDANA_11PT);
240     calculate.addActionListener(new java.awt.event.ActionListener()
241     {
242       @Override
243       public void actionPerformed(ActionEvent e)
244       {
245         calculate_actionPerformed();
246       }
247     });
248     JButton close = new JButton(MessageManager.getString("action.close"));
249     close.setFont(VERDANA_11PT);
250     close.addActionListener(new java.awt.event.ActionListener()
251     {
252       @Override
253       public void actionPerformed(ActionEvent e)
254       {
255         close_actionPerformed();
256       }
257     });
258     JPanel actionPanel = new JPanel();
259     actionPanel.setOpaque(false);
260     actionPanel.add(calculate);
261     actionPanel.add(close);
262
263     boolean includeParams = false;
264     this.add(calcChoicePanel, BorderLayout.CENTER);
265     calcChoicePanel.add(scoreModelPanel);
266     if (includeParams)
267     {
268       scoreModelPanel.add(paramsPanel);
269     }
270     this.add(actionPanel, BorderLayout.SOUTH);
271
272     int width = 350;
273     int height = includeParams ? 420 : 240;
274
275     setMinimumSize(new Dimension(325, height - 10));
276     String title = MessageManager.getString("label.choose_calculation");
277     if (af.getViewport().getViewName() != null)
278     {
279       title = title + " (" + af.getViewport().getViewName() + ")";
280     }
281
282     Desktop.addInternalFrame(frame, title, width, height, false);
283     calcChoicePanel.doLayout();
284     revalidate();
285     /*
286      * null the AlignmentPanel's reference to the dialog when it is closed
287      */
288     frame.addInternalFrameListener(new InternalFrameAdapter()
289     {
290       @Override
291       public void internalFrameClosed(InternalFrameEvent evt)
292       {
293         af.alignPanel.setCalculationDialog(null);
294       };
295     });
296
297     validateCalcTypes();
298     frame.setLayer(JLayeredPane.PALETTE_LAYER);
299   }
300
301   /**
302    * enable calculations applicable for the current alignment or selection.
303    */
304   protected void validateCalcTypes()
305   {
306     int size = af.getViewport().getAlignment().getHeight();
307     if (af.getViewport().getSelectionGroup() != null)
308     {
309       size = af.getViewport().getSelectionGroup().getSize();
310     }
311
312     /*
313      * disable calc options for which there is insufficient input data
314      * return value of true means enabled and selected
315      */
316     boolean checkPca = checkEnabled(pca, size, MIN_PCA_SELECTION);
317     boolean checkNeighbourJoining = checkEnabled(neighbourJoining, size,
318             MIN_TREE_SELECTION);
319     boolean checkAverageDistance = checkEnabled(averageDistance, size,
320             MIN_TREE_SELECTION);
321
322     if (checkPca || checkNeighbourJoining || checkAverageDistance)
323     {
324       calculate.setToolTipText(null);
325       calculate.setEnabled(true);
326     }
327     else
328     {
329       calculate.setEnabled(false);
330     }
331     updateScoreModels(modelNames, tips);
332   }
333
334   /**
335    * Check the input and disable a calculation's radio button if necessary. A
336    * tooltip is shown for disabled calculations.
337    * 
338    * @param calc
339    *          - radio button for the calculation being validated
340    * @param size
341    *          - size of input to calculation
342    * @param minsize
343    *          - minimum size for calculation
344    * @return true if size >= minsize and calc.isSelected
345    */
346   private boolean checkEnabled(JRadioButton calc, int size, int minsize)
347   {
348     String ttip = MessageManager
349             .formatMessage("label.you_need_at_least_n_sequences", minsize);
350
351     calc.setEnabled(size >= minsize);
352     if (!calc.isEnabled())
353     {
354       calc.setToolTipText(ttip);
355     }
356     else
357     {
358       calc.setToolTipText(null);
359     }
360     if (calc.isSelected())
361     {
362       modelNames.setEnabled(calc.isEnabled());
363       if (calc.isEnabled())
364       {
365         return true;
366       }
367       else
368       {
369         calculate.setToolTipText(ttip);
370       }
371     }
372     return false;
373   }
374
375   /**
376    * A rather elaborate helper method (blame Swing, not me) that builds a
377    * drop-down list of score models (by name) with descriptions as tooltips.
378    * There is also a tooltip shown for the currently selected item when hovering
379    * over it (without opening the list).
380    */
381   protected JComboBox<String> buildModelOptionsList()
382   {
383     final JComboBox<String> scoreModelsCombo = new JComboBox<>();
384     scoreModelsCombo.setRenderer(renderer);
385
386     /*
387      * show tooltip on mouse over the combobox
388      * note the listener has to be on the components that make up
389      * the combobox, doesn't work if just on the combobox
390      */
391     final MouseAdapter mouseListener = new MouseAdapter()
392     {
393       @Override
394       public void mouseEntered(MouseEvent e)
395       {
396         scoreModelsCombo.setToolTipText(
397                 tips.get(scoreModelsCombo.getSelectedIndex()));
398       }
399
400       @Override
401       public void mouseExited(MouseEvent e)
402       {
403         scoreModelsCombo.setToolTipText(null);
404       }
405     };
406     for (Component c : scoreModelsCombo.getComponents())
407     {
408       c.addMouseListener(mouseListener);
409     }
410
411     updateScoreModels(scoreModelsCombo, tips);
412
413     /*
414      * set the list of tooltips on the combobox's renderer
415      */
416     renderer.setTooltips(tips);
417
418     return scoreModelsCombo;
419   }
420
421   private void updateScoreModels(JComboBox<String> comboBox,
422           List<String> toolTips)
423   {
424     Object curSel = comboBox.getSelectedItem();
425     toolTips.clear();
426     DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
427
428     /*
429      * select the score models applicable to the alignment type
430      */
431     boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
432     List<ScoreModelI> models = getApplicableScoreModels(nucleotide,
433             pca.isSelected());
434
435     /*
436      * now we can actually add entries to the combobox,
437      * remembering their descriptions for tooltips
438      */
439     boolean selectedIsPresent = false;
440     for (ScoreModelI sm : models)
441     {
442       if (curSel != null && sm.getName().equals(curSel))
443       {
444         selectedIsPresent = true;
445         curSel = sm.getName();
446       }
447       model.addElement(sm.getName());
448
449       /*
450        * tooltip is description if provided, else text lookup with
451        * fallback on the model name
452        */
453       String tooltip = sm.getDescription();
454       if (tooltip == null)
455       {
456         tooltip = MessageManager.getStringOrReturn("label.score_model_",
457                 sm.getName());
458       }
459       toolTips.add(tooltip);
460     }
461
462     if (selectedIsPresent)
463     {
464       model.setSelectedItem(curSel);
465     }
466     // finally, update the model
467     comboBox.setModel(model);
468   }
469
470   /**
471    * Builds a list of score models which are applicable for the alignment and
472    * calculation type (peptide or generic models for protein, nucleotide or
473    * generic models for nucleotide).
474    * <p>
475    * As a special case, includes BLOSUM62 as an extra option for nucleotide PCA.
476    * This is for backwards compatibility with Jalview prior to 2.8 when BLOSUM62
477    * was the only score matrix supported. This is included if property
478    * BLOSUM62_PCA_FOR_NUCLEOTIDE is set to true in the Jalview properties file.
479    * 
480    * @param nucleotide
481    * @param forPca
482    * @return
483    */
484   protected static List<ScoreModelI> getApplicableScoreModels(
485           boolean nucleotide, boolean forPca)
486   {
487     List<ScoreModelI> filtered = new ArrayList<>();
488
489     ScoreModels scoreModels = ScoreModels.getInstance();
490     for (ScoreModelI sm : scoreModels.getModels())
491     {
492       if (!nucleotide && sm.isProtein() || nucleotide && sm.isDNA())
493       {
494         filtered.add(sm);
495       }
496     }
497
498     /*
499      * special case: add BLOSUM62 as last option for nucleotide PCA, 
500      * for backwards compatibility with Jalview < 2.8 (JAL-2962)
501      */
502     if (nucleotide && forPca
503             && Cache.getDefault("BLOSUM62_PCA_FOR_NUCLEOTIDE", false))
504     {
505       filtered.add(scoreModels.getBlosum62());
506     }
507
508     return filtered;
509   }
510
511   /**
512    * Open and calculate the selected tree or PCA on 'OK'
513    */
514   protected void calculate_actionPerformed()
515   {
516     boolean doPCA = pca.isSelected();
517     String modelName = modelNames.getSelectedItem().toString();
518     SimilarityParamsI params = getSimilarityParameters(doPCA);
519
520     if (doPCA)
521     {
522       openPcaPanel(modelName, params);
523     }
524     else
525     {
526       openTreePanel(modelName, params);
527     }
528
529     // closeFrame();
530   }
531
532   /**
533    * Open a new Tree panel on the desktop
534    * 
535    * @param modelName
536    * @param params
537    */
538   protected void openTreePanel(String modelName, SimilarityParamsI params)
539   {
540     /*
541      * gui validation shouldn't allow insufficient sequences here, but leave
542      * this check in in case this method gets exposed programmatically in future
543      */
544     AlignViewport viewport = af.getViewport();
545     SequenceGroup sg = viewport.getSelectionGroup();
546     if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
547     {
548       JvOptionPane.showMessageDialog(Desktop.desktop,
549               MessageManager.formatMessage(
550                       "label.you_need_at_least_n_sequences",
551                       MIN_TREE_SELECTION),
552               MessageManager.getString("label.not_enough_sequences"),
553               JvOptionPane.WARNING_MESSAGE);
554       return;
555     }
556
557     String treeType = neighbourJoining.isSelected()
558             ? TreeBuilder.NEIGHBOUR_JOINING
559             : TreeBuilder.AVERAGE_DISTANCE;
560     af.newTreePanel(treeType, modelName, params);
561   }
562
563   /**
564    * Open a new PCA panel on the desktop
565    * 
566    * @param modelName
567    * @param params
568    */
569   protected void openPcaPanel(String modelName, SimilarityParamsI params)
570   {
571     AlignViewport viewport = af.getViewport();
572
573     /*
574      * gui validation shouldn't allow insufficient sequences here, but leave
575      * this check in in case this method gets exposed programmatically in future
576      */
577     if (((viewport.getSelectionGroup() != null)
578             && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
579             && (viewport.getSelectionGroup().getSize() > 0))
580             || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
581     {
582       JvOptionPane.showInternalMessageDialog(this,
583               MessageManager.formatMessage(
584                       "label.you_need_at_least_n_sequences",
585                       MIN_PCA_SELECTION),
586               MessageManager
587                       .getString("label.sequence_selection_insufficient"),
588               JvOptionPane.WARNING_MESSAGE);
589       return;
590     }
591
592     /*
593      * construct the panel and kick off its calculation thread
594      */
595     pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
596     new Thread(pcaPanel).start();
597
598   }
599
600   /**
601    * 
602    */
603   protected void closeFrame()
604   {
605     try
606     {
607       frame.setClosed(true);
608     } catch (PropertyVetoException ex)
609     {
610     }
611   }
612
613   /**
614    * Returns a data bean holding parameters for similarity (or distance) model
615    * calculation
616    * 
617    * @param doPCA
618    * @return
619    */
620   protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
621   {
622     // commented out: parameter choices read from gui widgets
623     // SimilarityParamsI params = new SimilarityParams(
624     // includeGappedColumns.isSelected(), matchGaps.isSelected(),
625     // includeGaps.isSelected(), shorterSequence.isSelected());
626
627     boolean includeGapGap = true;
628     boolean includeGapResidue = true;
629     boolean matchOnShortestLength = false;
630
631     /*
632      * 'matchGaps' flag is only used in the PID calculation
633      * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA
634      * - set to true for Tree to reproduce Jalview 2.10.1 calculation
635      * - set to false for Tree for a more correct calculation (JAL-374)
636      */
637     boolean matchGap = doPCA ? false : treeMatchGaps;
638
639     return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
640             matchOnShortestLength);
641   }
642
643   /**
644    * Closes dialog on Close button press
645    */
646   protected void close_actionPerformed()
647   {
648     try
649     {
650       frame.setClosed(true);
651     } catch (Exception ex)
652     {
653     }
654   }
655
656   public PCAPanel getPcaPanel()
657   {
658     return pcaPanel;
659   }
660 }