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.
12 * Instances of this class are immutable and thread-safe.
14 public class PIDModel extends SimilarityScoreModel implements
17 private static final String NAME = "PID";
27 public String getName()
33 * Answers null for description. If a display name is needed, use getName() or
34 * an internationalized string built from the name.
37 public String getDescription()
43 public boolean isDNA()
49 public boolean isProtein()
55 * Answers 1 if c and d are the same residue (ignoring case), and not gap
56 * characters. Answers 0 for non-matching or gap characters.
59 public float getPairwiseScore(char c, char d)
63 if (c == d && !Comparison.isGap(c))
73 protected static char toUpper(char c)
75 if ('a' <= c && c <= 'z')
83 * Computes similarity scores based on pairwise percentage identity of
84 * sequences. For consistency with Jalview 2.10.1's SeqSpace mode PCA
85 * calculation, the percentage scores are rescaled to the width of the
86 * sequences (as if counts of identical residues). This method is thread-safe.
89 public MatrixI findSimilarities(AlignmentView seqData,
90 SimilarityParamsI options)
92 String[] seqs = seqData.getSequenceStrings(Comparison.GAP_DASH);
94 MatrixI result = findSimilarities(seqs, options);
96 result.multiply(seqData.getWidth() / 100d);
102 * A distance score is computed in the usual way (by reversing the range of
103 * the similarity score results), and then rescaled to percentage values
104 * (reversing the rescaling to count values done in findSimilarities). This
105 * method is thread-safe.
108 public MatrixI findDistances(AlignmentView seqData,
109 SimilarityParamsI options)
111 MatrixI result = super.findDistances(seqData, options);
113 if (seqData.getWidth() != 0)
115 result.multiply(100d / seqData.getWidth());
122 * Compute percentage identity scores, using the gap treatment and
123 * normalisation specified by the options parameter
129 protected MatrixI findSimilarities(String[] seqs,
130 SimilarityParamsI options)
132 // TODO reuse code in ScoreMatrix instead somehow
133 double[][] values = new double[seqs.length][];
134 for (int row = 0; row < seqs.length; row++)
136 values[row] = new double[seqs.length];
137 for (int col = 0; col < seqs.length; col++)
139 double total = computePID(seqs[row], seqs[col], options);
140 values[row][col] = total;
143 return new Matrix(values);
147 * Computes a percentage identity for two sequences, using the algorithm
148 * choices specified by the options parameter
155 public static double computePID(String seq1, String seq2,
156 SimilarityParamsI options)
158 int len1 = seq1.length();
159 int len2 = seq2.length();
160 int width = Math.max(len1, len2);
164 for (int i = 0; i < width; i++)
166 if (i >= len1 || i >= len2)
169 * off the end of one sequence; stop if we are only matching
170 * on the shorter sequence length, else treat as trailing gap
172 if (options.denominateByShortestLength())
176 if (options.includeGaps())
180 if (options.matchGaps())
186 char c1 = seq1.charAt(i);
187 char c2 = seq2.charAt(i);
188 boolean gap1 = Comparison.isGap(c1);
189 boolean gap2 = Comparison.isGap(c2);
194 * gap-gap: include if options say so, if so
195 * have to score as identity; else ignore
197 if (options.includeGappedColumns())
208 * gap-residue: include if options say so,
209 * count as match if options say so
211 if (options.includeGaps())
215 if (options.matchGaps())
223 * remaining case is gap-residue
225 if (toUpper(c1) == toUpper(c2))
232 return divideBy == 0 ? 0D : 100D * total / divideBy;