44bb8aa65391c821ee09b6f618f41c0a6221a55c
[jalview.git] / test / jalview / io / ScoreMatrixFileTest.java
1 package jalview.io;
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.assertTrue;
7 import static org.testng.Assert.fail;
8
9 import jalview.analysis.scoremodels.ScoreMatrix;
10
11 import java.io.IOException;
12 import java.net.MalformedURLException;
13
14 import org.testng.annotations.Test;
15
16 public class ScoreMatrixFileTest
17 {
18
19   /**
20    * Test a successful parse of a (small) score matrix file
21    * 
22    * @throws IOException
23    * @throws MalformedURLException
24    */
25   @Test(groups = "Functional")
26   public void testParse() throws MalformedURLException, IOException
27   {
28     /*
29      * some messy but valid input data, with comma, space
30      * or tab (or combinations) as score value delimiters
31      * this example includes 'guide' symbols on score rows
32      */
33     String data = "ScoreMatrix MyTest (example)\n" + "ATU tx-\n"
34             + "A,1.1,1.2,1.3,1.4, 1.5, 1.6, 1.7\n"
35             + "T,2.1 2.2 2.3 2.4 2.5 2.6 2.7\n"
36             + "U\t3.1\t3.2\t3.3\t3.4\t3.5\t3.6\t3.7\n"
37             + " 4.1 ,4.2,\t,4.3 ,\t4.4\t, \4.5,4.6 4.7\n"
38             + "t, 5.1,5.3,5.3,5.4,5.5, 5.6, 5.7\n"
39             + "x\t6.1, 6.2 6.3 6.4 6.5 6.6 6.7\n"
40             + "-, \t7.1\t7.2 7.3, 7.4, 7.5\t,7.6,7.7\n";
41     FileParse fp = new FileParse(data, DataSourceType.PASTE);
42     ScoreMatrixFile parser = new ScoreMatrixFile(fp);
43     ScoreMatrix sm = parser.parseMatrix();
44
45     assertNotNull(sm);
46     assertEquals(sm.getName(), "MyTest (example)");
47     assertTrue(sm.isDNA());
48     assertFalse(sm.isProtein());
49     assertEquals(sm.getPairwiseScore('A', 'A'), 1.1f);
50     assertEquals(sm.getPairwiseScore('A', 'T'), 1.2f);
51     assertEquals(sm.getPairwiseScore('a', 'T'), 1.2f); // A/a equivalent
52     assertEquals(sm.getPairwiseScore('A', 't'), 1.5f); // T/t not equivalent
53     assertEquals(sm.getPairwiseScore('a', 't'), 1.5f);
54     assertEquals(sm.getPairwiseScore('T', ' '), 2.4f);
55     assertEquals(sm.getPairwiseScore('U', 'x'), 3.6f);
56     assertEquals(sm.getPairwiseScore('u', 'x'), 3.6f);
57     assertEquals(sm.getPairwiseScore('U', 'X'), 0f); // X (upper) unmapped
58     assertEquals(sm.getPairwiseScore('A', '.'), 0f); // . unmapped
59     assertEquals(sm.getPairwiseScore('-', '-'), 7.7f);
60     assertEquals(sm.getPairwiseScore('A', (char) 128), 0f); // out of range
61
62     /*
63      * without guide symbols on score rows
64      */
65     data = "ScoreMatrix MyTest\nXY\n1 2\n3 4\n";
66     fp = new FileParse(data, DataSourceType.PASTE);
67     parser = new ScoreMatrixFile(fp);
68     sm = parser.parseMatrix();
69     assertNotNull(sm);
70     assertEquals(sm.getPairwiseScore('X', 'X'), 1f);
71     assertEquals(sm.getPairwiseScore('X', 'y'), 2f);
72     assertEquals(sm.getPairwiseScore('y', 'x'), 3f);
73     assertEquals(sm.getPairwiseScore('y', 'Y'), 4f);
74     assertEquals(sm.getPairwiseScore('D', 'R'), 0f);
75   }
76
77   @Test(groups = "Functional")
78   public void testParse_headerMissing()
79   {
80     String data;
81
82     data = "XY\n1 2\n3 4\n";
83     try
84     {
85       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
86               .parseMatrix();
87       fail("expected exception");
88     } catch (IOException e)
89     {
90       assertEquals(e.getMessage(),
91               "Format error: 'ScoreMatrix <name>' should be the first non-comment line");
92     }
93   }
94
95   @Test(groups = "Functional")
96   public void testParse_notEnoughRows()
97   {
98     String data = "ScoreMatrix MyTest\nXY\n1 2\n";
99     try
100     {
101       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
102               .parseMatrix();
103       fail("expected exception");
104     } catch (IOException e)
105     {
106       assertEquals(e.getMessage(),
107               "Expected 2 rows of score data in score matrix but only found 1");
108     }
109   }
110
111   @Test(groups = "Functional")
112   public void testParse_notEnoughColumns()
113   {
114     String data = "ScoreMatrix MyTest\nXY\n1 2\n3\n";
115     try
116     {
117       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
118               .parseMatrix();
119       fail("expected exception");
120     } catch (IOException e)
121     {
122       assertEquals(e.getMessage(),
123               "Expected 2 scores at line 4 but found 1");
124     }
125   }
126
127   @Test(groups = "Functional")
128   public void testParse_tooManyColumns()
129   {
130     /*
131      * with two too many columns:
132      */
133     String data = "ScoreMatrix MyTest\nXY\n1 2\n3 4 5 6\n";
134     try
135     {
136       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
137               .parseMatrix();
138       fail("expected exception");
139     } catch (IOException e)
140     {
141       assertEquals(e.getMessage(),
142               "Expected 2 scores at line 4 but found 4");
143     }
144
145     /*
146      * with guide character and one too many columns:
147      */
148     data = "ScoreMatrix MyTest\nXY\nX 1 2\nY 3 4 5\n";
149     try
150     {
151       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
152               .parseMatrix();
153       fail("expected exception");
154     } catch (IOException e)
155     {
156       assertEquals(e.getMessage(),
157               "Expected 2 scores at line 4 but found 4");
158     }
159
160     /*
161      * with no guide character and one too many columns:
162      * parser guesses the first column is the guide character
163      */
164     data = "ScoreMatrix MyTest\nXY\n1 2\n3 4 5\n";
165     try
166     {
167       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
168               .parseMatrix();
169       fail("expected exception");
170     } catch (IOException e)
171     {
172       assertEquals(e.getMessage(),
173               "Error parsing score matrix at line 4, expected 'Y' but found '3'");
174     }
175   }
176
177   @Test(groups = "Functional")
178   public void testParse_tooManyRows()
179   {
180     String data = "ScoreMatrix MyTest\nXY\n1 2\n3 4\n6 7";
181     try
182     {
183       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
184               .parseMatrix();
185       fail("expected exception");
186     } catch (IOException e)
187     {
188       assertEquals(e.getMessage(),
189               "Unexpected extra input line in score model file: '6 7'");
190     }
191   }
192
193   @Test(groups = "Functional")
194   public void testParse_badDelimiter()
195   {
196     String data = "ScoreMatrix MyTest\nXY\n1|2\n3|4\n";
197     try
198     {
199       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
200               .parseMatrix();
201       fail("expected exception");
202     } catch (IOException e)
203     {
204       assertEquals(e.getMessage(),
205               "Expected 2 scores at line 3 but found 1");
206     }
207   }
208
209   @Test(groups = "Functional")
210   public void testParse_badFloat()
211   {
212     String data = "ScoreMatrix MyTest\nXY\n1 2\n3 four\n";
213     try
214     {
215       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
216               .parseMatrix();
217       fail("expected exception");
218     } catch (IOException e)
219     {
220       assertEquals(e.getMessage(),
221               "Invalid score value 'four' at line 4 column 1");
222     }
223   }
224
225   @Test(groups = "Functional")
226   public void testParse_badGuideCharacter()
227   {
228     String data = "ScoreMatrix MyTest\nXY\nX 1 2\ny 3 4\n";
229     try
230     {
231       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
232               .parseMatrix();
233       fail("expected exception");
234     } catch (IOException e)
235     {
236       assertEquals(e.getMessage(),
237               "Error parsing score matrix at line 4, expected 'Y' but found 'y'");
238     }
239   }
240
241   @Test(groups = "Functional")
242   public void testParse_nameMissing()
243   {
244     /*
245      * Name missing
246      */
247     String data = "ScoreMatrix\nXY\n1 2\n3 4\n";
248     try
249     {
250       new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
251               .parseMatrix();
252       fail("expected exception");
253     } catch (IOException e)
254     {
255       assertEquals(
256               e.getMessage(),
257               "Format error: expected 'ScoreMatrix <name>', found 'ScoreMatrix' at line 1");
258     }
259   }
260
261   /**
262    * Test a successful parse of a (small) score matrix file
263    * 
264    * @throws IOException
265    * @throws MalformedURLException
266    */
267   @Test(groups = "Functional")
268   public void testParse_withResidueHeading() throws MalformedURLException,
269           IOException
270   {
271     String data = "ScoreMatrix MyTest\n" + "ABC\n" + "\tA\tB\tC\n"
272             + "A\t1.0\t2.0\t3.0\n" + "B\t4.0\t5.0\t6.0\n"
273             + "C\t7.0\t8.0\t9.0\n";
274     FileParse fp = new FileParse(data, DataSourceType.PASTE);
275     ScoreMatrixFile parser = new ScoreMatrixFile(fp);
276     ScoreMatrix sm = parser.parseMatrix();
277   
278     assertNotNull(sm);
279     assertEquals(sm.getName(), "MyTest");
280     assertEquals(sm.getPairwiseScore('A', 'A'), 1.0f);
281     assertEquals(sm.getPairwiseScore('B', 'c'), 6.0f);
282     assertEquals(sm.getSize(), 3);
283   }
284 }