JAL-1432 updated copyright notices
[jalview.git] / src / jalview / schemes / ScoreMatrix.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.schemes;
20
21 public class ScoreMatrix
22 {
23   String name;
24
25   /**
26    * reference to integer score matrix
27    */
28   int[][] matrix;
29
30   /**
31    * 0 for Protein Score matrix. 1 for dna score matrix
32    */
33   int type;
34
35   ScoreMatrix(String name, int[][] matrix, int type)
36   {
37     this.matrix = matrix;
38     this.type = type;
39   }
40
41   public boolean isDNA()
42   {
43     return type == 1;
44   }
45
46   public boolean isProtein()
47   {
48     return type == 0;
49   }
50
51   public int[][] getMatrix()
52   {
53     return matrix;
54   }
55
56   /**
57    * 
58    * @param A1
59    * @param A2
60    * @return score for substituting first char in A1 with first char in A2
61    */
62   public int getPairwiseScore(String A1, String A2)
63   {
64     return getPairwiseScore(A1.charAt(0), A2.charAt(0));
65   }
66
67   public int getPairwiseScore(char c, char d)
68   {
69     int pog = 0;
70
71     try
72     {
73       int a = (type == 0) ? ResidueProperties.aaIndex[c]
74               : ResidueProperties.nucleotideIndex[c];
75       int b = (type == 0) ? ResidueProperties.aaIndex[d]
76               : ResidueProperties.nucleotideIndex[d];
77
78       pog = matrix[a][b];
79     } catch (Exception e)
80     {
81       // System.out.println("Unknown residue in " + A1 + " " + A2);
82     }
83
84     return pog;
85   }
86
87   /**
88    * pretty print the matrix
89    */
90   public String toString()
91   {
92     return outputMatrix(false);
93   }
94
95   public String outputMatrix(boolean html)
96   {
97     StringBuffer sb = new StringBuffer();
98     int[] symbols = (type == 0) ? ResidueProperties.aaIndex
99             : ResidueProperties.nucleotideIndex;
100     int symMax = (type == 0) ? ResidueProperties.maxProteinIndex
101             : ResidueProperties.maxNucleotideIndex;
102     boolean header = true;
103     if (html)
104     {
105       sb.append("<table>");
106     }
107     for (char sym = 'A'; sym <= 'Z'; sym++)
108     {
109       if (symbols[sym] >= 0 && symbols[sym] < symMax)
110       {
111         if (header)
112         {
113           sb.append(html ? "<tr><td></td>" : "");
114           for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
115           {
116             if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
117             {
118               sb.append((html ? "<td>&nbsp;" : "\t") + sym2
119                       + (html ? "&nbsp;</td>" : ""));
120             }
121           }
122           header = false;
123           sb.append(html ? "</tr>\n" : "\n");
124         }
125         if (html)
126         {
127           sb.append("<tr>");
128         }
129         sb.append((html ? "<td>" : "") + sym + (html ? "</td>" : ""));
130         for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
131         {
132           if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
133           {
134             sb.append((html ? "<td>" : "\t")
135                     + matrix[symbols[sym]][symbols[sym2]]
136                     + (html ? "</td>" : ""));
137           }
138         }
139         sb.append(html ? "</tr>\n" : "\n");
140       }
141     }
142     if (html)
143     {
144       sb.append("</table>");
145     }
146     return sb.toString();
147   }
148 }