checkfor gap characters
[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    = 0;\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().charAt(i)+"";\r
45 \r
46             if (!jalview.util.Comparison.isGap(res.charAt(0)))\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 (!jalview.util.Comparison.isGap(res.charAt(0)) && 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 \r
88       residueHash.put("maxCount",new Integer(maxCount));\r
89       if(maxCount<0)\r
90         System.out.println("asasa "+maxCount);\r
91       residueHash.put("maxResidue", maxResidue);\r
92       residueHash.put("size", new Integer(sequences.size()));\r
93       residueHash.put("nongap", new Integer(nongap));\r
94       result.addElement(residueHash);\r
95     }\r
96 \r
97     return result;\r
98   }\r
99 \r
100     public static Vector calculatePID(SequenceI refseq,Vector sequences,int window,int start,int end) {\r
101 \r
102     Vector result = new Vector();\r
103 \r
104     boolean init = true;\r
105 \r
106 \r
107     Vector prev = null;\r
108 \r
109     for (int i = start;i <= end; i++) {\r
110         Vector values = new Vector();\r
111 \r
112         result.addElement(values);\r
113 \r
114         // If start < window/2 then set value to zero.\r
115 \r
116         if (i< window/2 || i >= refseq.getSequence().length()-window/2) {\r
117             for (int j = 0; j < sequences.size(); j++) {\r
118                 values.addElement(new Integer(0));\r
119             }\r
120         } else  if (init == true) {\r
121             init = false;\r
122 \r
123             int winstart = i-window/2;\r
124             int winend   = i+window/2;\r
125 \r
126             if (window%2 != 0) {\r
127               winend++;\r
128             }\r
129 \r
130             for (int j = 0; j < sequences.size(); j++) {\r
131                 values.addElement(new Integer(0));\r
132             }\r
133 \r
134             for (int k = winstart; k <= winend; k++) {\r
135                 String refchar = refseq.getSequence().substring(k,k+1);\r
136 \r
137                 for (int j = 0; j < sequences.size(); j++) {\r
138 \r
139                     if (refchar.equals("-") == false) {\r
140 \r
141                         Sequence s = (Sequence)sequences.elementAt(j);\r
142 \r
143                         if (s.getSequence().length() > k) {\r
144 \r
145                             String res = s.getSequence().substring(k,k+1);\r
146 \r
147                             if (res.equals(refchar)) {\r
148                                 int val = ((Integer)values.elementAt(j)).intValue();\r
149                                 val++;\r
150                                 values.setElementAt(new Integer(val),j);\r
151                             }\r
152                         }\r
153                     }\r
154                 }\r
155             }\r
156             prev = values;\r
157         } else {\r
158             int winstart = i-window/2;\r
159             int winend   = i+window/2;\r
160 \r
161             if (window%2 != 0) {\r
162               winend++;\r
163             }\r
164             // We need to take the previous set of values\r
165             // subtract the pid at winstart-1\r
166             // and add the pid at winend;\r
167 \r
168             String pre_refchar  = refseq.getSequence().substring(winstart-1,winstart);\r
169             String pos_refchar = "-";\r
170 \r
171             if (refseq.getSequence().length() > winend) {\r
172               pos_refchar  = refseq.getSequence().substring(winend,winend+1);\r
173             }\r
174 \r
175             for (int j = 0; j < sequences.size(); j++) {\r
176                 // First copy the pid value from i-1\r
177 \r
178                 int val = ((Integer)prev.elementAt(j)).intValue();\r
179 \r
180                 Sequence s = (Sequence)sequences.elementAt(j);\r
181 \r
182                 String pre_char = s.getSequence().substring(winstart-1,winstart);\r
183 \r
184                 String pos_char = "-";\r
185 \r
186                 if (s.getSequence().length() > winend) {\r
187                   pos_char = s.getSequence().substring(winend,winend+1);\r
188                 }\r
189 \r
190                 // Now substract 1 if the chars at winstart-1 match\r
191 \r
192                 if (pre_refchar.equals("-") == false && pre_char.equals(pre_refchar)) {\r
193                     val--;\r
194                 }\r
195 \r
196                 if (pos_refchar.equals("-") == false && pos_char.equals(pos_refchar)) {\r
197                     val++;\r
198                 }\r
199 \r
200                 values.addElement(new Integer(val));\r
201 \r
202 \r
203             }\r
204             prev = values;\r
205         }\r
206     }\r
207 \r
208     return result;\r
209     }\r
210 \r
211     public static Hashtable findBlocks(Vector seqs, int start, int end,Vector exc) {\r
212 \r
213         // start and end are in real (not relative coords);\r
214 \r
215         // The coords in the hashtable that is returned are in relative coords\r
216         // i.e. start from 0\r
217 \r
218         Hashtable blocks = new Hashtable();\r
219 \r
220         boolean prev = false;\r
221         int     bstart = -1;\r
222 \r
223         for (int i = start; i <= end ; i++) {\r
224             SequenceI seq = (SequenceI)seqs.elementAt(0);\r
225 \r
226             char      c   = seq.getCharAt(i);\r
227 \r
228             boolean found = true;\r
229 \r
230             int j = 1;\r
231 \r
232             while (j < seqs.size() && found == true) {\r
233 \r
234                 SequenceI jseq  = (SequenceI)seqs.elementAt(j);\r
235 \r
236                 if (!exc.contains(jseq)) {\r
237 \r
238                     char cc = jseq.getCharAt(i);\r
239 \r
240                     if ( cc != c) {\r
241                         found = false;\r
242                     }\r
243                 }\r
244                 j++;\r
245             }\r
246 \r
247 \r
248             if (prev == false && found == true) {\r
249                 bstart = i;\r
250             } else if (prev == true && found == false && bstart != -1) {\r
251 \r
252                 int blockstart = bstart-start;\r
253                 int blocklen   = i-bstart;\r
254 \r
255                 //System.out.println("Start len " + blockstart + " " + blocklen);\r
256 \r
257                 for (int jj = blockstart; jj < blockstart + blocklen;jj++) {\r
258                     blocks.put(new Integer(jj),new Integer(blocklen));\r
259                 }\r
260 \r
261                 bstart = -1;\r
262             }\r
263             prev = found;\r
264         }\r
265 \r
266         if (bstart != -1) {\r
267 \r
268             int blockstart = bstart-start;\r
269             int blocklen   = end-bstart;\r
270 \r
271           //  System.out.println("Start len " + blockstart + " " + blocklen);\r
272 \r
273             for (int jj = blockstart; jj < blockstart + blocklen;jj++) {\r
274                 blocks.put(new Integer(blockstart),new Integer(blocklen));\r
275             }\r
276 \r
277         }\r
278         return blocks;\r
279     }\r
280 \r
281 \r
282 \r
283     public static Hashtable findKmerCount(SequenceI seq, int start, int end,int window, int step,Vector kmers) {\r
284 \r
285        int tmpstart = start;\r
286        Hashtable vals = new Hashtable();\r
287 \r
288        while (tmpstart <= end) {\r
289 \r
290            String tmpstr = seq.getSequence().substring(tmpstart-window/2,tmpstart+window/2);\r
291 \r
292            int count = 0;\r
293 \r
294            //System.out.println("Str " + tmpstr);\r
295 \r
296            for (int ii = 0; ii < kmers.size(); ii++) {\r
297                String kmer = ((SequenceI)kmers.elementAt(ii)).getSequence();\r
298 \r
299                int i = -1;\r
300 \r
301                while (tmpstr.indexOf(kmer,i) != -1) {\r
302                i = tmpstr.indexOf(kmer,i);\r
303 \r
304                i++;\r
305                count++;\r
306                }\r
307                ii++;\r
308            }\r
309            vals.put(new Integer(tmpstart),new Integer(count));\r
310            tmpstart += step;\r
311        }\r
312        return vals;\r
313     }\r
314 \r
315     public static Hashtable findBlockStarts(Vector seqs, int start, int end,Vector exc) {\r
316 \r
317         // start and end are in real (not relative coords);\r
318 \r
319         // The coords in the hashtable that is returned are in relative coords\r
320         // i.e. start from 0\r
321 \r
322         Hashtable blocks = new Hashtable();\r
323 \r
324         boolean prev = false;\r
325         int     bstart = -1;\r
326 \r
327         for (int i = start; i <= end ; i++) {\r
328             SequenceI seq = (SequenceI)seqs.elementAt(0);\r
329 \r
330             char      c   = seq.getCharAt(i);\r
331 \r
332             boolean found = true;\r
333 \r
334             int j = 1;\r
335 \r
336             while (j < seqs.size() && found == true) {\r
337 \r
338                 SequenceI jseq  = (SequenceI)seqs.elementAt(j);\r
339 \r
340                 if (!exc.contains(jseq)) {\r
341 \r
342                     char cc = jseq.getCharAt(i);\r
343 \r
344                     if ( cc != c) {\r
345                         found = false;\r
346                     }\r
347                 }\r
348                 j++;\r
349             }\r
350 \r
351 \r
352             if (prev == false && found == true) {\r
353                 bstart = i;\r
354             } else if (prev == true && found == false && bstart != -1) {\r
355 \r
356                 int blockstart = bstart-start;\r
357                 int blocklen   = i-bstart;\r
358 \r
359                 //              System.out.println("Start len " + blockstart + " " + blocklen);\r
360 \r
361                 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {\r
362                     blocks.put(new Integer(blockstart),new Integer(blocklen));\r
363         //      }\r
364 \r
365                 bstart = -1;\r
366             }\r
367             prev = found;\r
368         }\r
369 \r
370         if (bstart != -1) {\r
371 \r
372             int blockstart = bstart-start;\r
373             int blocklen   = end-bstart;\r
374 \r
375           //  System.out.println("Start len " + blockstart + " " + blocklen);\r
376 \r
377             //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {\r
378                 blocks.put(new Integer(blockstart),new Integer(blocklen));\r
379            // }\r
380 \r
381         }\r
382         return blocks;\r
383     }\r
384 \r
385 }\r
386 \r