JAL-2428 separate TreeModel from TreeBuilder/NJTree/AverageDistanceTree
[jalview.git] / src / jalview / gui / TreeChooser.java
index 8e2bff0..41b7d48 100644 (file)
 package jalview.gui;
 
 import jalview.analysis.NJTree;
+import jalview.analysis.scoremodels.ScoreMatrix;
 import jalview.analysis.scoremodels.ScoreModels;
+import jalview.analysis.scoremodels.SimilarityParams;
+import jalview.api.analysis.DistanceScoreModelI;
 import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.SimilarityParamsI;
 import jalview.util.MessageManager;
 
 import java.awt.Color;
 import java.awt.FlowLayout;
 import java.awt.Font;
+import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+import java.beans.PropertyVetoException;
 
 import javax.swing.ButtonGroup;
 import javax.swing.JButton;
+import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
 import javax.swing.JInternalFrame;
+import javax.swing.JLabel;
 import javax.swing.JLayeredPane;
 import javax.swing.JPanel;
 import javax.swing.JRadioButton;
@@ -47,16 +57,28 @@ public class TreeChooser extends JPanel
 
   AlignFrame af;
 
+  JRadioButton pca;
+
+  JRadioButton tree;
+
   JRadioButton neighbourJoining;
 
   JRadioButton averageDistance;
 
-  JComboBox<String> matrixNames;
+  JComboBox<String> modelNames;
 
   private JInternalFrame frame;
 
   private ButtonGroup treeTypes;
 
+  private JCheckBox includeGaps;
+
+  private JCheckBox matchGaps;
+
+  private JCheckBox includeGappedColumns;
+
+  private JCheckBox shorterSequence;
+
   /**
    * Constructor
    * 
@@ -68,33 +90,111 @@ public class TreeChooser extends JPanel
     init();
   }
 
+  /**
+   * Lays out the panel and adds it to the desktop
+   */
   void init()
   {
     frame = new JInternalFrame();
     frame.setContentPane(this);
     this.setBackground(Color.white);
 
+    /*
+     * Layout consists of 5 panels:
+     * - first with choice of Tree or PCA
+     * - second with choice of tree method NJ or AV
+     * - third with choice of score model
+     * - fourth with score model parameter options
+     * - fifth with OK and Cancel
+     */
+    tree = new JRadioButton(MessageManager.getString("label.tree"));
+    tree.setOpaque(false);
+    pca = new JRadioButton(
+            MessageManager.getString("label.principal_component_analysis"));
+    pca.setOpaque(false);
     neighbourJoining = new JRadioButton(
             MessageManager.getString("label.tree_calc_nj"));
-    neighbourJoining.setOpaque(false);
     averageDistance = new JRadioButton(
             MessageManager.getString("label.tree_calc_av"));
+    ItemListener listener = new ItemListener()
+    {
+      @Override
+      public void itemStateChanged(ItemEvent e)
+      {
+        neighbourJoining.setEnabled(tree.isSelected());
+        averageDistance.setEnabled(tree.isSelected());
+      }
+    };
+    pca.addItemListener(listener);
+    tree.addItemListener(listener);
+    ButtonGroup calcTypes = new ButtonGroup();
+    calcTypes.add(pca);
+    calcTypes.add(tree);
+    JPanel calcChoicePanel = new JPanel();
+    calcChoicePanel.setOpaque(false);
+    tree.setSelected(true);
+    calcChoicePanel.add(tree);
+    calcChoicePanel.add(pca);
+
+    neighbourJoining.setOpaque(false);
     treeTypes = new ButtonGroup();
     treeTypes.add(neighbourJoining);
     treeTypes.add(averageDistance);
     neighbourJoining.setSelected(true);
+    JPanel treeChoicePanel = new JPanel();
+    treeChoicePanel.setOpaque(false);
+    treeChoicePanel.add(neighbourJoining);
+    treeChoicePanel.add(averageDistance);
 
-    matrixNames = new JComboBox<String>();
+    /*
+     * score model drop-down
+     */
+    modelNames = new JComboBox<String>();
     ScoreModels scoreModels = ScoreModels.getInstance();
     for (ScoreModelI sm : scoreModels.getModels())
     {
       boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
       if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
       {
-        matrixNames.addItem(sm.getName());
+        modelNames.addItem(sm.getName());
       }
     }
+    modelNames.addItemListener(new ItemListener()
+    {
+
+      @Override
+      public void itemStateChanged(ItemEvent e)
+      {
+        if (e.getStateChange() == ItemEvent.SELECTED)
+        {
+          scoreModelChanged((String) e.getItem());
+        }
+      }
+    });
+    JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
+    scoreModelPanel.setOpaque(false);
+    scoreModelPanel.add(modelNames, FlowLayout.LEFT);
+
+    /*
+     * score model parameters
+     */
+    JPanel paramsPanel = new JPanel(new GridLayout(5, 1));
+    paramsPanel.setOpaque(false);
+    includeGaps = new JCheckBox("Include gaps");
+    matchGaps = new JCheckBox("Match gaps");
+    includeGappedColumns = new JCheckBox("Include gapped columns");
+    shorterSequence = new JCheckBox("Match on shorter sequence");
+    paramsPanel.add(new JLabel("Pairwise sequence scoring options"));
+    paramsPanel.add(includeGaps);
+    paramsPanel.add(matchGaps);
+    paramsPanel.add(includeGappedColumns);
+    paramsPanel.add(shorterSequence);
+    // configure initial state of options
+    scoreModelChanged(modelNames.getItemAt(0));
 
+    /*
+     * OK / Cancel buttons
+     */
     JButton ok = new JButton(MessageManager.getString("action.ok"));
     ok.setFont(VERDANA_11PT);
     ok.addActionListener(new java.awt.event.ActionListener()
@@ -105,7 +205,6 @@ public class TreeChooser extends JPanel
         ok_actionPerformed(e);
       }
     });
