3 import jalview.analysis.scoremodels.ScoreMatrix;
4 import jalview.analysis.scoremodels.ScoreModels;
5 import jalview.datamodel.SequenceI;
7 import java.io.IOException;
8 import java.util.StringTokenizer;
11 * A class that can parse a file containing a substitution matrix and register
12 * it for use in Jalview
14 * Accepts 'NCBI' format (e.g.
15 * https://www.ncbi.nlm.nih.gov/Class/FieldGuide/BLOSUM62.txt), with the
16 * addition of a header line to provide a matrix name, e.g.
19 * ScoreMatrix BLOSUM62
22 * Also accepts 'AAindex' format (as described at
23 * http://www.genome.jp/aaindex/aaindex_help.html) with the minimum data
27 * H accession number (used as score matrix identifier in Jalview)
28 * D description (used for tooltip in Jalview)
30 * and the substitution scores
33 public class ScoreMatrixFile extends AlignFile implements
36 // first non-comment line identifier - also checked in IdentifyFile
37 public static final String SCOREMATRIX = "SCOREMATRIX";
39 private static final String DELIMITERS = " ,\t";
41 private static final String COMMENT_CHAR = "#";
43 private String matrixName;
46 * aaindex format has scores for diagonal and below only
48 boolean isLowerDiagonalOnly;
51 * ncbi format has symbols as first column on score rows
53 boolean hasGuideColumn;
61 public ScoreMatrixFile(FileParse source) throws IOException
67 public String print(SequenceI[] sqs, boolean jvsuffix)
73 * Parses the score matrix file, and if successful registers the matrix so it
74 * will be shown in Jalview menus. This method is not thread-safe (a separate
75 * instance of this class should be used by each thread).
78 public void parse() throws IOException
80 ScoreMatrix sm = parseMatrix();
82 ScoreModels.getInstance().registerScoreModel(sm);
86 * Parses the score matrix file and constructs a ScoreMatrix object. If an
87 * error is found in parsing, it is thrown as FileFormatException. Any
88 * warnings are written to syserr.
93 public ScoreMatrix parseMatrix() throws IOException
95 ScoreMatrix sm = null;
98 char[] alphabet = null;
99 float[][] scores = null;
104 isLowerDiagonalOnly = false;
106 while ((data = nextLine()) != null)
110 if (data.startsWith(COMMENT_CHAR) || data.length() == 0)
114 if (data.toUpperCase().startsWith(SCOREMATRIX))
117 * Parse name from ScoreMatrix <name>
118 * we allow any delimiter after ScoreMatrix then take the rest of the line
122 throw new FileFormatException(
123 "Error: 'ScoreMatrix' repeated in file at line "
126 StringTokenizer nameLine = new StringTokenizer(data, DELIMITERS);
127 if (nameLine.countTokens() < 2)
129 err = "Format error: expected 'ScoreMatrix <name>', found '"
130 + data + "' at line " + lineNo;
131 throw new FileFormatException(err);
133 nameLine.nextToken(); // 'ScoreMatrix'
134 name = nameLine.nextToken(); // next field
135 name = data.substring(1).substring(data.substring(1).indexOf(name));
138 else if (data.startsWith("H ") && name == null)
143 return parseAAIndexFormat(lineNo, data);
145 else if (name == null)
147 err = "Format error: 'ScoreMatrix <name>' should be the first non-comment line";
148 throw new FileFormatException(err);
152 * next non-comment line after ScoreMatrix should be the
153 * column header line with the alphabet of scored symbols
155 if (alphabet == null)
157 StringTokenizer columnHeadings = new StringTokenizer(data,
159 size = columnHeadings.countTokens();
160 alphabet = new char[size];
162 while (columnHeadings.hasMoreTokens())
164 alphabet[col++] = columnHeadings.nextToken().charAt(0);
166 scores = new float[size][];
171 * too much information
175 err = "Unexpected extra input line in score model file: '" + data
177 throw new FileFormatException(err);
180 parseValues(data, lineNo, scores, row, alphabet);
185 * out of data - check we found enough
190 .format("Expected %d rows of score data in score matrix but only found %d",
192 throw new FileFormatException(err);
196 * If we get here, then name, alphabet and scores have been parsed successfully
198 sm = new ScoreMatrix(name, alphabet, scores);
205 * Parse input as AAIndex format, starting from the header line with the
211 * @throws IOException
213 protected ScoreMatrix parseAAIndexFormat(int lineNo, String data)
216 String name = data.substring(2).trim();
217 String description = null;
219 float[][] scores = null;
220 char[] alphabet = null;
224 while ((data = nextLine()) != null)
228 if (skipAAindexLine(data))
232 if (data.startsWith("D "))
234 description = data.substring(2).trim();
236 else if (data.startsWith("M "))
238 alphabet = parseAAindexRowsColumns(lineNo, data);
239 size = alphabet.length;
240 scores = new float[size][size];
242 else if (scores == null)
244 throw new FileFormatException(
245 "No alphabet specified in matrix file");
247 else if (row >= size)
249 throw new FileFormatException("Too many data rows in matrix file");
253 parseValues(data, lineNo, scores, row, alphabet);
258 ScoreMatrix sm = new ScoreMatrix(name, description, alphabet, scores);
265 * Parse one row of score values, delimited by whitespace or commas. The line
266 * may optionally include the symbol from which the scores are defined. Values
267 * may be present for all columns, or only up to the diagonal (in which case
268 * upper diagonal values are set symmetrically).
271 * the line to be parsed
274 * the score matrix to add data to
276 * the row number / alphabet index position
280 * if invalid, or too few, or too many values
282 protected void parseValues(String data, int lineNo, float[][] scores,
283 int row, char[] alphabet) throws FileFormatException
286 int size = alphabet.length;
287 StringTokenizer scoreLine = new StringTokenizer(data, DELIMITERS);
289 int tokenCount = scoreLine.countTokens();
292 * inspect first row to see if it includes the symbol in the first column,
293 * and to see if it is lower diagonal values only (i.e. just one score)
297 if (data.startsWith(String.valueOf(alphabet[0])))
299 hasGuideColumn = true;
301 if (tokenCount == (hasGuideColumn ? 2 : 1))
303 isLowerDiagonalOnly = true;
310 * check 'guide' symbol is the row'th letter of the alphabet
312 String symbol = scoreLine.nextToken();
313 if (symbol.length() > 1 || symbol.charAt(0) != alphabet[row])
316 .format("Error parsing score matrix at line %d, expected '%s' but found '%s'",
317 lineNo, alphabet[row], symbol);
318 throw new FileFormatException(err);
320 tokenCount = scoreLine.countTokens(); // excluding guide symbol
324 * check the right number of values (lower diagonal or full format)
326 if (isLowerDiagonalOnly && tokenCount != row + 1)
329 "Expected %d scores at line %d: '%s' but found %d", row + 1,
330 lineNo, data, tokenCount);
331 throw new FileFormatException(err);
334 if (!isLowerDiagonalOnly && tokenCount != size)
337 "Expected %d scores at line %d: '%s' but found %d", size,
338 lineNo, data, scoreLine.countTokens());
339 throw new FileFormatException(err);
343 * parse and set the values, setting the symmetrical value
344 * as well if lower diagonal format data
346 scores[row] = new float[size];
349 while (scoreLine.hasMoreTokens())
353 value = scoreLine.nextToken();
354 scores[row][col] = Float.valueOf(value);
355 if (isLowerDiagonalOnly)
357 scores[col][row] = scores[row][col];
360 } catch (NumberFormatException e)
363 "Invalid score value '%s' at line %d column %d", value,
365 throw new FileFormatException(err);
371 * Parse the line in an aaindex file that looks like
374 * M rows = ARNDCQEGHILKMFPSTWYV, cols = ARNDCQEGHILKMFPSTWYV
377 * rejecting it if rows and cols do not match. Returns the string of
378 * characters in the row/cols alphabet.
383 * @throws FileFormatException
385 protected char[] parseAAindexRowsColumns(int lineNo, String data)
386 throws FileFormatException
388 String err = "Unexpected aaIndex score matrix data at line " + lineNo
393 String[] toks = data.split(",");
394 String rowsAlphabet = toks[0].split("=")[1].trim();
395 String colsAlphabet = toks[1].split("=")[1].trim();
396 if (!rowsAlphabet.equals(colsAlphabet))
398 throw new FileFormatException("rows != cols");
400 return rowsAlphabet.toCharArray();
401 } catch (Throwable t)
403 throw new FileFormatException(err + " " + t.getMessage());
408 * Answers true if line is one we are not interested in from AAindex format
414 protected boolean skipAAindexLine(String data)
416 if (data.startsWith(COMMENT_CHAR) || data.length() == 0)
420 if (data.startsWith("*") || data.startsWith("R ")
421 || data.startsWith("A ") || data.startsWith("T ")
422 || data.startsWith("J ") || data.startsWith("//"))
429 public String getMatrixName()