JAL-2403 JAL-1483 changes to ScoreModelI hierarchy and signatures to
[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.PairwiseScoreModelI;
24 import jalview.api.analysis.SimilarityScoreModelI;
25 import jalview.datamodel.AlignmentView;
26 import jalview.math.Matrix;
27 import jalview.math.MatrixI;
28
29 import java.util.Arrays;
30
31 public class ScoreMatrix implements SimilarityScoreModelI,
32         PairwiseScoreModelI
33 {
34   /*
35    * Jalview 2.10.1 treated gaps as X (peptide) or N (nucleotide)
36    * for pairwise scoring; 2.10.2 uses gap score (last column) in
37    * score matrix (JAL-2397)
38    * Set this flag to true (via Groovy) for 2.10.1 behaviour
39    */
40   private static boolean scoreGapAsAny = false;
41
42   public static final short UNMAPPED = (short) -1;
43
44   private static final String BAD_ASCII_ERROR = "Unexpected character %s in getPairwiseScore";
45
46   private static final int MAX_ASCII = 127;
47
48   /*
49    * the name of the model as shown in menus
50    */
51   private String name;
52
53   /*
54    * the characters that the model provides scores for
55    */
56   private char[] symbols;
57
58   /*
59    * the score matrix; both dimensions must equal the number of symbols
60    * matrix[i][j] is the substitution score for replacing symbols[i] with symbols[j]
61    */
62   private float[][] matrix;
63
64   /*
65    * quick lookup to convert from an ascii character value to the index
66    * of the corresponding symbol in the score matrix 
67    */
68   private short[] symbolIndex;
69
70   /*
71    * true for Protein Score matrix, false for dna score matrix
72    */
73   private boolean peptide;
74
75   private boolean symmetric;
76
77   /**
78    * Constructor given a name, symbol alphabet, and matrix of scores for pairs
79    * of symbols. The matrix should be square and of the same size as the
80    * alphabet, for example 20x20 for a 20 symbol alphabet.
81    * 
82    * @param name
83    *          Unique, human readable name for the matrix
84    * @param alphabet
85    *          the symbols to which scores apply
86    * @param matrix
87    *          Pairwise scores indexed according to the symbol alphabet
88    */
89   public ScoreMatrix(String name, char[] alphabet, float[][] matrix)
90   {
91     if (alphabet.length != matrix.length)
92     {
93       throw new IllegalArgumentException(
94               "score matrix size must match alphabet size");
95     }
96     for (float[] row : matrix)
97     {
98       if (row.length != alphabet.length)
99       {
100         throw new IllegalArgumentException(
101                 "score matrix size must be square");
102       }
103     }
104
105     this.matrix = matrix;
106     this.name = name;
107     this.symbols = alphabet;
108
109     symbolIndex = buildSymbolIndex(alphabet);
110
111     /*
112      * crude heuristic for now...
113      */
114     peptide = alphabet.length >= 20;
115   }
116
117   /**
118    * Returns an array A where A[i] is the position in the alphabet array of the
119    * character whose value is i. For example if the alphabet is { 'A', 'D', 'X'
120    * } then A['D'] = A[68] = 1.
121    * <p>
122    * Unmapped characters (not in the alphabet) get an index of -1.
123    * <p>
124    * Mappings are added automatically for lower case symbols (for non case
125    * sensitive scoring), unless they are explicitly present in the alphabet (are
126    * scored separately in the score matrix).
127    * 
128    * @param alphabet
129    * @return
130    */
131   static short[] buildSymbolIndex(char[] alphabet)
132   {
133     short[] index = new short[MAX_ASCII + 1];
134     Arrays.fill(index, UNMAPPED);
135     short pos = 0;
136     for (char c : alphabet)
137     {
138       if (c <= MAX_ASCII)
139       {
140         index[c] = pos;
141       }
142
143       /*
144        * also map lower-case character (unless separately mapped)
145        */
146       if (c >= 'A' && c <= 'Z')
147       {
148         short lowerCase = (short) (c + ('a' - 'A'));
149         if (index[lowerCase] == UNMAPPED)
150         {
151           index[lowerCase] = pos;
152         }
153       }
154       pos++;
155     }
156     return index;
157   }
158
159   @Override
160   public String getName()
161   {
162     return name;
163   }
164
165   @Override
166   public boolean isDNA()
167   {
168     return !peptide;
169   }
170
171   @Override
172   public boolean isProtein()
173   {
174     return peptide;
175   }
176
177   /**
178    * Returns a copy of the score matrix as used in getPairwiseScore. If using
179    * this matrix directly, callers <em>must</em> also call
180    * <code>getMatrixIndex</code> in order to get the matrix index for each
181    * character (symbol).
182    * 
183    * @return
184    * @see #getMatrixIndex(char)
185    */
186   public float[][] getMatrix()
187   {
188     float[][] v = new float[matrix.length][matrix.length];
189     for (int i = 0; i < matrix.length; i++)
190     {
191       v[i] = Arrays.copyOf(matrix[i], matrix[i].length);
192     }
193     return v;
194   }
195
196   /**
197    * Answers the matrix index for a given character, or -1 if unmapped in the
198    * matrix. Use this method only if using <code>getMatrix</code> in order to
199    * compute scores directly (without symbol lookup) for efficiency.
200    * 
201    * @param c
202    * @return
203    * @see #getMatrix()
204    */
205   public int getMatrixIndex(char c)
206   {
207     if (c < symbolIndex.length)
208     {
209       return symbolIndex[c];
210     }
211     else
212     {
213       return UNMAPPED;
214     }
215   }
216
217   /**
218    * Returns the pairwise score for substituting c with d, or zero if c or d is
219    * an unscored or unexpected character
220    */
221   @Override
222   public float getPairwiseScore(char c, char d)
223   {
224     if (c >= symbolIndex.length)
225     {
226       System.err.println(String.format(BAD_ASCII_ERROR, c));
227       return 0;
228     }
229     if (d >= symbolIndex.length)
230     {
231       System.err.println(String.format(BAD_ASCII_ERROR, d));
232       return 0;
233     }
234
235     int cIndex = symbolIndex[c];
236     int dIndex = symbolIndex[d];
237     if (cIndex != UNMAPPED && dIndex != UNMAPPED)
238     {
239       return matrix[cIndex][dIndex];
240     }
241     return 0;
242   }
243
244   /**
245    * pretty print the matrix
246    */
247   @Override
248   public String toString()
249   {
250     return outputMatrix(false);
251   }
252
253   /**
254    * Print the score matrix, optionally formatted as html, with the alphabet
255    * symbols as column headings and at the start of each row.
256    * <p>
257    * The non-html format should give an output which can be parsed as a score
258    * matrix file
259    * 
260    * @param html
261    * @return
262    */
263   public String outputMatrix(boolean html)
264   {
265     StringBuilder sb = new StringBuilder(512);
266
267     /*
268      * heading row with alphabet
269      */
270     if (html)
271     {
272       sb.append("<table border=\"1\">");
273       sb.append(html ? "<tr><th></th>" : "");
274     }
275     else
276     {
277       sb.append("ScoreMatrix ").append(getName()).append("\n");
278       sb.append(symbols).append("\n");
279     }
280     for (char sym : symbols)
281     {
282       if (html)
283       {
284         sb.append("<th>&nbsp;").append(sym).append("&nbsp;</th>");
285       }
286       else
287       {
288         sb.append("\t").append(sym);
289       }
290     }
291     sb.append(html ? "</tr>\n" : "\n");
292
293     /*
294      * table of scores
295      */
296     for (char c1 : symbols)
297     {
298       if (html)
299       {
300         sb.append("<tr><td>");
301       }
302       sb.append(c1).append(html ? "</td>" : "");
303       for (char c2 : symbols)
304       {
305         sb.append(html ? "<td>" : "\t")
306                 .append(matrix[symbolIndex[c1]][symbolIndex[c2]])
307                 .append(html ? "</td>" : "");
308       }
309       sb.append(html ? "</tr>\n" : "\n");
310     }
311     if (html)
312     {
313       sb.append("</table>");
314     }
315     return sb.toString();
316   }
317
318   /**
319    * Answers the number of symbols coded for (also equal to the number of rows
320    * and columns of the score matrix)
321    * 
322    * @return
323    */
324   public int getSize()
325   {
326     return symbols.length;
327   }
328
329   /**
330    * Computes an NxN matrix where N is the number of sequences, and entry [i, j]
331    * is sequence[i] pairwise multiplied with sequence[j], as a sum of scores
332    * computed using the current score matrix. For example
333    * <ul>
334    * <li>Sequences:</li>
335    * <li>FKL</li>
336    * <li>R-D</li>
337    * <li>QIA</li>
338    * <li>GWC</li>
339    * <li>Score matrix is BLOSUM62</li>
340    * <li>Gaps treated same as X (unknown)</li>
341    * <li>product [0, 0] = F.F + K.K + L.L = 6 + 5 + 4 = 15</li>
342    * <li>product [1, 1] = R.R + -.- + D.D = 5 + -1 + 6 = 10</li>
343    * <li>product [2, 2] = Q.Q + I.I + A.A = 5 + 4 + 4 = 13</li>
344    * <li>product [3, 3] = G.G + W.W + C.C = 6 + 11 + 9 = 26</li>
345    * <li>product[0, 1] = F.R + K.- + L.D = -3 + -1 + -3 = -8
346    * <li>and so on</li>
347    * </ul>
348    */
349   @Override
350   public MatrixI findSimilarities(AlignmentView seqstrings)
351   {
352     char gapChar = scoreGapAsAny ? (seqstrings.isNa() ? 'N' : 'X') : ' ';
353     String[] seqs = seqstrings.getSequenceStrings(gapChar);
354     return findSimilarities(seqs);
355   }
356
357   /**
358    * @param seqs
359    * @return
360    */
361   protected MatrixI findSimilarities(String[] seqs)
362   {
363     double[][] values = new double[seqs.length][];
364     for (int row = 0; row < seqs.length; row++)
365     {
366       values[row] = new double[seqs.length];
367       for (int col = 0; col < seqs.length; col++)
368       {
369         int total = 0;
370         int width = Math.min(seqs[row].length(), seqs[col].length());
371         for (int i = 0; i < width; i++)
372         {
373           char c1 = seqs[row].charAt(i);
374           char c2 = seqs[col].charAt(i);
375           float score = getPairwiseScore(c1, c2);
376           total += score;
377         }
378         values[row][col] = total;
379       }
380     }
381     return new Matrix(values);
382   }
383
384   /**
385    * Answers a hashcode computed from the symbol alphabet and the matrix score
386    * values
387    */
388   @Override
389   public int hashCode()
390   {
391     int hs = Arrays.hashCode(symbols);
392     for (float[] row : matrix)
393     {
394       hs = hs * 31 + Arrays.hashCode(row);
395     }
396     return hs;
397   }
398
399   /**
400    * Answers true if the argument is a ScoreMatrix with the same symbol alphabet
401    * and score values, else false
402    */
403   @Override
404   public boolean equals(Object obj)
405   {
406     if (!(obj instanceof ScoreMatrix))
407     {
408       return false;
409     }
410     ScoreMatrix sm = (ScoreMatrix) obj;
411     if (Arrays.equals(symbols, sm.symbols)
412             && Arrays.deepEquals(matrix, sm.matrix))
413     {
414       return true;
415     }
416     return false;
417   }
418
419   public boolean isSymmetric()
420   {
421     return symmetric;
422   }
423
424   /**
425    * Returns the alphabet the matrix scores for, as a string of characters
426    * 
427    * @return
428    */
429   public String getSymbols()
430   {
431     return new String(symbols);
432   }
433 }