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().charAt(i)+"";
\r
46 if (!jalview.util.Comparison.isGap(res.charAt(0)))
\r
49 if (residueHash.containsKey(res))
\r
52 int count = ((Integer)residueHash.get(res)).intValue() ;
\r
55 if (!jalview.util.Comparison.isGap(res.charAt(0)) && 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
88 residueHash.put("maxCount",new Integer(maxCount));
\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
100 public static Vector calculatePID(SequenceI refseq,Vector sequences,int window,int start,int end) {
\r
102 Vector result = new Vector();
\r
104 boolean init = true;
\r
107 Vector prev = null;
\r
109 for (int i = start;i <= end; i++) {
\r
110 Vector values = new Vector();
\r
112 result.addElement(values);
\r
114 // If start < window/2 then set value to zero.
\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
120 } else if (init == true) {
\r
123 int winstart = i-window/2;
\r
124 int winend = i+window/2;
\r
126 if (window%2 != 0) {
\r
130 for (int j = 0; j < sequences.size(); j++) {
\r
131 values.addElement(new Integer(0));
\r
134 for (int k = winstart; k <= winend; k++) {
\r
135 String refchar = refseq.getSequence().substring(k,k+1);
\r
137 for (int j = 0; j < sequences.size(); j++) {
\r
139 if (refchar.equals("-") == false) {
\r
141 Sequence s = (Sequence)sequences.elementAt(j);
\r
143 if (s.getSequence().length() > k) {
\r
145 String res = s.getSequence().substring(k,k+1);
\r
147 if (res.equals(refchar)) {
\r
148 int val = ((Integer)values.elementAt(j)).intValue();
\r
150 values.setElementAt(new Integer(val),j);
\r
158 int winstart = i-window/2;
\r
159 int winend = i+window/2;
\r
161 if (window%2 != 0) {
\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
168 String pre_refchar = refseq.getSequence().substring(winstart-1,winstart);
\r
169 String pos_refchar = "-";
\r
171 if (refseq.getSequence().length() > winend) {
\r
172 pos_refchar = refseq.getSequence().substring(winend,winend+1);
\r
175 for (int j = 0; j < sequences.size(); j++) {
\r
176 // First copy the pid value from i-1
\r
178 int val = ((Integer)prev.elementAt(j)).intValue();
\r
180 Sequence s = (Sequence)sequences.elementAt(j);
\r
182 String pre_char = s.getSequence().substring(winstart-1,winstart);
\r
184 String pos_char = "-";
\r
186 if (s.getSequence().length() > winend) {
\r
187 pos_char = s.getSequence().substring(winend,winend+1);
\r
190 // Now substract 1 if the chars at winstart-1 match
\r
192 if (pre_refchar.equals("-") == false && pre_char.equals(pre_refchar)) {
\r
196 if (pos_refchar.equals("-") == false && pos_char.equals(pos_refchar)) {
\r
200 values.addElement(new Integer(val));
\r
211 public static Hashtable findBlocks(Vector seqs, int start, int end,Vector exc) {
\r
213 // start and end are in real (not relative coords);
\r
215 // The coords in the hashtable that is returned are in relative coords
\r
216 // i.e. start from 0
\r
218 Hashtable blocks = new Hashtable();
\r
220 boolean prev = false;
\r
223 for (int i = start; i <= end ; i++) {
\r
224 SequenceI seq = (SequenceI)seqs.elementAt(0);
\r
226 char c = seq.getCharAt(i);
\r
228 boolean found = true;
\r
232 while (j < seqs.size() && found == true) {
\r
234 SequenceI jseq = (SequenceI)seqs.elementAt(j);
\r
236 if (!exc.contains(jseq)) {
\r
238 char cc = jseq.getCharAt(i);
\r
248 if (prev == false && found == true) {
\r
250 } else if (prev == true && found == false && bstart != -1) {
\r
252 int blockstart = bstart-start;
\r
253 int blocklen = i-bstart;
\r
255 //System.out.println("Start len " + blockstart + " " + blocklen);
\r
257 for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
258 blocks.put(new Integer(jj),new Integer(blocklen));
\r
266 if (bstart != -1) {
\r
268 int blockstart = bstart-start;
\r
269 int blocklen = end-bstart;
\r
271 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
273 for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
274 blocks.put(new Integer(blockstart),new Integer(blocklen));
\r
283 public static Hashtable findKmerCount(SequenceI seq, int start, int end,int window, int step,Vector kmers) {
\r
285 int tmpstart = start;
\r
286 Hashtable vals = new Hashtable();
\r
288 while (tmpstart <= end) {
\r
290 String tmpstr = seq.getSequence().substring(tmpstart-window/2,tmpstart+window/2);
\r
294 //System.out.println("Str " + tmpstr);
\r
296 for (int ii = 0; ii < kmers.size(); ii++) {
\r
297 String kmer = ((SequenceI)kmers.elementAt(ii)).getSequence();
\r
301 while (tmpstr.indexOf(kmer,i) != -1) {
\r
302 i = tmpstr.indexOf(kmer,i);
\r
309 vals.put(new Integer(tmpstart),new Integer(count));
\r
315 public static Hashtable findBlockStarts(Vector seqs, int start, int end,Vector exc) {
\r
317 // start and end are in real (not relative coords);
\r
319 // The coords in the hashtable that is returned are in relative coords
\r
320 // i.e. start from 0
\r
322 Hashtable blocks = new Hashtable();
\r
324 boolean prev = false;
\r
327 for (int i = start; i <= end ; i++) {
\r
328 SequenceI seq = (SequenceI)seqs.elementAt(0);
\r
330 char c = seq.getCharAt(i);
\r
332 boolean found = true;
\r
336 while (j < seqs.size() && found == true) {
\r
338 SequenceI jseq = (SequenceI)seqs.elementAt(j);
\r
340 if (!exc.contains(jseq)) {
\r
342 char cc = jseq.getCharAt(i);
\r
352 if (prev == false && found == true) {
\r
354 } else if (prev == true && found == false && bstart != -1) {
\r
356 int blockstart = bstart-start;
\r
357 int blocklen = i-bstart;
\r
359 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
361 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
362 blocks.put(new Integer(blockstart),new Integer(blocklen));
\r
370 if (bstart != -1) {
\r
372 int blockstart = bstart-start;
\r
373 int blocklen = end-bstart;
\r
375 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
377 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
378 blocks.put(new Integer(blockstart),new Integer(blocklen));
\r