2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.datamodel.*;
28 public class Comparison
31 public static final String GapChars = " .-";
41 * @return DOCUMENT ME!
43 public static final float compare(SequenceI ii, SequenceI jj)
45 return Comparison.compare(ii, jj, 0, ii.getLength() - 1);
49 * this was supposed to be an ungapped pid calculation
61 public static float compare(SequenceI ii, SequenceI jj, int start, int end)
63 String si = ii.getSequenceAsString();
64 String sj = jj.getSequenceAsString();
66 int ilen = si.length() - 1;
67 int jlen = sj.length() - 1;
69 while (jalview.util.Comparison.isGap(si.charAt(start + ilen)))
74 while (jalview.util.Comparison.isGap(sj.charAt(start + jlen)))
85 for (int j = 0; j < jlen; j++)
87 if (si.substring(start + j, start + j + 1).equals(
88 sj.substring(start + j, start + j + 1)))
96 pid = (float) match / (float) ilen * 100;
100 for (int j = 0; j < jlen; j++)
102 if (si.substring(start + j, start + j + 1).equals(
103 sj.substring(start + j, start + j + 1)))
111 pid = (float) match / (float) jlen * 100;
118 * this is a gapped PID calculation
126 public final static float PID(String seq1, String seq2)
128 return PID(seq1, seq2, 0, seq1.length());
131 static final int caseShift = 'a' - 'A';
133 // Another pid with region specification
134 public final static float PID(String seq1, String seq2, int start, int end)
136 return PID(seq1, seq2, start, end, true,false);
139 * Calculate percent identity for a pair of sequences over a particular range, with different options for ignoring gaps.
142 * @param start - position in seqs
143 * @param end - position in seqs
144 * @param wcGaps - if true - gaps match any character, if false, do not match anything
145 * @param ungappedOnly - if true - only count PID over ungapped columns
148 public final static float PID(String seq1, String seq2, int start, int end, boolean wcGaps, boolean ungappedOnly)
150 int s1len = seq1.length();
151 int s2len = seq2.length();
153 int len = Math.min(s1len, s2len);
162 start = len - 1; // we just use a single residue for the difference
165 int elen=len-start,bad = 0;
169 for (int i = start; i < len; i++)
171 chr1 = seq1.charAt(i);
173 chr2 = seq2.charAt(i);
174 agap = isGap(chr1) || isGap(chr2);
175 if ('a' <= chr1 && chr1 <= 'z')
178 // Faster than toUpperCase
181 if ('a' <= chr2 && chr2 <= 'z')
184 // Faster than toUpperCase
195 } else if (!wcGaps) {
204 if (elen<1) { return 0f; }
205 return ((float) 100 * (elen - bad)) / elen;
214 * @return DOCUMENT ME!
216 public static final boolean isGap(char c)
218 return (c == '-' || c == '.' || c == ' ') ? true : false;
221 public static final boolean isNucleotide(SequenceI[] seqs)
223 int i = 0, iSize = seqs.length, j, jSize;
224 float nt = 0, aa = 0;
228 jSize = seqs[i].getLength();
229 for (j = 0; j < jSize; j++)
231 c = seqs[i].getCharAt(j);
232 if ('a' <= c && c <= 'z')
237 if (c == 'A' || c == 'G' || c == 'C' || c == 'T' || c == 'U')
241 else if (!jalview.util.Comparison.isGap(seqs[i].getCharAt(j)))
249 if ((nt / (nt + aa)) > 0.85f)