last version stay many bugs ..
[jalview.git] / test / jalview / io / TCoffeeScoreFileTest.java
1 package jalview.io;
2
3 import static org.junit.Assert.*;
4 import jalview.io.TCoffeeScoreFile.Block;
5 import jalview.io.TCoffeeScoreFile.Header;
6
7 import java.io.BufferedReader;
8 import java.io.File;
9 import java.io.FileNotFoundException;
10 import java.io.FileReader;
11 import java.io.IOException;
12 import java.io.StringReader;
13 import java.util.List;
14
15 import javax.xml.parsers.ParserConfigurationException;
16
17 import org.junit.Test;
18 import org.xml.sax.SAXException;
19
20 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
21 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
22 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
23 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
24
25 public class TCoffeeScoreFileTest {
26
27         final static File SCORE_FILE = new File("test/jalview/io/tcoffee.score_ascii");
28         final static File ALIGN_FILE = new File("test/jalview/io/tcoffee.fasta_aln");
29         
30         @Test
31         public void testReadHeader() throws IOException, FileNotFoundException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses {
32
33                 TCoffeeScoreFile scoreFile = new TCoffeeScoreFile(SCORE_FILE.getPath(),AppletFormatAdapter.FILE);
34                 assertTrue(scoreFile.getWarningMessage(),scoreFile.isValid());
35                 Header header = scoreFile.header;
36                 assertNotNull(header);
37                 assertEquals( "T-COFFEE, Version_9.02.r1228 (2012-02-16 18:15:12 - Revision 1228 - Build 336)", header.head );
38                 assertEquals( 90, header.score );
39                 assertEquals( 89, header.getScoreFor("1PHT") );
40                 assertEquals( 90, header.getScoreFor("1BB9") );
41                 assertEquals( 94, header.getScoreFor("1UHC") );
42                 assertEquals( 94, header.getScoreFor("1YCS") );
43                 assertEquals( 93, header.getScoreFor("1OOT") );
44                 assertEquals( 94, header.getScoreFor("1ABO") );
45                 assertEquals( 94, header.getScoreFor("1FYN") );
46                 assertEquals( 94, header.getScoreFor("1QCF") );
47                 assertEquals( 90, header.getScoreFor("cons") );
48         }
49         
50         
51         @Test
52         public void testWrongFile() throws ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses {
53             try {
54                 TCoffeeScoreFile result = new TCoffeeScoreFile(ALIGN_FILE.getPath(), FormatAdapter.FILE);
55                 assertFalse(result.isValid());
56             } catch (IOException x)
57             {
58               assertTrue("File not found exception thrown",x instanceof FileNotFoundException);
59             }
60         } 
61
62         @Test
63         public void testHeightAndWidth() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses {
64                 TCoffeeScoreFile result = new TCoffeeScoreFile(SCORE_FILE.getPath(), FormatAdapter.FILE);
65                 assertTrue(result.isValid());
66                 assertEquals( 8, result.getHeight() );
67                 assertEquals( 83, result.getWidth() );
68         }       
69         
70         @Test
71         public void testReadBlock( ) throws IOException {
72                 
73                 String BLOCK = "\n" +
74                                 "\n" +
75                                 "\n" +
76                                 "1PHT   999999999999999999999999998762112222543211112134\n" +
77                                 "1BB9   99999999999999999999999999987-------4322----2234  \n" +
78                                 "1UHC   99999999999999999999999999987-------5321----2246\n" +
79                                 "1YCS   99999999999999999999999999986-------4321----1-35\n" +
80                                 "1OOT   999999999999999999999999999861-------3------1135  \n" +
81                                 "1ABO   99999999999999999999999999986-------422-------34\n" +
82                                 "1FYN   99999999999999999999999999985-------32--------35\n" +
83                                 "1QCF   99999999999999999999999999974-------2---------24\n" +
84                                 "cons   999999999999999999999999999851000110321100001134\n" +
85                                 "\n" +
86                                 "\n";
87                 FileParse source=new FileParse(BLOCK, FormatAdapter.PASTE);
88                 Block block = TCoffeeScoreFile.readBlock(source, 0);
89
90                 assertNotNull(block);
91                 assertEquals( "999999999999999999999999998762112222543211112134", block.getScoresFor("1PHT") );
92                 assertEquals( "99999999999999999999999999987-------4322----2234", block.getScoresFor("1BB9") );
93                 assertEquals( "99999999999999999999999999987-------5321----2246", block.getScoresFor("1UHC") );
94                 assertEquals( "99999999999999999999999999986-------4321----1-35", block.getScoresFor("1YCS") );
95                 assertEquals( "999999999999999999999999999861-------3------1135", block.getScoresFor("1OOT") );
96                 assertEquals( "99999999999999999999999999986-------422-------34", block.getScoresFor("1ABO") );
97                 assertEquals( "99999999999999999999999999985-------32--------35", block.getScoresFor("1FYN") );
98                 assertEquals( "99999999999999999999999999974-------2---------24", block.getScoresFor("1QCF") );
99                 assertEquals( "999999999999999999999999999851000110321100001134", block.getConsensus() );
100         }
101
102         @Test
103         public void testParse() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses {
104
105                 TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(), FormatAdapter.FILE);
106
107                 assertEquals( "999999999999999999999999998762112222543211112134----------5666642367889999999999889", parser.getScoresFor("1PHT") );
108                 assertEquals( "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889", parser.getScoresFor("1BB9") );
109                 assertEquals( "99999999999999999999999999987-------5321----2246----------788774--66789999999999889", parser.getScoresFor("1UHC") );
110                 assertEquals( "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889", parser.getScoresFor("1YCS") );
111                 assertEquals( "999999999999999999999999999861-------3------1135----------78877--356789999999997-67", parser.getScoresFor("1OOT") );
112                 assertEquals( "99999999999999999999999999986-------422-------34----------687774--56779999999999889", parser.getScoresFor("1ABO") );
113                 assertEquals( "99999999999999999999999999985-------32--------35----------6888842356789999999999889", parser.getScoresFor("1FYN") );
114                 assertEquals( "99999999999999999999999999974-------2---------24----------6878742356789999999999889", parser.getScoresFor("1QCF") );
115                 assertEquals( "99999999999999999999999999985100011032110000113400100000006877641356789999999999889", parser.getScoresFor("cons") );             
116         }
117
118         
119         @Test
120         public void testGetAsList() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses {
121                 
122           TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),FormatAdapter.FILE);
123           assertTrue(parser.getWarningMessage(),parser.isValid());
124                 List<String> scores = parser.getScoresList();
125                 assertEquals( "999999999999999999999999998762112222543211112134----------5666642367889999999999889", scores.get(0) );
126                 assertEquals( "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889", scores.get(1) );
127                 assertEquals( "99999999999999999999999999987-------5321----2246----------788774--66789999999999889", scores.get(2) );
128                 assertEquals( "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889", scores.get(3) );
129                 assertEquals( "999999999999999999999999999861-------3------1135----------78877--356789999999997-67", scores.get(4) );
130                 assertEquals( "99999999999999999999999999986-------422-------34----------687774--56779999999999889", scores.get(5) );
131                 assertEquals( "99999999999999999999999999985-------32--------35----------6888842356789999999999889", scores.get(6) );
132                 assertEquals( "99999999999999999999999999974-------2---------24----------6878742356789999999999889", scores.get(7) );
133                 assertEquals( "99999999999999999999999999985100011032110000113400100000006877641356789999999999889", scores.get(8) );           
134                 
135         } 
136         
137         
138         @Test
139         public void testGetAsArray() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses {
140                 
141           TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),FormatAdapter.FILE);
142           assertTrue(parser.getWarningMessage(),parser.isValid());
143                 byte[][] scores = parser.getScoresArray();
144         
145                 assertEquals( 9, scores[0][0] );
146                 assertEquals( 9, scores[1][0] );
147                 assertEquals( 9, scores[2][0] );
148                 assertEquals( 9, scores[3][0] );
149                 assertEquals( 9, scores[4][0] );
150                 assertEquals( 9, scores[5][0] );
151                 assertEquals( 9, scores[6][0] );
152                 assertEquals( 9, scores[7][0] );
153                 assertEquals( 9, scores[8][0] );
154                 
155                 assertEquals( 5, scores[0][36] );
156                 assertEquals( 4, scores[1][36] );
157                 assertEquals( 5, scores[2][36] );
158                 assertEquals( 4, scores[3][36] );
159                 assertEquals( -1, scores[4][36] );
160                 assertEquals( 4, scores[5][36] );
161                 assertEquals( 3, scores[6][36] );
162                 assertEquals( 2, scores[7][36] );
163                 assertEquals( 3, scores[8][36] );
164                 
165         } 
166         
167         
168         
169 }