JAL-3269 JAL-3370 JalviewJS interface upgrade
[jalview.git] / src / jalview / gui / CalculationChooser.java
index 886762a..13506a5 100644 (file)
@@ -25,6 +25,7 @@ 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.util.MessageManager;
 
@@ -61,8 +62,13 @@ import javax.swing.event.InternalFrameAdapter;
 import javax.swing.event.InternalFrameEvent;
 
 /**
- * A dialog where a user can choose and action Tree or PCA calculation options
+ * A dialog where a user can choose and action Tree or PCA calculation options.
+ * 
+ * Allows also for dialog-free static methods openPCAPanel(...) and
+ * openTreePanel(...) for scripted use.
+ * 
  */
+@SuppressWarnings("serial")
 public class CalculationChooser extends JPanel
 {
   /*
@@ -73,7 +79,7 @@ public class CalculationChooser extends JPanel
    */
   private static boolean treeMatchGaps = true;
 
-  private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
+  private static Font VERDANA_11PT;
 
   private static final int MIN_TREE_SELECTION = 3;
 
@@ -101,9 +107,9 @@ public class CalculationChooser extends JPanel
 
   private JCheckBox shorterSequence;
 
-  final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
+  private static ComboBoxTooltipRenderer renderer; // BH was not static
 
-  List<String> tips = new ArrayList<String>();
+  List<String> tips = new ArrayList<>();
 
   /*
    * the most recently opened PCA results panel
@@ -111,6 +117,37 @@ public class CalculationChooser extends JPanel
   private PCAPanel pcaPanel;
 
   /**
+   * Open a new Tree panel on the desktop statically. Params are standard (not
+   * set by Groovy). No dialog is opened.
+   * 
+   * @param af
+   * @param treeType
+   * @param modelName
+   * @return null if successful; the string
+   *         "label.you_need_at_least_n_sequences" if number of sequences
+   *         selected is inappropriate
+   */
+  public static Object openTreePanel(AlignFrame af, String treeType,
+          String modelName)
+  {
+    return openTreePanel(af, treeType, modelName, null);
+  }
+
+  /**
+   * public static method for JalviewJS API to open a PCAPanel without
+   * necessarily using a dialog.
+   * 
+   * @param af
+   * @param modelName
+   * @return the PCAPanel, or the string "label.you_need_at_least_n_sequences"
+   *         if number of sequences selected is inappropriate
+   */
+  public static Object openPcaPanel(AlignFrame af, String modelName)
+  {
+    return openPcaPanel(af, modelName, null);
+  }
+
+  /**
    * Constructor
    * 
    * @param af
@@ -155,12 +192,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);
@@ -169,7 +209,7 @@ public class CalculationChooser extends JPanel
     JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
     treePanel.setOpaque(false);
 
-    JvSwingUtils.createItalicTitledBorder(treePanel,
+    JvSwingUtils.createTitledBorder(treePanel,
             MessageManager.getString("label.tree"), true);
 
     // then copy the inset dimensions for the border-less PCA panel
@@ -228,6 +268,10 @@ public class CalculationChooser extends JPanel
     paramsPanel.add(includeGappedColumns);
     paramsPanel.add(shorterSequence);
 
+    if (VERDANA_11PT == null)
+    {
+      VERDANA_11PT = new Font("Verdana", 0, 11);
+    }
     /*
      * OK / Cancel buttons
      */
@@ -270,9 +314,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);
@@ -290,6 +334,7 @@ public class CalculationChooser extends JPanel
       };
     });
 
+    validateCalcTypes();
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
   }
 
