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