JAL-1953 2.11.2 with Archeopteryx!
[jalview.git] / src / jalview / gui / CalculationChooser.java
index 8a95594..ae92168 100644 (file)
 package jalview.gui;
 
 import jalview.analysis.TreeBuilder;
+import jalview.analysis.TreeCalculator;
+import jalview.analysis.TreeModel;
 import jalview.analysis.scoremodels.ScoreModels;
 import jalview.analysis.scoremodels.SimilarityParams;
 import jalview.api.analysis.ScoreModelI;
 import jalview.api.analysis.SimilarityParamsI;
+import jalview.bin.Cache;
 import jalview.datamodel.SequenceGroup;
+import jalview.ext.archaeopteryx.AptxInit;
 import jalview.util.MessageManager;
 
 import java.awt.BorderLayout;
@@ -43,6 +47,7 @@ import java.awt.event.FocusListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.beans.PropertyVetoException;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -103,7 +108,12 @@ public class CalculationChooser extends JPanel
 
   final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
 
-  List<String> tips = new ArrayList<String>();
+  List<String> tips = new ArrayList<>();
+
+  /*
+   * the most recently opened PCA results panel
+   */
+  private PCAPanel pcaPanel;
 
   /**
    * Constructor
@@ -150,12 +160,15 @@ public class CalculationChooser extends JPanel
     pca = new JRadioButton(
             MessageManager.getString("label.principal_component_analysis"));
     pca.setOpaque(false);
+
     neighbourJoining = new JRadioButton(
             MessageManager.getString("label.tree_calc_nj"));
     neighbourJoining.setSelected(true);
+    neighbourJoining.setOpaque(false);
+
     averageDistance = new JRadioButton(
             MessageManager.getString("label.tree_calc_av"));
-    neighbourJoining.setOpaque(false);
+    averageDistance.setOpaque(false);
 
     JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
     calcChoicePanel.setOpaque(false);
@@ -164,14 +177,14 @@ public class CalculationChooser extends JPanel
     JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
     treePanel.setOpaque(false);
 
-    treePanel.setBorder(BorderFactory.createTitledBorder(MessageManager
-            .getString("label.tree")));
+    JvSwingUtils.createTitledBorder(treePanel,
+            MessageManager.getString("label.tree"), true);
 
     // then copy the inset dimensions for the border-less PCA panel
     JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT));
     Insets b = treePanel.getBorder().getBorderInsets(treePanel);
-    pcaBorderless.setBorder(BorderFactory.createEmptyBorder(2, b.left, 2,
-            b.right));
+    pcaBorderless.setBorder(
+            BorderFactory.createEmptyBorder(2, b.left, 2, b.right));
     pcaBorderless.setOpaque(false);
 
     pcaBorderless.add(pca, FlowLayout.LEFT);
@@ -186,7 +199,7 @@ public class CalculationChooser extends JPanel
     calcTypes.add(pca);
     calcTypes.add(neighbourJoining);
     calcTypes.add(averageDistance);
-    
+
     ActionListener calcChanged = new ActionListener()
     {
       @Override
@@ -265,9 +278,9 @@ public class CalculationChooser extends JPanel
 
     setMinimumSize(new Dimension(325, height - 10));
     String title = MessageManager.getString("label.choose_calculation");
-    if (af.getViewport().viewName != null)
+    if (af.getViewport().getViewName() != null)
     {
-      title = title + " (" + af.getViewport().viewName + ")";
+      title = title + " (" + af.getViewport().getViewName() + ")";
     }
 
     Desktop.addInternalFrame(frame, title, width, height, false);
@@ -285,6 +298,7 @@ public class CalculationChooser extends JPanel
       };
     });
 
+    validateCalcTypes();
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
   }
 
@@ -335,8 +349,8 @@ public class CalculationChooser extends JPanel
    */
   private boolean checkEnabled(JRadioButton calc, int size, int minsize)
   {
-    String ttip = MessageManager.formatMessage(
-            "label.you_need_at_least_n_sequences", minsize);
+    String ttip = MessageManager
+            .formatMessage("label.you_need_at_least_n_sequences", minsize);
 
     calc.setEnabled(size >= minsize);
     if (!calc.isEnabled())
@@ -370,7 +384,7 @@ public class CalculationChooser extends JPanel
    */
   protected JComboBox<String> buildModelOptionsList()
   {
-    final JComboBox<String> scoreModelsCombo = new JComboBox<String>();
+    final JComboBox<String> scoreModelsCombo = new JComboBox<>();
     scoreModelsCombo.setRenderer(renderer);
 
     /*
@@ -383,7 +397,8 @@ public class CalculationChooser extends JPanel
       @Override
       public void mouseEntered(MouseEvent e)
       {
-        scoreModelsCombo.setToolTipText(tips.get(scoreModelsCombo.getSelectedIndex()));
+        scoreModelsCombo.setToolTipText(
+                tips.get(scoreModelsCombo.getSelectedIndex()));
       }
 
       @Override
@@ -412,39 +427,41 @@ public class CalculationChooser extends JPanel
   {
     Object curSel = comboBox.getSelectedItem();
     toolTips.clear();
-    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
+    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
+    /*
+     * select the score models applicable to the alignment type
+     */
+    boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
+    List<ScoreModelI> models = getApplicableScoreModels(nucleotide,
+            pca.isSelected());
 
     /*
      * now we can actually add entries to the combobox,
      * remembering their descriptions for tooltips
      */
-    ScoreModels scoreModels = ScoreModels.getInstance();
     boolean selectedIsPresent = false;
-    for (ScoreModelI sm : scoreModels.getModels())
+    for (ScoreModelI sm : models)
     {
-      boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
-      if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
+      if (curSel != null && sm.getName().equals(curSel))
+      {
+        selectedIsPresent = true;
+        curSel = sm.getName();
+      }
+      model.addElement(sm.getName());
+
+      /*
+       * tooltip is description if provided, else text lookup with
+       * fallback on the model name
+       */
+      String tooltip = sm.getDescription();
+      if (tooltip == null)
       {
-        if (curSel != null && sm.getName().equals(curSel))
-        {
-          selectedIsPresent = true;
-          curSel = sm.getName();
-        }
-        model.addElement(sm.getName());
-
-        /*
-         * tooltip is description if provided, else text lookup with
-         * fallback on the model name
-         */
-        String tooltip = sm.getDescription();
-        if (tooltip == null)
-        {
-          tooltip = MessageManager.getStringOrReturn("label.score_model_",
-                  sm.getName());
-        }
-        toolTips.add(tooltip);
+        tooltip = MessageManager.getStringOrReturn("label.score_model_",
+                sm.getName());
       }
+      toolTips.add(tooltip);
     }
+
     if (selectedIsPresent)
     {
       model.setSelectedItem(curSel);
@@ -454,57 +471,145 @@ public class CalculationChooser extends JPanel
   }
 
   /**
+   * Builds a list of score models which are applicable for the alignment and
+   * calculation type (peptide or generic models for protein, nucleotide or
+   * generic models for nucleotide).
+   * <p>
+   * As a special case, includes BLOSUM62 as an extra option for nucleotide PCA.
+   * This is for backwards compatibility with Jalview prior to 2.8 when BLOSUM62
+   * was the only score matrix supported. This is included if property
+   * BLOSUM62_PCA_FOR_NUCLEOTIDE is set to true in the Jalview properties file.
+   * 
+   * @param nucleotide
+   * @param forPca
+   * @return
+   */
+  protected static List<ScoreModelI> getApplicableScoreModels(
+          boolean nucleotide, boolean forPca)
+  {
+    List<ScoreModelI> filtered = new ArrayList<>();
+
+    ScoreModels scoreModels = ScoreModels.getInstance();
+    for (ScoreModelI sm : scoreModels.getModels())
+    {
+      if (!nucleotide && sm.isProtein() || nucleotide && sm.isDNA())
+      {
+        filtered.add(sm);
+      }
+    }
+
+    /*
+     * special case: add BLOSUM62 as last option for nucleotide PCA, 
+     * for backwards compatibility with Jalview < 2.8 (JAL-2962)
+     */
+    if (nucleotide && forPca
+            && Cache.getDefault("BLOSUM62_PCA_FOR_NUCLEOTIDE", false))
+    {
+      filtered.add(scoreModels.getBlosum62());
+    }
+
+    return filtered;
+  }
+
+  /**
    * Open and calculate the selected tree or PCA on 'OK'
+   * 
+   * @throws IOException
    */
   protected void calculate_actionPerformed()
   {
     boolean doPCA = pca.isSelected();
-    String modelName = modelNames.getSelectedItem().toString();
+    String substitutionMatrix = modelNames.getSelectedItem().toString();
     SimilarityParamsI params = getSimilarityParameters(doPCA);
 
     if (doPCA)
     {
-      openPcaPanel(modelName, params);
+      openPcaPanel(substitutionMatrix, params);
     }
     else
     {
-      openTreePanel(modelName, params);
+      try
+      {
+        createTree(substitutionMatrix, params);
+      } catch (IOException e)
+      {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+      }
+
+
+
+
+
+
     }
 
     // closeFrame();
   }
 
+  protected void createTree(String substitutionMatrix,
+          SimilarityParamsI params) throws IOException
+  {
+    String treeAlgo = determineTreeAlgo();
+    TreeCalculator treeCalculator = new TreeCalculator(treeAlgo,
+            substitutionMatrix, params);
+    TreeBuilder calculatedTree = treeCalculator.makeTree(af.getViewport());
+
+    // AptxInit.createInstanceFromCalculation(calculatedTree);
+
+    TreeModel tree = new TreeModel(calculatedTree);
+    jalview.io.NewickFile newick = new jalview.io.NewickFile(
+            tree.getTopNode());
+    String output = newick.print(tree.hasBootstrap(), tree.hasDistances(),
+            tree.hasRootDistance());
+    AptxInit.createInstanceFromNhx(af.getTitle(), output, 
+            af.getViewport());
+    // openTreePanel(tree, treeAlgo, substitutionMatrix);
+  }
+
+
+  protected String determineTreeAlgo() // to be modified & expanded
+  {
+    String treeAlgorithm = neighbourJoining.isSelected()
+            ? TreeBuilder.NEIGHBOUR_JOINING
+            : TreeBuilder.AVERAGE_DISTANCE;
+
+    return treeAlgorithm;
+
+  }
+
+  protected void checkEnoughSequences(AlignViewport viewport)
+  {
+    SequenceGroup sg = viewport.getSelectionGroup();
+    if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
+    {
+      JvOptionPane.showMessageDialog(Desktop.desktop,
+              MessageManager.formatMessage(
+                      "label.you_need_at_least_n_sequences",
+                      MIN_TREE_SELECTION),
+              MessageManager.getString("label.not_enough_sequences"),
+              JvOptionPane.WARNING_MESSAGE);
+      return;
+    }
+  }
+
   /**
    * Open a new Tree panel on the desktop
    * 
-   * @param modelName
+   * @param tree
    * @param params
+   * @param treeAlgo
    */
-  protected void openTreePanel(String modelName, SimilarityParamsI params)
+  protected void openTreePanel(TreeModel tree, String treeAlgo,
+          String substitutionMatrix)
   {
     /*
      * gui validation shouldn't allow insufficient sequences here, but leave
      * this check in in case this method gets exposed programmatically in future
      */
-    AlignViewport viewport = af.getViewport();
-    SequenceGroup sg = viewport.getSelectionGroup();
-    if (sg != null && sg.getSize() < MIN_TREE_SELECTION)
-    {
-      JvOptionPane
-              .showMessageDialog(
-                      Desktop.desktop,
-                      MessageManager
-              .formatMessage("label.you_need_at_least_n_sequences",
-                      MIN_TREE_SELECTION),
-                      MessageManager
-                              .getString("label.not_enough_sequences"),
-                      JvOptionPane.WARNING_MESSAGE);
-      return;
-    }
+    checkEnoughSequences(af.getViewport());
 
-    String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
-            : TreeBuilder.AVERAGE_DISTANCE;
-    af.newTreePanel(treeType, modelName, params);
+    af.newTreePanel(tree, treeAlgo, substitutionMatrix);
   }
 
   /**
@@ -522,18 +627,26 @@ public class CalculationChooser extends JPanel
      * this check in in case this method gets exposed programmatically in future
      */
     if (((viewport.getSelectionGroup() != null)
-            && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION) && (viewport
-            .getSelectionGroup().getSize() > 0))
+            && (viewport.getSelectionGroup().getSize() < MIN_PCA_SELECTION)
+            && (viewport.getSelectionGroup().getSize() > 0))
             || (viewport.getAlignment().getHeight() < MIN_PCA_SELECTION))
     {
-      JvOptionPane.showInternalMessageDialog(this, MessageManager
-              .formatMessage("label.you_need_at_least_n_sequences",
-                      MIN_PCA_SELECTION), MessageManager
-              .getString("label.sequence_selection_insufficient"),
+      JvOptionPane.showInternalMessageDialog(this,
+              MessageManager.formatMessage(
+                      "label.you_need_at_least_n_sequences",
+                      MIN_PCA_SELECTION),
+              MessageManager
+                      .getString("label.sequence_selection_insufficient"),
               JvOptionPane.WARNING_MESSAGE);
       return;
     }
-    new PCAPanel(af.alignPanel, modelName, params);
+
+    /*
+     * construct the panel and kick off its calculation thread
+     */
+    pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
+    new Thread(pcaPanel).start();
+
   }
 
   /**
@@ -575,7 +688,8 @@ public class CalculationChooser extends JPanel
      */
     boolean matchGap = doPCA ? false : treeMatchGaps;
 
-    return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength);
+    return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
+            matchOnShortestLength);
   }
 
   /**
@@ -590,4 +704,9 @@ public class CalculationChooser extends JPanel
     {
     }
   }
+
+  public PCAPanel getPcaPanel()
+  {
+    return pcaPanel;
+  }
 }