Merge branch 'JAL-1454_patch' into Release_2_8_1_Branch
authorJim Procter <jprocter@compbio.dundee.ac.uk>
Tue, 25 Feb 2014 16:44:48 +0000 (16:44 +0000)
committerJim Procter <jprocter@compbio.dundee.ac.uk>
Tue, 25 Feb 2014 16:44:48 +0000 (16:44 +0000)
src/jalview/analysis/PCA.java
src/jalview/math/Matrix.java

index 7a506cc..89c6353 100755 (executable)
@@ -237,6 +237,20 @@ public class PCA implements Runnable
    */
   public void run()
   {
+    PrintStream ps = new PrintStream(System.out)
+    {
+      public void print(String x)
+      {
+        details.append(x);
+      }
+
+      public void println()
+      {
+        details.append("\n");
+      }
+    };
+
+    try {
     details.append("PCA Calculation Mode is "
             + (jvCalcMode ? "Jalview variant" : "Original SeqSpace") + "\n");
     Matrix mt = m.transpose();
@@ -251,19 +265,6 @@ public class PCA implements Runnable
       eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace method
     }
 
-    PrintStream ps = new PrintStream(System.out)
-    {
-      public void print(String x)
-      {
-        details.append(x);
-      }
-
-      public void println()
-      {
-        details.append("\n");
-      }
-    };
-
     eigenvector.print(ps);
 
     symm = eigenvector.copy();
@@ -280,6 +281,12 @@ public class PCA implements Runnable
 
     // Now produce the diagonalization matrix
     eigenvector.tqli();
+    } catch (Exception q)
+    {
+      q.printStackTrace();
+      details.append("\n*** Unexpected exception when performing PCA ***\n"+q.getLocalizedMessage());
+      details.append("*** Matrices below may not be fully diagonalised. ***\n");
+    }
 
     details.append(" --- New diagonalization matrix ---\n");
     eigenvector.print(ps);
index 057bdbf..8d95203 100755 (executable)
@@ -48,6 +48,12 @@ public class Matrix
   public double[] e; // off diagonal
 
   /**
+   * maximum number of iterations for tqli 
+   * 
+   */
+  int maxIter = 45; // fudge - add 15 iterations, just in case
+
+  /**
    * Creates a new Matrix object.
    * 
    * @param value
@@ -342,11 +348,11 @@ public class Matrix
       }
     }
   }
-
+  
   /**
    * DOCUMENT ME!
    */
-  public void tqli()
+  public void tqli() throws Exception
   {
     int n = rows;
 
@@ -393,10 +399,9 @@ public class Matrix
         {
           iter++;
 
-          if (iter == 30)
+          if (iter == maxIter)
           {
-            System.err.print("Too many iterations in tqli");
-            System.exit(0); // JBPNote - should this really be here ???
+            throw new Exception("Too many iterations in tqli ("+maxIter+")");
           }
           else
           {
@@ -596,7 +601,7 @@ public class Matrix
   /**
    * DOCUMENT ME!
    */
-  public void tqli2()
+  public void tqli2() throws Exception
   {
     int n = rows;
 
@@ -643,10 +648,9 @@ public class Matrix
         {
           iter++;
 
-          if (iter == 30)
+          if (iter == maxIter)
           {
-            System.err.print("Too many iterations in tqli");
-            System.exit(0); // JBPNote - same as above - not a graceful exit!
+            throw new Exception ("Too many iterations in tqli2 (max is "+maxIter+")");
           }
           else
           {
@@ -780,7 +784,7 @@ public class Matrix
    * @param args
    *          DOCUMENT ME!
    */
-  public static void main(String[] args)
+  public static void main(String[] args) throws Exception
   {
     int n = Integer.parseInt(args[0]);
     double[][] in = new double[n][n];