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
19 package jalview.analysis;
\r
21 import jalview.analysis.*;
\r
23 import jalview.datamodel.*;
\r
25 import jalview.io.*;
\r
27 import jalview.jbgui.*;
\r
29 import java.applet.Applet;
\r
40 public class AAFrequency {
\r
41 // Takes in a vector of sequences and column start and column end
\r
42 // and returns a vector of size (end-start+1). Each element of the
\r
43 // vector contains a hashtable with the keys being residues and
\r
44 // the values being the count of each residue in that column.
\r
45 // This class is used extensively in calculating alignment colourschemes
\r
46 // that depend on the amount of conservation in each alignment column.
\r
47 public static Vector calculate(Vector sequences, int start, int end) {
\r
48 Vector result = new Vector();
\r
50 for (int i = start; i <= end; i++) {
\r
51 Hashtable residueHash = new Hashtable();
\r
53 String maxResidue = "-";
\r
56 for (int j = 0; j < sequences.size(); j++) {
\r
57 if (sequences.elementAt(j) instanceof Sequence) {
\r
58 Sequence s = (Sequence) sequences.elementAt(j);
\r
60 if (s.getSequence().length() > i) {
\r
61 String res = s.getSequence().charAt(i) + "";
\r
63 if (!jalview.util.Comparison.isGap(res.charAt(0))) {
\r
66 res = "-"; // we always use this for gaps in the property vectors
\r
69 if (residueHash.containsKey(res)) {
\r
70 int count = ((Integer) residueHash.get(res)).intValue();
\r
73 if (!jalview.util.Comparison.isGap(res.charAt(0)) &&
\r
74 (count >= maxCount)) {
\r
75 if (count > maxCount) {
\r
77 } else if (maxResidue.indexOf(res) == -1) {
\r
84 residueHash.put(res, new Integer(count));
\r
86 residueHash.put(res, new Integer(1));
\r
89 if (residueHash.containsKey("-")) {
\r
90 int count = ((Integer) residueHash.get("-")).intValue();
\r
92 residueHash.put("-", new Integer(count));
\r
94 residueHash.put("-", new Integer(1));
\r
100 residueHash.put("maxCount", new Integer(maxCount));
\r
102 if (maxCount < 0) {
\r
103 System.out.println("asasa " + maxCount);
\r
106 residueHash.put("maxResidue", maxResidue);
\r
107 residueHash.put("size", new Integer(sequences.size()));
\r
108 residueHash.put("nongap", new Integer(nongap));
\r
109 result.addElement(residueHash);
\r
115 public static Vector calculatePID(SequenceI refseq, Vector sequences,
\r
116 int window, int start, int end) {
\r
117 Vector result = new Vector();
\r
119 boolean init = true;
\r
121 Vector prev = null;
\r
123 for (int i = start; i <= end; i++) {
\r
124 Vector values = new Vector();
\r
126 result.addElement(values);
\r
128 // If start < window/2 then set value to zero.
\r
129 if ((i < (window / 2)) ||
\r
130 (i >= (refseq.getSequence().length() - (window / 2)))) {
\r
131 for (int j = 0; j < sequences.size(); j++) {
\r
132 values.addElement(new Integer(0));
\r
134 } else if (init == true) {
\r
137 int winstart = i - (window / 2);
\r
138 int winend = i + (window / 2);
\r
140 if ((window % 2) != 0) {
\r
144 for (int j = 0; j < sequences.size(); j++) {
\r
145 values.addElement(new Integer(0));
\r
148 for (int k = winstart; k <= winend; k++) {
\r
149 String refchar = refseq.getSequence().substring(k, k + 1);
\r
151 if (jalview.util.Comparison.isGap(refchar.charAt(0))) {
\r
154 for (int j = 0; j < sequences.size(); j++) {
\r
155 Sequence s = (Sequence) sequences.elementAt(j);
\r
157 if (s.getSequence().length() > k) {
\r
158 String res = s.getSequence().substring(k, k +
\r
159 1); // no gapchar test needed
\r
161 if (res.equals(refchar)) {
\r
162 int val = ((Integer) values.elementAt(j)).intValue();
\r
164 values.setElementAt(new Integer(val), j);
\r
173 int winstart = i - (window / 2);
\r
174 int winend = i + (window / 2);
\r
176 if ((window % 2) != 0) {
\r
180 // We need to take the previous set of values
\r
181 // subtract the pid at winstart-1
\r
182 // and add the pid at winend;
\r
183 String pre_refchar = refseq.getSequence().substring(winstart -
\r
185 String pos_refchar = "-";
\r
187 if (refseq.getSequence().length() > winend) {
\r
188 pos_refchar = refseq.getSequence().substring(winend,
\r
192 for (int j = 0; j < sequences.size(); j++) {
\r
193 // First copy the pid value from i-1
\r
194 int val = ((Integer) prev.elementAt(j)).intValue();
\r
196 Sequence s = (Sequence) sequences.elementAt(j);
\r
198 String pre_char = s.getSequence().substring(winstart - 1,
\r
201 String pos_char = "-";
\r
203 if (s.getSequence().length() > winend) {
\r
204 pos_char = s.getSequence().substring(winend, winend +
\r
208 // Now substract 1 if the chars at winstart-1 match
\r
209 if ((jalview.util.Comparison.isGap(pre_refchar.charAt(0)) == false) &&
\r
210 pre_char.equals(pre_refchar)) {
\r
214 if ((jalview.util.Comparison.isGap(pos_refchar.charAt(0)) == false) &&
\r
215 pos_char.equals(pos_refchar)) {
\r
219 values.addElement(new Integer(val));
\r
229 public static Hashtable findBlocks(Vector seqs, int start, int end,
\r
231 // start and end are in real (not relative coords);
\r
232 // The coords in the hashtable that is returned are in relative coords
\r
233 // i.e. start from 0
\r
234 Hashtable blocks = new Hashtable();
\r
236 boolean prev = false;
\r
239 for (int i = start; i <= end; i++) {
\r
240 SequenceI seq = (SequenceI) seqs.elementAt(0);
\r
242 char c = seq.getCharAt(i);
\r
244 boolean found = true;
\r
248 while ((j < seqs.size()) && (found == true)) {
\r
249 SequenceI jseq = (SequenceI) seqs.elementAt(j);
\r
251 if (!exc.contains(jseq)) {
\r
252 char cc = jseq.getCharAt(i);
\r
262 if ((prev == false) && (found == true)) {
\r
264 } else if ((prev == true) && (found == false) && (bstart != -1)) {
\r
265 int blockstart = bstart - start;
\r
266 int blocklen = i - bstart;
\r
268 //System.out.println("Start len " + blockstart + " " + blocklen);
\r
269 for (int jj = blockstart; jj < (blockstart + blocklen); jj++) {
\r
270 blocks.put(new Integer(jj), new Integer(blocklen));
\r
279 if (bstart != -1) {
\r
280 int blockstart = bstart - start;
\r
281 int blocklen = end - bstart;
\r
283 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
284 for (int jj = blockstart; jj < (blockstart + blocklen); jj++) {
\r
285 blocks.put(new Integer(blockstart), new Integer(blocklen));
\r
292 public static Hashtable findKmerCount(SequenceI seq, int start, int end,
\r
293 int window, int step, Vector kmers) {
\r
294 int tmpstart = start;
\r
295 Hashtable vals = new Hashtable();
\r
297 while (tmpstart <= end) {
\r
298 String tmpstr = seq.getSequence().substring(tmpstart -
\r
299 (window / 2), tmpstart + (window / 2));
\r
303 //System.out.println("Str " + tmpstr);
\r
304 for (int ii = 0; ii < kmers.size(); ii++) {
\r
305 String kmer = ((SequenceI) kmers.elementAt(ii)).getSequence();
\r
309 while (tmpstr.indexOf(kmer, i) != -1) {
\r
310 i = tmpstr.indexOf(kmer, i);
\r
319 vals.put(new Integer(tmpstart), new Integer(count));
\r
326 public static Hashtable findBlockStarts(Vector seqs, int start, int end,
\r
328 // start and end are in real (not relative coords);
\r
329 // The coords in the hashtable that is returned are in relative coords
\r
330 // i.e. start from 0
\r
331 Hashtable blocks = new Hashtable();
\r
333 boolean prev = false;
\r
336 for (int i = start; i <= end; i++) {
\r
337 SequenceI seq = (SequenceI) seqs.elementAt(0);
\r
339 char c = seq.getCharAt(i);
\r
341 boolean found = true;
\r
345 while ((j < seqs.size()) && (found == true)) {
\r
346 SequenceI jseq = (SequenceI) seqs.elementAt(j);
\r
348 if (!exc.contains(jseq)) {
\r
349 char cc = jseq.getCharAt(i);
\r
359 if ((prev == false) && (found == true)) {
\r
361 } else if ((prev == true) && (found == false) && (bstart != -1)) {
\r
362 int blockstart = bstart - start;
\r
363 int blocklen = i - bstart;
\r
365 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
366 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
367 blocks.put(new Integer(blockstart), new Integer(blocklen));
\r
376 if (bstart != -1) {
\r
377 int blockstart = bstart - start;
\r
378 int blocklen = end - bstart;
\r
380 // System.out.println("Start len " + blockstart + " " + blocklen);
\r
381 //for (int jj = blockstart; jj < blockstart + blocklen;jj++) {
\r
382 blocks.put(new Integer(blockstart), new Integer(blocklen));
\r