-
     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
     cancel.setFont(VERDANA_11PT);
     cancel.addActionListener(new java.awt.event.ActionListener()
@@ -116,49 +215,126 @@ public class TreeChooser extends JPanel
         cancel_actionPerformed(e);
       }
     });
+    JPanel actionPanel = new JPanel();
+    actionPanel.setOpaque(false);
+    actionPanel.add(ok);
+    actionPanel.add(cancel);
 
-    JPanel p1 = new JPanel();
-    p1.setOpaque(false);
-    p1.add(neighbourJoining);
-    p1.add(averageDistance);
-    this.add(p1);
-    JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
-    p2.setOpaque(false);
-    p2.add(matrixNames, FlowLayout.LEFT);
-    this.add(p2);
-    JPanel p3 = new JPanel();
-    p3.setOpaque(false);
-    p3.add(ok);
-    p3.add(cancel);
-    this.add(p3);
+    this.add(calcChoicePanel);
+    this.add(treeChoicePanel);
+    this.add(scoreModelPanel);
+    this.add(paramsPanel);
+    this.add(actionPanel);
 
     Desktop.addInternalFrame(frame,
-            MessageManager.getString("label.choose_tree"),
-              400, 200, false);
+            MessageManager.getString("label.choose_tree"), 400, 400, false);
 
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
   }
 
   /**
+   * Action on selection of score model
+   * 
+   * @param item
+   */
+  protected void scoreModelChanged(String modelName)
+  {
+    /*
+     * enable/disable options appropriate to score model
+     * NB this is temporary - will get score models to provide
+     * their own parameters
+     */
+    includeGaps.setEnabled(true);
+    matchGaps.setEnabled(true);
+    includeGappedColumns.setEnabled(true);
+    shorterSequence.setEnabled(true);
+
+    ScoreModelI sm = ScoreModels.getInstance().forName(modelName);
+    if (sm instanceof DistanceScoreModelI)
+    {
+      matchGaps.setEnabled(false);
+      includeGappedColumns.setEnabled(false);
+      shorterSequence.setEnabled(false);
+    }
+    if (sm instanceof ScoreMatrix)
+    {
+      matchGaps.setEnabled(false);
+    }
+    if (tree.isSelected())
+    {
+      // ?? tree requires specific parameter settings??
+      includeGaps.setSelected(true);
+      includeGaps.setEnabled(false);
+      matchGaps.setSelected(true);
+      includeGappedColumns.setSelected(true);
+      includeGappedColumns.setEnabled(false);
+      shorterSequence.setSelected(false);
+      shorterSequence.setEnabled(false);
+    }
+  }
+
+  /**
    * Open and calculate the selected tree on 'OK'
    * 
    * @param e
    */
   protected void ok_actionPerformed(ActionEvent e)
   {
-    try
+    ScoreModelI sm = ScoreModels.getInstance().forName(
+            modelNames.getSelectedItem().toString());
+    SimilarityParamsI params = getSimilarityParameters();
+
+    if (pca.isSelected())
+    {
+      AlignViewport viewport = af.getViewport();
+      if (((viewport.getSelectionGroup() != null)
+              && (viewport.getSelectionGroup().getSize() < 4) && (viewport
+              .getSelectionGroup().getSize() > 0))
+              || (viewport.getAlignment().getHeight() < 4))
+      {
+        JvOptionPane
+                .showInternalMessageDialog(
+                        this,
+                        MessageManager
+                                .getString("label.principal_component_analysis_must_take_least_four_input_sequences"),
+                        MessageManager
+                                .getString("label.sequence_selection_insufficient"),
+                        JvOptionPane.WARNING_MESSAGE);
+        return;
+      }
+      new PCAPanel(af.alignPanel, sm, params);
+    }
+    else
     {
-      frame.setClosed(true);
       String treeType = neighbourJoining.isSelected() ? NJTree.NEIGHBOUR_JOINING
               : NJTree.AVERAGE_DISTANCE;
-      ScoreModelI sm = ScoreModels.getInstance().forName(
-              matrixNames.getSelectedItem().toString());
-      af.newTreePanel(treeType, sm);
-    } catch (Exception ex)
+      af.newTreePanel(treeType, sm, params);
+    }
+
+    // closeFrame();
+  }
+
+  /**
+   * 
+   */
+  protected void closeFrame()
+  {
+    try
+    {
+      frame.setClosed(true);
+    } catch (PropertyVetoException ex)
     {
     }
   }
 
+  private SimilarityParamsI getSimilarityParameters()
+  {
+    SimilarityParamsI params = new SimilarityParams(
+            includeGappedColumns.isSelected(), matchGaps.isSelected(),
+            includeGaps.isSelected(), shorterSequence.isSelected());
+    return params;
+  }
+
   /**
    * Closes dialog on cancel
    *