@@ -375,7 +420,11 @@ public class CalculationChooser extends JPanel
    */
   protected JComboBox<String> buildModelOptionsList()
   {
-    final JComboBox<String> scoreModelsCombo = new JComboBox<String>();
+    JComboBox<String> scoreModelsCombo = new JComboBox<>();
+    if (renderer == null)
+    {
+      renderer = new ComboBoxTooltipRenderer();
+    }
     scoreModelsCombo.setRenderer(renderer);
 
     /*
@@ -418,39 +467,42 @@ 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))
       {
-        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);
+        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);
     }
+
     if (selectedIsPresent)
     {
       model.setSelectedItem(curSel);
@@ -460,6 +512,48 @@ 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(Preferences.BLOSUM62_PCA_FOR_NUCLEOTIDE,
+                    false))
+    {
+      filtered.add(scoreModels.getBlosum62());
+    }
+
+    return filtered;
+  }
+
+  /**
    * Open and calculate the selected tree or PCA on 'OK'
    */
   protected void calculate_actionPerformed()
@@ -488,6 +582,63 @@ public class CalculationChooser extends JPanel
    */
   protected void openTreePanel(String modelName, SimilarityParamsI params)
   {
+    Object ret = openTreePanel(af,
+            neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING
+                    : TreeBuilder.AVERAGE_DISTANCE,
+            modelName, params);
+    if (ret instanceof String)
+    {
+      JvOptionPane.showMessageDialog(this, // was opening on Desktop?
+              MessageManager.formatMessage(
+                      (String) ret,
+                      MIN_TREE_SELECTION),
+              MessageManager.getString("label.not_enough_sequences"),
+              JvOptionPane.WARNING_MESSAGE);
+
+    }
+  }
+
+  /**
+   * Open a new PCA panel on the desktop
+   * 
+   * @param modelName
+   * @param params
+   */
+  protected void openPcaPanel(String modelName, SimilarityParamsI params)
+  {
+    Object ret = openPcaPanel(af, modelName, params);
+    if (ret instanceof String)
+    {
+      JvOptionPane.showInternalMessageDialog(this,
+              MessageManager.formatMessage(
+                      (String) ret,
+                      MIN_PCA_SELECTION),
+              MessageManager
+                      .getString("label.sequence_selection_insufficient"),
+              JvOptionPane.WARNING_MESSAGE);
+    }
+    else
+    {
+      // only used for test suite
+      pcaPanel = (PCAPanel) ret;
+    }
+
+  }
+
+  /**
+   * Open a new Tree panel on the desktop statically
+   * 
+   * @param af
+   * @param treeType
+   * @param modelName
+   * @param params
+   * @return null, or the string "label.you_need_at_least_n_sequences" if number
+   *         of sequences selected is inappropriate
+   */
+  public static Object openTreePanel(AlignFrame af, String treeType,
+          String modelName, SimilarityParamsI params)
+  {
+
     /*
      * gui validation shouldn't allow insufficient sequences here, but leave
      * this check in in case this method gets exposed programmatically in future
@@ -496,50 +647,58 @@ public class CalculationChooser extends JPanel
     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;
+      return "label.you_need_at_least_n_sequences";
+    }
+
+    if (params == null)
+    {
+      params = getSimilarityParameters(false);
     }
 
-    String treeType = neighbourJoining.isSelected()
-            ? TreeBuilder.NEIGHBOUR_JOINING
-            : TreeBuilder.AVERAGE_DISTANCE;
     af.newTreePanel(treeType, modelName, params);
+    return null;
   }
 
   /**
-   * Open a new PCA panel on the desktop
+   * public static method for JalviewJS API
    * 
+   * @param af
    * @param modelName
    * @param params
+   * @return the PCAPanel, or null if number of sequences selected is
+   *         inappropriate
    */
-  protected void openPcaPanel(String modelName, SimilarityParamsI params)
+  public static Object openPcaPanel(AlignFrame af, String modelName,
+          SimilarityParamsI params)
   {
+
     AlignViewport viewport = af.getViewport();
 
     /*
      * gui validation shouldn't allow insufficient sequences here, but leave
      * 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.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.WARNING_MESSAGE);
-      return;
+      return "label.you_need_at_least_n_sequences";
+    }
+
+    if (params == null)
+    {
+      params = getSimilarityParameters(true);
     }
-    pcaPanel = new PCAPanel(af.alignPanel, modelName, params);
+
+    /*
+     * construct the panel and kick off its calculation thread
+     */
+    PCAPanel pcap = new PCAPanel(af.alignPanel, modelName, params);
+    new Thread(pcap).start();
+    return pcap;
   }
 
   /**
@@ -555,6 +714,7 @@ public class CalculationChooser extends JPanel
     }
   }
 
+
   /**
    * Returns a data bean holding parameters for similarity (or distance) model
    * calculation
@@ -562,7 +722,8 @@ public class CalculationChooser extends JPanel
    * @param doPCA
    * @return
    */
-  protected SimilarityParamsI getSimilarityParameters(boolean doPCA)
+  public static SimilarityParamsI getSimilarityParameters(
+          boolean doPCA)
   {
     // commented out: parameter choices read from gui widgets
     // SimilarityParamsI params = new SimilarityParams(
@@ -583,6 +744,7 @@ public class CalculationChooser extends JPanel
 
     return new SimilarityParams(includeGapGap, matchGap, includeGapResidue,
             matchOnShortestLength);
+
   }
 
   /**
@@ -600,6 +762,7 @@ public class CalculationChooser extends JPanel
 
   public PCAPanel getPcaPanel()
   {
+    // only called for FreeUpMemoryTest
     return pcaPanel;
   }
 }