JAL-2416 use '-' not space for gap in score matrices
[jalview.git] / test / jalview / analysis / scoremodels / ScoreMatrixTest.java
index 374d308..5c699d1 100644 (file)
@@ -11,6 +11,7 @@ import jalview.io.DataSourceType;
 import jalview.io.FileParse;
 import jalview.io.ScoreMatrixFile;
 import jalview.math.MatrixI;
+import jalview.schemes.ResidueProperties;
 
 import java.io.IOException;
 import java.net.MalformedURLException;
@@ -147,10 +148,10 @@ public class ScoreMatrixTest
     assertEquals(sm.getMatrixIndex('D'), 3);
     assertEquals(sm.getMatrixIndex('X'), 22);
     assertEquals(sm.getMatrixIndex('x'), 22);
-    assertEquals(sm.getMatrixIndex(' '), 23);
+    assertEquals(sm.getMatrixIndex('-'), 23);
     assertEquals(sm.getMatrixIndex('*'), 24);
     assertEquals(sm.getMatrixIndex('.'), -1);
-    assertEquals(sm.getMatrixIndex('-'), -1);
+    assertEquals(sm.getMatrixIndex(' '), -1);
     assertEquals(sm.getMatrixIndex('?'), -1);
     assertEquals(sm.getMatrixIndex((char) 128), -1);
   }
