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 java.util.Arrays;
25 public class ScoreMatrix implements PairwiseScoreModelI
27 public static final short UNMAPPED = (short) -1;
29 private static final String BAD_ASCII_ERROR = "Unexpected character %s in getPairwiseScore";
31 private static final int MAX_ASCII = 127;
34 * the name of the model as shown in menus
39 * the characters that the model provides scores for
41 private char[] symbols;
44 * the score matrix; both dimensions must equal the number of symbols
45 * matrix[i][j] is the substitution score for replacing symbols[i] with symbols[j]
47 private float[][] matrix;
50 * quick lookup to convert from an ascii character value to the index
51 * of the corresponding symbol in the score matrix
53 private short[] symbolIndex;
56 * true for Protein Score matrix, false for dna score matrix
58 private boolean peptide;
64 * Unique, human readable name for the matrix
66 * the symbols to which scores apply
68 * Pairwise scores indexed according to the symbol alphabet
70 public ScoreMatrix(String name, char[] alphabet, float[][] matrix)
74 this.symbols = alphabet;
76 symbolIndex = buildSymbolIndex(alphabet);
79 * crude heuristic for now...
81 peptide = alphabet.length >= 20;
85 * Returns an array A where A[i] is the position in the alphabet array of the
86 * character whose value is i. For example if the alphabet is { 'A', 'D', 'X'
87 * } then A['D'] = A[68] = 1.
89 * Unmapped characters (not in the alphabet) get an index of -1.
91 * Mappings are added automatically for lower case symbols (for non case
92 * sensitive scoring), unless they are explicitly present in the alphabet (are
93 * scored separately in the score matrix).
98 static short[] buildSymbolIndex(char[] alphabet)
100 short[] index = new short[MAX_ASCII + 1];
101 Arrays.fill(index, UNMAPPED);
103 for (char c : alphabet)
111 * also map lower-case character (unless separately mapped)
113 if (c >= 'A' && c <= 'Z')
115 short lowerCase = (short) (c + ('a' - 'A'));
116 if (index[lowerCase] == UNMAPPED)
118 index[lowerCase] = pos;
127 public String getName()
133 public boolean isDNA()
139 public boolean isProtein()
145 * Returns the score matrix as used in getPairwiseScore. If using this matrix
146 * directly, callers <em>must</em> also call <code>getMatrixIndex</code> in
147 * order to get the matrix index for each character (symbol).
150 * @see #getMatrixIndex(char)
152 public float[][] getMatrix()
158 * Answers the matrix index for a given character, or -1 if unmapped in the
159 * matrix. Use this method only if using <code>getMatrix</code> in order to
160 * compute scores directly (without symbol lookup) for efficiency.
166 public int getMatrixIndex(char c)
168 if (c < symbolIndex.length)
170 return symbolIndex[c];
179 * Returns the pairwise score for substituting c with d, or zero if c or d is
180 * an unscored or unexpected character
183 public float getPairwiseScore(char c, char d)
185 if (c >= symbolIndex.length)
187 System.err.println(String.format(BAD_ASCII_ERROR, c));
190 if (d >= symbolIndex.length)
192 System.err.println(String.format(BAD_ASCII_ERROR, d));
196 int cIndex = symbolIndex[c];
197 int dIndex = symbolIndex[d];
198 if (cIndex != UNMAPPED && dIndex != UNMAPPED)
200 return matrix[cIndex][dIndex];
206 * pretty print the matrix
209 public String toString()
211 return outputMatrix(false);
215 * Print the score matrix, optionally formatted as html, with the alphabet symbols as column headings and at the start of each row
219 public String outputMatrix(boolean html)
221 StringBuilder sb = new StringBuilder(512);
224 * heading row with alphabet
228 sb.append("<table border=\"1\">");
229 sb.append(html ? "<tr><th></th>" : "");
231 for (char sym : symbols)
235 sb.append("<th> ").append(sym).append(" </th>");
239 sb.append("\t").append(sym);
242 sb.append(html ? "</tr>\n" : "\n");
247 for (char c1 : symbols)
251 sb.append("<tr><td>");
253 sb.append(c1).append(html ? "</td>" : "");
254 for (char c2 : symbols)
256 sb.append(html ? "<td>" : "\t")
257 .append(matrix[symbolIndex[c1]][symbolIndex[c2]])
258 .append(html ? "</td>" : "");
260 sb.append(html ? "</tr>\n" : "\n");
264 sb.append("</table>");
266 return sb.toString();