0277884ab9d56c8057544cb5872390de8b2d02b6
[jalview.git] / test / jalview / io / TCoffeeScoreFileTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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         final static File SCORE_FILE = new File("test/jalview/io/tcoffee.score_ascii");
45     final static File ALIGN_FILE = new File("test/jalview/io/tcoffee.fasta_aln");
46         
47         @Test
48         public void testReadHeader() throws IOException {
49
50             TCoffeeScoreFile scoreFile = new TCoffeeScoreFile(SCORE_FILE.getPath(),AppletFormatAdapter.FILE);
51             assertTrue(scoreFile.getWarningMessage(),scoreFile.isValid());
52                 
53             Header header = scoreFile.header;
54                 assertNotNull(header);
55                 assertEquals( "T-COFFEE, Version_9.02.r1228 (2012-02-16 18:15:12 - Revision 1228 - Build 336)", header.head );
56                 assertEquals( 90, header.score );
57                 assertEquals( 89, header.getScoreFor("1PHT") );
58                 assertEquals( 90, header.getScoreFor("1BB9") );
59                 assertEquals( 94, header.getScoreFor("1UHC") );
60                 assertEquals( 94, header.getScoreFor("1YCS") );
61                 assertEquals( 93, header.getScoreFor("1OOT") );
62                 assertEquals( 94, header.getScoreFor("1ABO") );
63                 assertEquals( 94, header.getScoreFor("1FYN") );
64                 assertEquals( 94, header.getScoreFor("1QCF") );
65                 assertEquals( 90, header.getScoreFor("cons") );
66         }
67         
68         
69         @Test
70         public void testWrongFile()  {
71             try {
72                         TCoffeeScoreFile result = new TCoffeeScoreFile(ALIGN_FILE.getPath(), FormatAdapter.FILE);
73                         assertFalse(result.isValid());
74             } 
75             catch (IOException x)
76             {
77               assertTrue("File not found exception thrown",x instanceof FileNotFoundException);
78             }
79         } 
80
81         @Test
82         public void testHeightAndWidth() throws IOException {
83                 TCoffeeScoreFile result = new TCoffeeScoreFile(SCORE_FILE.getPath(), FormatAdapter.FILE);
84                 assertTrue(result.isValid());
85                 assertEquals( 8, result.getHeight() );
86                 assertEquals( 83, result.getWidth() );
87         }       
88         
89         @Test
90         public void testReadBlock( ) throws IOException {
91                 
92                 String BLOCK = "\n" +
93                                 "\n" +
94                                 "\n" +
95                                 "1PHT   999999999999999999999999998762112222543211112134\n" +
96                                 "1BB9   99999999999999999999999999987-------4322----2234  \n" +
97                                 "1UHC   99999999999999999999999999987-------5321----2246\n" +
98                                 "1YCS   99999999999999999999999999986-------4321----1-35\n" +
99                                 "1OOT   999999999999999999999999999861-------3------1135  \n" +
100                                 "1ABO   99999999999999999999999999986-------422-------34\n" +
101                                 "1FYN   99999999999999999999999999985-------32--------35\n" +
102                                 "1QCF   99999999999999999999999999974-------2---------24\n" +
103                                 "cons   999999999999999999999999999851000110321100001134\n" +
104                                 "\n" +
105                                 "\n";
106                 FileParse source=new FileParse(BLOCK, FormatAdapter.PASTE);
107                 Block block = TCoffeeScoreFile.readBlock(source, 0);
108
109                 assertNotNull(block);
110                 assertEquals( "999999999999999999999999998762112222543211112134", block.getScoresFor("1PHT") );
111                 assertEquals( "99999999999999999999999999987-------4322----2234", block.getScoresFor("1BB9") );
112                 assertEquals( "99999999999999999999999999987-------5321----2246", block.getScoresFor("1UHC") );
113                 assertEquals( "99999999999999999999999999986-------4321----1-35", block.getScoresFor("1YCS") );
114                 assertEquals( "999999999999999999999999999861-------3------1135", block.getScoresFor("1OOT") );
115                 assertEquals( "99999999999999999999999999986-------422-------34", block.getScoresFor("1ABO") );
116                 assertEquals( "99999999999999999999999999985-------32--------35", block.getScoresFor("1FYN") );
117                 assertEquals( "99999999999999999999999999974-------2---------24", block.getScoresFor("1QCF") );
118                 assertEquals( "999999999999999999999999999851000110321100001134", block.getConsensus() );
119         }
120
121         @Test
122         public void testParse() throws IOException {
123
124                 TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(), FormatAdapter.FILE);
125
126                 assertEquals( "999999999999999999999999998762112222543211112134----------5666642367889999999999889", parser.getScoresFor("1PHT") );
127                 assertEquals( "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889", parser.getScoresFor("1BB9") );
128                 assertEquals( "99999999999999999999999999987-------5321----2246----------788774--66789999999999889", parser.getScoresFor("1UHC") );
129                 assertEquals( "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889", parser.getScoresFor("1YCS") );
130                 assertEquals( "999999999999999999999999999861-------3------1135----------78877--356789999999997-67", parser.getScoresFor("1OOT") );
131                 assertEquals( "99999999999999999999999999986-------422-------34----------687774--56779999999999889", parser.getScoresFor("1ABO") );
132                 assertEquals( "99999999999999999999999999985-------32--------35----------6888842356789999999999889", parser.getScoresFor("1FYN") );
133                 assertEquals( "99999999999999999999999999974-------2---------24----------6878742356789999999999889", parser.getScoresFor("1QCF") );
134                 assertEquals( "99999999999999999999999999985100011032110000113400100000006877641356789999999999889", parser.getScoresFor("cons") );             
135         }
136
137         
138         @Test
139         public void testGetAsList() throws IOException {
140                 
141           TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),FormatAdapter.FILE);
142           assertTrue(parser.getWarningMessage(),parser.isValid());
143                 List<String> scores = parser.getScoresList();
144                 assertEquals( "999999999999999999999999998762112222543211112134----------5666642367889999999999889", scores.get(0) );
145                 assertEquals( "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889", scores.get(1) );
146                 assertEquals( "99999999999999999999999999987-------5321----2246----------788774--66789999999999889", scores.get(2) );
147                 assertEquals( "99999999999999999999999999986-------4321----1-35----------78777--356789999999999889", scores.get(3) );
148                 assertEquals( "999999999999999999999999999861-------3------1135----------78877--356789999999997-67", scores.get(4) );
149                 assertEquals( "99999999999999999999999999986-------422-------34----------687774--56779999999999889", scores.get(5) );
150                 assertEquals( "99999999999999999999999999985-------32--------35----------6888842356789999999999889", scores.get(6) );
151                 assertEquals( "99999999999999999999999999974-------2---------24----------6878742356789999999999889", scores.get(7) );
152                 assertEquals( "99999999999999999999999999985100011032110000113400100000006877641356789999999999889", scores.get(8) );           
153                 
154         } 
155         
156         
157         @Test
158         public void testGetAsArray() throws IOException {
159                 
160           TCoffeeScoreFile parser = new TCoffeeScoreFile(SCORE_FILE.getPath(),FormatAdapter.FILE);
161           assertTrue(parser.getWarningMessage(),parser.isValid());
162                 byte[][] scores = parser.getScoresArray();
163         
164                 assertEquals( 9, scores[0][0] );
165                 assertEquals( 9, scores[1][0] );
166                 assertEquals( 9, scores[2][0] );
167                 assertEquals( 9, scores[3][0] );
168                 assertEquals( 9, scores[4][0] );
169                 assertEquals( 9, scores[5][0] );
170                 assertEquals( 9, scores[6][0] );
171                 assertEquals( 9, scores[7][0] );
172                 assertEquals( 9, scores[8][0] );
173                 
174                 assertEquals( 5, scores[0][36] );
175                 assertEquals( 4, scores[1][36] );
176                 assertEquals( 5, scores[2][36] );
177                 assertEquals( 4, scores[3][36] );
178                 assertEquals( -1, scores[4][36] );
179                 assertEquals( 4, scores[5][36] );
180                 assertEquals( 3, scores[6][36] );
181                 assertEquals( 2, scores[7][36] );
182                 assertEquals( 3, scores[8][36] );
183                 
184         } 
185         
186         @Test
187         public void testHeightAndWidthWithResidueNumbers() throws Exception {
188                 String file = "test/jalview/io/tcoffee.score_ascii_with_residue_numbers";
189                 TCoffeeScoreFile result = new TCoffeeScoreFile(file, FormatAdapter.FILE);
190                 assertTrue(result.isValid());
191                 assertEquals( 5, result.getHeight() );
192                 assertEquals( 84, result.getWidth() );
193         }       
194         
195         
196         
197 }