3 import java.io.BufferedReader;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.LinkedHashMap;
12 import java.util.List;
16 * A file parse for T-Coffee score ascii format. This file contains the alignment consensus
17 * for each resude in any sequence.
19 * This file is procuded by <code>t_coffee</code> providing the option
20 * <code>-output=score_ascii </code> to the program command line
22 * An example file is the following
25 * T-COFFEE, Version_9.02.r1228 (2012-02-16 18:15:12 - Revision 1228 - Build 336)
42 * 1PHT 999999999999999999999999998762112222543211112134
43 * 1BB9 99999999999999999999999999987-------4322----2234
44 * 1UHC 99999999999999999999999999987-------5321----2246
45 * 1YCS 99999999999999999999999999986-------4321----1-35
46 * 1OOT 999999999999999999999999999861-------3------1135
47 * 1ABO 99999999999999999999999999986-------422-------34
48 * 1FYN 99999999999999999999999999985-------32--------35
49 * 1QCF 99999999999999999999999999974-------2---------24
50 * cons 999999999999999999999999999851000110321100001134
53 * 1PHT ----------5666642367889999999999889
54 * 1BB9 1111111111676653-355679999999999889
55 * 1UHC ----------788774--66789999999999889
56 * 1YCS ----------78777--356789999999999889
57 * 1OOT ----------78877--356789999999997-67
58 * 1ABO ----------687774--56779999999999889
59 * 1FYN ----------6888842356789999999999889
60 * 1QCF ----------6878742356789999999999889
61 * cons 00100000006877641356789999999999889
65 * @author Paolo Di Tommaso
68 public class TCoffeeScoreFile {
70 /** The {@link Header} structure holder */
74 * Holds the consensues values for each sequences. It uses a LinkedHashMap to maintaint the
77 LinkedHashMap<String,StringBuilder> scores = new LinkedHashMap<String,StringBuilder>();
81 * Get the string of score values for the specified seqeunce ID.
82 * @param id The sequence ID
83 * @return The scores as a string of values e.g. {@code 99999987-------432}.
84 * It return an empty string when the specified ID is missing.
86 public String getScoresFor( String id ) {
87 return scores.containsKey(id) ? scores.get(id).toString() : "";
91 * @return The list of score string as a {@link List} object, in the same ordeer of the insertion i.e. in the MSA
93 public List<String> getScoresList() {
94 List<String> result = new ArrayList<String>( scores.size() );
95 for( Map.Entry<String,StringBuilder> it : scores.entrySet() ) {
96 result.add(it.getValue().toString());
103 * @return The parsed score values a matrix of bytes
105 public byte[][] getScoresArray() {
106 byte[][] result = new byte[ scores.size() ][];
109 for( Map.Entry<String,StringBuilder> it : scores.entrySet() ) {
110 String line = it.getValue().toString();
111 byte[] seqValues = new byte[ line.length() ];
112 for( int j=0, c=line.length(); j<c; j++ ) {
114 byte val = (byte)(line.charAt(j) - '0');
116 seqValues[j] = ( val >= 0 && val <= 9 ) ? val : -1;
119 result[rowCount++] = seqValues;
126 * Parse the specified file.
128 * @param file The file to be parsed
130 public void parse(File file) {
132 parse(new FileReader(file));
134 catch (FileNotFoundException e) {
135 throw new RuntimeException(e);
140 * Parse the provided reader for the T-Coffee scores file format
144 public void parse(Reader reader) {
147 BufferedReader in = (BufferedReader) (reader instanceof BufferedReader ? reader : new BufferedReader(reader));
150 catch( Exception e) {
151 throw new RuntimeException(e);
155 private void doParsing(BufferedReader in) throws IOException {
160 header = readHeader(in);
163 * initilize the structure
165 for( Map.Entry<String,Integer> entry : header.scores.entrySet() ) {
166 scores.put( entry.getKey(), new StringBuilder());
170 * go with the reading
173 while( (block = readBlock(in, header.scores.size())) != null ) {
176 * append sequences read in the block
178 for( Map.Entry<String,String> entry : block.items.entrySet() ) {
179 StringBuilder scoreStringBuilder = scores.get(entry.getKey());
180 if( scoreStringBuilder == null ) {
181 throw new RuntimeException(String.format("Invalid T-Coffee score file. Sequence ID '%s' is not declared in header section", entry.getKey()));
184 scoreStringBuilder.append( entry.getValue() );
192 static int parseInt( String str ) {
194 return Integer.parseInt(str);
196 catch( NumberFormatException e ) {
197 // TODO report a warning ?
203 * Reaad the header section in the T-Coffee score file format
205 * @param reader The scores reader
206 * @return The parser {@link Header} instance
207 * @throws RuntimeException when the header is not in the expected format
209 static Header readHeader(BufferedReader reader) {
211 Header result = null;
213 result = new Header();
214 result.head = reader.readLine();
218 while( (line = reader.readLine()) != null ) {
219 if( line.startsWith("SCORE=")) {
220 result.score = parseInt( line.substring(6).trim() );
225 if( (line=reader.readLine())==null || !"*".equals(line.trim())) return null;
226 if( (line=reader.readLine())==null || !"BAD AVG GOOD".equals(line.trim())) return null;
227 if( (line=reader.readLine())==null || !"*".equals(line.trim())) return null;
230 * now are expected a list if sequences ID up to the first blank line
232 while( (line=reader.readLine()) != null ) {
233 if( "".equals(line) ) {
237 int p = line.indexOf(":");
239 // TODO report a warning
243 String id = line.substring(0,p).trim();
244 int val = parseInt(line.substring(p+1).trim());
245 if( "".equals(id) ) {
246 // TODO report warning
250 result.scores.put(id,val);
254 catch( IOException e ) {
255 throw new RuntimeException("Cannot parse T-Coffee score ascii file", e);
262 * Read a scores block ihe provided stream.
264 * @param reader The stream to parse
265 * @param size The expected number of the sequence to be read
266 * @return The {@link Block} instance read or {link null} null if the end of file has reached.
267 * @throws IOException Something went wrong on the 'wire'
269 static Block readBlock( BufferedReader reader, int size ) throws IOException {
270 Block result = new Block(size);
274 * read blank lines (eventually)
276 while( (line=reader.readLine()) != null && "".equals(line.trim())) {
277 // consume blank lines
280 if( line == null ) return null;
283 * read the scores block
286 if( "".equals(line.trim()) ) {
291 // split the line on the first blank
292 // the first part have to contain the sequence id
293 // theramining part are the scores values
294 int p = line.indexOf(" ");
296 //TODO This is an unexpected condition, log a warning or throw an exception ?
300 String id = line.substring(0,p).trim();
301 String val = line.substring(p+1).trim();
303 result.items.put(id, val);
305 } while( (line = reader.readLine()) != null );
312 * The score file header
314 static class Header {
318 LinkedHashMap<String,Integer> scores = new LinkedHashMap<String,Integer>();
320 public int getScoreAvg() { return score; }
322 public int getScoreFor( String ID ) {
324 return scores.containsKey(ID) ? scores.get(ID) : -1;
330 * Hold a single block values block in the score file
334 Map<String,String> items;
336 public Block( int size ) {
338 this.items = new HashMap<String,String>(size);
341 String getScoresFor( String id ) {
342 return items.get(id);
345 String getConsensus() {
346 return items.get("cons");