3147054460f73962b42145e6004369ae743421cb
[jalview.git] / test / jalview / io / TCoffeeScoreFileTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.io;
19
20 import static org.junit.Assert.*;
21 import jalview.io.TCoffeeScoreFile.Block;
22 import jalview.io.TCoffeeScoreFile.Header;
23
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.util.List;
28
29 import org.junit.Test;
30
31 public class TCoffeeScoreFileTest {
32
33         final static File SCORE_FILE = new File("test/jalview/io/tcoffee.score_ascii");
34     final static File ALIGN_FILE = new File("test/jalview/io/tcoffee.fasta_aln");
35         
36         @Test
37         public void testReadHeader() throws IOException, FileNotFoundException {
38
39             TCoffeeScoreFile scoreFile = new TCoffeeScoreFile(SCORE_FILE.getPath(),AppletFormatAdapter.FILE);
40             assertTrue(scoreFile.getWarningMessage(),scoreFile.isValid());
41                 
42             Header header = scoreFile.header;
43                 assertNotNull(header);
44                 assertEquals( "T-COFFEE, Version_9.02.r1228 (2012-02-16 18:15:12 - Revision 1228 - Build 336)", header.head );
45                 assertEquals( 90, header.score );
46                 assertEquals( 89, header.getScoreFor("1PHT") );
47                 assertEquals( 90, header.getScoreFor("1BB9") );
48                 assertEquals( 94, header.getScoreFor("1UHC") );
49                 assertEquals( 94, header.getScoreFor("1YCS") );
50                 assertEquals( 93, header.getScoreFor("1OOT") );
51                 assertEquals( 94, header.getScoreFor("1ABO") );
52                 assertEquals( 94, header.getScoreFor("1FYN") );
53                 assertEquals( 94, header.getScoreFor("1QCF") );
54                 assertEquals( 90, header.getScoreFor("cons") );
55         }
56         
57         
58         @Test
59         public void testWrongFile() {
60             try {
61                         TCoffeeScoreFile result = new TCoffeeScoreFile(ALIGN_FILE.getPath(), FormatAdapter.FILE);
62                         assertFalse(result.isValid());
63             } 
64             catch (IOException x)
65             {
66               assertTrue("File not found exception thrown",x instanceof FileNotFoundException);
67             }
68         } 
69
70         @Test
71         public void testHeightAndWidth() throws IOException {
72                 TCoffeeScoreFile result = new TCoffeeScoreFile(SCORE_FILE.getPath(), FormatAdapter.FILE);
73                 assertTrue(result.isValid());
74                 assertEquals( 8, result.getHeight() );
75                 assertEquals( 83, result.getWidth() );
76         }       
77         
78         @Test
79         public void testReadBlock( ) throws IOException {
80                 
81                 String BLOCK = "\n" +
82                                 "\n" +
83                                 "\n" +
84                                 "1PHT   999999999999999999999999998762112222543211112134\n" +
85                                 "1BB9   99999999999999999999999999987-------4322----2234  \n" +
86                                 "1UHC   99999999999999999999999999987-------5321----2246\n" +
87                                 "1YCS   99999999999999999999999999986-------4321----1-35\n" +
88                                 "1OOT   999999999999999999999999999861-------3------1135  \n" +
89                                 "1ABO   99999999999999999999999999986-------422-------34\n" +
90                                 "1FYN   99999999999999999999999999985-------32--------35\n" +
91                                 "1QCF   99999999999999999999999999974-------2---------24\n" +
92                                 "cons   999999999999999999999999999851000110321100001134\n" +
93                                 "\n" +
94                                 "\n";
95                 FileParse source=new FileParse(BLOCK, FormatAdapter.PASTE);
96                 Block block = TCoffeeScoreFile.readBlock(source, 0);
97
98                 assertNotNull(block);
99                 assertEquals( "999999999999999999999999998762112222543211112134", block.getScoresFor("1PHT") );
100                 assertEquals( "99999999999999999999999999987-------4322----2234", block.getScoresFor("1BB9") );
101                 assertEquals( "99999999999999999999999999987-------5321----2246", block.getScoresFor("1UHC") );
102                 assertEquals( "99999999999999999999999999986-------4321----1-35", block.getScoresFor("1YCS") );
103                 assertEquals( "999999999999999999999999999861-------3------1135", block.getScoresFor("1OOT") );
104                 assertEquals( "99999999999999999999999999986-------422-------34", block.getScoresFor("1ABO") );
105                 assertEquals( "99999999999999999999999999985-------32--------35", block.getScoresFor("1FYN") );
106                 assertEquals( "99999999999999999999999999974-------2---------24", block.getScoresFor("1QCF") );
107                 assertEquals( "999999999999999999999999999851000110321100001134", block.getConsensus() );
108         }
109
110         @Test
111         public void testParse() throws IOException {
112
113                 TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(), FormatAdapter.FILE);
114
115                 assertEquals( "999999999999999999999999998762112222543211112134----------5666642367889999999999889", parser.getScoresFor("1PHT") );
116                 assertEquals( "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889", parser.getScoresFor("1BB9") );
117                 assertEquals( "99999999999999999999999999987-------5321----2246----------788774--66789999999999889", parser.getScoresFor("1UHC") );
118                 assertEquals( "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889", parser.getScoresFor("1YCS") );
119                 assertEquals( "999999999999999999999999999861-------3------1135----------78877--356789999999997-67", parser.getScoresFor("1OOT") );
120                 assertEquals( "99999999999999999999999999986-------422-------34----------687774--56779999999999889", parser.getScoresFor("1ABO") );
121                 assertEquals( "99999999999999999999999999985-------32--------35----------6888842356789999999999889", parser.getScoresFor("1FYN") );
122                 assertEquals( "99999999999999999999999999974-------2---------24----------6878742356789999999999889", parser.getScoresFor("1QCF") );
123                 assertEquals( "99999999999999999999999999985100011032110000113400100000006877641356789999999999889", parser.getScoresFor("cons") );             
124         }
125
126         
127         @Test
128         public void testGetAsList() throws IOException {
129                 
130           TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),FormatAdapter.FILE);
131           assertTrue(parser.getWarningMessage(),parser.isValid());
132                 List<String> scores = parser.getScoresList();
133                 assertEquals( "999999999999999999999999998762112222543211112134----------5666642367889999999999889", scores.get(0) );
134                 assertEquals( "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889", scores.get(1) );
135                 assertEquals( "99999999999999999999999999987-------5321----2246----------788774--66789999999999889", scores.get(2) );
136                 assertEquals( "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889", scores.get(3) );
137                 assertEquals( "999999999999999999999999999861-------3------1135----------78877--356789999999997-67", scores.get(4) );
138                 assertEquals( "99999999999999999999999999986-------422-------34----------687774--56779999999999889", scores.get(5) );
139                 assertEquals( "99999999999999999999999999985-------32--------35----------6888842356789999999999889", scores.get(6) );
140                 assertEquals( "99999999999999999999999999974-------2---------24----------6878742356789999999999889", scores.get(7) );
141                 assertEquals( "99999999999999999999999999985100011032110000113400100000006877641356789999999999889", scores.get(8) );           
142                 
143         } 
144         
145         
146         @Test
147         public void testGetAsArray() throws IOException {
148                 
149           TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),FormatAdapter.FILE);
150           assertTrue(parser.getWarningMessage(),parser.isValid());
151                 byte[][] scores = parser.getScoresArray();
152         
153                 assertEquals( 9, scores[0][0] );
154                 assertEquals( 9, scores[1][0] );
155                 assertEquals( 9, scores[2][0] );
156                 assertEquals( 9, scores[3][0] );
157                 assertEquals( 9, scores[4][0] );
158                 assertEquals( 9, scores[5][0] );
159                 assertEquals( 9, scores[6][0] );
160                 assertEquals( 9, scores[7][0] );
161                 assertEquals( 9, scores[8][0] );
162                 
163                 assertEquals( 5, scores[0][36] );
164                 assertEquals( 4, scores[1][36] );
165                 assertEquals( 5, scores[2][36] );
166                 assertEquals( 4, scores[3][36] );
167                 assertEquals( -1, scores[4][36] );
168                 assertEquals( 4, scores[5][36] );
169                 assertEquals( 3, scores[6][36] );
170                 assertEquals( 2, scores[7][36] );
171                 assertEquals( 3, scores[8][36] );
172                 
173         } 
174         
175         @Test
176         public void testHeightAndWidthWithResidueNumbers() throws IOException {
177                 String file = "test/jalview/io/tcoffee.score_ascii_with_residue_numbers";
178                 TCoffeeScoreFile result = new TCoffeeScoreFile(file, FormatAdapter.FILE);
179                 assertTrue(result.isValid());
180                 assertEquals( 5, result.getHeight() );
181                 assertEquals( 84, result.getWidth() );
182         }       
183         
184         
185         
186 }