JAL-2416 add score model description field for tooltip in PCA panel
[jalview.git] / src / jalview / gui / PCAPanel.java
index 288ef1f..885c3bf 100644 (file)
  */
 package jalview.gui;
 
+import jalview.analysis.scoremodels.ScoreModels;
+import jalview.analysis.scoremodels.SimilarityParams;
+import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.SimilarityParamsI;
+import jalview.api.analysis.ViewBasedAnalysisI;
+import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentView;
@@ -27,7 +33,6 @@ import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.SeqCigar;
 import jalview.datamodel.SequenceI;
 import jalview.jbgui.GPCAPanel;
-import jalview.schemes.ResidueProperties;
 import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.viewmodel.PCAModel;
@@ -47,6 +52,8 @@ import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JColorChooser;
 import javax.swing.JMenuItem;
 import javax.swing.JRadioButtonMenuItem;
+import javax.swing.event.InternalFrameAdapter;
+import javax.swing.event.InternalFrameEvent;
 
 /**
  * DOCUMENT ME!
@@ -71,25 +78,47 @@ public class PCAPanel extends GPCAPanel implements Runnable,
   int top = 0;
 
   /**
-   * Creates a new PCAPanel object.
+   * Creates a new PCAPanel object using default score model and parameters
    * 
-   * @param av
-   *          DOCUMENT ME!
-   * @param s
-   *          DOCUMENT ME!
+   * @param alignPanel
    */
-  public PCAPanel(AlignmentPanel ap)
+  public PCAPanel(AlignmentPanel alignPanel)
   {
-    this.av = ap.av;
-    this.ap = ap;
+    this(alignPanel, ScoreModels.getInstance().getDefaultModel(
+            !alignPanel.av.getAlignment().isNucleotide()),
+            SimilarityParams.SeqSpace);
+  }
+
+  /**
+   * Constructor given sequence data, a similarity (or distance) score model,
+   * and score calculation parameters
+   * 
+   * @param alignPanel
+   * @param scoreModel
+   * @param params
+   */
+  public PCAPanel(AlignmentPanel alignPanel, ScoreModelI scoreModel,
+          SimilarityParamsI params)
+  {
+    super();
+    this.av = alignPanel.av;
+    this.ap = alignPanel;
+    boolean nucleotide = av.getAlignment().isNucleotide();
 
     progressBar = new ProgressBar(statusPanel, statusBar);
 
-    boolean sameLength = true;
+    addInternalFrameListener(new InternalFrameAdapter()
+    {
+      @Override
+      public void internalFrameClosed(InternalFrameEvent e)
+      {
+        close_actionPerformed();
+      }
+    });
+
     boolean selected = av.getSelectionGroup() != null
             && av.getSelectionGroup().getSize() > 0;
     AlignmentView seqstrings = av.getAlignmentView(selected);
-    boolean nucleotide = av.getAlignment().isNucleotide();
     SequenceI[] seqs;
     if (!selected)
     {
@@ -99,19 +128,10 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     {
       seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
     }
-    SeqCigar sq[] = seqstrings.getSequences();
-    int length = sq[0].getWidth();
-
-    for (int i = 0; i < seqs.length; i++)
-    {
-      if (sq[i].getWidth() != length)
-      {
-        sameLength = false;
-        break;
-      }
-    }
 
-    if (!sameLength)
+    // TODO can we allow PCA on unaligned data given choice of
+    // similarity measure parameters?
+    if (!checkAligned(seqstrings))
     {
       JvOptionPane.showMessageDialog(Desktop.desktop,
               MessageManager.getString("label.pca_sequences_not_aligned"),
@@ -120,49 +140,93 @@ public class PCAPanel extends GPCAPanel implements Runnable,
 
       return;
     }
-    pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
+
+    pcaModel = new PCAModel(seqstrings, seqs, nucleotide, scoreModel,
+            params);
     PaintRefresher.Register(this, av.getSequenceSetId());
 
-    rc = new RotatableCanvas(ap);
+    rc = new RotatableCanvas(alignPanel);
     this.getContentPane().add(rc, BorderLayout.CENTER);
     Thread worker = new Thread(this);
     worker.start();
   }
 
-  @Override
-  protected void scoreMatrix_menuSelected()
+  /**
+   * Answers true if all sequences have the same aligned length, else false
+   * 
+   * @param seqstrings
+   * @return
+   */
+  protected boolean checkAligned(AlignmentView seqstrings)
   {
-    scoreMatrixMenu.removeAll();
-    for (final String sm : ResidueProperties.scoreMatrices.keySet())
+    SeqCigar sq[] = seqstrings.getSequences();
+    int length = sq[0].getWidth();
+    boolean sameLength = true;
+    for (int i = 0; i < sq.length; i++)
     {
-      if (ResidueProperties.getScoreMatrix(sm) != null)
+      if (sq[i].getWidth() != length)
       {
-        // create an entry for this score matrix for use in PCA
-        JCheckBoxMenuItem jm = new JCheckBoxMenuItem();
-        jm.setText(MessageManager.getStringOrReturn("label.score_model_",
-                sm));
-        jm.setSelected(pcaModel.getScore_matrix().equals(sm));
-        if ((ResidueProperties.scoreMatrices.get(sm).isDNA() && ResidueProperties.scoreMatrices
-                .get(sm).isProtein())
-                || pcaModel.isNucleotide() == ResidueProperties.scoreMatrices
-                        .get(sm).isDNA())
+        sameLength = false;
+        break;
+      }
+    }
+    return sameLength;
+  }
+
+  /**
+   * Ensure references to potentially very large objects (the PCA matrices) are
+   * nulled when the frame is closed
+   */
+  protected void close_actionPerformed()
+  {
+    pcaModel = null;
+  }
+
+  /**
+   * Repopulate the options and actions under the score model menu when it is
+   * selected. Options will depend on whether 'nucleotide' or 'peptide'
+   * modelling is selected (and also possibly on whether any additional score
+   * models have been added).
+   */
+  @Override
+  protected void scoreModel_menuSelected()
+  {
+    scoreModelMenu.removeAll();
+    for (final ScoreModelI sm : ScoreModels.getInstance().getModels())
+    {
+      final String name = sm.getName();
+      JCheckBoxMenuItem jm = new JCheckBoxMenuItem(name);
+
+      /*
+       * if the score model doesn't provide a description, try to look one
+       * up in the text bundle, falling back on its name
+       */
+      String tooltip = sm.getDescription();
+      if (tooltip == null)
+      {
+        tooltip = MessageManager.getStringOrReturn("label.score_model_",
+                name);
+      }
+      jm.setToolTipText(tooltip);
+      jm.setSelected(pcaModel.getScoreModelName().equals(name));
+      if ((pcaModel.isNucleotide() && sm.isDNA())
+              || (!pcaModel.isNucleotide() && sm.isProtein()))
+      {
+        jm.addActionListener(new ActionListener()
         {
-          final PCAPanel us = this;
-          jm.addActionListener(new ActionListener()
+          @Override
+          public void actionPerformed(ActionEvent e)
           {
-            @Override
-            public void actionPerformed(ActionEvent e)
+            if (!pcaModel.getScoreModelName().equals(name))
             {
-              if (!pcaModel.getScore_matrix().equals(sm))
-              {
-                pcaModel.setScore_matrix(sm);
-                Thread worker = new Thread(us);
-                worker.start();
-              }
+              ScoreModelI sm2 = configureScoreModel(sm);
+              pcaModel.setScoreModel(sm2);
+              Thread worker = new Thread(PCAPanel.this);
+              worker.start();
             }
-          });
-          scoreMatrixMenu.add(jm);
-        }
+          }
+        });
+        scoreModelMenu.add(jm);
       }
     }
   }
