JAL-3205 faster calculation with symmetric score matrix
[jalview.git] / test / jalview / analysis / scoremodels / ScoreMatrixTest.java
index 1a5d43c..669c452 100644 (file)
@@ -22,6 +22,8 @@ import java.util.Arrays;
 
 import org.testng.annotations.Test;
 
+import junit.extensions.PA;
+
 public class ScoreMatrixTest
 {
   @Test(groups = "Functional")
@@ -33,6 +35,7 @@ public class ScoreMatrixTest
     scores[1] = new float[] { -4f, 5f, 6f };
     scores[2] = new float[] { 7f, 8f, 9f };
     ScoreMatrix sm = new ScoreMatrix("Test", "ABC".toCharArray(), scores);
+    assertFalse(sm.isSymmetric());
     assertEquals(sm.getSize(), 3);
     assertArrayEquals(scores, sm.getMatrix());
     assertEquals(sm.getPairwiseScore('A', 'a'), 1f);
@@ -585,4 +588,33 @@ public class ScoreMatrixTest
             + "</table>";
     assertEquals(html, expected);
   }
+
+  @Test(groups = "Functional")
+  public void testIsSymmetric()
+  {
+    float[][] scores = new float[2][];
+    scores[0] = new float[] { 1f, -2f };
+    scores[1] = new float[] { -2f, 1f };
+    ScoreMatrix sm = new ScoreMatrix("Test", "AB".toCharArray(), scores);
+    assertTrue(sm.isSymmetric());
+
+    scores[1] = new float[] { 2f, 1f };
+    sm = new ScoreMatrix("Test", "AB".toCharArray(), scores);
+    assertFalse(sm.isSymmetric());
+
+    /*
+     * verify that forcing an asymmetric matrix to use
+     * symmetric calculation gives a different (wrong) result
+     */
+    SimilarityParamsI params = new SimilarityParams(true, true, true,
+            false);
+    String[] seqs = new String[] { "AAABBBAA", "AABBABBA" };
+    MatrixI res1 = sm.findSimilarities(seqs, params);
+    MatrixI res2 = sm.findSimilarities(seqs, params);
+    assertTrue(res1.equals(res2));
+    PA.setValue(sm, "symmetric", true);
+    assertTrue(sm.isSymmetric()); // it's not true!
+    res2 = sm.findSimilarities(seqs, params);
+    assertFalse(res1.equals(res2));
+  }
 }