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 char GAP_TILDE = '~';
45 public static final char GAP_PIPE = '|';
47 public static final char GAP_COLON = ':';
49 public static final char GAP_LPAREN = '(';
51 public static final char GAP_RPAREN = ')';
53 public static final char GAP_LSQBR = '[';
55 public static final char GAP_RSQBR = ']';
57 public static final char GAP_LBRACE = '{';
59 public static final char GAP_RBRACE = '}';
61 public static final String GapChars = new String(
63 { GAP_SPACE, GAP_DOT, GAP_DASH, GAP_TILDE, GAP_PIPE, GAP_COLON,
65 GAP_RPAREN, GAP_LSQBR, GAP_RSQBR, GAP_LBRACE, GAP_RBRACE });
75 * @return DOCUMENT ME!
77 public static final float compare(SequenceI ii, SequenceI jj)
79 return Comparison.compare(ii, jj, 0, ii.getLength() - 1);
83 * this was supposed to be an ungapped pid calculation
95 public static float compare(SequenceI ii, SequenceI jj, int start,
98 String si = ii.getSequenceAsString();
99 String sj = jj.getSequenceAsString();
101 int ilen = si.length() - 1;
102 int jlen = sj.length() - 1;
104 while (Comparison.isGap(si.charAt(start + ilen)))
109 while (Comparison.isGap(sj.charAt(start + jlen)))
120 for (int j = 0; j < jlen; j++)
122 if (si.substring(start + j, start + j + 1)
123 .equals(sj.substring(start + j, start + j + 1)))
131 pid = (float) match / (float) ilen * 100;
135 for (int j = 0; j < jlen; j++)
137 if (si.substring(start + j, start + j + 1)
138 .equals(sj.substring(start + j, start + j + 1)))
146 pid = (float) match / (float) jlen * 100;
153 * this is a gapped PID calculation
160 * @deprecated use PIDModel.computePID()
163 public final static float PID(String seq1, String seq2)
165 return PID(seq1, seq2, 0, seq1.length());
168 static final int caseShift = 'a' - 'A';
170 // Another pid with region specification
172 * @deprecated use PIDModel.computePID()
175 public final static float PID(String seq1, String seq2, int start,
178 return PID(seq1, seq2, start, end, true, false);
182 * Calculate percent identity for a pair of sequences over a particular range,
183 * with different options for ignoring gaps.
192 * - if true - gaps match any character, if false, do not match
194 * @param ungappedOnly
195 * - if true - only count PID over ungapped columns
197 * @deprecated use PIDModel.computePID()
200 public final static float PID(String seq1, String seq2, int start,
201 int end, boolean wcGaps, boolean ungappedOnly)
203 int s1len = seq1.length();
204 int s2len = seq2.length();
206 int len = Math.min(s1len, s2len);
215 start = len - 1; // we just use a single residue for the difference
218 int elen = len - start, bad = 0;
222 for (int i = start; i < len; i++)
224 chr1 = seq1.charAt(i);
226 chr2 = seq2.charAt(i);
227 agap = isGap(chr1) || isGap(chr2);
228 if ('a' <= chr1 && chr1 <= 'z')
231 // Faster than toUpperCase
234 if ('a' <= chr2 && chr2 <= 'z')
237 // Faster than toUpperCase
265 return ((float) 100 * (elen - bad)) / elen;
269 * Answers true if the supplied character is a recognised gap character, else
270 * false. Currently hard-coded to recognise '-', '-' or ' ' (hyphen / dot /
277 public static final boolean isGap(char c)
300 * Overloaded method signature to test whether a single sequence is nucleotide
301 * (that is, more than 85% CGTA)
306 public static final boolean isNucleotide(SequenceI seq)
308 return isNucleotide(new SequenceI[] { seq });
312 * Answers true if more than 85% of the sequence residues (ignoring gaps) are
313 * A, G, C, T or U, else false. This is just a heuristic guess and may give a
314 * wrong answer (as AGCT are also amino acid codes).
319 public static final boolean isNucleotide(SequenceI[] seqs)
328 for (SequenceI seq : seqs)
334 // TODO could possibly make an informed guess just from the first sequence
335 // to save a lengthy calculation
336 int len = seq.getLength();
337 for (int i = 0; i < len; i++)
339 char c = seq.getCharAt(i);
352 * Check for nucleotide count > 85% of total count (in a form that evades
353 * int / float conversion or divide by zero).
355 if (ntCount * 100 > EIGHTY_FIVE * (ntCount + aaCount))
367 * Answers true if the character is one of aAcCgGtTuU
372 public static boolean isNucleotide(char c)
374 if ('a' <= c && c <= 'z')
392 * Answers true if every character in the string is one of aAcCgGtTuU, or
393 * (optionally) a gap character (dot, dash, space), else false
399 public static boolean isNucleotideSequence(String s, boolean allowGaps)
405 for (int i = 0; i < s.length(); i++)
407 char c = s.charAt(i);
408 if (!isNucleotide(c))
410 if (!allowGaps || !isGap(c))
420 * Convenience overload of isNucleotide
425 public static boolean isNucleotide(SequenceI[][] seqs)
431 List<SequenceI> flattened = new ArrayList<>();
432 for (SequenceI[] ss : seqs)
434 for (SequenceI s : ss)
439 final SequenceI[] oneDArray = flattened
440 .toArray(new SequenceI[flattened.size()]);
441 return isNucleotide(oneDArray);
445 * Compares two residues either case sensitively or case insensitively
446 * depending on the caseSensitive flag
451 * second char to compare with
452 * @param caseSensitive
453 * if true comparison will be case sensitive otherwise its not
456 public static boolean isSameResidue(char c1, char c2,
457 boolean caseSensitive)
465 return Character.toUpperCase(c1) == Character.toUpperCase(c2);