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