JAL-2416 more validation in ScoreMatrix constructor
[jalview.git] / test / jalview / analysis / scoremodels / ScoreMatrixTest.java
1 package jalview.analysis.scoremodels;
2 import static org.testng.Assert.assertEquals;
3 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
4
5 import org.testng.annotations.Test;
6
7 public class ScoreMatrixTest
8 {
9   @Test(groups = "Functional")
10   public void testConstructor()
11   {
12     // note score matrix does not have to be symmetric (though it should be!)
13     float[][] scores = new float[3][];
14     scores[0] = new float[] { 1f, 2f, 3f };
15     scores[1] = new float[] { 4f, 5f, 6f };
16     scores[2] = new float[] { 7f, 8f, 9f };
17     ScoreMatrix sm = new ScoreMatrix("Test", "ABC".toCharArray(), scores);
18     assertEquals(sm.getSize(), 3);
19     assertArrayEquals(scores, sm.getMatrix());
20     assertEquals(sm.getPairwiseScore('A', 'a'), 1f);
21     assertEquals(sm.getPairwiseScore('b', 'c'), 6f);
22     assertEquals(sm.getPairwiseScore('c', 'b'), 8f);
23     assertEquals(sm.getPairwiseScore('A', 'D'), 0f);
24     assertEquals(sm.getMatrixIndex('c'), 2);
25     assertEquals(sm.getMatrixIndex(' '), -1);
26   }
27
28   @Test(
29     groups = "Functional",
30     expectedExceptions = { IllegalArgumentException.class })
31   public void testConstructor_matrixTooSmall()
32   {
33     float[][] scores = new float[2][];
34     scores[0] = new float[] { 1f, 2f };
35     scores[1] = new float[] { 3f, 4f };
36     new ScoreMatrix("Test", "ABC".toCharArray(), scores);
37   }
38
39   @Test(
40     groups = "Functional",
41     expectedExceptions = { IllegalArgumentException.class })
42   public void testConstructor_matrixTooBig()
43   {
44     float[][] scores = new float[2][];
45     scores[0] = new float[] { 1f, 2f };
46     scores[1] = new float[] { 3f, 4f };
47     new ScoreMatrix("Test", "A".toCharArray(), scores);
48   }
49
50   @Test(
51     groups = "Functional",
52     expectedExceptions = { IllegalArgumentException.class })
53   public void testConstructor_matrixNotSquare()
54   {
55     float[][] scores = new float[2][];
56     scores[0] = new float[] { 1f, 2f };
57     scores[1] = new float[] { 3f };
58     new ScoreMatrix("Test", "AB".toCharArray(), scores);
59   }
60
61   @Test(groups = "Functional")
62   public void testBuildSymbolIndex()
63   {
64     short[] index = ScoreMatrix.buildSymbolIndex("AX-. yxYp".toCharArray());
65
66     assertEquals(index.length, 128); // ASCII character set size
67
68     assertEquals(index['A'], 0);
69     assertEquals(index['a'], 0); // lower-case mapping added
70     assertEquals(index['X'], 1);
71     assertEquals(index['-'], 2);
72     assertEquals(index['.'], 3);
73     assertEquals(index[' '], 4);
74     assertEquals(index['y'], 5); // lower-case override
75     assertEquals(index['x'], 6); // lower-case override
76     assertEquals(index['Y'], 7);
77     assertEquals(index['p'], 8);
78     assertEquals(index['P'], -1); // lower-case doesn't map upper-case
79
80     /*
81      * check all unmapped symbols have index for unmapped
82      */
83     for (int c = 0; c < index.length; c++)
84     {
85       if (!"AaXx-. Yyp".contains(String.valueOf((char) c)))
86       {
87         assertEquals(index[c], -1);
88       }
89     }
90   }
91
92   /**
93    * check that characters not in the basic ASCII set are simply ignored
94    */
95   @Test(groups = "Functional")
96   public void testBuildSymbolIndex_nonAscii()
97   {
98     char[] weird = new char[] { 128, 245, 'P' };
99     short[] index = ScoreMatrix.buildSymbolIndex(weird);
100     assertEquals(index.length, 128);
101     assertEquals(index['P'], 2);
102     assertEquals(index['p'], 2);
103     for (int c = 0; c < index.length; c++)
104     {
105       if (c != 'P' && c != 'p')
106       {
107         assertEquals(index[c], -1);
108       }
109     }
110   }
111
112   @Test(groups = "Functional")
113   public void testGetMatrixIndex()
114   {
115     ScoreMatrix sm = ScoreModels.getInstance().getBlosum62();
116     assertEquals(sm.getMatrixIndex('A'), 0);
117     assertEquals(sm.getMatrixIndex('R'), 1);
118     assertEquals(sm.getMatrixIndex('r'), 1);
119     assertEquals(sm.getMatrixIndex('N'), 2);
120     assertEquals(sm.getMatrixIndex('D'), 3);
121     assertEquals(sm.getMatrixIndex('X'), 22);
122     assertEquals(sm.getMatrixIndex('x'), 22);
123     assertEquals(sm.getMatrixIndex(' '), 23);
124     assertEquals(sm.getMatrixIndex('*'), 24);
125     assertEquals(sm.getMatrixIndex('.'), -1);
126     assertEquals(sm.getMatrixIndex('-'), -1);
127     assertEquals(sm.getMatrixIndex('?'), -1);
128     assertEquals(sm.getMatrixIndex((char) 128), -1);
129   }
130
131   @Test(groups = "Functional")
132   public void testGetSize()
133   {
134     ScoreMatrix sm = ScoreModels.getInstance().getBlosum62();
135     assertEquals(sm.getMatrix().length, sm.getSize());
136   }
137 }