b7da5e43e8524ad41cdc15fd1b33c473ff6c6f9e
[jalview.git] / test / jalview / io / TCoffeeScoreFileTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.io.TCoffeeScoreFile.Block;
24 import jalview.io.TCoffeeScoreFile.Header;
25
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.util.List;
30
31 import org.testng.AssertJUnit;
32 import org.testng.annotations.Test;
33
34 public class TCoffeeScoreFileTest
35 {
36
37   final static File SCORE_FILE = new File(
38           "test/jalview/io/tcoffee.score_ascii");
39
40   final static File ALIGN_FILE = new File(
41           "test/jalview/io/tcoffee.fasta_aln");
42
43   @Test
44   public void testReadHeader() throws IOException
45   {
46
47     TCoffeeScoreFile scoreFile = new TCoffeeScoreFile(SCORE_FILE.getPath(),
48             AppletFormatAdapter.FILE);
49     AssertJUnit.assertTrue(scoreFile.getWarningMessage(), scoreFile.isValid());
50
51     Header header = scoreFile.header;
52     AssertJUnit.assertNotNull(header);
53     AssertJUnit.assertEquals(
54             "T-COFFEE, Version_9.02.r1228 (2012-02-16 18:15:12 - Revision 1228 - Build 336)",
55             header.head);
56     AssertJUnit.assertEquals(90, header.score);
57     AssertJUnit.assertEquals(89, header.getScoreFor("1PHT"));
58     AssertJUnit.assertEquals(90, header.getScoreFor("1BB9"));
59     AssertJUnit.assertEquals(94, header.getScoreFor("1UHC"));
60     AssertJUnit.assertEquals(94, header.getScoreFor("1YCS"));
61     AssertJUnit.assertEquals(93, header.getScoreFor("1OOT"));
62     AssertJUnit.assertEquals(94, header.getScoreFor("1ABO"));
63     AssertJUnit.assertEquals(94, header.getScoreFor("1FYN"));
64     AssertJUnit.assertEquals(94, header.getScoreFor("1QCF"));
65     AssertJUnit.assertEquals(90, header.getScoreFor("cons"));
66   }
67
68   @Test
69   public void testWrongFile()
70   {
71     try
72     {
73       TCoffeeScoreFile result = new TCoffeeScoreFile(ALIGN_FILE.getPath(),
74               FormatAdapter.FILE);
75       AssertJUnit.assertFalse(result.isValid());
76     } catch (IOException x)
77     {
78       AssertJUnit.assertTrue("File not found exception thrown",
79               x instanceof FileNotFoundException);
80     }
81   }
82
83   @Test
84   public void testHeightAndWidth() throws IOException
85   {
86     TCoffeeScoreFile result = new TCoffeeScoreFile(SCORE_FILE.getPath(),
87             FormatAdapter.FILE);
88     AssertJUnit.assertTrue(result.isValid());
89     AssertJUnit.assertEquals(8, result.getHeight());
90     AssertJUnit.assertEquals(83, result.getWidth());
91   }
92
93   @Test
94   public void testReadBlock() throws IOException
95   {
96
97     String BLOCK = "\n" + "\n" + "\n"
98             + "1PHT   999999999999999999999999998762112222543211112134\n"
99             + "1BB9   99999999999999999999999999987-------4322----2234  \n"
100             + "1UHC   99999999999999999999999999987-------5321----2246\n"
101             + "1YCS   99999999999999999999999999986-------4321----1-35\n"
102             + "1OOT   999999999999999999999999999861-------3------1135  \n"
103             + "1ABO   99999999999999999999999999986-------422-------34\n"
104             + "1FYN   99999999999999999999999999985-------32--------35\n"
105             + "1QCF   99999999999999999999999999974-------2---------24\n"
106             + "cons   999999999999999999999999999851000110321100001134\n"
107             + "\n" + "\n";
108     FileParse source = new FileParse(BLOCK, FormatAdapter.PASTE);
109     Block block = TCoffeeScoreFile.readBlock(source, 0);
110
111     AssertJUnit.assertNotNull(block);
112     AssertJUnit.assertEquals("999999999999999999999999998762112222543211112134",
113             block.getScoresFor("1PHT"));
114     AssertJUnit.assertEquals("99999999999999999999999999987-------4322----2234",
115             block.getScoresFor("1BB9"));
116     AssertJUnit.assertEquals("99999999999999999999999999987-------5321----2246",
117             block.getScoresFor("1UHC"));
118     AssertJUnit.assertEquals("99999999999999999999999999986-------4321----1-35",
119             block.getScoresFor("1YCS"));
120     AssertJUnit.assertEquals("999999999999999999999999999861-------3------1135",
121             block.getScoresFor("1OOT"));
122     AssertJUnit.assertEquals("99999999999999999999999999986-------422-------34",
123             block.getScoresFor("1ABO"));
124     AssertJUnit.assertEquals("99999999999999999999999999985-------32--------35",
125             block.getScoresFor("1FYN"));
126     AssertJUnit.assertEquals("99999999999999999999999999974-------2---------24",
127             block.getScoresFor("1QCF"));
128     AssertJUnit.assertEquals("999999999999999999999999999851000110321100001134",
129             block.getConsensus());
130   }
131
132   @Test
133   public void testParse() throws IOException
134   {
135
136     TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),
137             FormatAdapter.FILE);
138
139     AssertJUnit.assertEquals(
140             "999999999999999999999999998762112222543211112134----------5666642367889999999999889",
141             parser.getScoresFor("1PHT"));
142     AssertJUnit.assertEquals(
143             "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889",
144             parser.getScoresFor("1BB9"));
145     AssertJUnit.assertEquals(
146             "99999999999999999999999999987-------5321----2246----------788774--66789999999999889",
147             parser.getScoresFor("1UHC"));
148     AssertJUnit.assertEquals(
149             "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889",
150             parser.getScoresFor("1YCS"));
151     AssertJUnit.assertEquals(
152             "999999999999999999999999999861-------3------1135----------78877--356789999999997-67",
153             parser.getScoresFor("1OOT"));
154     AssertJUnit.assertEquals(
155             "99999999999999999999999999986-------422-------34----------687774--56779999999999889",
156             parser.getScoresFor("1ABO"));
157     AssertJUnit.assertEquals(
158             "99999999999999999999999999985-------32--------35----------6888842356789999999999889",
159             parser.getScoresFor("1FYN"));
160     AssertJUnit.assertEquals(
161             "99999999999999999999999999974-------2---------24----------6878742356789999999999889",
162             parser.getScoresFor("1QCF"));
163     AssertJUnit.assertEquals(
164             "99999999999999999999999999985100011032110000113400100000006877641356789999999999889",
165             parser.getScoresFor("cons"));
166   }
167
168   @Test
169   public void testGetAsList() throws IOException
170   {
171
172     TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),
173             FormatAdapter.FILE);
174     AssertJUnit.assertTrue(parser.getWarningMessage(), parser.isValid());
175     List<String> scores = parser.getScoresList();
176     AssertJUnit.assertEquals(
177             "999999999999999999999999998762112222543211112134----------5666642367889999999999889",
178             scores.get(0));
179     AssertJUnit.assertEquals(
180             "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889",
181             scores.get(1));
182     AssertJUnit.assertEquals(
183             "99999999999999999999999999987-------5321----2246----------788774--66789999999999889",
184             scores.get(2));
185     AssertJUnit.assertEquals(
186             "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889",
187             scores.get(3));
188     AssertJUnit.assertEquals(
189             "999999999999999999999999999861-------3------1135----------78877--356789999999997-67",
190             scores.get(4));
191     AssertJUnit.assertEquals(
192             "99999999999999999999999999986-------422-------34----------687774--56779999999999889",
193             scores.get(5));
194     AssertJUnit.assertEquals(
195             "99999999999999999999999999985-------32--------35----------6888842356789999999999889",
196             scores.get(6));
197     AssertJUnit.assertEquals(
198             "99999999999999999999999999974-------2---------24----------6878742356789999999999889",
199             scores.get(7));
200     AssertJUnit.assertEquals(
201             "99999999999999999999999999985100011032110000113400100000006877641356789999999999889",
202             scores.get(8));
203
204   }
205
206   @Test
207   public void testGetAsArray() throws IOException
208   {
209
210     TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),
211             FormatAdapter.FILE);
212     AssertJUnit.assertTrue(parser.getWarningMessage(), parser.isValid());
213     byte[][] scores = parser.getScoresArray();
214
215     AssertJUnit.assertEquals(9, scores[0][0]);
216     AssertJUnit.assertEquals(9, scores[1][0]);
217     AssertJUnit.assertEquals(9, scores[2][0]);
218     AssertJUnit.assertEquals(9, scores[3][0]);
219     AssertJUnit.assertEquals(9, scores[4][0]);
220     AssertJUnit.assertEquals(9, scores[5][0]);
221     AssertJUnit.assertEquals(9, scores[6][0]);
222     AssertJUnit.assertEquals(9, scores[7][0]);
223     AssertJUnit.assertEquals(9, scores[8][0]);
224
225     AssertJUnit.assertEquals(5, scores[0][36]);
226     AssertJUnit.assertEquals(4, scores[1][36]);
227     AssertJUnit.assertEquals(5, scores[2][36]);
228     AssertJUnit.assertEquals(4, scores[3][36]);
229     AssertJUnit.assertEquals(-1, scores[4][36]);
230     AssertJUnit.assertEquals(4, scores[5][36]);
231     AssertJUnit.assertEquals(3, scores[6][36]);
232     AssertJUnit.assertEquals(2, scores[7][36]);
233     AssertJUnit.assertEquals(3, scores[8][36]);
234
235   }
236
237   @Test
238   public void testHeightAndWidthWithResidueNumbers() throws Exception
239   {
240     String file = "test/jalview/io/tcoffee.score_ascii_with_residue_numbers";
241     TCoffeeScoreFile result = new TCoffeeScoreFile(file, FormatAdapter.FILE);
242     AssertJUnit.assertTrue(result.isValid());
243     AssertJUnit.assertEquals(5, result.getHeight());
244     AssertJUnit.assertEquals(84, result.getWidth());
245   }
246
247 }