2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
20 package jalview.analysis;
\r
22 import jalview.jbgui.*;
\r
23 import jalview.datamodel.*;
\r
24 import jalview.io.*;
\r
25 import jalview.analysis.*;
\r
28 import java.applet.Applet;
\r
33 public class AAFrequency {
\r
35 // Takes in a vector of sequences and column start and column end
\r
36 // and returns a vector of size (end-start+1). Each element of the
\r
37 // vector contains a hashtable with the keys being residues and
\r
38 // the values being the count of each residue in that column.
\r
39 // This class is used extensively in calculating alignment colourschemes
\r
40 // that depend on the amount of conservation in each alignment column.
\r
43 public static Vector calculate(Vector sequences,int start,int end) {
\r
45 Vector result = new Vector();
\r
47 for (int i = start;i <= end; i++)
\r
50 Hashtable residueHash = new Hashtable();
\r
52 String maxResidue = "-";
\r
54 for (int j=0; j < sequences.size(); j++)
\r
57 if (sequences.elementAt(j) instanceof Sequence)
\r
59 Sequence s = (Sequence)sequences.elementAt(j);
\r
61 if (s.getSequence().length() > i)
\r
64 String res = s.getSequence().charAt(i)+"";
\r
66 if (!jalview.util.Comparison.isGap(res.charAt(0)))
\r
69 res = "-"; // we always use this for gaps in the property vectors
\r
71 if (residueHash.containsKey(res))
\r
74 int count = ((Integer)residueHash.get(res)).intValue() ;
\r
77 if (!jalview.util.Comparison.isGap(res.charAt(0)) && count >= maxCount)
\r
82 else if(maxResidue.indexOf(res)==-1)
\r
88 residueHash.put(res,new Integer(count));
\r
91 residueHash.put(res,new Integer(1));
\r
97 if (residueHash.containsKey("-"))
\r
99 int count = ((Integer)residueHash.get("-")).intValue() ;
\r
101 residueHash.put("-",new Integer(count));
\r
104 residueHash.put("-",new Integer(1));
\r
110 residueHash.put("maxCount",new Integer(maxCount));
\r
112 System.out.println("asasa "+maxCount);
\r
113 residueHash.put("maxResidue", maxResidue);
\r
114 residueHash.put("size", new Integer(sequences.size()));
\r
115 residueHash.put("nongap", new Integer(nongap));
\r
116 result.addElement(residueHash);
\r
122 public static Vector calculatePID(SequenceI refseq,Vector sequences,int window,int start,int end) {
\r
124 Vector result = new Vector();
\r
126 boolean init = true;
\r
129 Vector prev = null;
\r
131 for (int i = start;i <= end; i++) {
\r
132 Vector values = new Vector();
\r
134 result.addElement(values);
\r
136 // If start < window/2 then set value to zero.
\r
138 if (i< window/2 || i >= refseq.getSequence().length()-window/2) {
\r
139 for (int j = 0; j < sequences.size(); j++) {
\r
140 values.addElement(new Integer(0));
\r
142 } else if (init == true) {
\r
145 int winstart = i-window/2;
\r
146 int winend = i+window/2;
\r
148 if (window%2 != 0) {
\r
152 for (int j = 0; j < sequences.size(); j++) {
\r
153 values.addElement(new Integer(0));
\r
156 for (int k = winstart; k <= winend; k++) {
\r
157 String refchar = refseq.getSequence().substring(k,k+1);
\r
158 if (jalview.util.Comparison.isGap(refchar.charAt(0)))
\r
161 for (int j = 0; j < sequences.size(); j++) {
\r
163 Sequence s = (Sequence)sequences.elementAt(j);
\r
165 if (s.getSequence().length() > k) {
\r
167 String res = s.getSequence().substring(k,k+1); // no gapchar test needed
\r
169 if (res.equals(refchar)) {
\r
170 int val = ((Integer)values.elementAt(j)).intValue();
\r
172 values.setElementAt(new Integer(val),j);
\r
182 int winstart = i-window/2;
\r
183 int winend = i+window/2;
\r
185 if (window%2 != 0) {
\r
188 // We need to take the previous set of values
\r
189 // subtract the pid at winstart-1
\r
190 // and add the pid at winend;
\r
192 String pre_refchar = refseq.getSequence().substring(winstart-1,winstart);
\r
193 String pos_refchar = "-";
\r
195 if (refseq.getSequence().length() > winend) {
\r
196 pos_refchar = refseq.getSequence().substring(winend,winend+1);
\r
199 for (int j = 0; j < sequences.size(); j++) {
\r
200 // First copy the pid value from i-1
\r
202 int val = ((Integer)prev.elementAt(j)).intValue();
\r
204 Sequence s = (Sequence)sequences.elementAt(j);
\r
206 String pre_char = s.getSequence().substring(winstart-1,winstart);
\r
208 String pos_char = "-";
\r
210 if (s.getSequence().length() > winend) {
\r
211 pos_char = s.getSequence().substring(winend,winend+1);
\r
214 // Now substract 1 if the chars at winstart-1 match
\r
216 if (jalview.util.Comparison.isGap(pre_refchar.charAt(0)) == false
\r
217 && pre_char.equals(pre_refchar)) {
\r
221 if (jalview.util.Comparison.isGap(pos_refchar.charAt(0)) == false
\r
222 && pos_char.equals(pos_refchar)) {
\r
226 values.addElement(new Integer(val));
\r
237 public static Hashtable findBlocks(Vector seqs, int start, int end,Vector exc) {
\r
239 // start and end are in real (not relative coords);
\r
241 // The coords in the hashtable that is returned are in relative coords
\r
242 // i.e. start from 0
\r
244 Hashtable blocks = new Hashtable();
\r
246 boolean prev = false;
\r
249 for (int i = start; i <= end ; i++) {
\r
250 SequenceI seq = (SequenceI)seqs.elementAt(0);
\r
252 char c = seq.getCharAt(i);
\r
254 boolean found = true;
\r
258 while (j < seqs.size() && found == true) {
\r
260 SequenceI jseq = (SequenceI)seqs.elementAt(j);
\r
262 if (!exc.contains(jseq)) {
\r
264 char cc = jseq.getCharAt(i);
\r
274 if (prev == false && found == true) {
\r
276 } else if (prev == true && found == false && bstart != -1) {
\r
278 int blockstart = bstart-start;
\r
279 int blocklen = i-bstart;
\r
281 //System.out.println("Start len " + blockstart + " " + blocklen);
\r
283 for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
284 blocks.put(new Integer(jj),new Integer(blocklen));
\r
292 if (bstart != -1) {
\r
294 int blockstart = bstart-start;
\r
295 int blocklen = end-bstart;
\r
297 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
299 for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
300 blocks.put(new Integer(blockstart),new Integer(blocklen));
\r
309 public static Hashtable findKmerCount(SequenceI seq, int start, int end,int window, int step,Vector kmers) {
\r
311 int tmpstart = start;
\r
312 Hashtable vals = new Hashtable();
\r
314 while (tmpstart <= end) {
\r
316 String tmpstr = seq.getSequence().substring(tmpstart-window/2,tmpstart+window/2);
\r
320 //System.out.println("Str " + tmpstr);
\r
322 for (int ii = 0; ii < kmers.size(); ii++) {
\r
323 String kmer = ((SequenceI)kmers.elementAt(ii)).getSequence();
\r
327 while (tmpstr.indexOf(kmer,i) != -1) {
\r
328 i = tmpstr.indexOf(kmer,i);
\r
335 vals.put(new Integer(tmpstart),new Integer(count));
\r
341 public static Hashtable findBlockStarts(Vector seqs, int start, int end,Vector exc) {
\r
343 // start and end are in real (not relative coords);
\r
345 // The coords in the hashtable that is returned are in relative coords
\r
346 // i.e. start from 0
\r
348 Hashtable blocks = new Hashtable();
\r
350 boolean prev = false;
\r
353 for (int i = start; i <= end ; i++) {
\r
354 SequenceI seq = (SequenceI)seqs.elementAt(0);
\r
356 char c = seq.getCharAt(i);
\r
358 boolean found = true;
\r
362 while (j < seqs.size() && found == true) {
\r
364 SequenceI jseq = (SequenceI)seqs.elementAt(j);
\r
366 if (!exc.contains(jseq)) {
\r
368 char cc = jseq.getCharAt(i);
\r
378 if (prev == false && found == true) {
\r
380 } else if (prev == true && found == false && bstart != -1) {
\r
382 int blockstart = bstart-start;
\r
383 int blocklen = i-bstart;
\r
385 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
387 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
388 blocks.put(new Integer(blockstart),new Integer(blocklen));
\r
396 if (bstart != -1) {
\r
398 int blockstart = bstart-start;
\r
399 int blocklen = end-bstart;
\r
401 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
403 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
404 blocks.put(new Integer(blockstart),new Integer(blocklen));
\r