1 package jalview.analysis;
\r
3 import jalview.jbgui.*;
\r
4 import jalview.datamodel.*;
\r
6 import jalview.analysis.*;
\r
9 import java.applet.Applet;
\r
14 public class AAFrequency {
\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
23 public static Vector calculate(Vector sequences,int start,int end) {
\r
25 Vector result = new Vector();
\r
27 for (int i = start;i <= end; i++)
\r
30 Hashtable residueHash = new Hashtable();
\r
32 String maxResidue = "-";
\r
34 for (int j=0; j < sequences.size(); j++)
\r
37 if (sequences.elementAt(j) instanceof Sequence)
\r
39 Sequence s = (Sequence)sequences.elementAt(j);
\r
41 if (s.getSequence().length() > i)
\r
44 String res = s.getSequence().substring(i,i+1);
\r
46 if (!res.equals("-"))
\r
49 if (residueHash.containsKey(res))
\r
52 int count = ((Integer)residueHash.get(res)).intValue() ;
\r
55 if (!res.equals("-") && count >= maxCount)
\r
60 else if(maxResidue.indexOf(res)==-1)
\r
66 residueHash.put(res,new Integer(count));
\r
69 residueHash.put(res,new Integer(1));
\r
75 if (residueHash.containsKey("-"))
\r
77 int count = ((Integer)residueHash.get("-")).intValue() ;
\r
79 residueHash.put("-",new Integer(count));
\r
82 residueHash.put("-",new Integer(1));
\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
97 public static Vector calculatePID(SequenceI refseq,Vector sequences,int window,int start,int end) {
\r
99 Vector result = new Vector();
\r
101 boolean init = true;
\r
104 Vector prev = null;
\r
106 for (int i = start;i <= end; i++) {
\r
107 Vector values = new Vector();
\r
109 result.addElement(values);
\r
111 // If start < window/2 then set value to zero.
\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
117 } else if (init == true) {
\r
120 int winstart = i-window/2;
\r
121 int winend = i+window/2;
\r
123 if (window%2 != 0) {
\r
127 for (int j = 0; j < sequences.size(); j++) {
\r
128 values.addElement(new Integer(0));
\r
131 for (int k = winstart; k <= winend; k++) {
\r
132 String refchar = refseq.getSequence().substring(k,k+1);
\r
134 for (int j = 0; j < sequences.size(); j++) {
\r
136 if (refchar.equals("-") == false) {
\r
138 Sequence s = (Sequence)sequences.elementAt(j);
\r
140 if (s.getSequence().length() > k) {
\r
142 String res = s.getSequence().substring(k,k+1);
\r
144 if (res.equals(refchar)) {
\r
145 int val = ((Integer)values.elementAt(j)).intValue();
\r
147 values.setElementAt(new Integer(val),j);
\r
155 int winstart = i-window/2;
\r
156 int winend = i+window/2;
\r
158 if (window%2 != 0) {
\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
165 String pre_refchar = refseq.getSequence().substring(winstart-1,winstart);
\r
166 String pos_refchar = "-";
\r
168 if (refseq.getSequence().length() > winend) {
\r
169 pos_refchar = refseq.getSequence().substring(winend,winend+1);
\r
172 for (int j = 0; j < sequences.size(); j++) {
\r
173 // First copy the pid value from i-1
\r
175 int val = ((Integer)prev.elementAt(j)).intValue();
\r
177 Sequence s = (Sequence)sequences.elementAt(j);
\r
179 String pre_char = s.getSequence().substring(winstart-1,winstart);
\r
181 String pos_char = "-";
\r
183 if (s.getSequence().length() > winend) {
\r
184 pos_char = s.getSequence().substring(winend,winend+1);
\r
187 // Now substract 1 if the chars at winstart-1 match
\r
189 if (pre_refchar.equals("-") == false && pre_char.equals(pre_refchar)) {
\r
193 if (pos_refchar.equals("-") == false && pos_char.equals(pos_refchar)) {
\r
197 values.addElement(new Integer(val));
\r
208 public static Hashtable findBlocks(Vector seqs, int start, int end,Vector exc) {
\r
210 // start and end are in real (not relative coords);
\r
212 // The coords in the hashtable that is returned are in relative coords
\r
213 // i.e. start from 0
\r
215 Hashtable blocks = new Hashtable();
\r
217 boolean prev = false;
\r
220 for (int i = start; i <= end ; i++) {
\r
221 SequenceI seq = (SequenceI)seqs.elementAt(0);
\r
223 char c = seq.getCharAt(i);
\r
225 boolean found = true;
\r
229 while (j < seqs.size() && found == true) {
\r
231 SequenceI jseq = (SequenceI)seqs.elementAt(j);
\r
233 if (!exc.contains(jseq)) {
\r
235 char cc = jseq.getCharAt(i);
\r
245 if (prev == false && found == true) {
\r
247 } else if (prev == true && found == false && bstart != -1) {
\r
249 int blockstart = bstart-start;
\r
250 int blocklen = i-bstart;
\r
252 //System.out.println("Start len " + blockstart + " " + blocklen);
\r
254 for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
255 blocks.put(new Integer(jj),new Integer(blocklen));
\r
263 if (bstart != -1) {
\r
265 int blockstart = bstart-start;
\r
266 int blocklen = end-bstart;
\r
268 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
270 for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
271 blocks.put(new Integer(blockstart),new Integer(blocklen));
\r
280 public static Hashtable findKmerCount(SequenceI seq, int start, int end,int window, int step,Vector kmers) {
\r
282 int tmpstart = start;
\r
283 Hashtable vals = new Hashtable();
\r
285 while (tmpstart <= end) {
\r
287 String tmpstr = seq.getSequence().substring(tmpstart-window/2,tmpstart+window/2);
\r
291 //System.out.println("Str " + tmpstr);
\r
293 for (int ii = 0; ii < kmers.size(); ii++) {
\r
294 String kmer = ((SequenceI)kmers.elementAt(ii)).getSequence();
\r
298 while (tmpstr.indexOf(kmer,i) != -1) {
\r
299 i = tmpstr.indexOf(kmer,i);
\r
306 vals.put(new Integer(tmpstart),new Integer(count));
\r
312 public static Hashtable findBlockStarts(Vector seqs, int start, int end,Vector exc) {
\r
314 // start and end are in real (not relative coords);
\r
316 // The coords in the hashtable that is returned are in relative coords
\r
317 // i.e. start from 0
\r
319 Hashtable blocks = new Hashtable();
\r
321 boolean prev = false;
\r
324 for (int i = start; i <= end ; i++) {
\r
325 SequenceI seq = (SequenceI)seqs.elementAt(0);
\r
327 char c = seq.getCharAt(i);
\r
329 boolean found = true;
\r
333 while (j < seqs.size() && found == true) {
\r
335 SequenceI jseq = (SequenceI)seqs.elementAt(j);
\r
337 if (!exc.contains(jseq)) {
\r
339 char cc = jseq.getCharAt(i);
\r
349 if (prev == false && found == true) {
\r
351 } else if (prev == true && found == false && bstart != -1) {
\r
353 int blockstart = bstart-start;
\r
354 int blocklen = i-bstart;
\r
356 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
358 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
359 blocks.put(new Integer(blockstart),new Integer(blocklen));
\r
367 if (bstart != -1) {
\r
369 int blockstart = bstart-start;
\r
370 int blocklen = end-bstart;
\r
372 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
374 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
375 blocks.put(new Integer(blockstart),new Integer(blocklen));
\r