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