JAL-2416 parse score matrices in either NCBI or AAindex format
[jalview.git] / test / jalview / io / ScoreMatrixFileTest.java
index 44bb8aa..cb30874 100644 (file)
@@ -3,6 +3,7 @@ package jalview.io;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
@@ -30,48 +31,34 @@ public class ScoreMatrixFileTest
      * or tab (or combinations) as score value delimiters
      * this example includes 'guide' symbols on score rows
      */
-    String data = "ScoreMatrix MyTest (example)\n" + "ATU tx-\n"
-            + "A,1.1,1.2,1.3,1.4, 1.5, 1.6, 1.7\n"
-            + "T,2.1 2.2 2.3 2.4 2.5 2.6 2.7\n"
-            + "U\t3.1\t3.2\t3.3\t3.4\t3.5\t3.6\t3.7\n"
-            + " 4.1 ,4.2,\t,4.3 ,\t4.4\t, \4.5,4.6 4.7\n"
-            + "t, 5.1,5.3,5.3,5.4,5.5, 5.6, 5.7\n"
-            + "x\t6.1, 6.2 6.3 6.4 6.5 6.6 6.7\n"
-            + "-, \t7.1\t7.2 7.3, 7.4, 7.5\t,7.6,7.7\n";
+    String data = "ScoreMatrix MyTest (example)\n" + "A\tT\tU\tt\tx\t-\n"
+            + "A,1.1,1.2,1.3,1.4, 1.5, 1.6\n"
+            + "T,2.1 2.2 2.3 2.4 2.5 2.6\n"
+            + "U\t3.1\t3.2\t3.3\t3.4\t3.5\t3.6\t\n"
+            + "t, 5.1,5.3,5.3,5.4,5.5, 5.6\n"
+            + "x\t6.1, 6.2 6.3 6.4 6.5 6.6\n"
+            + "-, \t7.1\t7.2 7.3, 7.4, 7.5\t,7.6\n";
     FileParse fp = new FileParse(data, DataSourceType.PASTE);
     ScoreMatrixFile parser = new ScoreMatrixFile(fp);
     ScoreMatrix sm = parser.parseMatrix();
 
     assertNotNull(sm);
     assertEquals(sm.getName(), "MyTest (example)");
+    assertEquals(sm.getSize(), 6);
+    assertNull(sm.getDescription());
     assertTrue(sm.isDNA());
     assertFalse(sm.isProtein());
     assertEquals(sm.getPairwiseScore('A', 'A'), 1.1f);
     assertEquals(sm.getPairwiseScore('A', 'T'), 1.2f);
     assertEquals(sm.getPairwiseScore('a', 'T'), 1.2f); // A/a equivalent
-    assertEquals(sm.getPairwiseScore('A', 't'), 1.5f); // T/t not equivalent
-    assertEquals(sm.getPairwiseScore('a', 't'), 1.5f);
-    assertEquals(sm.getPairwiseScore('T', ' '), 2.4f);
-    assertEquals(sm.getPairwiseScore('U', 'x'), 3.6f);
-    assertEquals(sm.getPairwiseScore('u', 'x'), 3.6f);
+    assertEquals(sm.getPairwiseScore('A', 't'), 1.4f); // T/t not equivalent
+    assertEquals(sm.getPairwiseScore('a', 't'), 1.4f);
+    assertEquals(sm.getPairwiseScore('U', 'x'), 3.5f);
+    assertEquals(sm.getPairwiseScore('u', 'x'), 3.5f);
     assertEquals(sm.getPairwiseScore('U', 'X'), 0f); // X (upper) unmapped
     assertEquals(sm.getPairwiseScore('A', '.'), 0f); // . unmapped
-    assertEquals(sm.getPairwiseScore('-', '-'), 7.7f);
+    assertEquals(sm.getPairwiseScore('-', '-'), 7.6f);
     assertEquals(sm.getPairwiseScore('A', (char) 128), 0f); // out of range
-
-    /*
-     * without guide symbols on score rows
-     */
-    data = "ScoreMatrix MyTest\nXY\n1 2\n3 4\n";
-    fp = new FileParse(data, DataSourceType.PASTE);
-    parser = new ScoreMatrixFile(fp);
-    sm = parser.parseMatrix();
-    assertNotNull(sm);
-    assertEquals(sm.getPairwiseScore('X', 'X'), 1f);
-    assertEquals(sm.getPairwiseScore('X', 'y'), 2f);
-    assertEquals(sm.getPairwiseScore('y', 'x'), 3f);
-    assertEquals(sm.getPairwiseScore('y', 'Y'), 4f);
-    assertEquals(sm.getPairwiseScore('D', 'R'), 0f);
   }
 
   @Test(groups = "Functional")
