X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fanalysis%2Fscoremodels%2FScoreMatrix.java;h=7f71d0fc7858dfdf5d9c9c64876727a482276a61;hb=993b6a26f415bf05ce6dce0b93e96eaf95733d4b;hp=eb94817f4d441f322bc7df25145a78a2a41eacfe;hpb=7c5c70f4bbe69a722dd6e8bd269d7d01e1089034;p=jalview.git diff --git a/src/jalview/analysis/scoremodels/ScoreMatrix.java b/src/jalview/analysis/scoremodels/ScoreMatrix.java index eb94817..7f71d0f 100644 --- a/src/jalview/analysis/scoremodels/ScoreMatrix.java +++ b/src/jalview/analysis/scoremodels/ScoreMatrix.java @@ -20,10 +20,25 @@ */ package jalview.analysis.scoremodels; +import jalview.api.analysis.PairwiseScoreModelI; +import jalview.api.analysis.SimilarityScoreModelI; +import jalview.datamodel.AlignmentView; +import jalview.math.Matrix; +import jalview.math.MatrixI; + import java.util.Arrays; -public class ScoreMatrix implements PairwiseScoreModelI +public class ScoreMatrix implements SimilarityScoreModelI, + PairwiseScoreModelI { + /* + * Jalview 2.10.1 treated gaps as X (peptide) or N (nucleotide) + * for pairwise scoring; 2.10.2 uses gap score (last column) in + * score matrix (JAL-2397) + * Set this flag to true (via Groovy) for 2.10.1 behaviour + */ + private static boolean scoreGapAsAny = false; + public static final short UNMAPPED = (short) -1; private static final String BAD_ASCII_ERROR = "Unexpected character %s in getPairwiseScore"; @@ -158,16 +173,22 @@ public class ScoreMatrix implements PairwiseScoreModelI } /** - * Returns the score matrix as used in getPairwiseScore. If using this matrix - * directly, callers must also call getMatrixIndex in - * order to get the matrix index for each character (symbol). + * Returns a copy of the score matrix as used in getPairwiseScore. If using + * this matrix directly, callers must also call + * getMatrixIndex in order to get the matrix index for each + * character (symbol). * * @return * @see #getMatrixIndex(char) */ public float[][] getMatrix() { - return matrix; + float[][] v = new float[matrix.length][matrix.length]; + for (int i = 0; i < matrix.length; i++) + { + v[i] = Arrays.copyOf(matrix[i], matrix[i].length); + } + return v; } /** @@ -228,7 +249,12 @@ public class ScoreMatrix implements PairwiseScoreModelI } /** - * Print the score matrix, optionally formatted as html, with the alphabet symbols as column headings and at the start of each row + * Print the score matrix, optionally formatted as html, with the alphabet + * symbols as column headings and at the start of each row. + *

+ * The non-html format should give an output which can be parsed as a score + * matrix file + * * @param html * @return */ @@ -244,6 +270,11 @@ public class ScoreMatrix implements PairwiseScoreModelI sb.append(""); sb.append(html ? "" : ""); } + else + { + sb.append("ScoreMatrix ").append(getName()).append("\n"); + sb.append(symbols).append("\n"); + } for (char sym : symbols) { if (html) @@ -292,4 +323,104 @@ public class ScoreMatrix implements PairwiseScoreModelI { return symbols.length; } + + /** + * Computes an NxN matrix where N is the number of sequences, and entry [i, j] + * is sequence[i] pairwise multiplied with sequence[j], as a sum of scores + * computed using the current score matrix. For example + * + */ + @Override + public MatrixI findSimilarities(AlignmentView seqstrings) + { + char gapChar = scoreGapAsAny ? (seqstrings.isNa() ? 'N' : 'X') : ' '; + String[] seqs = seqstrings.getSequenceStrings(gapChar); + return findSimilarities(seqs); + } + + /** + * @param seqs + * @return + */ + protected MatrixI findSimilarities(String[] seqs) + { + double[][] values = new double[seqs.length][]; + for (int row = 0; row < seqs.length; row++) + { + values[row] = new double[seqs.length]; + for (int col = 0; col < seqs.length; col++) + { + int total = 0; + int width = Math.min(seqs[row].length(), seqs[col].length()); + for (int i = 0; i < width; i++) + { + char c1 = seqs[row].charAt(i); + char c2 = seqs[col].charAt(i); + float score = getPairwiseScore(c1, c2); + total += score; + } + values[row][col] = total; + } + } + return new Matrix(values); + } + + /** + * Answers a hashcode computed from the symbol alphabet and the matrix score + * values + */ + @Override + public int hashCode() + { + int hs = Arrays.hashCode(symbols); + for (float[] row : matrix) + { + hs = hs * 31 + Arrays.hashCode(row); + } + return hs; + } + + /** + * Answers true if the argument is a ScoreMatrix with the same symbol alphabet + * and score values, else false + */ + @Override + public boolean equals(Object obj) + { + if (!(obj instanceof ScoreMatrix)) + { + return false; + } + ScoreMatrix sm = (ScoreMatrix) obj; + if (Arrays.equals(symbols, sm.symbols) + && Arrays.deepEquals(matrix, sm.matrix)) + { + return true; + } + return false; + } + + /** + * Returns the alphabet the matrix scores for, as a string of characters + * + * @return + */ + public String getSymbols() + { + return new String(symbols); + } }