@@ -166,10 +167,9 @@ public class ScoreMatrixTest
   public void testComputePairwiseScores()
   {
     /*
-     * NB score matrix assumes space for gap - Jalview converts
-     * space to gap before computing PCA or Tree
+     * NB score matrix expects '-' for gap
      */
-    String[] seqs = new String[] { "FKL", "R D", "QIA", "GWC" };
+    String[] seqs = new String[] { "FKL", "R-D", "QIA", "GWC" };
     ScoreMatrix sm = ScoreModels.getInstance().getBlosum62();
 
     MatrixI pairwise = sm.findSimilarities(seqs, SimilarityParams.Jalview);
@@ -252,16 +252,11 @@ public class ScoreMatrixTest
   @Test(groups = "Functional")
   public void testcomputeSimilarity_matchLongestSequence()
   {
-    // TODO params.matchGaps() is not used for ScoreMatrix
-    // - includeGaps is sufficient (there is no denominator)
-    // ==> bespoke parameters only 3 booleans?
     /*
-     * for now, using space for gap to match callers of
-     * AlignmentView.getSequenceStrings()
-     * may change this to '-' (with corresponding change to matrices)
+     * ScoreMatrix expects '-' for gaps
      */
-    String s1 = "FR K S";
-    String s2 = "FS  L";
+    String s1 = "FR-K-S";
+    String s2 = "FS--L";
     ScoreMatrix blosum = ScoreModels.getInstance().getBlosum62();
   
     /*
@@ -317,16 +312,11 @@ public class ScoreMatrixTest
   @Test(groups = "Functional")
   public void testcomputeSimilarity_matchShortestSequence()
   {
-    // TODO params.matchGaps() is not used for ScoreMatrix
-    // - includeGaps is sufficient (there is no denominator)
-    // ==> bespoke parameters only 3 booleans?
     /*
-     * for now, using space for gap to match callers of
-     * AlignmentView.getSequenceStrings()
-     * may change this to '-' (with corresponding change to matrices)
+     * ScoreMatrix expects '-' for gaps
      */
-    String s1 = "FR K S";
-    String s2 = "FS  L";
+    String s1 = "FR-K-S";
+    String s2 = "FS--L";
     ScoreMatrix blosum = ScoreModels.getInstance().getBlosum62();
 
     /*
@@ -374,4 +364,117 @@ public class ScoreMatrixTest
     params = new SimilarityParams(false, true, false, true);
     assertEquals(blosum.computeSimilarity(s1, s2, params), 5d);
   }
+
+  @Test(groups = "Functional")
+  public void testSymmetric()
+  {
+    verifySymmetric(ScoreModels.getInstance().getBlosum62());
+    verifySymmetric(ScoreModels.getInstance().getPam250());
+    verifySymmetric(ScoreModels.getInstance().getDefaultModel(false)); // dna
+  }
+
+  private void verifySymmetric(ScoreMatrix sm)
+  {
+    float[][] m = sm.getMatrix();
+    int rows = m.length;
+    for (int row = 0; row < rows; row++)
+    {
+      assertEquals(m[row].length, rows);
+      for (int col = 0; col < rows; col++)
+      {
+        assertEquals(m[row][col], m[col][row], String.format("%s [%s, %s]",
+                sm.getName(), ResidueProperties.aa[row],
+                ResidueProperties.aa[col]));
+      }
+    }
+  }
+
+  /**
+   * A test that just asserts the expected values in the Blosum62 score matrix
+   */
+  @Test(groups = "Functional")
+  public void testBlosum62_values()
+  {
+    ScoreMatrix sm = ScoreModels.getInstance().getBlosum62();
+
+    /*
+     * verify expected scores against ARNDCQEGHILKMFPSTWYVBZX
+     * scraped from https://www.ncbi.nlm.nih.gov/Class/FieldGuide/BLOSUM62.txt
+     */
+    verifyValues(sm, 'A', new float[] { 4, -1, -2, -2, 0, -1, -1, 0, -2,
+        -1,
+        -1, -1, -1, -2, -1, 1, 0, -3, -2, 0, -2, -1, 0 });
+    verifyValues(sm, 'R', new float[] { -1, 5, 0, -2, -3, 1, 0, -2, 0, -3,
+        -2, 2, -1, -3, -2, -1, -1, -3, -2, -3, -1, 0, -1 });
+    verifyValues(sm, 'N', new float[] { -2, 0, 6, 1, -3, 0, 0, 0, 1, -3,
+        -3,
+        0, -2, -3, -2, 1, 0, -4, -2, -3, 3, 0, -1 });
+    verifyValues(sm, 'D', new float[] { -2, -2, 1, 6, -3, 0, 2, -1, -1, -3,
+        -4, -1, -3, -3, -1, 0, -1, -4, -3, -3, 4, 1, -1 });
+    verifyValues(sm, 'C', new float[] { 0, -3, -3, -3, 9, -3, -4, -3, -3,
+        -1,
+        -1, -3, -1, -2, -3, -1, -1, -2, -2, -1, -3, -3, -2 });
+    verifyValues(sm, 'Q', new float[] { -1, 1, 0, 0, -3, 5, 2, -2, 0, -3,
+        -2,
+        1, 0, -3, -1, 0, -1, -2, -1, -2, 0, 3, -1 });
+    verifyValues(sm, 'E', new float[] { -1, 0, 0, 2, -4, 2, 5, -2, 0, -3,
+        -3,
+        1, -2, -3, -1, 0, -1, -3, -2, -2, 1, 4, -1 });
+    verifyValues(sm, 'G', new float[] { 0, -2, 0, -1, -3, -2, -2, 6, -2,
+        -4,
+        -4, -2, -3, -3, -2, 0, -2, -2, -3, -3, -1, -2, -1 });
+    verifyValues(sm, 'H', new float[] { -2, 0, 1, -1, -3, 0, 0, -2, 8, -3,
+        -3, -1, -2, -1, -2, -1, -2, -2, 2, -3, 0, 0, -1 });
+    verifyValues(sm, 'I', new float[] { -1, -3, -3, -3, -1, -3, -3, -4, -3,
+        4, 2, -3, 1, 0, -3, -2, -1, -3, -1, 3, -3, -3, -1 });
+    verifyValues(sm, 'L', new float[] { -1, -2, -3, -4, -1, -2, -3, -4, -3,
+        2, 4, -2, 2, 0, -3, -2, -1, -2, -1, 1, -4, -3, -1 });
+    verifyValues(sm, 'K', new float[] { -1, 2, 0, -1, -3, 1, 1, -2, -1, -3,
+        -2, 5, -1, -3, -1, 0, -1, -3, -2, -2, 0, 1, -1 });
+    verifyValues(sm, 'M', new float[] { -1, -1, -2, -3, -1, 0, -2, -3, -2,
+        1,
+        2, -1, 5, 0, -2, -1, -1, -1, -1, 1, -3, -1, -1 });
+    verifyValues(sm, 'F', new float[] { -2, -3, -3, -3, -2, -3, -3, -3, -1,
+        0, 0, -3, 0, 6, -4, -2, -2, 1, 3, -1, -3, -3, -1 });
+    verifyValues(sm, 'P', new float[] { -1, -2, -2, -1, -3, -1, -1, -2, -2,
+        -3, -3, -1, -2, -4, 7, -1, -1, -4, -3, -2, -2, -1, -2 });
+    verifyValues(sm, 'S', new float[] { 1, -1, 1, 0, -1, 0, 0, 0, -1, -2,
+        -2,
+        0, -1, -2, -1, 4, 1, -3, -2, -2, 0, 0, 0 });
+    verifyValues(sm, 'T', new float[] { 0, -1, 0, -1, -1, -1, -1, -2, -2,
+        -1,
+        -1, -1, -1, -2, -1, 1, 5, -2, -2, 0, -1, -1, 0 });
+    verifyValues(sm, 'W', new float[] { -3, -3, -4, -4, -2, -2, -3, -2, -2,
+        -3, -2, -3, -1, 1, -4, -3, -2, 11, 2, -3, -4, -3, -2 });
+    verifyValues(sm, 'Y', new float[] { -2, -2, -2, -3, -2, -1, -2, -3, 2,
+        -1, -1, -2, -1, 3, -3, -2, -2, 2, 7, -1, -3, -2, -1 });
+    verifyValues(sm, 'V', new float[] { 0, -3, -3, -3, -1, -2, -2, -3, -3,
+        3,
+        1, -2, 1, -1, -2, -2, 0, -3, -1, 4, -3, -2, -1 });
+    verifyValues(sm, 'B', new float[] { -2, -1, 3, 4, -3, 0, 1, -1, 0, -3,
+        -4, 0, -3, -3, -2, 0, -1, -4, -3, -3, 4, 1, -1 });
+    verifyValues(sm, 'Z', new float[] { -1, 0, 0, 1, -3, 3, 4, -2, 0, -3,
+        -3,
+        1, -1, -3, -1, 0, -1, -3, -2, -2, 1, 4, -1 });
+    verifyValues(sm, 'X', new float[] { 0, -1, -1, -1, -2, -1, -1, -1, -1,
+        -1, -1, -1, -1, -1, -2, 0, 0, -2, -1, -1, -1, -1, -1 });
+  }
+
+  /**
+   * Helper method to check pairwise scores for one residue
+   * 
+   * @param sm
+   * @param res
+   * @param expected
+   *          score values against 'res', in ResidueProperties.aaIndex order
+   */
+  private void verifyValues(ScoreMatrix sm, char res, float[] expected)
+  {
+    for (int j = 0; j < expected.length; j++)
+    {
+      char c2 = ResidueProperties.aa[j].charAt(0);
+      assertEquals(sm.getPairwiseScore(res, c2), expected[j],
+              String.format("%s->%s", res, c2));
+    }
+  }
 }