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 private static final char GAP_SPACE = ' ';
39 private static final char GAP_DOT = '.';
41 private static final char GAP_DASH = '-';
43 public static final String GapChars = new String(new char[] { GAP_SPACE,
54 * @return DOCUMENT ME!
56 public static final float compare(SequenceI ii, SequenceI jj)
58 return Comparison.compare(ii, jj, 0, ii.getLength() - 1);
62 * this was supposed to be an ungapped pid calculation
74 public static float compare(SequenceI ii, SequenceI jj, int start, int end)
76 String si = ii.getSequenceAsString();
77 String sj = jj.getSequenceAsString();
79 int ilen = si.length() - 1;
80 int jlen = sj.length() - 1;
82 while (Comparison.isGap(si.charAt(start + ilen)))
87 while (Comparison.isGap(sj.charAt(start + jlen)))
98 for (int j = 0; j < jlen; j++)
100 if (si.substring(start + j, start + j + 1).equals(
101 sj.substring(start + j, start + j + 1)))
109 pid = (float) match / (float) ilen * 100;
113 for (int j = 0; j < jlen; j++)
115 if (si.substring(start + j, start + j + 1).equals(
116 sj.substring(start + j, start + j + 1)))
124 pid = (float) match / (float) jlen * 100;
131 * this is a gapped PID calculation
139 public final static float PID(String seq1, String seq2)
141 return PID(seq1, seq2, 0, seq1.length());
144 static final int caseShift = 'a' - 'A';
146 // Another pid with region specification
147 public final static float PID(String seq1, String seq2, int start, int end)
149 return PID(seq1, seq2, start, end, true, false);
153 * Calculate percent identity for a pair of sequences over a particular range,
154 * with different options for ignoring gaps.
163 * - if true - gaps match any character, if false, do not match
165 * @param ungappedOnly
166 * - if true - only count PID over ungapped columns
169 public final static float PID(String seq1, String seq2, int start,
170 int end, boolean wcGaps, boolean ungappedOnly)
172 int s1len = seq1.length();
173 int s2len = seq2.length();
175 int len = Math.min(s1len, s2len);
184 start = len - 1; // we just use a single residue for the difference
187 int elen = len - start, bad = 0;
191 for (int i = start; i < len; i++)
193 chr1 = seq1.charAt(i);
195 chr2 = seq2.charAt(i);
196 agap = isGap(chr1) || isGap(chr2);
197 if ('a' <= chr1 && chr1 <= 'z')
200 // Faster than toUpperCase
203 if ('a' <= chr2 && chr2 <= 'z')
206 // Faster than toUpperCase
234 return ((float) 100 * (elen - bad)) / elen;
238 * Answers true if the supplied character is a recognised gap character, else
239 * false. Currently hard-coded to recognise '-', '-' or ' ' (hyphen / dot /
246 public static final boolean isGap(char c)
248 return (c == GAP_DASH || c == GAP_DOT || c == GAP_SPACE) ? true : false;
252 * Answers true if more than 85% of the sequence residues (ignoring gaps) are
253 * A, G, C, T or U, else false. This is just a heuristic guess and may give a
254 * wrong answer (as AGCT are also amino acid codes).
259 public static final boolean isNucleotide(SequenceI[] seqs)
265 char[][] letters = new char[seqs.length][];
266 for (int i = 0; i < seqs.length; i++)
270 char[] sequence = seqs[i].getSequence();
271 if (sequence != null)
273 letters[i] = sequence;
278 return areNucleotide(letters);
282 * Answers true if more than 85% of the sequence residues (ignoring gaps) are
283 * A, G, C, T or U, else false. This is just a heuristic guess and may give a
284 * wrong answer (as AGCT are also amino acid codes).
289 static final boolean areNucleotide(char[][] letters)
293 for (char[] seq : letters)
299 // TODO could possibly make an informed guess just from the first sequence
300 // to save a lengthy calculation
315 * Check for nucleotide count > 85% of total count (in a form that evades
316 * int / float conversion or divide by zero).
318 if (ntCount * 100 > EIGHTY_FIVE * (ntCount + aaCount))
330 * Answers true if the character is one of aAcCgGtTuU
335 public static boolean isNucleotide(char c)
337 if ('a' <= c && c <= 'z')
355 * Answers true if every character in the string is one of aAcCgGtTuU, or
356 * (optionally) a gap character (dot, dash, space), else false
362 public static boolean isNucleotideSequence(String s, boolean allowGaps)
368 for (int i = 0; i < s.length(); i++)
370 char c = s.charAt(i);
371 if (!isNucleotide(c))
373 if (!allowGaps || !isGap(c))
383 * Convenience overload of isNucleotide
388 public static boolean isNucleotide(SequenceI[][] seqs)
394 List<SequenceI> flattened = new ArrayList<SequenceI>();
395 for (SequenceI[] ss : seqs)
397 for (SequenceI s : ss)
402 final SequenceI[] oneDArray = flattened.toArray(new SequenceI[flattened
404 return isNucleotide(oneDArray);