@@ -236,7 +300,8 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     if (!pcaModel.isNucleotide())
     {
       pcaModel.setNucleotide(true);
-      pcaModel.setScore_matrix("DNA");
+      pcaModel.setScoreModel(ScoreModels.getInstance().getDefaultModel(
+              false));
       Thread worker = new Thread(this);
       worker.start();
     }
@@ -250,7 +315,8 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     if (pcaModel.isNucleotide())
     {
       pcaModel.setNucleotide(false);
-      pcaModel.setScore_matrix("BLOSUM62");
+      pcaModel.setScoreModel(ScoreModels.getInstance()
+              .getDefaultModel(true));
       Thread worker = new Thread(this);
       worker.start();
     }
@@ -754,4 +820,28 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     top = t;
     zCombobox.setSelectedIndex(2);
   }
+
+  /**
+   * If the score model is one that requires to get state data from the current
+   * view, allow it to do so
+   * 
+   * @param sm
+   * @return
+   */
+  protected ScoreModelI configureScoreModel(ScoreModelI sm)
+  {
+    if (sm instanceof ViewBasedAnalysisI)
+    {
+      try
+      {
+        sm = sm.getClass().newInstance();
+        ((ViewBasedAnalysisI) sm).configureFromAlignmentView(ap);
+      } catch (Exception q)
+      {
+        Cache.log.error("Couldn't create a scoremodel instance for "
+                + sm.getName());
+      }
+    }
+    return sm;
+  }
 }