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 import static org.testng.Assert.fail;
10 import jalview.analysis.scoremodels.ScoreMatrix;
11 import jalview.analysis.scoremodels.ScoreModels;
13 import java.io.IOException;
14 import java.net.MalformedURLException;
16 import org.testng.annotations.Test;
18 public class ScoreMatrixFileTest
22 * Test a successful parse of a (small) score matrix file
25 * @throws MalformedURLException
27 @Test(groups = "Functional")
28 public void testParseMatrix_ncbiMixedDelimiters()
29 throws MalformedURLException,
33 * some messy but valid input data, with comma, space
34 * or tab (or combinations) as score value delimiters
35 * this example includes 'guide' symbols on score rows
37 String data = "ScoreMatrix MyTest (example)\n" + "A\tT\tU\tt\tx\t-\n"
38 + "A,1.1,1.2,1.3,1.4, 1.5, 1.6\n"
39 + "T,2.1 2.2 2.3 2.4 2.5 2.6\n"
40 + "U\t3.1\t3.2\t3.3\t3.4\t3.5\t3.6\t\n"
41 + "t, 5.1,5.3,5.3,5.4,5.5, 5.6\n"
42 + "x\t6.1, 6.2 6.3 6.4 6.5 6.6\n"
43 + "-, \t7.1\t7.2 7.3, 7.4, 7.5\t,7.6\n";
44 FileParse fp = new FileParse(data, DataSourceType.PASTE);
45 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
46 ScoreMatrix sm = parser.parseMatrix();
49 assertEquals(sm.getName(), "MyTest (example)");
50 assertEquals(sm.getSize(), 6);
51 assertNull(sm.getDescription());
52 assertTrue(sm.isDNA());
53 assertFalse(sm.isProtein());
54 assertEquals(sm.getMinimumScore(), 1.1f);
55 assertEquals(sm.getPairwiseScore('A', 'A'), 1.1f);
56 assertEquals(sm.getPairwiseScore('A', 'T'), 1.2f);
57 assertEquals(sm.getPairwiseScore('a', 'T'), 1.2f); // A/a equivalent
58 assertEquals(sm.getPairwiseScore('A', 't'), 1.4f); // T/t not equivalent
59 assertEquals(sm.getPairwiseScore('a', 't'), 1.4f);
60 assertEquals(sm.getPairwiseScore('U', 'x'), 3.5f);
61 assertEquals(sm.getPairwiseScore('u', 'x'), 3.5f);
62 // X (upper) and '.' unmapped - get minimum score
63 assertEquals(sm.getPairwiseScore('U', 'X'), 1.1f);
64 assertEquals(sm.getPairwiseScore('A', '.'), 1.1f);
65 assertEquals(sm.getPairwiseScore('-', '-'), 7.6f);
66 assertEquals(sm.getPairwiseScore('A', (char) 128), 0f); // out of range
69 @Test(groups = "Functional")
70 public void testParseMatrix_headerMissing()
74 data = "X Y\n1 2\n3 4\n";
77 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
79 fail("expected exception");
80 } catch (IOException e)
82 assertEquals(e.getMessage(),
83 "Format error: 'ScoreMatrix <name>' should be the first non-comment line");
87 @Test(groups = "Functional")
88 public void testParseMatrix_ncbiNotEnoughRows()
90 String data = "ScoreMatrix MyTest\nX Y Z\n1 2 3\n4 5 6\n";
93 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
95 fail("expected exception");
96 } catch (IOException e)
98 assertEquals(e.getMessage(),
99 "Expected 3 rows of score data in score matrix but only found 2");
103 @Test(groups = "Functional")
104 public void testParseMatrix_ncbiNotEnoughColumns()
106 String data = "ScoreMatrix MyTest\nX Y Z\n1 2 3\n4 5\n7 8 9\n";
109 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
111 fail("expected exception");
112 } catch (IOException e)
114 assertEquals(e.getMessage(),
115 "Expected 3 scores at line 4: '4 5' but found 2");
119 @Test(groups = "Functional")
120 public void testParseMatrix_ncbiTooManyColumns()
123 * with two too many columns:
125 String data = "ScoreMatrix MyTest\nX\tY\tZ\n1 2 3\n4 5 6 7\n8 9 10\n";
128 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
130 fail("expected exception");
131 } catch (IOException e)
133 assertEquals(e.getMessage(),
134 "Expected 3 scores at line 4: '4 5 6 7' but found 4");
138 * with guide character and one too many columns:
140 data = "ScoreMatrix MyTest\nX Y\nX 1 2\nY 3 4 5\n";
143 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
145 fail("expected exception");
146 } catch (IOException e)
148 assertEquals(e.getMessage(),
149 "Expected 2 scores at line 4: 'Y 3 4 5' but found 3");
153 * with no guide character and one too many columns
155 data = "ScoreMatrix MyTest\nX Y\n1 2\n3 4 5\n";
158 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
160 fail("expected exception");
161 } catch (IOException e)
163 assertEquals(e.getMessage(),
164 "Expected 2 scores at line 4: '3 4 5' but found 3");
168 @Test(groups = "Functional")
169 public void testParseMatrix_ncbiTooManyRows()
171 String data = "ScoreMatrix MyTest\n\tX\tY\tZ\n1 2 3\n4 5 6\n7 8 9\n10 11 12\n";
174 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
176 fail("expected exception");
177 } catch (IOException e)
179 assertEquals(e.getMessage(),
180 "Unexpected extra input line in score model file: '10 11 12'");
184 @Test(groups = "Functional")
185 public void testParseMatrix_ncbiBadDelimiter()
187 String data = "ScoreMatrix MyTest\n X Y Z\n1|2|3\n4|5|6\n";
190 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
192 fail("expected exception");
193 } catch (IOException e)
195 assertEquals(e.getMessage(),
196 "Invalid score value '1|2|3' at line 3 column 0");
200 @Test(groups = "Functional")
201 public void testParseMatrix_ncbiBadFloat()
203 String data = "ScoreMatrix MyTest\n\tX\tY\tZ\n1 2 3\n4 five 6\n7 8 9\n";
206 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
208 fail("expected exception");
209 } catch (IOException e)
211 assertEquals(e.getMessage(),
212 "Invalid score value 'five' at line 4 column 1");
216 @Test(groups = "Functional")
217 public void testParseMatrix_ncbiBadGuideCharacter()
219 String data = "ScoreMatrix MyTest\n\tX Y\nX 1 2\ny 3 4\n";
222 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
224 fail("expected exception");
225 } catch (IOException e)
227 assertEquals(e.getMessage(),
228 "Error parsing score matrix at line 4, expected 'Y' but found 'y'");
231 data = "ScoreMatrix MyTest\n\tX Y\nXX 1 2\nY 3 4\n";
234 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
236 fail("expected exception");
237 } catch (IOException e)
239 assertEquals(e.getMessage(),
240 "Error parsing score matrix at line 3, expected 'X' but found 'XX'");
244 @Test(groups = "Functional")
245 public void testParseMatrix_ncbiNameMissing()
248 * Name missing on ScoreMatrix header line
250 String data = "ScoreMatrix\nX Y\n1 2\n3 4\n";
253 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
255 fail("expected exception");
256 } catch (IOException e)
260 "Format error: expected 'ScoreMatrix <name>', found 'ScoreMatrix' at line 1");
265 * Test a successful parse of a (small) score matrix file
267 * @throws IOException
268 * @throws MalformedURLException
270 @Test(groups = "Functional")
271 public void testParseMatrix_ncbiFormat() throws MalformedURLException,
274 // input including comment and blank lines
275 String data = "ScoreMatrix MyTest\n#comment\n\n" + "\tA\tB\tC\n"
276 + "A\t1.0\t2.0\t3.0\n" + "B\t4.0\t5.0\t6.0\n"
277 + "C\t7.0\t8.0\t9.0\n";
278 FileParse fp = new FileParse(data, DataSourceType.PASTE);
279 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
280 ScoreMatrix sm = parser.parseMatrix();
283 assertEquals(sm.getName(), "MyTest");
284 assertEquals(parser.getMatrixName(), "MyTest");
285 assertEquals(sm.getPairwiseScore('A', 'A'), 1.0f);
286 assertEquals(sm.getPairwiseScore('B', 'c'), 6.0f);
287 assertEquals(sm.getSize(), 3);
291 * Test a successful parse of a (small) score matrix file
293 * @throws IOException
294 * @throws MalformedURLException
296 @Test(groups = "Functional")
297 public void testParseMatrix_aaIndexBlosum80()
298 throws MalformedURLException,
301 FileParse fp = new FileParse("resources/scoreModel/blosum80.scm",
302 DataSourceType.FILE);
303 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
304 ScoreMatrix sm = parser.parseMatrix();
307 assertEquals(sm.getName(), "HENS920103");
308 assertEquals(sm.getDescription(),
309 "BLOSUM80 substitution matrix (Henikoff-Henikoff, 1992)");
310 assertFalse(sm.isDNA());
311 assertTrue(sm.isProtein());
312 assertEquals(20, sm.getSize());
314 assertEquals(sm.getPairwiseScore('A', 'A'), 7f);
315 assertEquals(sm.getPairwiseScore('A', 'R'), -3f);
316 assertEquals(sm.getPairwiseScore('r', 'a'), -3f); // A/a equivalent
320 * Test a successful parse of a (small) score matrix file
322 * @throws IOException
323 * @throws MalformedURLException
325 @Test(groups = "Functional")
326 public void testParseMatrix_aaindexFormat() throws MalformedURLException,
330 * aaindex format has scores for diagonal and below only
332 String data = "H MyTest\n" + "D My description\n" + "R PMID:1438297\n"
333 + "A Authors, names\n" + "T Journal title\n"
334 + "J Journal reference\n" + "* matrix in 1/3 Bit Units\n"
335 + "M rows = ABC, cols = ABC\n" + "A\t1.0\n"
337 + "C\t7.0\t8.0\t9.0\n";
338 FileParse fp = new FileParse(data, DataSourceType.PASTE);
339 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
340 ScoreMatrix sm = parser.parseMatrix();
343 assertEquals(sm.getSize(), 3);
344 assertEquals(sm.getName(), "MyTest");
345 assertEquals(sm.getDescription(), "My description");
346 assertEquals(sm.getPairwiseScore('A', 'A'), 1.0f);
347 assertEquals(sm.getPairwiseScore('A', 'B'), 4.0f);
348 assertEquals(sm.getPairwiseScore('A', 'C'), 7.0f);
349 assertEquals(sm.getPairwiseScore('B', 'A'), 4.0f);
350 assertEquals(sm.getPairwiseScore('B', 'B'), 5.0f);
351 assertEquals(sm.getPairwiseScore('B', 'C'), 8.0f);
352 assertEquals(sm.getPairwiseScore('C', 'C'), 9.0f);
353 assertEquals(sm.getPairwiseScore('C', 'B'), 8.0f);
354 assertEquals(sm.getPairwiseScore('C', 'A'), 7.0f);
357 @Test(groups = "Functional")
358 public void testParseMatrix_aaindex_mMissing()
359 throws MalformedURLException,
363 * aaindex format but M cols=, rows= is missing
365 String data = "H MyTest\n" + "A\t1.0\n"
367 + "C\t7.0\t8.0\t9.0\n";
368 FileParse fp = new FileParse(data, DataSourceType.PASTE);
369 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
372 parser.parseMatrix();
373 fail("Expected exception");
374 } catch (FileFormatException e)
376 assertEquals(e.getMessage(), "No alphabet specified in matrix file");
380 @Test(groups = "Functional")
381 public void testParseMatrix_aaindex_rowColMismatch()
382 throws MalformedURLException,
385 String data = "H MyTest\n" + "M rows=ABC, cols=ABD\n" + "A\t1.0\n"
387 + "C\t7.0\t8.0\t9.0\n";
388 FileParse fp = new FileParse(data, DataSourceType.PASTE);
389 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
392 parser.parseMatrix();
393 fail("Expected exception");
394 } catch (FileFormatException e)
398 "Unexpected aaIndex score matrix data at line 2: M rows=ABC, cols=ABD rows != cols");
402 @Test(groups = "Functional")
403 public void testParseMatrix_ncbiHeaderRepeated()
405 String data = "ScoreMatrix BLOSUM\nScoreMatrix PAM250\nX Y\n1 2\n3 4\n";
408 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
410 fail("expected exception");
411 } catch (IOException e)
413 assertEquals(e.getMessage(),
414 "Error: 'ScoreMatrix' repeated in file at line 2");
418 @Test(groups = "Functional")
419 public void testParseMatrix_aaindex_tooManyRows()
420 throws MalformedURLException,
423 String data = "H MyTest\n" + "M rows=ABC, cols=ABC\n" + "A\t1.0\n"
424 + "B\t4.0\t5.0\n" + "C\t7.0\t8.0\t9.0\n" + "C\t7.0\t8.0\t9.0\n";
425 FileParse fp = new FileParse(data, DataSourceType.PASTE);
426 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
429 parser.parseMatrix();
430 fail("Expected exception");
431 } catch (FileFormatException e)
433 assertEquals(e.getMessage(), "Too many data rows in matrix file");
437 @Test(groups = "Functional")
438 public void testParseMatrix_aaindex_extraDataLines()
439 throws MalformedURLException,
442 String data = "H MyTest\n" + "M rows=ABC, cols=ABC\n" + "A\t1.0\n"
443 + "B\t4.0\t5.0\n" + "C\t7.0\t8.0\t9.0\n" + "something extra\n";
444 FileParse fp = new FileParse(data, DataSourceType.PASTE);
445 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
448 parser.parseMatrix();
449 fail("Expected exception");
450 } catch (FileFormatException e)
452 assertEquals(e.getMessage(), "Too many data rows in matrix file");
456 @Test(groups = "Functional")
457 public void testParseMatrix_aaindex_tooFewColumns()
458 throws MalformedURLException,
461 String data = "H MyTest\n" + "M rows=ABC, cols=ABC\n" + "A\t1.0\n"
462 + "B\t4.0\t5.0\n" + "C\t7.0\t8.0\n";
463 FileParse fp = new FileParse(data, DataSourceType.PASTE);
464 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
467 parser.parseMatrix();
468 fail("Expected exception");
469 } catch (FileFormatException e)
473 "Expected 3 scores at line 5: 'C\t7.0\t8.0' but found 2");
478 * Test a successful parse and register of a score matrix file
480 * @throws IOException
481 * @throws MalformedURLException
483 @Test(groups = "Functional")
484 public void testParse_ncbiFormat() throws MalformedURLException,
487 assertNull(ScoreModels.getInstance().getScoreModel("MyNewTest", null));
489 String data = "ScoreMatrix MyNewTest\n" + "\tA\tB\tC\n"
490 + "A\t1.0\t2.0\t3.0\n" + "B\t4.0\t5.0\t6.0\n"
491 + "C\t7.0\t8.0\t9.0\n";
492 FileParse fp = new FileParse(data, DataSourceType.PASTE);
493 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
497 ScoreMatrix sm = (ScoreMatrix) ScoreModels.getInstance().getScoreModel(
500 assertEquals(sm.getName(), "MyNewTest");
501 assertEquals(parser.getMatrixName(), "MyNewTest");
502 assertEquals(sm.getPairwiseScore('A', 'A'), 1.0f);
503 assertEquals(sm.getPairwiseScore('B', 'c'), 6.0f);
504 assertEquals(sm.getSize(), 3);