formatting
[jalview.git] / src / jalview / schemes / ScoreMatrix.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
4  * \r
5  * This file is part of Jalview.\r
6  * \r
7  * Jalview is free software: you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License \r
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
10  * \r
11  * Jalview is distributed in the hope that it will be useful, but \r
12  * WITHOUT ANY WARRANTY; without even the implied warranty \r
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
14  * PURPOSE.  See the GNU General Public License for more details.\r
15  * \r
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
17  */\r
18 package jalview.schemes;\r
19 \r
20 public class ScoreMatrix\r
21 {\r
22   String name;\r
23 \r
24   /**\r
25    * reference to integer score matrix\r
26    */\r
27   int[][] matrix;\r
28 \r
29   /**\r
30    * 0 for Protein Score matrix. 1 for dna score matrix\r
31    */\r
32   int type;\r
33 \r
34   ScoreMatrix(String name, int[][] matrix, int type)\r
35   {\r
36     this.matrix = matrix;\r
37     this.type = type;\r
38   }\r
39 \r
40   public boolean isDNA()\r
41   {\r
42     return type == 1;\r
43   }\r
44 \r
45   public boolean isProtein()\r
46   {\r
47     return type == 0;\r
48   }\r
49 \r
50   public int[][] getMatrix()\r
51   {\r
52     return matrix;\r
53   }\r
54 \r
55   /**\r
56    * \r
57    * @param A1\r
58    * @param A2\r
59    * @return score for substituting first char in A1 with first char in A2\r
60    */\r
61   public int getPairwiseScore(String A1, String A2)\r
62   {\r
63     return getPairwiseScore(A1.charAt(0), A2.charAt(0));\r
64   }\r
65 \r
66   public int getPairwiseScore(char c, char d)\r
67   {\r
68     int pog = 0;\r
69 \r
70     try\r
71     {\r
72       int a = (type == 0) ? ResidueProperties.aaIndex[c]\r
73               : ResidueProperties.nucleotideIndex[c];\r
74       int b = (type == 0) ? ResidueProperties.aaIndex[d]\r
75               : ResidueProperties.nucleotideIndex[d];\r
76 \r
77       pog = matrix[a][b];\r
78     } catch (Exception e)\r
79     {\r
80       // System.out.println("Unknown residue in " + A1 + " " + A2);\r
81     }\r
82 \r
83     return pog;\r
84   }\r
85 \r
86   /**\r
87    * pretty print the matrix\r
88    */\r
89   public String toString()\r
90   {\r
91     return outputMatrix(false);\r
92   }\r
93 \r
94   public String outputMatrix(boolean html)\r
95   {\r
96     StringBuffer sb = new StringBuffer();\r
97     int[] symbols = (type == 0) ? ResidueProperties.aaIndex\r
98             : ResidueProperties.nucleotideIndex;\r
99     int symMax = (type == 0) ? ResidueProperties.maxProteinIndex\r
100             : ResidueProperties.maxNucleotideIndex;\r
101     boolean header = true;\r
102     if (html)\r
103     {\r
104       sb.append("<table>");\r
105     }\r
106     for (char sym = 'A'; sym <= 'Z'; sym++)\r
107     {\r
108       if (symbols[sym] >= 0 && symbols[sym] < symMax)\r
109       {\r
110         if (header)\r
111         {\r
112           sb.append(html ? "<tr><td></td>" : "");\r
113           for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)\r
114           {\r
115             if (symbols[sym2] >= 0 && symbols[sym2] < symMax)\r
116             {\r
117               sb.append((html ? "<td>&nbsp;" : "\t") + sym2\r
118                       + (html ? "&nbsp;</td>" : ""));\r
119             }\r
120           }\r
121           header = false;\r
122           sb.append(html ? "</tr>\n" : "\n");\r
123         }\r
124         if (html)\r
125         {\r
126           sb.append("<tr>");\r
127         }\r
128         sb.append((html ? "<td>" : "") + sym + (html ? "</td>" : ""));\r
129         for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)\r
130         {\r
131           if (symbols[sym2] >= 0 && symbols[sym2] < symMax)\r
132           {\r
133             sb.append((html ? "<td>" : "\t")\r
134                     + matrix[symbols[sym]][symbols[sym2]]\r
135                     + (html ? "</td>" : ""));\r
136           }\r
137         }\r
138         sb.append(html ? "</tr>\n" : "\n");\r
139       }\r
140     }\r
141     if (html)\r
142     {\r
143       sb.append("</table>");\r
144     }\r
145     return sb.toString();\r
146   }\r
147 }\r