Jalview Imported Sources
[jalview.git] / src / jalview / analysis / AAFrequency.java
1 package jalview.analysis;\r
2 \r
3 import jalview.jbgui.*;\r
4 import jalview.datamodel.*;\r
5 import jalview.io.*;\r
6 import jalview.analysis.*;\r
7 \r
8 import java.awt.*;\r
9 import java.applet.Applet;\r
10 import java.util.*;\r
11 import java.net.*;\r
12 import java.io.*;\r
13 \r
14 public class AAFrequency {\r
15 \r
16     // Takes in a vector of sequences and column start and column end\r
17     // and returns a vector of size (end-start+1). Each element of the\r
18     // vector contains a hashtable with the keys being residues and\r
19     // the values being the count of each residue in that column.\r
20     // This class is used extensively in calculating alignment colourschemes\r
21     // that depend on the amount of conservation in each alignment column.\r
22 \r
23     public static Vector calculate(Vector sequences,int start,int end) {\r
24 \r
25     Vector result = new Vector();\r
26 \r
27     for (int i = start;i <= end; i++)\r
28     {\r
29 \r
30       Hashtable residueHash = new Hashtable();\r
31       int       maxCount    = -1;\r
32       String    maxResidue  = "-";\r
33       int       nongap      = 0;\r
34       for (int j=0; j < sequences.size(); j++)\r
35       {\r
36 \r
37         if (sequences.elementAt(j) instanceof Sequence)\r
38         {\r
39           Sequence s = (Sequence)sequences.elementAt(j);\r
40 \r
41           if (s.getSequence().length() > i)\r
42           {\r
43 \r
44             String res = s.getSequence().substring(i,i+1);\r
45 \r
46             if (!res.equals("-"))\r
47               nongap++;\r
48 \r
49             if (residueHash.containsKey(res))\r
50             {\r
51 \r
52               int count = ((Integer)residueHash.get(res)).intValue() ;\r
53               count++;\r
54 \r
55               if (!res.equals("-") && count >= maxCount)\r
56               {\r
57 \r
58                   if(count>maxCount)\r
59                       maxResidue = res;\r
60                   else if(maxResidue.indexOf(res)==-1)\r
61                       maxResidue += res;\r
62 \r
63                   maxCount = count;\r
64               }\r
65 \r
66               residueHash.put(res,new Integer(count));\r
67             }\r
68             else\r
69               residueHash.put(res,new Integer(1));\r
70 \r
71 \r
72           }\r
73           else\r
74           {\r
75             if (residueHash.containsKey("-"))\r
76             {\r
77               int count = ((Integer)residueHash.get("-")).intValue() ;\r
78               count++;\r
79               residueHash.put("-",new Integer(count));\r
80             }\r
81             else\r
82               residueHash.put("-",new Integer(1));\r
83 \r
84           }\r
85         }\r
86       }\r
87       residueHash.put("maxCount",new Integer(maxCount));\r
88       residueHash.put("maxResidue", maxResidue);\r
89       residueHash.put("size", new Integer(sequences.size()));\r
90       residueHash.put("nongap", new Integer(nongap));\r
91       result.addElement(residueHash);\r
92     }\r
93 \r
94     return result;\r
95   }\r
96 \r
97     public static Vector calculatePID(SequenceI refseq,Vector sequences,int window,int start,int end) {\r
98 \r
99     Vector result = new Vector();\r
100 \r
101     boolean init = true;\r
102 \r
103 \r
104     Vector prev = null;\r
105 \r
106     for (int i = start;i <= end; i++) {\r
107         Vector values = new Vector();\r
108 \r
109         result.addElement(values);\r
110 \r
111         // If start < window/2 then set value to zero.\r
112 \r
113         if (i< window/2 || i >= refseq.getSequence().length()-window/2) {\r
114             for (int j = 0; j < sequences.size(); j++) {\r
115                 values.addElement(new Integer(0));\r
116             }\r
117         } else  if (init == true) {\r
118             init = false;\r
119 \r
120             int winstart = i-window/2;\r
121             int winend   = i+window/2;\r
122 \r
123             if (window%2 != 0) {\r
124               winend++;\r
125             }\r
126 \r
127             for (int j = 0; j < sequences.size(); j++) {\r
128                 values.addElement(new Integer(0));\r
129             }\r
130 \r
131             for (int k = winstart; k <= winend; k++) {\r
132                 String refchar = refseq.getSequence().substring(k,k+1);\r
133 \r
134                 for (int j = 0; j < sequences.size(); j++) {\r
135 \r
136                     if (refchar.equals("-") == false) {\r
137 \r
138                         Sequence s = (Sequence)sequences.elementAt(j);\r
139 \r
140                         if (s.getSequence().length() > k) {\r
141 \r
142                             String res = s.getSequence().substring(k,k+1);\r
143 \r
144                             if (res.equals(refchar)) {\r
145                                 int val = ((Integer)values.elementAt(j)).intValue();\r
146                                 val++;\r
147                                 values.setElementAt(new Integer(val),j);\r
148                             }\r
149                         }\r
150                     }\r
151                 }\r
152             }\r
153             prev = values;\r
154         } else {\r
155             int winstart = i-window/2;\r
156             int winend   = i+window/2;\r
157 \r
158             if (window%2 != 0) {\r
159               winend++;\r
160             }\r
161             // We need to take the previous set of values\r
162             // subtract the pid at winstart-1\r
163             // and add the pid at winend;\r
164 \r
165             String pre_refchar  = refseq.getSequence().substring(winstart-1,winstart);\r
166             String pos_refchar = "-";\r
167 \r
168             if (refseq.getSequence().length() > winend) {\r
169               pos_refchar  = refseq.getSequence().substring(winend,winend+1);\r
170             }\r
171 \r
172             for (int j = 0; j < sequences.size(); j++) {\r
173                 // First copy the pid value from i-1\r
174 \r
175                 int val = ((Integer)prev.elementAt(j)).intValue();\r
176 \r
177                 Sequence s = (Sequence)sequences.elementAt(j);\r
178 \r
179                 String pre_char = s.getSequence().substring(winstart-1,winstart);\r
180 \r
181                 String pos_char = "-";\r
182 \r
183                 if (s.getSequence().length() > winend) {\r
184                   pos_char = s.getSequence().substring(winend,winend+1);\r
185                 }\r
186 \r
187                 // Now substract 1 if the chars at winstart-1 match\r
188 \r
189                 if (pre_refchar.equals("-") == false && pre_char.equals(pre_refchar)) {\r
190                     val--;\r
191                 }\r
192 \r
193                 if (pos_refchar.equals("-") == false && pos_char.equals(pos_refchar)) {\r
194                     val++;\r
195                 }\r
196 \r
197                 values.addElement(new Integer(val));\r
198 \r
199 \r
200             }\r
201             prev = values;\r
202         }\r
203     }\r
204 \r
205     return result;\r
206     }\r
207 \r
208     public static Hashtable findBlocks(Vector seqs, int start, int end,Vector exc) {\r
209 \r
210         // start and end are in real (not relative coords);\r
211 \r
212         // The coords in the hashtable that is returned are in relative coords\r
213         // i.e. start from 0\r
214 \r
215         Hashtable blocks = new Hashtable();\r
216 \r
217         boolean prev = false;\r
218         int     bstart = -1;\r
219 \r
220         for (int i = start; i <= end ; i++) {\r
221             SequenceI seq = (SequenceI)seqs.elementAt(0);\r
222 \r
223             char      c   = seq.getCharAt(i);\r
224 \r
225             boolean found = true;\r
226 \r
227             int j = 1;\r
228 \r
229             while (j < seqs.size() && found == true) {\r
230 \r
231                 SequenceI jseq  = (SequenceI)seqs.elementAt(j);\r
232 \r
233                 if (!exc.contains(jseq)) {\r
234 \r
235                     char cc = jseq.getCharAt(i);\r
236 \r
237                     if ( cc != c) {\r
238                         found = false;\r
239                     }\r
240                 }\r
241                 j++;\r
242             }\r
243 \r
244 \r
245             if (prev == false && found == true) {\r
246                 bstart = i;\r
247             } else if (prev == true && found == false && bstart != -1) {\r
248 \r
249                 int blockstart = bstart-start;\r
250                 int blocklen   = i-bstart;\r
251 \r
252                 //System.out.println("Start len " + blockstart + " " + blocklen);\r
253 \r
254                 for (int jj = blockstart; jj < blockstart + blocklen;jj++) {\r
255                     blocks.put(new Integer(jj),new Integer(blocklen));\r
256                 }\r
257 \r
258                 bstart = -1;\r
259             }\r
260             prev = found;\r
261         }\r
262 \r
263         if (bstart != -1) {\r
264 \r
265             int blockstart = bstart-start;\r
266             int blocklen   = end-bstart;\r
267 \r
268           //  System.out.println("Start len " + blockstart + " " + blocklen);\r
269 \r
270             for (int jj = blockstart; jj < blockstart + blocklen;jj++) {\r
271                 blocks.put(new Integer(blockstart),new Integer(blocklen));\r
272             }\r
273 \r
274         }\r
275         return blocks;\r
276     }\r
277 \r
278 \r
279 \r
280     public static Hashtable findKmerCount(SequenceI seq, int start, int end,int window, int step,Vector kmers) {\r
281 \r
282        int tmpstart = start;\r
283        Hashtable vals = new Hashtable();\r
284 \r
285        while (tmpstart <= end) {\r
286 \r
287            String tmpstr = seq.getSequence().substring(tmpstart-window/2,tmpstart+window/2);\r
288 \r
289            int count = 0;\r
290 \r
291            //System.out.println("Str " + tmpstr);\r
292 \r
293            for (int ii = 0; ii < kmers.size(); ii++) {\r
294                String kmer = ((SequenceI)kmers.elementAt(ii)).getSequence();\r
295 \r
296                int i = -1;\r
297 \r
298                while (tmpstr.indexOf(kmer,i) != -1) {\r
299                i = tmpstr.indexOf(kmer,i);\r
300 \r
301                i++;\r
302                count++;\r
303                }\r
304                ii++;\r
305            }\r
306            vals.put(new Integer(tmpstart),new Integer(count));\r
307            tmpstart += step;\r
308        }\r
309        return vals;\r
310     }\r
311 \r
312     public static Hashtable findBlockStarts(Vector seqs, int start, int end,Vector exc) {\r
313 \r
314         // start and end are in real (not relative coords);\r
315 \r
316         // The coords in the hashtable that is returned are in relative coords\r
317         // i.e. start from 0\r
318 \r
319         Hashtable blocks = new Hashtable();\r
320 \r
321         boolean prev = false;\r
322         int     bstart = -1;\r
323 \r
324         for (int i = start; i <= end ; i++) {\r
325             SequenceI seq = (SequenceI)seqs.elementAt(0);\r
326 \r
327             char      c   = seq.getCharAt(i);\r
328 \r
329             boolean found = true;\r
330 \r
331             int j = 1;\r
332 \r
333             while (j < seqs.size() && found == true) {\r
334 \r
335                 SequenceI jseq  = (SequenceI)seqs.elementAt(j);\r
336 \r
337                 if (!exc.contains(jseq)) {\r
338 \r
339                     char cc = jseq.getCharAt(i);\r
340 \r
341                     if ( cc != c) {\r
342                         found = false;\r
343                     }\r
344                 }\r
345                 j++;\r
346             }\r
347 \r
348 \r
349             if (prev == false && found == true) {\r
350                 bstart = i;\r
351             } else if (prev == true && found == false && bstart != -1) {\r
352 \r
353                 int blockstart = bstart-start;\r
354                 int blocklen   = i-bstart;\r
355 \r
356                 //              System.out.println("Start len " + blockstart + " " + blocklen);\r
357 \r
358                 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {\r
359                     blocks.put(new Integer(blockstart),new Integer(blocklen));\r
360         //      }\r
361 \r
362                 bstart = -1;\r
363             }\r
364             prev = found;\r
365         }\r
366 \r
367         if (bstart != -1) {\r
368 \r
369             int blockstart = bstart-start;\r
370             int blocklen   = end-bstart;\r
371 \r
372           //  System.out.println("Start len " + blockstart + " " + blocklen);\r
373 \r
374             //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {\r
375                 blocks.put(new Integer(blockstart),new Integer(blocklen));\r
376            // }\r
377 \r
378         }\r
379         return blocks;\r
380     }\r
381 \r
382 }\r
383 \r