da9ea1ebde2688aa4a4976bebe896bf2325d2fa5
[jalview.git] / src / jalview / util / Comparison.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
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.
9 *
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.
14 *
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
18 */
19 package jalview.util;
20
21 import jalview.datamodel.*;
22
23
24 /**
25  * DOCUMENT ME!
26  *
27  * @author $author$
28  * @version $Revision$
29  */
30 public class Comparison
31 {
32   /** DOCUMENT ME!! */
33   public static String GapChars = " .-";
34
35   /**
36    * DOCUMENT ME!
37    *
38    * @param ii DOCUMENT ME!
39    * @param jj DOCUMENT ME!
40    *
41    * @return DOCUMENT ME!
42    */
43   public static float compare(SequenceI ii, SequenceI jj)
44   {
45     return Comparison.compare(ii, jj, 0, ii.getLength() - 1);
46   }
47
48   /**
49    * this was supposed to be an ungapped pid calculation
50    * @param ii SequenceI
51    * @param jj SequenceI
52    * @param start int
53    * @param end int
54    * @return float
55    */
56   public static float compare(SequenceI ii, SequenceI jj, int start, int end)
57   {
58     String si = ii.getSequence();
59     String sj = jj.getSequence();
60
61     int ilen = si.length() - 1;
62     int jlen = sj.length() - 1;
63
64     while (jalview.util.Comparison.isGap(si.charAt(start + ilen)))
65     {
66       ilen--;
67     }
68
69     while (jalview.util.Comparison.isGap(sj.charAt(start + jlen)))
70     {
71       jlen--;
72     }
73
74     int count = 0;
75     int match = 0;
76     float pid = -1;
77
78     if (ilen > jlen)
79     {
80       for (int j = 0; j < jlen; j++)
81       {
82         if (si.substring(start + j, start + j + 1).equals(sj.substring(start +
83             j, start + j + 1)))
84         {
85           match++;
86         }
87
88         count++;
89       }
90
91       pid = (float) match / (float) ilen * 100;
92     }
93     else
94     {
95       for (int j = 0; j < jlen; j++)
96       {
97         if (si.substring(start + j, start + j + 1).equals(sj.substring(start +
98             j, start + j + 1)))
99         {
100           match++;
101         }
102
103         count++;
104       }
105
106       pid = (float) match / (float) jlen * 100;
107     }
108
109     return pid;
110   }
111
112   /**
113    * this is a gapped PID calculation
114    *
115    * @param s1 SequenceI
116    * @param s2 SequenceI
117    * @return float
118    */
119   public final static float PID(String seq1, String seq2)
120   {
121     return PID(seq1, seq2, 0, seq1.length());
122   }
123
124   static final int caseShift = 'a' - 'A';
125
126   // Another pid with region specification
127   public final static  float PID(String seq1, String seq2, int start, int end)
128   {
129
130     int s1len = seq1.length();
131     int s2len = seq2.length();
132
133     int len = Math.min(s1len, s2len);
134
135     if (end < len)
136     {
137       len = end;
138     }
139
140     if (len < start)
141     {
142       start = len - 1; // we just use a single residue for the difference
143     }
144
145
146     int bad = 0;
147     char chr1;
148     char chr2;
149
150
151     for (int i = start; i < len; i++)
152     {
153       chr1 =  seq1.charAt(i) ;
154
155       chr2 =  seq2.charAt(i) ;
156
157       if ('a' <= chr1 && chr1 <= 'z')
158       {
159         // TO UPPERCASE !!!
160         //Faster than toUpperCase
161         chr1 -= caseShift;
162       }
163       if ('a' <= chr2 && chr2 <= 'z')
164       {
165         // TO UPPERCASE !!!
166         //Faster than toUpperCase
167         chr2 -= caseShift;
168       }
169
170
171       if (chr1!=chr2 && !isGap(chr1) && !isGap(chr2) )
172       {
173           bad++;
174       }
175     }
176
177     return ( (float) 100 * (len - bad)) / len;
178   }
179
180   /**
181    * DOCUMENT ME!
182    *
183    * @param c DOCUMENT ME!
184    *
185    * @return DOCUMENT ME!
186    */
187   public static boolean isGap(char c)
188   {
189     return (c == '-' || c == '.' || c == ' ') ? true : false;
190   }
191
192   public static boolean isNucleotide(SequenceI [] seqs)
193   {
194     int i = 0, iSize = seqs.length, j, jSize;
195     float nt = 0, aa = 0;
196     char c;
197     while (i < iSize)
198     {
199       jSize = seqs[i].getLength();
200       for (j = 0; j < jSize; j++)
201       {
202         c = seqs[i].getCharAt(j);
203         if ('a' <= c && c <= 'z')
204           c -= ('a' - 'A');
205
206         if (c == 'A' || c == 'G' || c == 'C' || c == 'T' || c == 'U')
207           nt++;
208         else if (!jalview.util.Comparison.isGap( seqs[i].getCharAt(j)))
209         {
210           aa++;
211         }
212       }
213       i++;
214     }
215
216     if ( (nt / (nt + aa)) > 0.85f)
217       return true;
218     else
219       return false;
220
221   }
222 }