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.schemes;
23 import jalview.analysis.scoremodels.PairwiseSeqScoreModel;
24 import jalview.math.Matrix;
25 import jalview.math.MatrixI;
27 public class ScoreMatrix extends PairwiseSeqScoreModel
32 public String getName()
38 * reference to integer score matrix
43 * 0 for Protein Score matrix. 1 for dna score matrix
50 * Unique, human readable name for the matrix
52 * Pairwise scores indexed according to appropriate symbol alphabet
54 * 0 for Protein, 1 for NA
56 ScoreMatrix(String name, int[][] matrix, int type)
64 public boolean isDNA()
70 public boolean isProtein()
76 public int[][] getMatrix()
82 * Answers the score for substituting first char in A1 with first char in A2
88 public int getPairwiseScore(String A1, String A2)
90 return getPairwiseScore(A1.charAt(0), A2.charAt(0));
94 public int getPairwiseScore(char c, char d)
100 int a = (type == 0) ? ResidueProperties.aaIndex[c]
101 : ResidueProperties.nucleotideIndex[c];
102 int b = (type == 0) ? ResidueProperties.aaIndex[d]
103 : ResidueProperties.nucleotideIndex[d];
104 score = matrix[a][b];
105 } catch (Exception e)
107 // System.out.println("Unknown residue in " + A1 + " " + A2);
114 * pretty print the matrix
117 public String toString()
119 return outputMatrix(false);
122 public String outputMatrix(boolean html)
124 StringBuffer sb = new StringBuffer();
125 int[] symbols = (type == 0) ? ResidueProperties.aaIndex
126 : ResidueProperties.nucleotideIndex;
127 int symMax = (type == 0) ? ResidueProperties.maxProteinIndex
128 : ResidueProperties.maxNucleotideIndex;
129 boolean header = true;
132 sb.append("<table border=\"1\">");
134 for (char sym = 'A'; sym <= 'Z'; sym++)
136 if (symbols[sym] >= 0 && symbols[sym] < symMax)
140 sb.append(html ? "<tr><td></td>" : "");
141 for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
143 if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
145 sb.append((html ? "<td> " : "\t") + sym2
146 + (html ? " </td>" : ""));
150 sb.append(html ? "</tr>\n" : "\n");
156 sb.append((html ? "<td>" : "") + sym + (html ? "</td>" : ""));
157 for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
159 if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
161 sb.append((html ? "<td>" : "\t")
162 + matrix[symbols[sym]][symbols[sym2]]
163 + (html ? "</td>" : ""));
166 sb.append(html ? "</tr>\n" : "\n");
171 sb.append("</table>");
173 return sb.toString();
177 * Computes an NxN matrix where N is the number of sequences, and entry [i, j]
178 * is sequence[i] pairwise multiplied with sequence[j], as a sum of scores
179 * computed using the current score matrix. For example
181 * <li>Sequences:</li>
186 * <li>Score matrix is BLOSUM62</li>
187 * <li>Gaps treated same as X (unknown)</li>
188 * <li>product [0, 0] = F.F + K.K + L.L = 6 + 5 + 4 = 15</li>
189 * <li>product [1, 1] = R.R + -.- + D.D = 5 + -1 + 6 = 10</li>
190 * <li>product [2, 2] = Q.Q + I.I + A.A = 5 + 4 + 4 = 13</li>
191 * <li>product [3, 3] = G.G + W.W + C.C = 6 + 11 + 9 = 26</li>
192 * <li>product[0, 1] = F.R + K.- + L.D = -3 + -1 + -3 = -8
196 public MatrixI computePairwiseScores(String[] seqs)
198 double[][] values = new double[seqs.length][];
199 for (int row = 0; row < seqs.length; row++)
201 values[row] = new double[seqs.length];
202 for (int col = 0; col < seqs.length; col++)
205 int width = Math.min(seqs[row].length(), seqs[col].length());
206 for (int i = 0; i < width; i++)
208 char c1 = seqs[row].charAt(i);
209 char c2 = seqs[col].charAt(i);
210 int score = getPairwiseScore(c1, c2);
213 values[row][col] = total;
216 return new Matrix(values);