Merge branch 'features/JAL-1767pcaInProject' into bug/JAL-3171_maintain_datasets_acro...
[jalview.git] / src / jalview / gui / PCAPanel.java
index f5e33cd..da1dffe 100644 (file)
@@ -23,12 +23,14 @@ package jalview.gui;
 import jalview.analysis.scoremodels.ScoreModels;
 import jalview.api.analysis.ScoreModelI;
 import jalview.api.analysis.SimilarityParamsI;
+import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.SequenceI;
 import jalview.jbgui.GPCAPanel;
+import jalview.math.RotatableMatrix.Axis;
 import jalview.util.ImageMaker;
 import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
@@ -53,11 +55,15 @@ import javax.swing.event.InternalFrameAdapter;
 import javax.swing.event.InternalFrameEvent;
 
 /**
- * The panel holding the Principal Component Analysis 3-d visualisation
+ * The panel holding the Principal Component Analysis 3-D visualisation
  */
 public class PCAPanel extends GPCAPanel
         implements Runnable, IProgressIndicator
 {
+  private static final int MIN_WIDTH = 470;
+
+  private static final int MIN_HEIGHT = 250;
+
   RotatableCanvas rc;
 
   AlignmentPanel ap;
@@ -68,10 +74,6 @@ public class PCAPanel extends GPCAPanel
 
   int top = 0;
 
-  private static final int MIN_WIDTH = 470;
-
-  private static final int MIN_HEIGHT = 250;
-
   private IProgressIndicator progressBar;
 
   private boolean working;
@@ -125,11 +127,10 @@ public class PCAPanel extends GPCAPanel
     rc = new RotatableCanvas(alignPanel);
     this.getContentPane().add(rc, BorderLayout.CENTER);
 
-    /*
-     * perform calculation in a new Thread
-     */
-    Thread worker = new Thread(this);
-    worker.start();
+    addKeyListener(rc);
+    validate();
+
+    this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
   }
 
   /**
@@ -161,6 +162,7 @@ public class PCAPanel extends GPCAPanel
   @Override
   public void run()
   {
+    working = true;
     long progId = System.currentTimeMillis();
     IProgressIndicator progress = this;
     String message = MessageManager.getString("label.pca_recalculating");
@@ -170,11 +172,10 @@ public class PCAPanel extends GPCAPanel
       message = MessageManager.getString("label.pca_calculating");
     }
     progress.setProgressBar(message, progId);
-    working = true;
     try
     {
       pcaModel.calculate();
-      // ////////////////
+
       xCombobox.setSelectedIndex(0);
       yCombobox.setSelectedIndex(1);
       zCombobox.setSelectedIndex(2);
@@ -196,12 +197,10 @@ public class PCAPanel extends GPCAPanel
     repaint();
     if (getParent() == null)
     {
-      addKeyListener(rc);
       Desktop.addInternalFrame(this,
               MessageManager.formatMessage("label.calc_title", "PCA",
                       pcaModel.getScoreModelName()),
               475, 450);
-      this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
     }
     working = false;
   }
@@ -223,7 +222,30 @@ public class PCAPanel extends GPCAPanel
     int dim3 = top - zCombobox.getSelectedIndex();
     pcaModel.updateRcView(dim1, dim2, dim3);
     rc.resetView();
-    rc.paint(rc.getGraphics());
+  }
+
+  /**
+   * Sets the selected checkbox item index for PCA dimension (1, 2, 3...) for
+   * the given axis (X/Y/Z)
+   * 
+   * @param index
+   * @param axis
+   */
+  public void setSelectedDimensionIndex(int index, Axis axis)
+  {
+    switch (axis)
+    {
+    case X:
+      xCombobox.setSelectedIndex(index);
+      break;
+    case Y:
+      yCombobox.setSelectedIndex(index);
+      break;
+    case Z:
+      zCombobox.setSelectedIndex(index);
+      break;
+    default:
+    }
   }
 
   @Override
@@ -255,20 +277,23 @@ public class PCAPanel extends GPCAPanel
     printer.start();
   }
 
