import jalview.api.analysis.ScoreModelI;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.util.Arrays;
-import java.util.StringTokenizer;
public class ScoreMatrix extends PairwiseSeqScoreModel implements
ScoreModelI
{
public static final short UNMAPPED = (short) -1;
- private static final String DELIMITERS = " ,\t";
-
- private static final String COMMENT_CHAR = "#";
-
private static final String BAD_ASCII_ERROR = "Unexpected character %s in getPairwiseScore";
private static final int MAX_ASCII = 127;
}
return sb.toString();
}
-
- /**
- * Parse a score matrix from the given input stream and returns a ScoreMatrix
- * object. If parsing fails, error messages are written to syserr and null is
- * returned. It is the caller's responsibility to close the input stream.
- * Expected format:
- *
- * <pre>
- * ScoreMatrix displayName
- * # comment lines begin with hash sign
- * # symbol alphabet should be the next non-comment line
- * ARNDCQEGHILKMFPSTWYVBZX *
- * # scores matrix, with space, comma or tab delimited values
- * # [i, j] = score for substituting symbol[i] with symbol[j]
- * # first column in each row is optionally the 'substituted' symbol
- * A 4 -1 -2 -2 0 -1 -1 0 -2 -1 -1 -1 -1 -2 -1 1 0 -3 -2 0 -2 -1 0 -4 -4
- * ..etc..
- * </pre>
- *
- * @param is
- * @return
- */
- public static ScoreMatrix parse(InputStream is)
- {
- ScoreMatrix sm = null;
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- int lineNo = 0;
- String name = null;
- String alphabet = null;
- float[][] scores = null;
- int size = 0;
- int row = 0;
-
- try
- {
- String data;
-
- while ((data = br.readLine()) != null)
- {
- lineNo++;
- data = data.trim();
- if (data.startsWith(COMMENT_CHAR))
- {
- continue;
- }
- if (data.toLowerCase().startsWith("scorematrix"))
- {
- /*
- * Parse name from ScoreMatrix <name>
- */
- if (name != null)
- {
- System.err
- .println("Warning: 'ScoreMatrix' repeated in file at line "
- + lineNo);
- }
- StringTokenizer nameLine = new StringTokenizer(data, DELIMITERS);
- if (nameLine.countTokens() != 2)
- {
- System.err
- .println("Format error: expected 'ScoreMatrix <name>', found '"
- + data + "' at line " + lineNo);
- return null;
- }
- nameLine.nextToken();
- name = nameLine.nextToken();
- continue;
- }
- else if (name == null)
- {
- System.err
- .println("Format error: 'ScoreMatrix <name>' should be the first non-comment line");
- return null;
- }
-
- /*
- * next line after ScoreMatrix should be the alphabet of scored symbols
- */
- if (alphabet == null)
- {
- alphabet = data;
- size = alphabet.length();
- scores = new float[size][];
- continue;
- }
-
- /*
- * too much information?
- */
- if (row >= size && data.length() > 0) {
- System.err
- .println("Unexpected extra input line in score model file "
- + data);
- return null;
- }
-
- /*
- * subsequent lines should be the symbol scores
- * optionally with the symbol as the first column for readability
- */
- StringTokenizer scoreLine = new StringTokenizer(data, DELIMITERS);
- if (scoreLine.countTokens() == size + 1)
- {
- /*
- * check 'guide' symbol is the row'th letter of the alphabet
- */
- String symbol = scoreLine.nextToken();
- if (symbol.length() > 1
- || symbol.charAt(0) != alphabet.charAt(row))
- {
- System.err
- .println(String
- .format("Error parsing score matrix at line %d, expected %s but found %s",
- lineNo, alphabet.charAt(row), symbol));
- return null;
- }
- }
- if (scoreLine.countTokens() != size)
- {
- System.err.println(String.format(
- "Expected %d scores at line %d but found %d", size,
- lineNo, scoreLine.countTokens()));
- return null;
- }
- scores[row] = new float[size];
- int col = 0;
- String value = null;
- while (scoreLine.hasMoreTokens())
- {
- try
- {
- value = scoreLine.nextToken();
- scores[row][col] = Float.valueOf(value);
- col++;
- } catch (NumberFormatException e)
- {
- System.err.println(String.format(
- "Invalid score value %s at line %d column %d", value,
- lineNo, col));
- return null;
- }
- }
- row++;
- }
- } catch (IOException e)
- {
- System.err.println("Error reading score matrix file: "
- + e.getMessage() + " at line " + lineNo);
- }
-
- /*
- * out of data - check we found enough
- */
- if (row < size)
- {
- System.err
- .println(String
- .format("Expected %d rows of score data in score matrix but only found %d",
- size, row));
- return null;
- }
-
- /*
- * If we get here, then name, alphabet and scores have been parsed successfully
- */
- sm = new ScoreMatrix(name, alphabet.toCharArray(), scores);
- return sm;
- }
}