JAL-535 - refactored core PCA viewer code to viewmodel
authorjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 15 Jun 2012 14:59:40 +0000 (15:59 +0100)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 15 Jun 2012 15:02:37 +0000 (16:02 +0100)
JAL-1013 - port protein/nucleotide settings to applet PCA viewer

src/jalview/appletgui/PCAPanel.java

index 37b4281..8c87d7c 100755 (executable)
@@ -24,28 +24,18 @@ import java.awt.event.*;
 
 import jalview.analysis.*;
 import jalview.datamodel.*;
+import jalview.viewmodel.PCAModel;
 
 public class PCAPanel extends EmbmenuFrame implements Runnable,
         ActionListener, ItemListener
 {
-  PCA pca;
-
-  int top;
-
   RotatableCanvas rc;
 
   AlignViewport av;
 
-  AlignmentView seqstrings;
-
-  SequenceI[] seqs;
-
-  
-  /**
-   * use the identity matrix for calculating similarity between sequences. 
-   */
-  private boolean useidentity=false;
+  PCAModel pcaModel;
 
+  int top = 0;
 
   public PCAPanel(AlignViewport av)
   {
@@ -65,8 +55,10 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
     }
 
     this.av = av;
-    seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
-    useidentity=av.getAlignment().isNucleotide();
+    AlignmentView seqstrings = av
+            .getAlignmentView(av.getSelectionGroup() != null);
+    boolean nucleotide = av.getAlignment().isNucleotide();
+    SequenceI[] seqs;
     if (av.getSelectionGroup() == null)
     {
       seqs = av.getAlignment().getSequencesArray();
@@ -87,13 +79,14 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
         return;
       }
     }
+    pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
 
     rc = new RotatableCanvas(av);
     embedMenuIfNeeded(rc);
     add(rc, BorderLayout.CENTER);
 
     jalview.bin.JalviewLite.addFrame(this, "Principal component analysis",
-            400, 400);
+            475, 400);
 
     Thread worker = new Thread(this);
     worker.start();
@@ -104,46 +97,32 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
    */
   public void run()
   {
-    pca = new PCA(seqstrings.getSequenceStrings(' '), useidentity);
-    pca.run();
-
-    // Now find the component coordinates
-    int ii = 0;
-
-    while ((ii < seqs.length) && (seqs[ii] != null))
-    {
-      ii++;
-    }
-
-    double[][] comps = new double[ii][ii];
-
-    for (int i = 0; i < ii; i++)
+    // TODO progress indicator
+    calcSettings.setEnabled(false);
+    rc.setEnabled(false);
+    try
     {
-      if (pca.getEigenvalue(i) > 1e-4)
-      {
-        comps[i] = pca.component(i);
-      }
-    }
-
-    // ////////////////
-    xCombobox.select(0);
-    yCombobox.select(1);
-    zCombobox.select(2);
-
-    top = pca.getM().rows - 1;
-
-    Vector points = new Vector();
-    float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
-
-    for (int i = 0; i < pca.getM().rows; i++)
+      nuclSetting.setState(pcaModel.isNucleotide());
+      protSetting.setState(!pcaModel.isNucleotide());
+      pcaModel.run();
+      // ////////////////
+      xCombobox.select(0);
+      yCombobox.select(1);
+      zCombobox.select(2);
+
+      pcaModel.updateRc(rc);
+      // rc.invalidate();
+      top = pcaModel.getTop();
+    } catch (OutOfMemoryError x)
     {
-      SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
-      points.addElement(sp);
+      System.err.println("Out of memory when calculating PCA.");
+      return;
     }
+    calcSettings.setEnabled(true);
 
-    rc.setPoints(points, pca.getM().rows);
+    // TODO revert progress indicator
+    rc.setEnabled(true);
     rc.repaint();
-    seqs = null;
     this.repaint();
   }
 
@@ -153,17 +132,11 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
     {
       return;
     }
-
+    
     int dim1 = top - xCombobox.getSelectedIndex();
     int dim2 = top - yCombobox.getSelectedIndex();
     int dim3 = top - zCombobox.getSelectedIndex();
