JAL-1503 update version in GPL header
[jalview.git] / src / jalview / schemes / ScoreMatrix.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 import jalview.analysis.scoremodels.PairwiseSeqScoreModel;
22 import jalview.api.analysis.ScoreModelI;
23
24 public class ScoreMatrix extends PairwiseSeqScoreModel implements ScoreModelI
25 {
26   String name;
27   
28   @Override
29   public String getName()
30   {
31     return name;
32   }
33
34   /**
35    * reference to integer score matrix
36    */
37   int[][] matrix;
38
39   /**
40    * 0 for Protein Score matrix. 1 for dna score matrix
41    */
42   int type;
43   /**
44    * 
45    * @param name Unique, human readable name for the matrix
46    * @param matrix Pairwise scores indexed according to appropriate symbol alphabet
47    * @param type 0 for Protein, 1 for NA
48    */
49   ScoreMatrix(String name, int[][] matrix, int type)
50   {
51     this.matrix = matrix;
52     this.type = type;
53     this.name = name;
54   }
55
56   @Override
57   public boolean isDNA()
58   {
59     return type == 1;
60   }
61   @Override
62   public boolean isProtein()
63   {
64     return type == 0;
65   }
66
67   @Override
68   public int[][] getMatrix()
69   {
70     return matrix;
71   }
72
73   /**
74    * 
75    * @param A1
76    * @param A2
77    * @return score for substituting first char in A1 with first char in A2
78    */
79   public int getPairwiseScore(String A1, String A2)
80   {
81     return getPairwiseScore(A1.charAt(0), A2.charAt(0));
82   }
83
84   public int getPairwiseScore(char c, char d)
85   {
86     int pog = 0;
87
88     try
89     {
90       int a = (type == 0) ? ResidueProperties.aaIndex[c]
91               : ResidueProperties.nucleotideIndex[c];
92       int b = (type == 0) ? ResidueProperties.aaIndex[d]
93               : ResidueProperties.nucleotideIndex[d];
94
95       pog = matrix[a][b];
96     } catch (Exception e)
97     {
98       // System.out.println("Unknown residue in " + A1 + " " + A2);
99     }
100
101     return pog;
102   }
103
104   /**
105    * pretty print the matrix
106    */
107   public String toString()
108   {
109     return outputMatrix(false);
110   }
111
112   public String outputMatrix(boolean html)
113   {
114     StringBuffer sb = new StringBuffer();
115     int[] symbols = (type == 0) ? ResidueProperties.aaIndex
116             : ResidueProperties.nucleotideIndex;
117     int symMax = (type == 0) ? ResidueProperties.maxProteinIndex
118             : ResidueProperties.maxNucleotideIndex;
119     boolean header = true;
120     if (html)
121     {
122       sb.append("<table>");
123     }
124     for (char sym = 'A'; sym <= 'Z'; sym++)
125     {
126       if (symbols[sym] >= 0 && symbols[sym] < symMax)
127       {
128         if (header)
129         {
130           sb.append(html ? "<tr><td></td>" : "");
131           for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
132           {
133             if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
134             {
135               sb.append((html ? "<td>&nbsp;" : "\t") + sym2
136                       + (html ? "&nbsp;</td>" : ""));
137             }
138           }
139           header = false;
140           sb.append(html ? "</tr>\n" : "\n");
141         }
142         if (html)
143         {
144           sb.append("<tr>");
145         }
146         sb.append((html ? "<td>" : "") + sym + (html ? "</td>" : ""));
147         for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
148         {
149           if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
150           {
151             sb.append((html ? "<td>" : "\t")
152                     + matrix[symbols[sym]][symbols[sym2]]
153                     + (html ? "</td>" : ""));
154           }
155         }
156         sb.append(html ? "</tr>\n" : "\n");
157       }
158     }
159     if (html)
160     {
161       sb.append("</table>");
162     }
163     return sb.toString();
164   }
165 }