2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis.scoremodels;
23 import jalview.api.AlignmentViewPanel;
24 import jalview.api.analysis.PairwiseScoreModelI;
25 import jalview.api.analysis.ScoreModelI;
26 import jalview.api.analysis.SimilarityParamsI;
27 import jalview.datamodel.AlignmentView;
28 import jalview.math.Matrix;
29 import jalview.math.MatrixI;
30 import jalview.util.Comparison;
32 import java.util.Arrays;
35 * A class that models a substitution score matrix for any given alphabet of
36 * symbols. Instances of this class are immutable and thread-safe, so the same
37 * object is returned from calls to getInstance().
39 public class ScoreMatrix extends SimilarityScoreModel
40 implements PairwiseScoreModelI
42 private static final char GAP_CHARACTER = Comparison.GAP_DASH;
45 * an arbitrary score to assign for identity of an unknown symbol
46 * (this is the value on the diagonal in the * column of the NCBI matrix)
47 * (though a case could be made for using the minimum diagonal value)
49 private static final int UNKNOWN_IDENTITY_SCORE = 1;
52 * Jalview 2.10.1 treated gaps as X (peptide) or N (nucleotide)
53 * for pairwise scoring; 2.10.2 uses gap score (last column) in
54 * score matrix (JAL-2397)
55 * Set this flag to true (via Groovy) for 2.10.1 behaviour
57 // BH 2019.05.08 was static but not ever set
58 private boolean scoreGapAsAny = false;
60 public static final short UNMAPPED = (short) -1;
62 private static final String BAD_ASCII_ERROR = "Unexpected character %s in getPairwiseScore";
64 private static final int MAX_ASCII = 127;
67 * the name of the model as shown in menus
68 * each score model in use should have a unique name
73 * a description for the model as shown in tooltips
75 private String description;
78 * the characters that the model provides scores for
80 private char[] symbols;
83 * the score matrix; both dimensions must equal the number of symbols
84 * matrix[i][j] is the substitution score for replacing symbols[i] with symbols[j]
86 private float[][] matrix;
89 * quick lookup to convert from an ascii character value to the index
90 * of the corresponding symbol in the score matrix
92 private short[] symbolIndex;
95 * true for Protein Score matrix, false for dna score matrix
97 private boolean peptide;
99 private float minValue;
101 private float maxValue;
103 private boolean symmetric;
106 * Constructor given a name, symbol alphabet, and matrix of scores for pairs
107 * of symbols. The matrix should be square and of the same size as the
108 * alphabet, for example 20x20 for a 20 symbol alphabet.
111 * Unique, human readable name for the matrix
113 * the symbols to which scores apply
115 * Pairwise scores indexed according to the symbol alphabet
117 public ScoreMatrix(String theName, char[] alphabet, float[][] values)
119 this(theName, null, alphabet, values);
123 * Constructor given a name, description, symbol alphabet, and matrix of
124 * scores for pairs of symbols. The matrix should be square and of the same
125 * size as the alphabet, for example 20x20 for a 20 symbol alphabet.
128 * Unique, human readable name for the matrix
129 * @param theDescription
130 * descriptive display name suitable for use in menus
132 * the symbols to which scores apply
134 * Pairwise scores indexed according to the symbol alphabet
136 public ScoreMatrix(String theName, String theDescription, char[] alphabet,
139 if (alphabet.length != values.length)
141 throw new IllegalArgumentException(
142 "score matrix size must match alphabet size");
144 for (float[] row : values)
146 if (row.length != alphabet.length)
148 throw new IllegalArgumentException(
149 "score matrix size must be square");
153 this.matrix = values;
155 this.description = theDescription;
156 this.symbols = alphabet;
158 symbolIndex = buildSymbolIndex(alphabet);
162 symmetric = checkSymmetry();
165 * crude heuristic for now...
167 peptide = alphabet.length >= 20;
171 * Answers true if the matrix is symmetric, else false. Usually, substitution
172 * matrices are symmetric, which allows calculations to be short cut.
176 private boolean checkSymmetry()
178 for (int i = 0; i < matrix.length; i++)
180 for (int j = i; j < matrix.length; j++)
182 if (matrix[i][j] != matrix[j][i])
192 * Record the minimum and maximum score values
194 protected void findMinMax()
196 float min = Float.MAX_VALUE;
197 float max = -Float.MAX_VALUE;
200 for (float[] row : matrix)
206 min = Math.min(min, f);
207 max = Math.max(max, f);
217 * Returns an array A where A[i] is the position in the alphabet array of the
218 * character whose value is i. For example if the alphabet is { 'A', 'D', 'X'
219 * } then A['D'] = A[68] = 1.
221 * Unmapped characters (not in the alphabet) get an index of -1.
223 * Mappings are added automatically for lower case symbols (for non case
224 * sensitive scoring), unless they are explicitly present in the alphabet (are
225 * scored separately in the score matrix).
227 * the gap character (space, dash or dot) included in the alphabet (if any) is
228 * recorded in a field
233 short[] buildSymbolIndex(char[] alphabet)
235 short[] index = new short[MAX_ASCII + 1];
236 Arrays.fill(index, UNMAPPED);
238 for (char c : alphabet)
246 * also map lower-case character (unless separately mapped)
248 if (c >= 'A' && c <= 'Z')
250 short lowerCase = (short) (c + ('a' - 'A'));
251 if (index[lowerCase] == UNMAPPED)
253 index[lowerCase] = pos;
262 public String getName()
268 public String getDescription()
274 public boolean isDNA()
280 public boolean isProtein()
286 * Returns a copy of the score matrix as used in getPairwiseScore. If using
287 * this matrix directly, callers <em>must</em> also call
288 * <code>getMatrixIndex</code> in order to get the matrix index for each
289 * character (symbol).
292 * @see #getMatrixIndex(char)
294 public float[][] getMatrix()
296 float[][] v = new float[matrix.length][matrix.length];
297 for (int i = 0; i < matrix.length; i++)
299 v[i] = Arrays.copyOf(matrix[i], matrix[i].length);
305 * Answers the matrix index for a given character, or -1 if unmapped in the
306 * matrix. Use this method only if using <code>getMatrix</code> in order to
307 * compute scores directly (without symbol lookup) for efficiency.
313 public int getMatrixIndex(char c)
315 if (c < symbolIndex.length)
317 return symbolIndex[c];
326 * Returns the pairwise score for substituting c with d. If either c or d is
327 * an unexpected character, returns 1 for identity (c == d), else the minimum
328 * score value in the matrix.
331 public float getPairwiseScore(char c, char d)
333 if (c >= symbolIndex.length)
335 System.err.println(String.format(BAD_ASCII_ERROR, c));
338 if (d >= symbolIndex.length)
340 System.err.println(String.format(BAD_ASCII_ERROR, d));
344 int cIndex = symbolIndex[c];
345 int dIndex = symbolIndex[d];
346 if (cIndex != UNMAPPED && dIndex != UNMAPPED)
348 return matrix[cIndex][dIndex];
352 * one or both symbols not found in the matrix
353 * currently scoring as 1 (for identity) or the minimum
354 * matrix score value (otherwise)
355 * (a case could be made for using minimum row/column value instead)
357 return c == d ? UNKNOWN_IDENTITY_SCORE : getMinimumScore();
361 * pretty print the matrix
364 public String toString()
366 return outputMatrix(false);
370 * Print the score matrix, optionally formatted as html, with the alphabet
371 * symbols as column headings and at the start of each row.
373 * The non-html format should give an output which can be parsed as a score
379 public String outputMatrix(boolean html)
381 StringBuilder sb = new StringBuilder(512);
384 * heading row with alphabet
388 sb.append("<table border=\"1\">");
389 sb.append(html ? "<tr><th></th>" : "");
393 sb.append("ScoreMatrix ").append(getName()).append("\n");
395 for (char sym : symbols)
399 sb.append("<th> ").append(sym).append(" </th>");
403 sb.append("\t").append(sym);
406 sb.append(html ? "</tr>\n" : "\n");
411 for (char c1 : symbols)
415 sb.append("<tr><td>");
417 sb.append(c1).append(html ? "</td>" : "");
418 for (char c2 : symbols)
420 sb.append(html ? "<td>" : "\t")
421 .append(matrix[symbolIndex[c1]][symbolIndex[c2]])
422 .append(html ? "</td>" : "");
424 sb.append(html ? "</tr>\n" : "\n");
428 sb.append("</table>");
430 return sb.toString();
434 * Answers the number of symbols coded for (also equal to the number of rows
435 * and columns of the score matrix)
441 return symbols.length;
445 * Computes an NxN matrix where N is the number of sequences, and entry [i, j]
446 * is sequence[i] pairwise multiplied with sequence[j], as a sum of scores
447 * computed using the current score matrix. For example
449 * <li>Sequences:</li>
454 * <li>Score matrix is BLOSUM62</li>
455 * <li>Gaps treated same as X (unknown)</li>
456 * <li>product [0, 0] = F.F + K.K + L.L = 6 + 5 + 4 = 15</li>
457 * <li>product [1, 1] = R.R + -.- + D.D = 5 + -1 + 6 = 10</li>
458 * <li>product [2, 2] = Q.Q + I.I + A.A = 5 + 4 + 4 = 13</li>
459 * <li>product [3, 3] = G.G + W.W + C.C = 6 + 11 + 9 = 26</li>
460 * <li>product[0, 1] = F.R + K.- + L.D = -3 + -1 + -3 = -8
463 * This method is thread-safe.
466 public MatrixI findSimilarities(AlignmentView seqstrings,
467 SimilarityParamsI options)
469 char gapChar = scoreGapAsAny ? (seqstrings.isNa() ? 'N' : 'X')
471 String[] seqs = seqstrings.getSequenceStrings(gapChar);
472 return findSimilarities(seqs, options);
476 * Computes pairwise similarities of a set of sequences using the given
483 protected MatrixI findSimilarities(String[] seqs,
484 SimilarityParamsI params)
486 double[][] values = new double[seqs.length][seqs.length];
487 for (int row = 0; row < seqs.length; row++)
489 for (int col = symmetric ? row : 0; col < seqs.length; col++)
491 double total = computeSimilarity(seqs[row], seqs[col], params);
492 values[row][col] = total;
495 values[col][row] = total;
499 return new Matrix(values);
503 * Calculates the pairwise similarity of two strings using the given
504 * calculation parameters
511 protected double computeSimilarity(String seq1, String seq2,
512 SimilarityParamsI params)
514 int len1 = seq1.length();
515 int len2 = seq2.length();
518 int width = Math.max(len1, len2);
519 for (int i = 0; i < width; i++)
521 if (i >= len1 || i >= len2)
524 * off the end of one sequence; stop if we are only matching
525 * on the shorter sequence length, else treat as trailing gap
527 if (params.denominateByShortestLength())
533 char c1 = i >= len1 ? GAP_CHARACTER : seq1.charAt(i);
534 char c2 = i >= len2 ? GAP_CHARACTER : seq2.charAt(i);
535 boolean gap1 = Comparison.isGap(c1);
536 boolean gap2 = Comparison.isGap(c2);
541 * gap-gap: include if options say so, else ignore
543 if (!params.includeGappedColumns())
548 else if (gap1 || gap2)
551 * gap-residue: score if options say so
553 if (!params.includeGaps())
558 float score = getPairwiseScore(c1, c2);
565 * Answers a hashcode computed from the symbol alphabet and the matrix score
569 public int hashCode()
571 int hs = Arrays.hashCode(symbols);
572 for (float[] row : matrix)
574 hs = hs * 31 + Arrays.hashCode(row);
580 * Answers true if the argument is a ScoreMatrix with the same symbol alphabet
581 * and score values, else false
584 public boolean equals(Object obj)
586 if (!(obj instanceof ScoreMatrix))
590 ScoreMatrix sm = (ScoreMatrix) obj;
591 if (Arrays.equals(symbols, sm.symbols)
592 && Arrays.deepEquals(matrix, sm.matrix))
600 * Returns the alphabet the matrix scores for, as a string of characters
606 return new String(symbols);
609 public float getMinimumScore()
614 public float getMaximumScore()
620 public ScoreModelI getInstance(AlignmentViewPanel avp)
625 public boolean isSymmetric()