-
-    float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
-    for (int i = 0; i < pca.getM().rows; i++)
-    {
-      ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
-    }
-
+    pcaModel.updateRcView(dim1, dim2, dim3);
     rc.img = null;
     rc.rotmat.setIdentity();
     rc.initAxes();
@@ -176,7 +149,14 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
     {
       showOriginalData();
     }
-    else
+    if (evt.getSource() == resetButton)
+    {
+      xCombobox.select(0);
+      yCombobox.select(1);
+      zCombobox.select(2);
+      doDimensionChange();
+    }
+    if (evt.getSource() == values)
     {
       values_actionPerformed();
     }
@@ -196,6 +176,22 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
     {
       zCombobox_actionPerformed();
     }
+    else if (evt.getSource() == nuclSetting)
+    {
+      if (!pcaModel.isNucleotide())
+      {
+        pcaModel.setNucleotide(true);
+        new Thread(this).start();
+      }
+    }
+    else if (evt.getSource() == protSetting)
+    {
+      if (pcaModel.isNucleotide())
+      {
+        pcaModel.setNucleotide(false);
+        new Thread(this).start();
+      }
+    }
   }
 
   protected void xCombobox_actionPerformed()
@@ -221,7 +217,7 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
     frame.add(cap);
     jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500);
 
-    cap.setText(pca.getDetails());
+    cap.setText(pcaModel.getDetails());
   }
 
   void showOriginalData()
@@ -242,7 +238,8 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
     {
     }
     ;
-    Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
+    Object[] alAndColsel = pcaModel.getSeqtrings()
+            .getAlignmentAndColumnSelection(gc);
 
     if (alAndColsel != null && alAndColsel[0] != null)
     {
@@ -273,6 +270,8 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
 
   protected Choice zCombobox = new Choice();
 
+  protected Button resetButton = new Button();
+
   FlowLayout flowLayout1 = new FlowLayout();
 
   BorderLayout borderLayout1 = new BorderLayout();
@@ -283,8 +282,14 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
 
   Menu menu2 = new Menu();
 
+  Menu calcSettings = new Menu();
+
   protected CheckboxMenuItem labels = new CheckboxMenuItem();
 
+  protected CheckboxMenuItem protSetting = new CheckboxMenuItem();
+
+  protected CheckboxMenuItem nuclSetting = new CheckboxMenuItem();
+
   MenuItem values = new MenuItem();
 
   MenuItem inputData = new MenuItem();
@@ -306,14 +311,22 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
     yCombobox.addItemListener(this);
     xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
     xCombobox.addItemListener(this);
+    resetButton.setFont(new java.awt.Font("Verdana", 0, 12));
+    resetButton.setLabel("Reset");
+    resetButton.addActionListener(this);
     this.setMenuBar(menuBar1);
     menu1.setLabel("File");
     menu2.setLabel("View");
+    calcSettings.setLabel("Change Parameters");
     labels.setLabel("Labels");
     labels.addItemListener(this);
     values.setLabel("Output Values...");
     values.addActionListener(this);
     inputData.setLabel("Input Data...");
+    nuclSetting.setLabel("Nucleotide matrix");
+    nuclSetting.addItemListener(this);
+    protSetting.setLabel("Protein matrix");
+    protSetting.addItemListener(this);
     this.add(jPanel2, BorderLayout.SOUTH);
     jPanel2.add(jLabel1, null);
     jPanel2.add(xCombobox, null);
@@ -321,11 +334,15 @@ public class PCAPanel extends EmbmenuFrame implements Runnable,
     jPanel2.add(yCombobox, null);
     jPanel2.add(jLabel3, null);
     jPanel2.add(zCombobox, null);
+    jPanel2.add(resetButton, null);
     menuBar1.add(menu1);
     menuBar1.add(menu2);
+    menuBar1.add(calcSettings);
     menu2.add(labels);
     menu1.add(values);
     menu1.add(inputData);
+    calcSettings.add(nuclSetting);
+    calcSettings.add(protSetting);
     inputData.addActionListener(this);
   }