@@ -79,7 +66,7 @@ public class ScoreMatrixFileTest
   {
     String data;
 
-    data = "XY\n1 2\n3 4\n";
+    data = "X Y\n1 2\n3 4\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -95,7 +82,7 @@ public class ScoreMatrixFileTest
   @Test(groups = "Functional")
   public void testParse_notEnoughRows()
   {
-    String data = "ScoreMatrix MyTest\nXY\n1 2\n";
+    String data = "ScoreMatrix MyTest\nX Y\n1 2\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -111,7 +98,7 @@ public class ScoreMatrixFileTest
   @Test(groups = "Functional")
   public void testParse_notEnoughColumns()
   {
-    String data = "ScoreMatrix MyTest\nXY\n1 2\n3\n";
+    String data = "ScoreMatrix MyTest\nX Y\n1 2\n3\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -130,7 +117,7 @@ public class ScoreMatrixFileTest
     /*
      * with two too many columns:
      */
-    String data = "ScoreMatrix MyTest\nXY\n1 2\n3 4 5 6\n";
+    String data = "ScoreMatrix MyTest\nX\tY\n1 2\n3 4 5 6\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -145,7 +132,7 @@ public class ScoreMatrixFileTest
     /*
      * with guide character and one too many columns:
      */
-    data = "ScoreMatrix MyTest\nXY\nX 1 2\nY 3 4 5\n";
+    data = "ScoreMatrix MyTest\nX Y\nX 1 2\nY 3 4 5\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -161,7 +148,7 @@ public class ScoreMatrixFileTest
      * with no guide character and one too many columns:
      * parser guesses the first column is the guide character
      */
-    data = "ScoreMatrix MyTest\nXY\n1 2\n3 4 5\n";
+    data = "ScoreMatrix MyTest\nX Y\n1 2\n3 4 5\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -177,7 +164,7 @@ public class ScoreMatrixFileTest
   @Test(groups = "Functional")
   public void testParse_tooManyRows()
   {
-    String data = "ScoreMatrix MyTest\nXY\n1 2\n3 4\n6 7";
+    String data = "ScoreMatrix MyTest\n\tX\tY\n1 2\n3 4\n6 7";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -193,7 +180,7 @@ public class ScoreMatrixFileTest
   @Test(groups = "Functional")
   public void testParse_badDelimiter()
   {
-    String data = "ScoreMatrix MyTest\nXY\n1|2\n3|4\n";
+    String data = "ScoreMatrix MyTest\n X Y Z\n1|2|3\n4|5|6\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -202,14 +189,14 @@ public class ScoreMatrixFileTest
     } catch (IOException e)
     {
       assertEquals(e.getMessage(),
-              "Expected 2 scores at line 3 but found 1");
+              "Invalid score value '1|2|3' at line 3 column 0");
     }
   }
 
   @Test(groups = "Functional")
   public void testParse_badFloat()
   {
-    String data = "ScoreMatrix MyTest\nXY\n1 2\n3 four\n";
+    String data = "ScoreMatrix MyTest\n\tX\tY\n1 2\n3 four\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -225,7 +212,7 @@ public class ScoreMatrixFileTest
   @Test(groups = "Functional")
   public void testParse_badGuideCharacter()
   {
-    String data = "ScoreMatrix MyTest\nXY\nX 1 2\ny 3 4\n";
+    String data = "ScoreMatrix MyTest\n\tX Y\nX 1 2\ny 3 4\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -244,7 +231,7 @@ public class ScoreMatrixFileTest
     /*
      * Name missing
      */
-    String data = "ScoreMatrix\nXY\n1 2\n3 4\n";
+    String data = "ScoreMatrix\nX Y\n1 2\n3 4\n";
     try
     {
       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
@@ -265,10 +252,10 @@ public class ScoreMatrixFileTest
    * @throws MalformedURLException
    */
   @Test(groups = "Functional")
-  public void testParse_withResidueHeading() throws MalformedURLException,
+  public void testParse_ncbiFormat() throws MalformedURLException,
           IOException
   {
-    String data = "ScoreMatrix MyTest\n" + "ABC\n" + "\tA\tB\tC\n"
+    String data = "ScoreMatrix MyTest\n" + "\tA\tB\tC\n"
             + "A\t1.0\t2.0\t3.0\n" + "B\t4.0\t5.0\t6.0\n"
             + "C\t7.0\t8.0\t9.0\n";
     FileParse fp = new FileParse(data, DataSourceType.PASTE);
@@ -281,4 +268,33 @@ public class ScoreMatrixFileTest
     assertEquals(sm.getPairwiseScore('B', 'c'), 6.0f);
     assertEquals(sm.getSize(), 3);
   }
+
+  /**
+   * Test a successful parse of a (small) score matrix file
+   * 
+   * @throws IOException
+   * @throws MalformedURLException
+   */
+  @Test(groups = "Functional")
+  public void testParse_aaIndexBlosum80() throws MalformedURLException,
+          IOException
+  {
+    FileParse fp = new FileParse("resources/scoreModel/blosum80.scm",
+            DataSourceType.FILE);
+    ScoreMatrixFile parser = new ScoreMatrixFile(fp);
+    ScoreMatrix sm = parser.parseMatrix();
+  
+    assertNotNull(sm);
+    assertEquals(sm.getName(), "HENS920103");
+    assertEquals(sm.getDescription(),
+            "BLOSUM80 substitution matrix (Henikoff-Henikoff, 1992)");
+    assertFalse(sm.isDNA());
+    assertTrue(sm.isProtein());
+    assertEquals(20, sm.getSize());
+    assertEquals(sm.getGapIndex(), -1);
+
+    assertEquals(sm.getPairwiseScore('A', 'A'), 7f);
+    assertEquals(sm.getPairwiseScore('A', 'R'), -3f);
+    assertEquals(sm.getPairwiseScore('r', 'a'), -3f); // A/a equivalent
+  }
 }