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;
25 public class ScoreMatrix extends PairwiseSeqScoreModel
30 public String getName()
36 * reference to integer score matrix
41 * 0 for Protein Score matrix. 1 for dna score matrix
48 * Unique, human readable name for the matrix
50 * Pairwise scores indexed according to appropriate symbol alphabet
52 * 0 for Protein, 1 for NA
54 ScoreMatrix(String name, int[][] matrix, int type)
62 public boolean isDNA()
68 public boolean isProtein()
74 public int[][] getMatrix()
83 * @return score for substituting first char in A1 with first char in A2
85 public int getPairwiseScore(String A1, String A2)
87 return getPairwiseScore(A1.charAt(0), A2.charAt(0));
91 public int getPairwiseScore(char c, char d)
97 int a = (type == 0) ? ResidueProperties.aaIndex[c]
98 : ResidueProperties.nucleotideIndex[c];
99 int b = (type == 0) ? ResidueProperties.aaIndex[d]
100 : ResidueProperties.nucleotideIndex[d];
103 * hack to convert unassigned / unknown (including gap)
104 * to index of unknown (X for amino acids, N for nucleotide)
105 * TODO: statically assign gap characters to this index?
109 if (a == ResidueProperties.maxProteinIndex)
111 a = ResidueProperties.aaIndex['X'];
113 if (b == ResidueProperties.maxProteinIndex)
115 b = ResidueProperties.aaIndex['X'];
120 if (a == ResidueProperties.maxNucleotideIndex)
122 a = ResidueProperties.nucleotideIndex['N'];
124 if (b == ResidueProperties.maxNucleotideIndex)
126 b = ResidueProperties.nucleotideIndex['N'];
130 } catch (Exception e)
132 // System.out.println("Unknown residue in " + A1 + " " + A2);
139 * pretty print the matrix
142 public String toString()
144 return outputMatrix(false);
147 public String outputMatrix(boolean html)
149 StringBuffer sb = new StringBuffer();
150 int[] symbols = (type == 0) ? ResidueProperties.aaIndex
151 : ResidueProperties.nucleotideIndex;
152 int symMax = (type == 0) ? ResidueProperties.maxProteinIndex
153 : ResidueProperties.maxNucleotideIndex;
154 boolean header = true;
157 sb.append("<table border=\"1\">");
159 for (char sym = 'A'; sym <= 'Z'; sym++)
161 if (symbols[sym] >= 0 && symbols[sym] < symMax)
165 sb.append(html ? "<tr><td></td>" : "");
166 for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
168 if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
170 sb.append((html ? "<td> " : "\t") + sym2
171 + (html ? " </td>" : ""));
175 sb.append(html ? "</tr>\n" : "\n");
181 sb.append((html ? "<td>" : "") + sym + (html ? "</td>" : ""));
182 for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
184 if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
186 sb.append((html ? "<td>" : "\t")
187 + matrix[symbols[sym]][symbols[sym2]]
188 + (html ? "</td>" : ""));
191 sb.append(html ? "</tr>\n" : "\n");
196 sb.append("</table>");
198 return sb.toString();