JAL-1620 version bump and release notes
[jalview.git] / test / jalview / io / TCoffeeScoreFileTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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 static org.junit.Assert.*;
24 import jalview.io.TCoffeeScoreFile.Block;
25 import jalview.io.TCoffeeScoreFile.Header;
26
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.util.List;
31
32 import javax.xml.parsers.ParserConfigurationException;
33
34 import org.junit.Test;
35 import org.xml.sax.SAXException;
36
37 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
38 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
39 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
40 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
41
42 public class TCoffeeScoreFileTest
43 {
44
45   final static File SCORE_FILE = new File(
46           "test/jalview/io/tcoffee.score_ascii");
47
48   final static File ALIGN_FILE = new File(
49           "test/jalview/io/tcoffee.fasta_aln");
50
51   @Test
52   public void testReadHeader() throws IOException
53   {
54
55     TCoffeeScoreFile scoreFile = new TCoffeeScoreFile(SCORE_FILE.getPath(),
56             AppletFormatAdapter.FILE);
57     assertTrue(scoreFile.getWarningMessage(), scoreFile.isValid());
58
59     Header header = scoreFile.header;
60     assertNotNull(header);
61     assertEquals(
62             "T-COFFEE, Version_9.02.r1228 (2012-02-16 18:15:12 - Revision 1228 - Build 336)",
63             header.head);
64     assertEquals(90, header.score);
65     assertEquals(89, header.getScoreFor("1PHT"));
66     assertEquals(90, header.getScoreFor("1BB9"));
67     assertEquals(94, header.getScoreFor("1UHC"));
68     assertEquals(94, header.getScoreFor("1YCS"));
69     assertEquals(93, header.getScoreFor("1OOT"));
70     assertEquals(94, header.getScoreFor("1ABO"));
71     assertEquals(94, header.getScoreFor("1FYN"));
72     assertEquals(94, header.getScoreFor("1QCF"));
73     assertEquals(90, header.getScoreFor("cons"));
74   }
75
76   @Test
77   public void testWrongFile()
78   {
79     try
80     {
81       TCoffeeScoreFile result = new TCoffeeScoreFile(ALIGN_FILE.getPath(),
82               FormatAdapter.FILE);
83       assertFalse(result.isValid());
84     } catch (IOException x)
85     {
86       assertTrue("File not found exception thrown",
87               x instanceof FileNotFoundException);
88     }
89   }
90
91   @Test
92   public void testHeightAndWidth() throws IOException
93   {
94     TCoffeeScoreFile result = new TCoffeeScoreFile(SCORE_FILE.getPath(),
95             FormatAdapter.FILE);
96     assertTrue(result.isValid());
97     assertEquals(8, result.getHeight());
98     assertEquals(83, result.getWidth());
99   }
100
101   @Test
102   public void testReadBlock() throws IOException
103   {
104
105     String BLOCK = "\n" + "\n" + "\n"
106             + "1PHT   999999999999999999999999998762112222543211112134\n"
107             + "1BB9   99999999999999999999999999987-------4322----2234  \n"
108             + "1UHC   99999999999999999999999999987-------5321----2246\n"
109             + "1YCS   99999999999999999999999999986-------4321----1-35\n"
110             + "1OOT   999999999999999999999999999861-------3------1135  \n"
111             + "1ABO   99999999999999999999999999986-------422-------34\n"
112             + "1FYN   99999999999999999999999999985-------32--------35\n"
113             + "1QCF   99999999999999999999999999974-------2---------24\n"
114             + "cons   999999999999999999999999999851000110321100001134\n"
115             + "\n" + "\n";
116     FileParse source = new FileParse(BLOCK, FormatAdapter.PASTE);
117     Block block = TCoffeeScoreFile.readBlock(source, 0);
118
119     assertNotNull(block);
120     assertEquals("999999999999999999999999998762112222543211112134",
121             block.getScoresFor("1PHT"));
122     assertEquals("99999999999999999999999999987-------4322----2234",
123             block.getScoresFor("1BB9"));
124     assertEquals("99999999999999999999999999987-------5321----2246",
125             block.getScoresFor("1UHC"));
126     assertEquals("99999999999999999999999999986-------4321----1-35",
127             block.getScoresFor("1YCS"));
128     assertEquals("999999999999999999999999999861-------3------1135",
129             block.getScoresFor("1OOT"));
130     assertEquals("99999999999999999999999999986-------422-------34",
131             block.getScoresFor("1ABO"));
132     assertEquals("99999999999999999999999999985-------32--------35",
133             block.getScoresFor("1FYN"));
134     assertEquals("99999999999999999999999999974-------2---------24",
135             block.getScoresFor("1QCF"));
136     assertEquals("999999999999999999999999999851000110321100001134",
137             block.getConsensus());
138   }
139
140   @Test
141   public void testParse() throws IOException
142   {
143
144     TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),
145             FormatAdapter.FILE);
146
147     assertEquals(
148             "999999999999999999999999998762112222543211112134----------5666642367889999999999889",
149             parser.getScoresFor("1PHT"));
150     assertEquals(
151             "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889",
152             parser.getScoresFor("1BB9"));
153     assertEquals(
154             "99999999999999999999999999987-------5321----2246----------788774--66789999999999889",
155             parser.getScoresFor("1UHC"));
156     assertEquals(
157             "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889",
158             parser.getScoresFor("1YCS"));
159     assertEquals(
160             "999999999999999999999999999861-------3------1135----------78877--356789999999997-67",
161             parser.getScoresFor("1OOT"));
162     assertEquals(
163             "99999999999999999999999999986-------422-------34----------687774--56779999999999889",
164             parser.getScoresFor("1ABO"));
165     assertEquals(
166             "99999999999999999999999999985-------32--------35----------6888842356789999999999889",
167             parser.getScoresFor("1FYN"));
168     assertEquals(
169             "99999999999999999999999999974-------2---------24----------6878742356789999999999889",
170             parser.getScoresFor("1QCF"));
171     assertEquals(
172             "99999999999999999999999999985100011032110000113400100000006877641356789999999999889",
173             parser.getScoresFor("cons"));
174   }
175
176   @Test
177   public void testGetAsList() throws IOException
178   {
179
180     TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),
181             FormatAdapter.FILE);
182     assertTrue(parser.getWarningMessage(), parser.isValid());
183     List<String> scores = parser.getScoresList();
184     assertEquals(
185             "999999999999999999999999998762112222543211112134----------5666642367889999999999889",
186             scores.get(0));
187     assertEquals(
188             "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889",
189             scores.get(1));
190     assertEquals(
191             "99999999999999999999999999987-------5321----2246----------788774--66789999999999889",
192             scores.get(2));
193     assertEquals(
194             "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889",
195             scores.get(3));
196     assertEquals(
197             "999999999999999999999999999861-------3------1135----------78877--356789999999997-67",
198             scores.get(4));
199     assertEquals(
200             "99999999999999999999999999986-------422-------34----------687774--56779999999999889",
201             scores.get(5));
202     assertEquals(
203             "99999999999999999999999999985-------32--------35----------6888842356789999999999889",
204             scores.get(6));
205     assertEquals(
206             "99999999999999999999999999974-------2---------24----------6878742356789999999999889",
207             scores.get(7));
208     assertEquals(
209             "99999999999999999999999999985100011032110000113400100000006877641356789999999999889",
210             scores.get(8));
211
212   }
213
214   @Test
215   public void testGetAsArray() throws IOException
216   {
217
218     TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),
219             FormatAdapter.FILE);
220     assertTrue(parser.getWarningMessage(), parser.isValid());
221     byte[][] scores = parser.getScoresArray();
222
223     assertEquals(9, scores[0][0]);
224     assertEquals(9, scores[1][0]);
225     assertEquals(9, scores[2][0]);
226     assertEquals(9, scores[3][0]);
227     assertEquals(9, scores[4][0]);
228     assertEquals(9, scores[5][0]);
229     assertEquals(9, scores[6][0]);
230     assertEquals(9, scores[7][0]);
231     assertEquals(9, scores[8][0]);
232
233     assertEquals(5, scores[0][36]);
234     assertEquals(4, scores[1][36]);
235     assertEquals(5, scores[2][36]);
236     assertEquals(4, scores[3][36]);
237     assertEquals(-1, scores[4][36]);
238     assertEquals(4, scores[5][36]);
239     assertEquals(3, scores[6][36]);
240     assertEquals(2, scores[7][36]);
241     assertEquals(3, scores[8][36]);
242
243   }
244
245   @Test
246   public void testHeightAndWidthWithResidueNumbers() throws Exception
247   {
248     String file = "test/jalview/io/tcoffee.score_ascii_with_residue_numbers";
249     TCoffeeScoreFile result = new TCoffeeScoreFile(file, FormatAdapter.FILE);
250     assertTrue(result.isValid());
251     assertEquals(5, result.getHeight());
252     assertEquals(84, result.getWidth());
253   }
254
255 }