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.
23 import jalview.datamodel.SequenceI;
25 import java.util.ArrayList;
26 import java.util.List;
29 * Assorted methods for analysing or comparing sequences.
31 public class Comparison
33 private static final int EIGHTY_FIVE = 85;
35 private static final int TO_UPPER_CASE = 'a' - 'A';
37 public static final char GAP_SPACE = ' ';
39 public static final char GAP_DOT = '.';
41 public static final char GAP_DASH = '-';
43 public static final String GapChars = new String(
45 { GAP_SPACE, GAP_DOT, GAP_DASH });
55 * @return DOCUMENT ME!
57 public static final float compare(SequenceI ii, SequenceI jj)
59 return Comparison.compare(ii, jj, 0, ii.getLength() - 1);
63 * this was supposed to be an ungapped pid calculation
75 public static float compare(SequenceI ii, SequenceI jj, int start,
78 String si = ii.getSequenceAsString();
79 String sj = jj.getSequenceAsString();
81 int ilen = si.length() - 1;
82 int jlen = sj.length() - 1;
84 while (Comparison.isGap(si.charAt(start + ilen)))
89 while (Comparison.isGap(sj.charAt(start + jlen)))
100 for (int j = 0; j < jlen; j++)
102 if (si.substring(start + j, start + j + 1)
103 .equals(sj.substring(start + j, start + j + 1)))
111 pid = (float) match / (float) ilen * 100;
115 for (int j = 0; j < jlen; j++)
117 if (si.substring(start + j, start + j + 1)
118 .equals(sj.substring(start + j, start + j + 1)))
126 pid = (float) match / (float) jlen * 100;
133 * this is a gapped PID calculation
140 * @deprecated use PIDModel.computePID()
143 public final static float PID(String seq1, String seq2)
145 return PID(seq1, seq2, 0, seq1.length());
148 static final int caseShift = 'a' - 'A';
150 // Another pid with region specification
152 * @deprecated use PIDModel.computePID()
155 public final static float PID(String seq1, String seq2, int start,
158 return PID(seq1, seq2, start, end, true, false);
162 * Calculate percent identity for a pair of sequences over a particular range,
163 * with different options for ignoring gaps.
172 * - if true - gaps match any character, if false, do not match
174 * @param ungappedOnly
175 * - if true - only count PID over ungapped columns
177 * @deprecated use PIDModel.computePID()
180 public final static float PID(String seq1, String seq2, int start,
181 int end, boolean wcGaps, boolean ungappedOnly)
183 int s1len = seq1.length();
184 int s2len = seq2.length();
186 int len = Math.min(s1len, s2len);
195 start = len - 1; // we just use a single residue for the difference
198 int elen = len - start, bad = 0;
202 for (int i = start; i < len; i++)
204 chr1 = seq1.charAt(i);
206 chr2 = seq2.charAt(i);
207 agap = isGap(chr1) || isGap(chr2);
208 if ('a' <= chr1 && chr1 <= 'z')
211 // Faster than toUpperCase
214 if ('a' <= chr2 && chr2 <= 'z')
217 // Faster than toUpperCase
245 return ((float) 100 * (elen - bad)) / elen;
249 * Answers true if the supplied character is a recognised gap character, else
250 * false. Currently hard-coded to recognise '-', '-' or ' ' (hyphen / dot /
257 public static final boolean isGap(char c)
259 return (c == GAP_DASH || c == GAP_DOT || c == GAP_SPACE) ? true : false;
263 * Overloaded method signature to test whether a single sequence is nucleotide
264 * (that is, more than 85% CGTAUNX)
269 public static final boolean isNucleotide(SequenceI seq)
279 int len = seq.getLength();
280 for (int i = 0; i < len; i++)
282 char c = seq.getCharAt(i);
283 if (isNucleotide(c) || isX(c))
297 * Check for nucleotide count > 85% of total count (in a form that evades
298 * int / float conversion or divide by zero).
300 if ((ntCount+nCount) * 100 > EIGHTY_FIVE * (ntCount + aaCount))
302 return ntCount>0; // all N is considered protein. Could use a threshold here too
311 * Answers true if more than 85% of the sequence residues (ignoring gaps) are
312 * A, G, C, T or U, else false. This is just a heuristic guess and may give a
313 * wrong answer (as AGCT are also amino acid codes).
318 public static final boolean isNucleotide(SequenceI[] seqs)
324 // true if we have seen a nucleotide sequence
326 for (SequenceI seq : seqs)
333 // TODO could possibly make an informed guess just from the first sequence
334 // to save a lengthy calculation
337 // if even one looks like protein, the alignment is protein
345 * Answers true if the character is one of aAcCgGtTuU
350 public static boolean isNucleotide(char c)
352 if ('a' <= c && c <= 'z')
368 public static boolean isN(char c)
379 public static boolean isX(char c)
391 * Answers true if every character in the string is one of aAcCgGtTuU, or
392 * (optionally) a gap character (dot, dash, space), else false
398 public static boolean isNucleotideSequence(String s, boolean allowGaps)
404 for (int i = 0; i < s.length(); i++)
406 char c = s.charAt(i);
407 if (!isNucleotide(c))
409 if (!allowGaps || !isGap(c))
419 * Convenience overload of isNucleotide
424 public static boolean isNucleotide(SequenceI[][] seqs)
430 List<SequenceI> flattened = new ArrayList<SequenceI>();
431 for (SequenceI[] ss : seqs)
433 for (SequenceI s : ss)
438 final SequenceI[] oneDArray = flattened
439 .toArray(new SequenceI[flattened.size()]);
440 return isNucleotide(oneDArray);
444 * Compares two residues either case sensitively or case insensitively
445 * depending on the caseSensitive flag
450 * second char to compare with
451 * @param caseSensitive
452 * if true comparison will be case sensitive otherwise its not
455 public static boolean isSameResidue(char c1, char c2,
456 boolean caseSensitive)
464 return Character.toUpperCase(c1) == Character.toUpperCase(c2);