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.api.analysis.ScoreModelI;
25 import java.util.Arrays;
27 public class ScoreMatrix extends PairwiseSeqScoreModel implements
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;
67 * Unique, human readable name for the matrix
69 * the symbols to which scores apply
71 * Pairwise scores indexed according to the symbol alphabet
73 public ScoreMatrix(String name, char[] alphabet, float[][] matrix)
77 this.symbols = alphabet;
79 symbolIndex = buildSymbolIndex(alphabet);
82 * crude heuristic for now...
84 peptide = alphabet.length >= 20;
88 * Returns an array A where A[i] is the position in the alphabet array of the
89 * character whose value is i. For example if the alphabet is { 'A', 'D', 'X'
90 * } then A['D'] = A[68] = 1.
92 * Unmapped characters (not in the alphabet) get an index of -1.
94 * Mappings are added automatically for lower case symbols (for non case
95 * sensitive scoring), unless they are explicitly present in the alphabet (are
96 * scored separately in the score matrix).
101 static short[] buildSymbolIndex(char[] alphabet)
103 short[] index = new short[MAX_ASCII + 1];
104 Arrays.fill(index, UNMAPPED);
106 for (char c : alphabet)
114 * also map lower-case character (unless separately mapped)
116 if (c >= 'A' && c <= 'Z')
118 short lowerCase = (short) (c + ('a' - 'A'));
119 if (index[lowerCase] == UNMAPPED)
121 index[lowerCase] = pos;
130 public String getName()
136 public boolean isDNA()
142 public boolean isProtein()
148 public float[][] getMatrix()
154 * Returns the pairwise score for substituting c with d, or zero if c or d is
155 * an unscored or unexpected character
158 public float getPairwiseScore(char c, char d)
162 System.err.println(String.format(BAD_ASCII_ERROR, c));
167 System.err.println(String.format(BAD_ASCII_ERROR, d));
171 int cIndex = symbolIndex[c];
172 int dIndex = symbolIndex[d];
173 if (cIndex != UNMAPPED && dIndex != UNMAPPED)
175 return matrix[cIndex][dIndex];
181 * pretty print the matrix
184 public String toString()
186 return outputMatrix(false);
190 * Print the score matrix, optionally formatted as html, with the alphabet symbols as column headings and at the start of each row
194 public String outputMatrix(boolean html)
196 StringBuilder sb = new StringBuilder(512);
199 * heading row with alphabet
203 sb.append("<table border=\"1\">");
204 sb.append(html ? "<tr><th></th>" : "");
206 for (char sym : symbols)
210 sb.append("<th> ").append(sym).append(" </th>");
214 sb.append("\t").append(sym);
217 sb.append(html ? "</tr>\n" : "\n");
222 for (char c1 : symbols)
226 sb.append("<tr><td>");
228 sb.append(c1).append(html ? "</td>" : "");
229 for (char c2 : symbols)
231 sb.append(html ? "<td>" : "\t")
232 .append(matrix[symbolIndex[c1]][symbolIndex[c2]])
233 .append(html ? "</td>" : "");
235 sb.append(html ? "</tr>\n" : "\n");
239 sb.append("</table>");
241 return sb.toString();