2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
23 import jalview.datamodel.SequenceI;
26 * Assorted methods for analysing or comparing sequences.
28 public class Comparison
30 private static final int EIGHTY_FIVE = 85;
32 private static final int TO_UPPER_CASE = 'a' - 'A';
34 public static final String GapChars = " .-";
44 * @return DOCUMENT ME!
46 public static final float compare(SequenceI ii, SequenceI jj)
48 return Comparison.compare(ii, jj, 0, ii.getLength() - 1);
52 * this was supposed to be an ungapped pid calculation
64 public static float compare(SequenceI ii, SequenceI jj, int start, int end)
66 String si = ii.getSequenceAsString();
67 String sj = jj.getSequenceAsString();
69 int ilen = si.length() - 1;
70 int jlen = sj.length() - 1;
72 while (Comparison.isGap(si.charAt(start + ilen)))
77 while (Comparison.isGap(sj.charAt(start + jlen)))
88 for (int j = 0; j < jlen; j++)
90 if (si.substring(start + j, start + j + 1).equals(
91 sj.substring(start + j, start + j + 1)))
99 pid = (float) match / (float) ilen * 100;
103 for (int j = 0; j < jlen; j++)
105 if (si.substring(start + j, start + j + 1).equals(
106 sj.substring(start + j, start + j + 1)))
114 pid = (float) match / (float) jlen * 100;
121 * this is a gapped PID calculation
129 public final static float PID(String seq1, String seq2)
131 return PID(seq1, seq2, 0, seq1.length());
134 static final int caseShift = 'a' - 'A';
136 // Another pid with region specification
137 public final static float PID(String seq1, String seq2, int start, int end)
139 return PID(seq1, seq2, start, end, true, false);
143 * Calculate percent identity for a pair of sequences over a particular range,
144 * with different options for ignoring gaps.
153 * - if true - gaps match any character, if false, do not match
155 * @param ungappedOnly
156 * - if true - only count PID over ungapped columns
159 public final static float PID(String seq1, String seq2, int start,
160 int end, boolean wcGaps, boolean ungappedOnly)
162 int s1len = seq1.length();
163 int s2len = seq2.length();
165 int len = Math.min(s1len, s2len);
174 start = len - 1; // we just use a single residue for the difference
177 int elen = len - start, bad = 0;
181 for (int i = start; i < len; i++)
183 chr1 = seq1.charAt(i);
185 chr2 = seq2.charAt(i);
186 agap = isGap(chr1) || isGap(chr2);
187 if ('a' <= chr1 && chr1 <= 'z')
190 // Faster than toUpperCase
193 if ('a' <= chr2 && chr2 <= 'z')
196 // Faster than toUpperCase
224 return ((float) 100 * (elen - bad)) / elen;
228 * Answers true if the supplied character is a recognised gap character, else
229 * false. Currently hard-coded to recognise '-', '-' or ' ' (hyphen / dot /
236 public static final boolean isGap(char c)
238 return (c == '-' || c == '.' || c == ' ') ? true : false;
242 * Answers true if more than 85% of the sequence residues (ignoring gaps) are
243 * A, G, C, T or U, else false. This is just a heuristic guess and may give a
244 * wrong answer (as AGCT are also animo acid codes).
249 public static final boolean isNucleotide(SequenceI[] seqs)
257 for (SequenceI seq : seqs)
259 for (char c : seq.getSequence())
261 if ('a' <= c && c <= 'z')
266 if (c == 'A' || c == 'G' || c == 'C' || c == 'T' || c == 'U')
270 else if (!Comparison.isGap(c))
278 * Check for nucleotide count > 85% of total count (in a form that evades
279 * int / float conversion or divide by zero).
281 if (ntCount * 100 > EIGHTY_FIVE * (ntCount + aaCount))