1 package jalview.analysis.scoremodels;
3 import jalview.api.analysis.PairwiseScoreModelI;
4 import jalview.api.analysis.SimilarityParamsI;
5 import jalview.api.analysis.SimilarityScoreModelI;
6 import jalview.datamodel.AlignmentView;
7 import jalview.math.Matrix;
8 import jalview.math.MatrixI;
9 import jalview.util.Comparison;
12 * A class to provide sequence pairwise similarity based on residue identity
14 public class PIDModel implements SimilarityScoreModelI,
19 public String getName()
21 return "% Identity (PID)";
25 public boolean isDNA()
31 public boolean isProtein()
37 * Answers 1 if c and d are the same residue (ignoring case), and not gap
38 * characters. Answers 0 for non-matching or gap characters.
41 public float getPairwiseScore(char c, char d)
45 if (c == d && !Comparison.isGap(c))
55 protected static char toUpper(char c)
57 if ('a' <= c && c <= 'z')
65 public MatrixI findSimilarities(AlignmentView seqData,
66 SimilarityParamsI options)
68 String[] seqs = seqData.getSequenceStrings(' ');
69 return findSimilarities(seqs, options);
73 * Compute percentage identity scores, using the gap treatment and
74 * normalisation specified by the options parameter
80 protected MatrixI findSimilarities(String[] seqs,
81 SimilarityParamsI options)
83 // TODO reuse code in ScoreMatrix instead somehow
84 double[][] values = new double[seqs.length][];
85 for (int row = 0; row < seqs.length; row++)
87 values[row] = new double[seqs.length];
88 for (int col = 0; col < seqs.length; col++)
90 double total = computePID(seqs[row], seqs[col], options);
91 values[row][col] = total;
94 return new Matrix(values);
98 * Computes a percentage identity for two sequences, using the algorithm
99 * choices specified by the options parameter
106 public static double computePID(String seq1, String seq2,
107 SimilarityParamsI options)
109 int len1 = seq1.length();
110 int len2 = seq2.length();
111 int width = Math.max(len1, len2);
115 for (int i = 0; i < width; i++)
117 if (i >= len1 || i >= len2)
120 * off the end of one sequence; stop if we are only matching
121 * on the shorter sequence length, else treat as trailing gap
123 if (options.denominateByShortestLength())
127 if (options.includeGaps())
131 if (options.matchGaps())
137 char c1 = seq1.charAt(i);
138 char c2 = seq2.charAt(i);
139 boolean gap1 = Comparison.isGap(c1);
140 boolean gap2 = Comparison.isGap(c2);
145 * gap-gap: include if options say so, if so
146 * have to score as identity; else ignore
148 if (options.includeGappedColumns())
159 * gap-residue: include if options say so,
160 * count as match if options say so
162 if (options.includeGaps())
166 if (options.matchGaps())
174 * remaining case is gap-residue
176 if (toUpper(c1) == toUpper(c2))
183 return divideBy == 0 ? 0D : 100D * total / divideBy;