JAL-1632 JAL-2416 load score matrices from file, as float[][]
[jalview.git] / test / jalview / analysis / scoremodels / ScoreMatrixTest.java
1 package jalview.analysis.scoremodels;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertNotNull;
6 import static org.testng.Assert.assertNull;
7 import static org.testng.Assert.assertTrue;
8
9 import java.io.ByteArrayInputStream;
10
11 import org.testng.annotations.Test;
12
13 public class ScoreMatrixTest
14 {
15   @Test(groups = "Functional")
16   public void testBuildSymbolIndex()
17   {
18     short[] index = ScoreMatrix.buildSymbolIndex("AX-. yxYp".toCharArray());
19
20     assertEquals(index.length, 128); // ASCII character set size
21
22     assertEquals(index['A'], 0);
23     assertEquals(index['a'], 0); // lower-case mapping added
24     assertEquals(index['X'], 1);
25     assertEquals(index['-'], 2);
26     assertEquals(index['.'], 3);
27     assertEquals(index[' '], 4);
28     assertEquals(index['y'], 5); // lower-case override
29     assertEquals(index['x'], 6); // lower-case override
30     assertEquals(index['Y'], 7);
31     assertEquals(index['p'], 8);
32     assertEquals(index['P'], -1); // lower-case doesn't map upper-case
33
34     /*
35      * check all unmapped symbols have index for unmapped
36      */
37     for (int c = 0; c < index.length; c++)
38     {
39       if (!"AaXx-. Yyp".contains(String.valueOf((char) c)))
40       {
41         assertEquals(index[c], -1);
42       }
43     }
44   }
45
46   /**
47    * check that characters not in the basic ASCII set are simply ignored
48    */
49   @Test(groups = "Functional")
50   public void testBuildSymbolIndex_nonAscii()
51   {
52     char[] weird = new char[] { 128, 245, 'P' };
53     short[] index = ScoreMatrix.buildSymbolIndex(weird);
54     assertEquals(index.length, 128);
55     assertEquals(index['P'], 2);
56     assertEquals(index['p'], 2);
57     for (int c = 0; c < index.length; c++)
58     {
59       if (c != 'P' && c != 'p')
60       {
61         assertEquals(index[c], -1);
62       }
63     }
64   }
65
66   /**
67    * Test a successful parse of a (small) score matrix file
68    */
69   @Test(groups = "Functional")
70   public void testParse()
71   {
72     /*
73      * some messy but valid input data, with comma, space
74      * or tab (or combinations) as score value delimiters
75      */
76     String data = "ScoreMatrix MyTest\n" + "ATU tx-\n"
77             + "1.1,1.2,1.3,1.4, 1.5, 1.6, 1.7\n"
78             + "2.1 2.2 2.3 2.4 2.5 2.6 2.7\n"
79             + "3.1\t3.2\t3.3\t3.4\t3.5\t3.6\t3.7\n"
80             + " 4.1 ,4.2,\t,4.3 ,\t4.4\t, \4.5,4.6 4.7\n"
81             + ", 5.1,5.3,5.3,5.4,5.5, 5.6, 5.7\n"
82             + "\t6.1, 6.2 6.3 6.4 6.5 6.6 6.7\n"
83             + ", \t7.1\t7.2 7.3, 7.4, 7.5\t,7.6,7.7\n";
84     ScoreMatrix sm = ScoreMatrix.parse(new ByteArrayInputStream(data
85             .getBytes()));
86     assertNotNull(sm);
87     assertEquals(sm.getName(), "MyTest");
88     assertTrue(sm.isDNA());
89     assertFalse(sm.isProtein());
90     assertEquals(sm.getPairwiseScore('A', 'A'), 1.1f);
91     assertEquals(sm.getPairwiseScore('A', 'T'), 1.2f);
92     assertEquals(sm.getPairwiseScore('a', 'T'), 1.2f); // A/a equivalent
93     assertEquals(sm.getPairwiseScore('A', 't'), 1.5f); // T/t not equivalent
94     assertEquals(sm.getPairwiseScore('a', 't'), 1.5f);
95     assertEquals(sm.getPairwiseScore('T', ' '), 2.4f);
96     assertEquals(sm.getPairwiseScore('U', 'x'), 3.6f);
97     assertEquals(sm.getPairwiseScore('u', 'x'), 3.6f);
98     assertEquals(sm.getPairwiseScore('U', 'X'), 0f); // X (upper) unmapped
99     assertEquals(sm.getPairwiseScore('A', '.'), 0f); // . unmapped
100     assertEquals(sm.getPairwiseScore('-', '-'), 7.7f);
101     assertEquals(sm.getPairwiseScore('A', (char) 128), 0f); // out of range
102   }
103
104   @Test(groups = "Functional")
105   public void testParse_invalidInput()
106   {
107     /*
108      * valid first
109      */
110     String data = "ScoreMatrix MyTest\nXY\n1 2\n3 4\n";
111     ScoreMatrix sm = ScoreMatrix.parse(new ByteArrayInputStream(data
112             .getBytes()));
113     assertNotNull(sm);
114
115     /*
116      * Name missing
117      */
118     data = "ScoreMatrix\nXY\n1 2\n3 4\n";
119     sm = ScoreMatrix.parse(new ByteArrayInputStream(data.getBytes()));
120     assertNull(sm);
121
122     /*
123      * ScoreMatrix header missing
124      */
125     data = "XY\n1 2\n3 4\n";
126     sm = ScoreMatrix.parse(new ByteArrayInputStream(data.getBytes()));
127     assertNull(sm);
128
129     /*
130      * Not enough rows
131      */
132     data = "ScoreMatrix MyTest\nXY\n1 2\n";
133     sm = ScoreMatrix.parse(new ByteArrayInputStream(data.getBytes()));
134     assertNull(sm);
135
136     /*
137      * Not enough columns
138      */
139     data = "ScoreMatrix MyTest\nXY\n1 2\n3\n";
140     sm = ScoreMatrix.parse(new ByteArrayInputStream(data.getBytes()));
141     assertNull(sm);
142
143     /*
144      * Too many columns
145      */
146     data = "ScoreMatrix MyTest\nXY\n1 2\n3 4 5\n";
147     sm = ScoreMatrix.parse(new ByteArrayInputStream(data.getBytes()));
148     assertNull(sm);
149
150     /*
151      * Too many rows
152      */
153     data = "ScoreMatrix MyTest\nXY\n1 2\n3 4\n6 7";
154     sm = ScoreMatrix.parse(new ByteArrayInputStream(data.getBytes()));
155     assertNull(sm);
156
157     /*
158      * unsupported delimiter |
159      */
160     data = "ScoreMatrix MyTest\nXY\n1|2\n3|4\n";
161     sm = ScoreMatrix.parse(new ByteArrayInputStream(data.getBytes()));
162     assertNull(sm);
163
164     /*
165      * Bad float value
166      */
167     data = "ScoreMatrix MyTest\nXY\n1 2\n3 four\n";
168     sm = ScoreMatrix.parse(new ByteArrayInputStream(data.getBytes()));
169     assertNull(sm);
170
171   }
172 }