+  /**
+   * If available, shows the data which formed the inputs for the PCA as a new
+   * alignment
+   */
   @Override
   public void originalSeqData_actionPerformed()
   {
-    // this was cut'n'pasted from the equivalent TreePanel method - we should
-    // make this an abstract function of all jalview analysis windows
-    if (pcaModel.getSeqtrings() == null)
+    // JAL-2647 disabled after load from project (until save to project done)
+    if (pcaModel.getInputData() == null)
     {
-      jalview.bin.Cache.log.info(
+      Cache.log.info(
               "Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
       return;
     }
     // decide if av alignment is sufficiently different to original data to
     // warrant a new window to be created
-    // create new alignmnt window with hidden regions (unhiding hidden regions
+    // create new alignment window with hidden regions (unhiding hidden regions
     // yields unaligned seqs)
     // or create a selection box around columns in alignment view
     // test Alignment(SeqCigar[])
@@ -282,7 +307,7 @@ public class PCAPanel extends GPCAPanel
     {
     }
 
-    Object[] alAndColsel = pcaModel.getSeqtrings()
+    Object[] alAndColsel = pcaModel.getInputData()
             .getAlignmentAndHiddenColumns(gc);
 
     if (alAndColsel != null && alAndColsel[0] != null)
@@ -364,7 +389,7 @@ public class PCAPanel extends GPCAPanel
     {
       pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
 
-      rc.drawBackground(pg, rc.bgColour);
+      rc.drawBackground(pg);
       rc.drawScene(pg);
       if (rc.drawAxes)
       {
@@ -407,20 +432,19 @@ public class PCAPanel extends GPCAPanel
 
     ImageMaker im;
 
-    if (type == ImageMaker.TYPE.PNG)
+    switch (type)
     {
+    case PNG:
       im = new ImageMaker(this, ImageMaker.TYPE.PNG,
               "Make PNG image from PCA", width, height, null, null, null, 0,
               false);
-    }
-    else if (type == jalview.util.ImageMaker.TYPE.EPS)
-    {
+      break;
+    case EPS:
       im = new ImageMaker(this, ImageMaker.TYPE.EPS,
               "Make EPS file from PCA", width, height, null,
               this.getTitle(), null, 0, false);
-    }
-    else
-    {
+      break;
+    default:
       im = new ImageMaker(this, ImageMaker.TYPE.SVG,
               "Make SVG file from PCA", width, height, null,
               this.getTitle(), null, 0, false);
@@ -428,7 +452,7 @@ public class PCAPanel extends GPCAPanel
 
     if (im.getGraphics() != null)
     {
-      rc.drawBackground(im.getGraphics(), Color.black);
+      rc.drawBackground(im.getGraphics());
       rc.drawScene(im.getGraphics());
       if (rc.drawAxes)
       {
@@ -474,8 +498,8 @@ public class PCAPanel extends GPCAPanel
 
     for (int i = 0; i < iSize; i++)
     {
-      final AlignmentPanel panel = aps[i];
-      item = new JRadioButtonMenuItem(panel.av.viewName, panel.av == rc.av);
+      final AlignmentPanel ap = aps[i];
+      item = new JRadioButtonMenuItem(ap.av.getViewName(), ap.av == rc.av);
       buttonGroup.add(item);
       item.addActionListener(new ActionListener()
       {
@@ -679,4 +703,42 @@ public class PCAPanel extends GPCAPanel
   {
     return working;
   }
+
+  /**
+   * Answers the selected checkbox item index for PCA dimension for the X, Y or
+   * Z axis of the display
+   * 
+   * @param axis
+   * @return
+   */
+  public int getSelectedDimensionIndex(Axis axis)
+  {
+    switch (axis)
+    {
+    case X:
+      return xCombobox.getSelectedIndex();
+    case Y:
+      return yCombobox.getSelectedIndex();
+    default:
+      return zCombobox.getSelectedIndex();
+    }
+  }
+
+  public void setShowLabels(boolean show)
+  {
+    showLabels.setSelected(show);
+  }
+
+  /**
+   * Sets the input data used to calculate the PCA. This is provided for
+   * 'restore from project', which does not currently support this (AL-2647), so
+   * sets the value to null, and hides the menu option for "Input Data...". J
+   * 
+   * @param data
+   */
+  public void setInputData(AlignmentView data)
+  {
+    pcaModel.setInputData(data);
+    originalSeqData.setVisible(data != null);
+  }
 }