JAL-2379 'direct' pairwise score calculation for PCA (no encoding)
[jalview.git] / src / jalview / schemes / ScoreMatrix.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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
10  * of the License, or (at your option) any later version.
11  *  
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.
16  * 
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.
20  */
21 package jalview.schemes;
22
23 import jalview.analysis.scoremodels.PairwiseSeqScoreModel;
24
25 public class ScoreMatrix extends PairwiseSeqScoreModel
26 {
27   String name;
28
29   @Override
30   public String getName()
31   {
32     return name;
33   }
34
35   /**
36    * reference to integer score matrix
37    */
38   int[][] matrix;
39
40   /**
41    * 0 for Protein Score matrix. 1 for dna score matrix
42    */
43   int type;
44
45   /**
46    * 
47    * @param name
48    *          Unique, human readable name for the matrix
49    * @param matrix
50    *          Pairwise scores indexed according to appropriate symbol alphabet
51    * @param type
52    *          0 for Protein, 1 for NA
53    */
54   ScoreMatrix(String name, int[][] matrix, int type)
55   {
56     this.matrix = matrix;
57     this.type = type;
58     this.name = name;
59   }
60
61   @Override
62   public boolean isDNA()
63   {
64     return type == 1;
65   }
66
67   @Override
68   public boolean isProtein()
69   {
70     return type == 0;
71   }
72
73   @Override
74   public int[][] getMatrix()
75   {
76     return matrix;
77   }
78
79   /**
80    * 
81    * @param A1
82    * @param A2
83    * @return score for substituting first char in A1 with first char in A2
84    */
85   public int getPairwiseScore(String A1, String A2)
86   {
87     return getPairwiseScore(A1.charAt(0), A2.charAt(0));
88   }
89
90   @Override
91   public int getPairwiseScore(char c, char d)
92   {
93     int pog = 0;
94
95     try
96     {
97       int a = (type == 0) ? ResidueProperties.aaIndex[c]
98               : ResidueProperties.nucleotideIndex[c];
99       int b = (type == 0) ? ResidueProperties.aaIndex[d]
100               : ResidueProperties.nucleotideIndex[d];
101
102       /*
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?
106        */
107       if (type == 0)
108       {
109         if (a == ResidueProperties.maxProteinIndex)
110         {
111           a = ResidueProperties.aaIndex['X'];
112         }
113         if (b == ResidueProperties.maxProteinIndex)
114         {
115           b = ResidueProperties.aaIndex['X'];
116         }
117       }
118       if (type != 0)
119       {
120         if (a == ResidueProperties.maxNucleotideIndex)
121       {
122           a = ResidueProperties.nucleotideIndex['N'];
123       }
124         if (b == ResidueProperties.maxNucleotideIndex)
125       {
126           b = ResidueProperties.nucleotideIndex['N'];
127         }
128       }
129       pog = matrix[a][b];
130     } catch (Exception e)
131     {
132       // System.out.println("Unknown residue in " + A1 + " " + A2);
133     }
134
135     return pog;
136   }
137
138   /**
139    * pretty print the matrix
140    */
141   @Override
142   public String toString()
143   {
144     return outputMatrix(false);
145   }
146
147   public String outputMatrix(boolean html)
148   {
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;
155     if (html)
156     {
157       sb.append("<table border=\"1\">");
158     }
159     for (char sym = 'A'; sym <= 'Z'; sym++)
160     {
161       if (symbols[sym] >= 0 && symbols[sym] < symMax)
162       {
163         if (header)
164         {
165           sb.append(html ? "<tr><td></td>" : "");
166           for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
167           {
168             if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
169             {
170               sb.append((html ? "<td>&nbsp;" : "\t") + sym2
171                       + (html ? "&nbsp;</td>" : ""));
172             }
173           }
174           header = false;
175           sb.append(html ? "</tr>\n" : "\n");
176         }
177         if (html)
178         {
179           sb.append("<tr>");
180         }
181         sb.append((html ? "<td>" : "") + sym + (html ? "</td>" : ""));
182         for (char sym2 = 'A'; sym2 <= 'Z'; sym2++)
183         {
184           if (symbols[sym2] >= 0 && symbols[sym2] < symMax)
185           {
186             sb.append((html ? "<td>" : "\t")
187                     + matrix[symbols[sym]][symbols[sym2]]
188                     + (html ? "</td>" : ""));
189           }
190         }
191         sb.append(html ? "</tr>\n" : "\n");
192       }
193     }
194     if (html)
195     {
196       sb.append("</table>");
197     }
198     return sb.toString();
199   }
200 }