Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.io

File ScoreMatrixFileTest.java

 

Code metrics

0
174
20
1
506
414
38
0.22
8.7
20
1.9

Classes

Class Line # Actions
ScoreMatrixFileTest 18 174 38 18
0.907216590.7%
 

Contributing tests

This file is covered by 20 tests. .

Source view

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.assertNull;
7    import static org.testng.Assert.assertTrue;
8    import static org.testng.Assert.fail;
9   
10    import jalview.analysis.scoremodels.ScoreMatrix;
11    import jalview.analysis.scoremodels.ScoreModels;
12   
13    import java.io.IOException;
14    import java.net.MalformedURLException;
15   
16    import org.testng.annotations.Test;
17   
 
18    public class ScoreMatrixFileTest
19    {
20   
21    /**
22    * Test a successful parse of a (small) score matrix file
23    *
24    * @throws IOException
25    * @throws MalformedURLException
26    */
 
27  1 toggle @Test(groups = "Functional")
28    public void testParseMatrix_ncbiMixedDelimiters()
29    throws MalformedURLException,
30    IOException
31    {
32    /*
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
36    */
37  1 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  1 FileParse fp = new FileParse(data, DataSourceType.PASTE);
45  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
46  1 ScoreMatrix sm = parser.parseMatrix();
47   
48  1 assertNotNull(sm);
49  1 assertEquals(sm.getName(), "MyTest (example)");
50  1 assertEquals(sm.getSize(), 6);
51  1 assertNull(sm.getDescription());
52  1 assertTrue(sm.isDNA());
53  1 assertFalse(sm.isProtein());
54  1 assertEquals(sm.getMinimumScore(), 1.1f);
55  1 assertEquals(sm.getPairwiseScore('A', 'A'), 1.1f);
56  1 assertEquals(sm.getPairwiseScore('A', 'T'), 1.2f);
57  1 assertEquals(sm.getPairwiseScore('a', 'T'), 1.2f); // A/a equivalent
58  1 assertEquals(sm.getPairwiseScore('A', 't'), 1.4f); // T/t not equivalent
59  1 assertEquals(sm.getPairwiseScore('a', 't'), 1.4f);
60  1 assertEquals(sm.getPairwiseScore('U', 'x'), 3.5f);
61  1 assertEquals(sm.getPairwiseScore('u', 'x'), 3.5f);
62    // X (upper) and '.' unmapped - get minimum score
63  1 assertEquals(sm.getPairwiseScore('U', 'X'), 1.1f);
64  1 assertEquals(sm.getPairwiseScore('A', '.'), 1.1f);
65  1 assertEquals(sm.getPairwiseScore('-', '-'), 7.6f);
66  1 assertEquals(sm.getPairwiseScore('A', (char) 128), 0f); // out of range
67    }
68   
 
69  1 toggle @Test(groups = "Functional")
70    public void testParseMatrix_headerMissing()
71    {
72  1 String data;
73   
74  1 data = "X Y\n1 2\n3 4\n";
75  1 try
76    {
77  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
78    .parseMatrix();
79  0 fail("expected exception");
80    } catch (IOException e)
81    {
82  1 assertEquals(e.getMessage(),
83    "Format error: 'ScoreMatrix <name>' should be the first non-comment line");
84    }
85    }
86   
 
87  1 toggle @Test(groups = "Functional")
88    public void testParseMatrix_ncbiNotEnoughRows()
89    {
90  1 String data = "ScoreMatrix MyTest\nX Y Z\n1 2 3\n4 5 6\n";
91  1 try
92    {
93  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
94    .parseMatrix();
95  0 fail("expected exception");
96    } catch (IOException e)
97    {
98  1 assertEquals(e.getMessage(),
99    "Expected 3 rows of score data in score matrix but only found 2");
100    }
101    }
102   
 
103  1 toggle @Test(groups = "Functional")
104    public void testParseMatrix_ncbiNotEnoughColumns()
105    {
106  1 String data = "ScoreMatrix MyTest\nX Y Z\n1 2 3\n4 5\n7 8 9\n";
107  1 try
108    {
109  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
110    .parseMatrix();
111  0 fail("expected exception");
112    } catch (IOException e)
113    {
114  1 assertEquals(e.getMessage(),
115    "Expected 3 scores at line 4: '4 5' but found 2");
116    }
117    }
118   
 
119  1 toggle @Test(groups = "Functional")
120    public void testParseMatrix_ncbiTooManyColumns()
121    {
122    /*
123    * with two too many columns:
124    */
125  1 String data = "ScoreMatrix MyTest\nX\tY\tZ\n1 2 3\n4 5 6 7\n8 9 10\n";
126  1 try
127    {
128  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
129    .parseMatrix();
130  0 fail("expected exception");
131    } catch (IOException e)
132    {
133  1 assertEquals(e.getMessage(),
134    "Expected 3 scores at line 4: '4 5 6 7' but found 4");
135    }
136   
137    /*
138    * with guide character and one too many columns:
139    */
140  1 data = "ScoreMatrix MyTest\nX Y\nX 1 2\nY 3 4 5\n";
141  1 try
142    {
143  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
144    .parseMatrix();
145  0 fail("expected exception");
146    } catch (IOException e)
147    {
148  1 assertEquals(e.getMessage(),
149    "Expected 2 scores at line 4: 'Y 3 4 5' but found 3");
150    }
151   
152    /*
153    * with no guide character and one too many columns
154    */
155  1 data = "ScoreMatrix MyTest\nX Y\n1 2\n3 4 5\n";
156  1 try
157    {
158  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
159    .parseMatrix();
160  0 fail("expected exception");
161    } catch (IOException e)
162    {
163  1 assertEquals(e.getMessage(),
164    "Expected 2 scores at line 4: '3 4 5' but found 3");
165    }
166    }
167   
 
168  1 toggle @Test(groups = "Functional")
169    public void testParseMatrix_ncbiTooManyRows()
170    {
171  1 String data = "ScoreMatrix MyTest\n\tX\tY\tZ\n1 2 3\n4 5 6\n7 8 9\n10 11 12\n";
172  1 try
173    {
174  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
175    .parseMatrix();
176  0 fail("expected exception");
177    } catch (IOException e)
178    {
179  1 assertEquals(e.getMessage(),
180    "Unexpected extra input line in score model file: '10 11 12'");
181    }
182    }
183   
 
184  1 toggle @Test(groups = "Functional")
185    public void testParseMatrix_ncbiBadDelimiter()
186    {
187  1 String data = "ScoreMatrix MyTest\n X Y Z\n1|2|3\n4|5|6\n";
188  1 try
189    {
190  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
191    .parseMatrix();
192  0 fail("expected exception");
193    } catch (IOException e)
194    {
195  1 assertEquals(e.getMessage(),
196    "Invalid score value '1|2|3' at line 3 column 0");
197    }
198    }
199   
 
200  1 toggle @Test(groups = "Functional")
201    public void testParseMatrix_ncbiBadFloat()
202    {
203  1 String data = "ScoreMatrix MyTest\n\tX\tY\tZ\n1 2 3\n4 five 6\n7 8 9\n";
204  1 try
205    {
206  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
207    .parseMatrix();
208  0 fail("expected exception");
209    } catch (IOException e)
210    {
211  1 assertEquals(e.getMessage(),
212    "Invalid score value 'five' at line 4 column 1");
213    }
214    }
215   
 
216  1 toggle @Test(groups = "Functional")
217    public void testParseMatrix_ncbiBadGuideCharacter()
218    {
219  1 String data = "ScoreMatrix MyTest\n\tX Y\nX 1 2\ny 3 4\n";
220  1 try
221    {
222  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
223    .parseMatrix();
224  0 fail("expected exception");
225    } catch (IOException e)
226    {
227  1 assertEquals(e.getMessage(),
228    "Error parsing score matrix at line 4, expected 'Y' but found 'y'");
229    }
230   
231  1 data = "ScoreMatrix MyTest\n\tX Y\nXX 1 2\nY 3 4\n";
232  1 try
233    {
234  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
235    .parseMatrix();
236  0 fail("expected exception");
237    } catch (IOException e)
238    {
239  1 assertEquals(e.getMessage(),
240    "Error parsing score matrix at line 3, expected 'X' but found 'XX'");
241    }
242    }
243   
 
244  1 toggle @Test(groups = "Functional")
245    public void testParseMatrix_ncbiNameMissing()
246    {
247    /*
248    * Name missing on ScoreMatrix header line
249    */
250  1 String data = "ScoreMatrix\nX Y\n1 2\n3 4\n";
251  1 try
252    {
253  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
254    .parseMatrix();
255  0 fail("expected exception");
256    } catch (IOException e)
257    {
258  1 assertEquals(
259    e.getMessage(),
260    "Format error: expected 'ScoreMatrix <name>', found 'ScoreMatrix' at line 1");
261    }
262    }
263   
264    /**
265    * Test a successful parse of a (small) score matrix file
266    *
267    * @throws IOException
268    * @throws MalformedURLException
269    */
 
270  1 toggle @Test(groups = "Functional")
271    public void testParseMatrix_ncbiFormat() throws MalformedURLException,
272    IOException
273    {
274    // input including comment and blank lines
275  1 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  1 FileParse fp = new FileParse(data, DataSourceType.PASTE);
279  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
280  1 ScoreMatrix sm = parser.parseMatrix();
281   
282  1 assertNotNull(sm);
283  1 assertEquals(sm.getName(), "MyTest");
284  1 assertEquals(parser.getMatrixName(), "MyTest");
285  1 assertEquals(sm.getPairwiseScore('A', 'A'), 1.0f);
286  1 assertEquals(sm.getPairwiseScore('B', 'c'), 6.0f);
287  1 assertEquals(sm.getSize(), 3);
288    }
289   
290    /**
291    * Test a successful parse of a (small) score matrix file
292    *
293    * @throws IOException
294    * @throws MalformedURLException
295    */
 
296  1 toggle @Test(groups = "Functional")
297    public void testParseMatrix_aaIndexBlosum80()
298    throws MalformedURLException,
299    IOException
300    {
301  1 FileParse fp = new FileParse("resources/scoreModel/blosum80.scm",
302    DataSourceType.FILE);
303  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
304  1 ScoreMatrix sm = parser.parseMatrix();
305   
306  1 assertNotNull(sm);
307  1 assertEquals(sm.getName(), "HENS920103");
308  1 assertEquals(sm.getDescription(),
309    "BLOSUM80 substitution matrix (Henikoff-Henikoff, 1992)");
310  1 assertFalse(sm.isDNA());
311  1 assertTrue(sm.isProtein());
312  1 assertEquals(20, sm.getSize());
313   
314  1 assertEquals(sm.getPairwiseScore('A', 'A'), 7f);
315  1 assertEquals(sm.getPairwiseScore('A', 'R'), -3f);
316  1 assertEquals(sm.getPairwiseScore('r', 'a'), -3f); // A/a equivalent
317    }
318   
319    /**
320    * Test a successful parse of a (small) score matrix file
321    *
322    * @throws IOException
323    * @throws MalformedURLException
324    */
 
325  1 toggle @Test(groups = "Functional")
326    public void testParseMatrix_aaindexFormat() throws MalformedURLException,
327    IOException
328    {
329    /*
330    * aaindex format has scores for diagonal and below only
331    */
332  1 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"
336    + "B\t4.0\t5.0\n"
337    + "C\t7.0\t8.0\t9.0\n";
338  1 FileParse fp = new FileParse(data, DataSourceType.PASTE);
339  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
340  1 ScoreMatrix sm = parser.parseMatrix();
341   
342  1 assertNotNull(sm);
343  1 assertEquals(sm.getSize(), 3);
344  1 assertEquals(sm.getName(), "MyTest");
345  1 assertEquals(sm.getDescription(), "My description");
346  1 assertEquals(sm.getPairwiseScore('A', 'A'), 1.0f);
347  1 assertEquals(sm.getPairwiseScore('A', 'B'), 4.0f);
348  1 assertEquals(sm.getPairwiseScore('A', 'C'), 7.0f);
349  1 assertEquals(sm.getPairwiseScore('B', 'A'), 4.0f);
350  1 assertEquals(sm.getPairwiseScore('B', 'B'), 5.0f);
351  1 assertEquals(sm.getPairwiseScore('B', 'C'), 8.0f);
352  1 assertEquals(sm.getPairwiseScore('C', 'C'), 9.0f);
353  1 assertEquals(sm.getPairwiseScore('C', 'B'), 8.0f);
354  1 assertEquals(sm.getPairwiseScore('C', 'A'), 7.0f);
355    }
356   
 
357  1 toggle @Test(groups = "Functional")
358    public void testParseMatrix_aaindex_mMissing()
359    throws MalformedURLException,
360    IOException
361    {
362    /*
363    * aaindex format but M cols=, rows= is missing
364    */
365  1 String data = "H MyTest\n" + "A\t1.0\n"
366    + "B\t4.0\t5.0\n"
367    + "C\t7.0\t8.0\t9.0\n";
368  1 FileParse fp = new FileParse(data, DataSourceType.PASTE);
369  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
370  1 try
371    {
372  1 parser.parseMatrix();
373  0 fail("Expected exception");
374    } catch (FileFormatException e)
375    {
376  1 assertEquals(e.getMessage(), "No alphabet specified in matrix file");
377    }
378    }
379   
 
380  1 toggle @Test(groups = "Functional")
381    public void testParseMatrix_aaindex_rowColMismatch()
382    throws MalformedURLException,
383    IOException
384    {
385  1 String data = "H MyTest\n" + "M rows=ABC, cols=ABD\n" + "A\t1.0\n"
386    + "B\t4.0\t5.0\n"
387    + "C\t7.0\t8.0\t9.0\n";
388  1 FileParse fp = new FileParse(data, DataSourceType.PASTE);
389  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
390  1 try
391    {
392  1 parser.parseMatrix();
393  0 fail("Expected exception");
394    } catch (FileFormatException e)
395    {
396  1 assertEquals(
397    e.getMessage(),
398    "Unexpected aaIndex score matrix data at line 2: M rows=ABC, cols=ABD rows != cols");
399    }
400    }
401   
 
402  1 toggle @Test(groups = "Functional")
403    public void testParseMatrix_ncbiHeaderRepeated()
404    {
405  1 String data = "ScoreMatrix BLOSUM\nScoreMatrix PAM250\nX Y\n1 2\n3 4\n";
406  1 try
407    {
408  1 new ScoreMatrixFile(new FileParse(data, DataSourceType.PASTE))
409    .parseMatrix();
410  0 fail("expected exception");
411    } catch (IOException e)
412    {
413  1 assertEquals(e.getMessage(),
414    "Error: 'ScoreMatrix' repeated in file at line 2");
415    }
416    }
417   
 
418  1 toggle @Test(groups = "Functional")
419    public void testParseMatrix_aaindex_tooManyRows()
420    throws MalformedURLException,
421    IOException
422    {
423  1 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  1 FileParse fp = new FileParse(data, DataSourceType.PASTE);
426  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
427  1 try
428    {
429  1 parser.parseMatrix();
430  0 fail("Expected exception");
431    } catch (FileFormatException e)
432    {
433  1 assertEquals(e.getMessage(), "Too many data rows in matrix file");
434    }
435    }
436   
 
437  1 toggle @Test(groups = "Functional")
438    public void testParseMatrix_aaindex_extraDataLines()
439    throws MalformedURLException,
440    IOException
441    {
442  1 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  1 FileParse fp = new FileParse(data, DataSourceType.PASTE);
445  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
446  1 try
447    {
448  1 parser.parseMatrix();
449  0 fail("Expected exception");
450    } catch (FileFormatException e)
451    {
452  1 assertEquals(e.getMessage(), "Too many data rows in matrix file");
453    }
454    }
455   
 
456  1 toggle @Test(groups = "Functional")
457    public void testParseMatrix_aaindex_tooFewColumns()
458    throws MalformedURLException,
459    IOException
460    {
461  1 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  1 FileParse fp = new FileParse(data, DataSourceType.PASTE);
464  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
465  1 try
466    {
467  1 parser.parseMatrix();
468  0 fail("Expected exception");
469    } catch (FileFormatException e)
470    {
471  1 assertEquals(
472    e.getMessage(),
473    "Expected 3 scores at line 5: 'C\t7.0\t8.0' but found 2");
474    }
475    }
476   
477    /**
478    * Test a successful parse and register of a score matrix file
479    *
480    * @throws IOException
481    * @throws MalformedURLException
482    */
 
483  1 toggle @Test(groups = "Functional")
484    public void testParse_ncbiFormat() throws MalformedURLException,
485    IOException
486    {
487  1 assertNull(ScoreModels.getInstance().getScoreModel("MyNewTest", null));
488   
489  1 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  1 FileParse fp = new FileParse(data, DataSourceType.PASTE);
493  1 ScoreMatrixFile parser = new ScoreMatrixFile(fp);
494   
495  1 parser.parse();
496   
497  1 ScoreMatrix sm = (ScoreMatrix) ScoreModels.getInstance().getScoreModel(
498    "MyNewTest", null);
499  1 assertNotNull(sm);
500  1 assertEquals(sm.getName(), "MyNewTest");
501  1 assertEquals(parser.getMatrixName(), "MyNewTest");
502  1 assertEquals(sm.getPairwiseScore('A', 'A'), 1.0f);
503  1 assertEquals(sm.getPairwiseScore('B', 'c'), 6.0f);
504  1 assertEquals(sm.getSize(), 3);
505    }
506    }