1 package jalview.analysis.scoremodels;
2 import static org.testng.Assert.assertEquals;
3 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
5 import org.testng.annotations.Test;
7 public class ScoreMatrixTest
9 @Test(groups = "Functional")
10 public void testConstructor()
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);
29 groups = "Functional",
30 expectedExceptions = { IllegalArgumentException.class })
31 public void testConstructor_matrixTooSmall()
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);
40 groups = "Functional",
41 expectedExceptions = { IllegalArgumentException.class })
42 public void testConstructor_matrixTooBig()
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);
51 groups = "Functional",
52 expectedExceptions = { IllegalArgumentException.class })
53 public void testConstructor_matrixNotSquare()
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);
61 @Test(groups = "Functional")
62 public void testBuildSymbolIndex()
64 short[] index = ScoreMatrix.buildSymbolIndex("AX-. yxYp".toCharArray());
66 assertEquals(index.length, 128); // ASCII character set size
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
81 * check all unmapped symbols have index for unmapped
83 for (int c = 0; c < index.length; c++)
85 if (!"AaXx-. Yyp".contains(String.valueOf((char) c)))
87 assertEquals(index[c], -1);
93 * check that characters not in the basic ASCII set are simply ignored
95 @Test(groups = "Functional")
96 public void testBuildSymbolIndex_nonAscii()
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++)
105 if (c != 'P' && c != 'p')
107 assertEquals(index[c], -1);
112 @Test(groups = "Functional")
113 public void testGetMatrixIndex()
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);
131 @Test(groups = "Functional")
132 public void testGetSize()
134 ScoreMatrix sm = ScoreModels.getInstance().getBlosum62();
135 assertEquals(sm.getMatrix().length, sm.getSize());