1 package jalview.analysis.scoremodels;
3 import jalview.api.analysis.PairwiseScoreModelI;
4 import jalview.api.analysis.SimilarityParamsI;
5 import jalview.datamodel.AlignmentView;
6 import jalview.math.Matrix;
7 import jalview.math.MatrixI;
8 import jalview.util.Comparison;
11 * A class to provide sequence pairwise similarity based on residue identity
13 public class PIDModel extends SimilarityScoreModel implements
16 private static final String NAME = "PID";
18 private String description;
28 public String getName()
34 public String getDescription()
40 public boolean isDNA()
46 public boolean isProtein()
52 * Answers 1 if c and d are the same residue (ignoring case), and not gap
53 * characters. Answers 0 for non-matching or gap characters.
56 public float getPairwiseScore(char c, char d)
60 if (c == d && !Comparison.isGap(c))
70 protected static char toUpper(char c)
72 if ('a' <= c && c <= 'z')
80 * Computes similarity scores based on pairwise percentage identity of
81 * sequences. For consistency with Jalview 2.10.1's SeqSpace mode PCA
82 * calculation, the percentage scores are rescaled to the width of the
83 * sequences (as if counts of identical residues).
86 public MatrixI findSimilarities(AlignmentView seqData,
87 SimilarityParamsI options)
89 String[] seqs = seqData.getSequenceStrings(Comparison.GAP_DASH);
91 MatrixI result = findSimilarities(seqs, options);
93 result.multiply(seqData.getWidth() / 100d);
99 * A distance score is computed in the usual way (by reversing the range of
100 * the similarity score results), and then rescaled to percentage values
101 * (reversing the rescaling to count values done in findSimilarities)
104 public MatrixI findDistances(AlignmentView seqData,
105 SimilarityParamsI options)
107 MatrixI result = super.findDistances(seqData, options);
109 if (seqData.getWidth() != 0)
111 result.multiply(100d / seqData.getWidth());
118 * Compute percentage identity scores, using the gap treatment and
119 * normalisation specified by the options parameter
125 protected MatrixI findSimilarities(String[] seqs,
126 SimilarityParamsI options)
128 // TODO reuse code in ScoreMatrix instead somehow
129 double[][] values = new double[seqs.length][];
130 for (int row = 0; row < seqs.length; row++)
132 values[row] = new double[seqs.length];
133 for (int col = 0; col < seqs.length; col++)
135 double total = computePID(seqs[row], seqs[col], options);
136 values[row][col] = total;
139 return new Matrix(values);
143 * Computes a percentage identity for two sequences, using the algorithm
144 * choices specified by the options parameter
151 public static double computePID(String seq1, String seq2,
152 SimilarityParamsI options)
154 int len1 = seq1.length();
155 int len2 = seq2.length();
156 int width = Math.max(len1, len2);
160 for (int i = 0; i < width; i++)
162 if (i >= len1 || i >= len2)
165 * off the end of one sequence; stop if we are only matching
166 * on the shorter sequence length, else treat as trailing gap
168 if (options.denominateByShortestLength())
172 if (options.includeGaps())
176 if (options.matchGaps())
182 char c1 = seq1.charAt(i);
183 char c2 = seq2.charAt(i);
184 boolean gap1 = Comparison.isGap(c1);
185 boolean gap2 = Comparison.isGap(c2);
190 * gap-gap: include if options say so, if so
191 * have to score as identity; else ignore
193 if (options.includeGappedColumns())
204 * gap-residue: include if options say so,
205 * count as match if options say so
207 if (options.includeGaps())
211 if (options.matchGaps())
219 * remaining case is gap-residue
221 if (toUpper(c1) == toUpper(c2))
228 return divideBy == 0 ? 0D : 100D * total / divideBy;