2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis.scoremodels;
23 import jalview.math.Matrix;
24 import jalview.math.MatrixI;
26 import java.util.Arrays;
28 public class ScoreMatrix implements PairwiseScoreModelI
30 public static final short UNMAPPED = (short) -1;
32 private static final String BAD_ASCII_ERROR = "Unexpected character %s in getPairwiseScore";
34 private static final int MAX_ASCII = 127;
37 * the name of the model as shown in menus
42 * the characters that the model provides scores for
44 private char[] symbols;
47 * the score matrix; both dimensions must equal the number of symbols
48 * matrix[i][j] is the substitution score for replacing symbols[i] with symbols[j]
50 private float[][] matrix;
53 * quick lookup to convert from an ascii character value to the index
54 * of the corresponding symbol in the score matrix
56 private short[] symbolIndex;
59 * true for Protein Score matrix, false for dna score matrix
61 private boolean peptide;
63 private boolean symmetric;
66 * Constructor given a name, symbol alphabet, and matrix of scores for pairs
67 * of symbols. The matrix should be square and of the same size as the
68 * alphabet, for example 20x20 for a 20 symbol alphabet.
71 * Unique, human readable name for the matrix
73 * the symbols to which scores apply
75 * Pairwise scores indexed according to the symbol alphabet
77 public ScoreMatrix(String name, char[] alphabet, float[][] matrix)
79 if (alphabet.length != matrix.length)
81 throw new IllegalArgumentException(
82 "score matrix size must match alphabet size");
84 for (float[] row : matrix)
86 if (row.length != alphabet.length)
88 throw new IllegalArgumentException(
89 "score matrix size must be square");
95 this.symbols = alphabet;
97 symbolIndex = buildSymbolIndex(alphabet);
100 * crude heuristic for now...
102 peptide = alphabet.length >= 20;
106 * Returns an array A where A[i] is the position in the alphabet array of the
107 * character whose value is i. For example if the alphabet is { 'A', 'D', 'X'
108 * } then A['D'] = A[68] = 1.
110 * Unmapped characters (not in the alphabet) get an index of -1.
112 * Mappings are added automatically for lower case symbols (for non case
113 * sensitive scoring), unless they are explicitly present in the alphabet (are
114 * scored separately in the score matrix).
119 static short[] buildSymbolIndex(char[] alphabet)
121 short[] index = new short[MAX_ASCII + 1];
122 Arrays.fill(index, UNMAPPED);
124 for (char c : alphabet)
132 * also map lower-case character (unless separately mapped)
134 if (c >= 'A' && c <= 'Z')
136 short lowerCase = (short) (c + ('a' - 'A'));
137 if (index[lowerCase] == UNMAPPED)
139 index[lowerCase] = pos;
148 public String getName()
154 public boolean isDNA()
160 public boolean isProtein()
166 * Returns a copy of the score matrix as used in getPairwiseScore. If using
167 * this matrix directly, callers <em>must</em> also call
168 * <code>getMatrixIndex</code> in order to get the matrix index for each
169 * character (symbol).
172 * @see #getMatrixIndex(char)
174 public float[][] getMatrix()
176 float[][] v = new float[matrix.length][matrix.length];
177 for (int i = 0; i < matrix.length; i++)
179 v[i] = Arrays.copyOf(matrix[i], matrix[i].length);
185 * Answers the matrix index for a given character, or -1 if unmapped in the
186 * matrix. Use this method only if using <code>getMatrix</code> in order to
187 * compute scores directly (without symbol lookup) for efficiency.
193 public int getMatrixIndex(char c)
195 if (c < symbolIndex.length)
197 return symbolIndex[c];
206 * Returns the pairwise score for substituting c with d, or zero if c or d is
207 * an unscored or unexpected character
210 public float getPairwiseScore(char c, char d)
212 if (c >= symbolIndex.length)
214 System.err.println(String.format(BAD_ASCII_ERROR, c));
217 if (d >= symbolIndex.length)
219 System.err.println(String.format(BAD_ASCII_ERROR, d));
223 int cIndex = symbolIndex[c];
224 int dIndex = symbolIndex[d];
225 if (cIndex != UNMAPPED && dIndex != UNMAPPED)
227 return matrix[cIndex][dIndex];
233 * pretty print the matrix
236 public String toString()
238 return outputMatrix(false);
242 * Print the score matrix, optionally formatted as html, with the alphabet
243 * symbols as column headings and at the start of each row.
245 * The non-html format should give an output which can be parsed as a score
251 public String outputMatrix(boolean html)
253 StringBuilder sb = new StringBuilder(512);
256 * heading row with alphabet
260 sb.append("<table border=\"1\">");
261 sb.append(html ? "<tr><th></th>" : "");
265 sb.append("ScoreMatrix ").append(getName()).append("\n");
266 sb.append(symbols).append("\n");
268 for (char sym : symbols)
272 sb.append("<th> ").append(sym).append(" </th>");
276 sb.append("\t").append(sym);
279 sb.append(html ? "</tr>\n" : "\n");
284 for (char c1 : symbols)
288 sb.append("<tr><td>");
290 sb.append(c1).append(html ? "</td>" : "");
291 for (char c2 : symbols)
293 sb.append(html ? "<td>" : "\t")
294 .append(matrix[symbolIndex[c1]][symbolIndex[c2]])
295 .append(html ? "</td>" : "");
297 sb.append(html ? "</tr>\n" : "\n");
301 sb.append("</table>");
303 return sb.toString();
307 * Answers the number of symbols coded for (also equal to the number of rows
308 * and columns of the score matrix)
314 return symbols.length;
318 * Computes an NxN matrix where N is the number of sequences, and entry [i, j]
319 * is sequence[i] pairwise multiplied with sequence[j], as a sum of scores
320 * computed using the current score matrix. For example
322 * <li>Sequences:</li>
327 * <li>Score matrix is BLOSUM62</li>
328 * <li>Gaps treated same as X (unknown)</li>
329 * <li>product [0, 0] = F.F + K.K + L.L = 6 + 5 + 4 = 15</li>
330 * <li>product [1, 1] = R.R + -.- + D.D = 5 + -1 + 6 = 10</li>
331 * <li>product [2, 2] = Q.Q + I.I + A.A = 5 + 4 + 4 = 13</li>
332 * <li>product [3, 3] = G.G + W.W + C.C = 6 + 11 + 9 = 26</li>
333 * <li>product[0, 1] = F.R + K.- + L.D = -3 + -1 + -3 = -8
337 public MatrixI computePairwiseScores(String[] seqs)
339 double[][] values = new double[seqs.length][];
340 for (int row = 0; row < seqs.length; row++)
342 values[row] = new double[seqs.length];
343 for (int col = 0; col < seqs.length; col++)
346 int width = Math.min(seqs[row].length(), seqs[col].length());
347 for (int i = 0; i < width; i++)
349 char c1 = seqs[row].charAt(i);
350 char c2 = seqs[col].charAt(i);
351 float score = getPairwiseScore(c1, c2);
354 values[row][col] = total;
357 return new Matrix(values);
361 * Answers a hashcode computed from the symbol alphabet and the matrix score
365 public int hashCode()
367 int hs = Arrays.hashCode(symbols);
368 for (float[] row : matrix)
370 hs = hs * 31 + Arrays.hashCode(row);
376 * Answers true if the argument is a ScoreMatrix with the same symbol alphabet
377 * and score values, else false
380 public boolean equals(Object obj)
382 if (!(obj instanceof ScoreMatrix))
386 ScoreMatrix sm = (ScoreMatrix) obj;
387 if (Arrays.equals(symbols, sm.symbols)
388 && Arrays.deepEquals(matrix, sm.matrix))
395 public boolean isSymmetric()
401 * Returns the alphabet the matrix scores for, as a string of characters
405 public String getSymbols()
407 return new String(symbols);