8041d8074a4364df0ec23245f167e6bf3366924d
[jalview.git] / src / jalview / util / Comparison.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.util;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 \r
24 public class Comparison {\r
25     public static String GapChars = " .-";\r
26 \r
27     public static float compare(SequenceI ii, SequenceI jj) {\r
28         return Comparison.compare(ii, jj, 0, ii.getLength() - 1);\r
29     }\r
30 \r
31     /**\r
32      * this was supposed to be an ungapped pid calculation\r
33      * @param ii SequenceI\r
34      * @param jj SequenceI\r
35      * @param start int\r
36      * @param end int\r
37      * @return float\r
38      */\r
39     public static float compare(SequenceI ii, SequenceI jj, int start, int end) {\r
40         String si = ii.getSequence();\r
41         String sj = jj.getSequence();\r
42 \r
43         int ilen = si.length() - 1;\r
44         int jlen = sj.length() - 1;\r
45 \r
46         while (jalview.util.Comparison.isGap(si.charAt(start + ilen))) {\r
47             ilen--;\r
48         }\r
49 \r
50         while (jalview.util.Comparison.isGap(sj.charAt(start + jlen))) {\r
51             jlen--;\r
52         }\r
53 \r
54         int count = 0;\r
55         int match = 0;\r
56         float pid = -1;\r
57 \r
58         if (ilen > jlen) {\r
59             for (int j = 0; j < jlen; j++) {\r
60                 if (si.substring(start + j, start + j + 1).equals(sj.substring(start +\r
61                                 j, start + j + 1))) {\r
62                     match++;\r
63                 }\r
64 \r
65                 count++;\r
66             }\r
67 \r
68             pid = (float) match / (float) ilen * 100;\r
69         } else {\r
70             for (int j = 0; j < jlen; j++) {\r
71                 if (si.substring(start + j, start + j + 1).equals(sj.substring(start +\r
72                                 j, start + j + 1))) {\r
73                     match++;\r
74                 }\r
75 \r
76                 count++;\r
77             }\r
78 \r
79             pid = (float) match / (float) jlen * 100;\r
80         }\r
81 \r
82         return pid;\r
83     }\r
84 \r
85     /**\r
86      * this is a gapped PID calculation\r
87      *\r
88      * @param s1 SequenceI\r
89      * @param s2 SequenceI\r
90      * @return float\r
91      */\r
92     public static float PID(SequenceI s1, SequenceI s2) {\r
93         int len;\r
94 \r
95         if (s1.getSequence().length() > s2.getSequence().length()) {\r
96             len = s1.getSequence().length();\r
97         } else {\r
98             len = s2.getSequence().length();\r
99         }\r
100 \r
101         int bad = 0;\r
102 \r
103         for (int i = 0; i < len; i++) {\r
104             char chr1;\r
105             char chr2;\r
106 \r
107             if (i < s1.getSequence().length()) {\r
108                 chr1 = s1.getSequence().charAt(i);\r
109             } else {\r
110                 chr1 = '.';\r
111             }\r
112 \r
113             if (i < s2.getSequence().length()) {\r
114                 chr2 = s2.getSequence().charAt(i);\r
115             } else {\r
116                 chr2 = '.';\r
117             }\r
118 \r
119             if (!(jalview.util.Comparison.isGap(chr1)) &&\r
120                     !(jalview.util.Comparison.isGap(chr2))) {\r
121                 if (chr1 != chr2) {\r
122                     bad++;\r
123                 }\r
124             }\r
125         }\r
126 \r
127         return ((float) 100 * (len - bad)) / len;\r
128     }\r
129 \r
130     // Another pid with region specification\r
131     public static float PID(SequenceI s1, SequenceI s2, int start, int end) {\r
132         int len;\r
133 \r
134         if (s1.getSequence().length() > s2.getSequence().length()) {\r
135             len = s1.getSequence().length();\r
136         } else {\r
137             len = s2.getSequence().length();\r
138         }\r
139 \r
140         if (end < len) {\r
141             len = end;\r
142         }\r
143 \r
144         if (len < start) {\r
145             start = len - 1; // we just use a single residue for the difference\r
146         }\r
147 \r
148         int bad = 0;\r
149 \r
150         for (int i = start; i < len; i++) {\r
151             char chr1;\r
152             char chr2;\r
153 \r
154             if (i < s1.getSequence().length()) {\r
155                 chr1 = s1.getSequence().charAt(i);\r
156             } else {\r
157                 chr1 = '.';\r
158             }\r
159 \r
160             if (i < s2.getSequence().length()) {\r
161                 chr2 = s2.getSequence().charAt(i);\r
162             } else {\r
163                 chr2 = '.';\r
164             }\r
165 \r
166             if (!(jalview.util.Comparison.isGap(chr1)) &&\r
167                     !(jalview.util.Comparison.isGap(chr2))) {\r
168                 if (chr1 != chr2) {\r
169                     bad++;\r
170                 }\r
171             }\r
172         }\r
173 \r
174         return ((float) 100 * (len - bad)) / len;\r
175     }\r
176 \r
177     public static boolean isGap(char c) {\r
178         return ((c != '.') && (c != '-') && (c != ' ')) ? false : true;\r
179     }\r
180 }\r