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;
9 import jalview.analysis.scoremodels.ScoreMatrix;
11 import java.io.IOException;
12 import java.net.MalformedURLException;
14 import org.testng.annotations.Test;
16 public class ScoreMatrixFileTest
20 * Test a successful parse of a (small) score matrix file
23 * @throws MalformedURLException
25 @Test(groups = "Functional")
26 public void testParse() throws MalformedURLException, IOException
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
33 String data = "ScoreMatrix MyTest\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();
46 assertEquals(sm.getName(), "MyTest");
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
63 * without guide symbols on score rows
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();
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);
77 @Test(groups = "Functional")
78 public void testParse_headerMissing()
82 data = "XY\n1 2\n3 4\n";
85 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
87 fail("expected exception");
88 } catch (IOException e)
90 assertEquals(e.getMessage(),
91 "Format error: 'ScoreMatrix <name>' should be the first non-comment line");
95 @Test(groups = "Functional")
96 public void testParse_notEnoughRows()
98 String data = "ScoreMatrix MyTest\nXY\n1 2\n";
101 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
103 fail("expected exception");
104 } catch (IOException e)
106 assertEquals(e.getMessage(),
107 "Expected 2 rows of score data in score matrix but only found 1");
111 @Test(groups = "Functional")
112 public void testParse_notEnoughColumns()
114 String data = "ScoreMatrix MyTest\nXY\n1 2\n3\n";
117 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
119 fail("expected exception");
120 } catch (IOException e)
122 assertEquals(e.getMessage(),
123 "Expected 2 scores at line 4 but found 1");
127 @Test(groups = "Functional")
128 public void testParse_tooManyColumns()
131 * with two too many columns:
133 String data = "ScoreMatrix MyTest\nXY\n1 2\n3 4 5 6\n";
136 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
138 fail("expected exception");
139 } catch (IOException e)
141 assertEquals(e.getMessage(),
142 "Expected 2 scores at line 4 but found 4");
146 * with guide character and one too many columns:
148 data = "ScoreMatrix MyTest\nXY\nX 1 2\nY 3 4 5\n";
151 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
153 fail("expected exception");
154 } catch (IOException e)
156 assertEquals(e.getMessage(),
157 "Expected 2 scores at line 4 but found 4");
161 * with no guide character and one too many columns:
162 * parser guesses the first column is the guide character
164 data = "ScoreMatrix MyTest\nXY\n1 2\n3 4 5\n";
167 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
169 fail("expected exception");
170 } catch (IOException e)
172 assertEquals(e.getMessage(),
173 "Error parsing score matrix at line 4, expected 'Y' but found '3'");
177 @Test(groups = "Functional")
178 public void testParse_tooManyRows()
180 String data = "ScoreMatrix MyTest\nXY\n1 2\n3 4\n6 7";
183 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
185 fail("expected exception");
186 } catch (IOException e)
188 assertEquals(e.getMessage(),
189 "Unexpected extra input line in score model file: '6 7'");
193 @Test(groups = "Functional")
194 public void testParse_badDelimiter()
196 String data = "ScoreMatrix MyTest\nXY\n1|2\n3|4\n";
199 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
201 fail("expected exception");
202 } catch (IOException e)
204 assertEquals(e.getMessage(),
205 "Expected 2 scores at line 3 but found 1");
209 @Test(groups = "Functional")
210 public void testParse_badFloat()
212 String data = "ScoreMatrix MyTest\nXY\n1 2\n3 four\n";
215 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
217 fail("expected exception");
218 } catch (IOException e)
220 assertEquals(e.getMessage(),
221 "Invalid score value 'four' at line 4 column 1");
225 @Test(groups = "Functional")
226 public void testParse_badGuideCharacter()
228 String data = "ScoreMatrix MyTest\nXY\nX 1 2\ny 3 4\n";
231 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
233 fail("expected exception");
234 } catch (IOException e)
236 assertEquals(e.getMessage(),
237 "Error parsing score matrix at line 4, expected 'Y' but found 'y'");
241 @Test(groups = "Functional")
242 public void testParse_nameMissing()
247 String data = "ScoreMatrix\nXY\n1 2\n3 4\n";
250 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
252 fail("expected exception");
253 } catch (IOException e)
257 "Format error: expected 'ScoreMatrix <name>', found 'ScoreMatrix' at line 1");