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;
33 * A class to provide sequence pairwise similarity based on residue identity.
34 * Instances of this class are immutable and thread-safe, so the same object is
35 * returned from calls to getInstance().
37 public class PIDModel extends SimilarityScoreModel
38 implements PairwiseScoreModelI
40 private static final String NAME = "PID";
50 public String getName()
56 * Answers null for description. If a display name is needed, use getName() or
57 * an internationalized string built from the name.
60 public String getDescription()
66 public boolean isDNA()
72 public boolean isProtein()
78 * Answers 1 if c and d are the same residue (ignoring case), and not gap
79 * characters. Answers 0 for non-matching or gap characters.
82 public float getPairwiseScore(char c, char d)
86 if (c == d && !Comparison.isGap(c))
96 protected static char toUpper(char c)
98 if ('a' <= c && c <= 'z')
106 * Computes similarity scores based on pairwise percentage identity of
107 * sequences. For consistency with Jalview 2.10.1's SeqSpace mode PCA
108 * calculation, the percentage scores are rescaled to the width of the
109 * sequences (as if counts of identical residues). This method is thread-safe.
112 public MatrixI findSimilarities(AlignmentView seqData,
113 SimilarityParamsI options)
115 String[] seqs = seqData.getSequenceStrings(Comparison.GAP_DASH);
117 MatrixI result = findSimilarities(seqs, options);
119 result.multiply(seqData.getWidth() / 100d);
125 * A distance score is computed in the usual way (by reversing the range of
126 * the similarity score results), and then rescaled to percentage values
127 * (reversing the rescaling to count values done in findSimilarities). This
128 * method is thread-safe.
131 public MatrixI findDistances(AlignmentView seqData,
132 SimilarityParamsI options)
134 MatrixI result = super.findDistances(seqData, options);
136 if (seqData.getWidth() != 0)
138 result.multiply(100d / seqData.getWidth());
145 * Compute percentage identity scores, using the gap treatment and
146 * normalisation specified by the options parameter
152 protected MatrixI findSimilarities(String[] seqs,
153 SimilarityParamsI options)
155 // TODO reuse code in ScoreMatrix instead somehow
156 double[][] values = new double[seqs.length][];
157 for (int row = 0; row < seqs.length; row++)
159 values[row] = new double[seqs.length];
160 for (int col = 0; col < seqs.length; col++)
162 double total = computePID(seqs[row], seqs[col], options);
163 values[row][col] = total;
166 return new Matrix(values);
170 * Computes a percentage identity for two sequences, using the algorithm
171 * choices specified by the options parameter
178 public static double computePID(String seq1, String seq2,
179 SimilarityParamsI options)
181 int len1 = seq1.length();
182 int len2 = seq2.length();
183 int width = Math.max(len1, len2);
187 for (int i = 0; i < width; i++)
189 if (i >= len1 || i >= len2)
192 * off the end of one sequence; stop if we are only matching
193 * on the shorter sequence length, else treat as trailing gap
195 if (options.denominateByShortestLength())
199 if (options.includeGaps())
203 if (options.matchGaps())
209 char c1 = seq1.charAt(i);
210 char c2 = seq2.charAt(i);
211 boolean gap1 = Comparison.isGap(c1);
212 boolean gap2 = Comparison.isGap(c2);
217 * gap-gap: include if options say so, if so
218 * have to score as identity; else ignore
220 if (options.includeGappedColumns())
231 * gap-residue: include if options say so,
232 * count as match if options say so
234 if (options.includeGaps())
238 if (options.matchGaps())
246 * remaining case is gap-residue
248 if (toUpper(c1) == toUpper(c2))
255 return divideBy == 0 ? 0D : 100D * total / divideBy;
259 public ScoreModelI getInstance(AlignmentViewPanel avp)