2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import jalview.datamodel.*;
30 public class Comparison
33 public static String GapChars = " .-";
38 * @param ii DOCUMENT ME!
39 * @param jj DOCUMENT ME!
41 * @return DOCUMENT ME!
43 public static 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
56 public static float compare(SequenceI ii, SequenceI jj, int start, int end)
58 String si = ii.getSequence();
59 String sj = jj.getSequence();
61 int ilen = si.length() - 1;
62 int jlen = sj.length() - 1;
64 while (jalview.util.Comparison.isGap(si.charAt(start + ilen)))
69 while (jalview.util.Comparison.isGap(sj.charAt(start + jlen)))
80 for (int j = 0; j < jlen; j++)
82 if (si.substring(start + j, start + j + 1).equals(sj.substring(start +
91 pid = (float) match / (float) ilen * 100;
95 for (int j = 0; j < jlen; j++)
97 if (si.substring(start + j, start + j + 1).equals(sj.substring(start +
106 pid = (float) match / (float) jlen * 100;
113 * this is a gapped PID calculation
115 * @param s1 SequenceI
116 * @param s2 SequenceI
119 public final static float PID(String seq1, String seq2)
121 return PID(seq1, seq2, 0, seq1.length());
124 static final int caseShift = 'a' - 'A';
126 // Another pid with region specification
127 public final static float PID(String seq1, String seq2, int start, int end)
130 int s1len = seq1.length();
131 int s2len = seq2.length();
133 int len = Math.min(s1len, s2len);
142 start = len - 1; // we just use a single residue for the difference
151 for (int i = start; i < len; i++)
153 chr1 = seq1.charAt(i) ;
155 chr2 = seq2.charAt(i) ;
157 if ('a' <= chr1 && chr1 <= 'z')
160 //Faster than toUpperCase
163 if ('a' <= chr2 && chr2 <= 'z')
166 //Faster than toUpperCase
171 if (chr1!=chr2 && !isGap(chr1) && !isGap(chr2) )
177 return ( (float) 100 * (len - bad)) / len;
183 * @param c DOCUMENT ME!
185 * @return DOCUMENT ME!
187 public static boolean isGap(char c)
189 return (c == '-' || c == '.' || c == ' ') ? true : false;
192 public static boolean isNucleotide(SequenceI [] seqs)
194 int i = 0, iSize = seqs.length, j, jSize;
195 float nt = 0, aa = 0;
199 jSize = seqs[i].getLength();
200 for (j = 0; j < jSize; j++)
202 c = seqs[i].getCharAt(j);
203 if ('a' <= c && c <= 'z')
206 if (c == 'A' || c == 'G' || c == 'C' || c == 'T' || c == 'U')
208 else if (!jalview.util.Comparison.isGap( seqs[i].getCharAt(j)))
216 if ( (nt / (nt + aa)) > 0.85f)