Merge branch 'features/JAL-2393customMatrices' into develop
[jalview.git] / src / jalview / analysis / PCA.java
index 9babaee..3ec7995 100755 (executable)
  */
 package jalview.analysis;
 
+import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.SimilarityParamsI;
+import jalview.datamodel.AlignmentView;
 import jalview.math.MatrixI;
-import jalview.schemes.ResidueProperties;
-import jalview.schemes.ScoreMatrix;
 
 import java.io.PrintStream;
 
@@ -31,8 +32,6 @@ import java.io.PrintStream;
  */
 public class PCA implements Runnable
 {
-  boolean jvCalcMode = true;
-
   MatrixI symm;
 
   double[] eigenvalue;
@@ -41,55 +40,19 @@ public class PCA implements Runnable
 
   StringBuilder details = new StringBuilder(1024);
 
-  private String[] seqs;
-
-  private ScoreMatrix scoreMatrix;
+  final private AlignmentView seqs;
 
-  /**
-   * Creates a new PCA object. By default, uses blosum62 matrix to generate
-   * sequence similarity matrices
-   * 
-   * @param s
-   *          Set of amino acid sequences to perform PCA on
-   */
-  public PCA(String[] s)
-  {
-    this(s, false);
-  }
-
-  /**
-   * Creates a new PCA object. By default, uses blosum62 matrix to generate
-   * sequence similarity matrices
-   * 
-   * @param s
-   *          Set of sequences to perform PCA on
-   * @param nucleotides
-   *          if true, uses standard DNA/RNA matrix for sequence similarity
-   *          calculation.
-   */
-  public PCA(String[] s, boolean nucleotides)
-  {
-    this(s, nucleotides, null);
-  }
+  private ScoreModelI scoreModel;
+  
+  private SimilarityParamsI similarityParams;
 
-  public PCA(String[] s, boolean nucleotides, String s_m)
+  public PCA(AlignmentView s, ScoreModelI sm, SimilarityParamsI options)
   {
     this.seqs = s;
-
-    scoreMatrix = null;
-    String sm = s_m;
-    if (sm != null)
-    {
-      scoreMatrix = ResidueProperties.getScoreMatrix(sm);
-    }
-    if (scoreMatrix == null)
-    {
-      // either we were given a non-existent score matrix or a scoremodel that
-      // isn't based on a pairwise symbol score matrix
-      scoreMatrix = ResidueProperties
-              .getScoreMatrix(sm = (nucleotides ? "DNA" : "BLOSUM62"));
-    }
-    details.append("PCA calculation using " + sm
+    this.similarityParams = options;
+    this.scoreModel = sm;
+    
+    details.append("PCA calculation using " + sm.getName()
             + " sequence similarity matrix\n========\n\n");
   }
 
@@ -206,11 +169,7 @@ public class PCA implements Runnable
     // long now = System.currentTimeMillis();
     try
     {
-      details.append("PCA Calculation Mode is "
-              + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
-              + "\n");
-
-      eigenvector = scoreMatrix.computePairwiseScores(seqs);
+      eigenvector = scoreModel.findSimilarities(seqs, similarityParams);
 
       details.append(" --- OrigT * Orig ---- \n");
       eigenvector.print(ps, "%8.2f");
@@ -252,11 +211,6 @@ public class PCA implements Runnable
     // + (System.currentTimeMillis() - now) + "ms"));
   }
 
-  public void setJvCalcMode(boolean calcMode)
-  {
-    this.jvCalcMode = calcMode;
-  }
-
   /**
    * Answers the N dimensions of the NxN PCA matrix. This is the number of
    * sequences involved in the pairwise score calculation.
@@ -266,6 +220,6 @@ public class PCA implements Runnable
   public int getHeight()
   {
     // TODO can any of seqs[] be null?
-    return seqs.length;
+    return seqs.getSequences().length;
   }
 }