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.datamodel.*;
\r
27 * Calculates conservation values for a given set of sequences
\r
30 * @version $Revision$
\r
32 public class Conservation
\r
37 Vector seqNums; // vector of int vectors where first is sequence checksum
\r
38 int maxLength = 0; // used by quality calcs
\r
39 boolean seqNumsChanged = false; // updated after any change via calcSeqNum;
\r
40 Vector total = new Vector();
\r
42 /** Stores calculated quality values */
\r
43 public Vector quality;
\r
45 /** Stores maximum and minimum values of quality values */
\r
46 public Double[] qualityRange = new Double[2];
\r
47 String consString = "";
\r
48 Sequence consSequence;
\r
55 * Creates a new Conservation object.
\r
57 * @param name Name of conservation
\r
58 * @param propHash DOCUMENT ME!
\r
59 * @param threshold to count the residues in residueHash(). commonly used value is 3
\r
60 * @param sequences sequences to be used in calculation
\r
61 * @param start start residue position
\r
62 * @param end end residue position
\r
64 public Conservation(String name, Hashtable propHash, int threshold,
\r
65 Vector sequences, int start, int end)
\r
68 this.propHash = propHash;
\r
69 this.threshold = threshold;
\r
70 this.sequences = sequences;
\r
73 seqNums = new Vector(sequences.size());
\r
80 private void calcSeqNums()
\r
82 for (int i = 0; i < sequences.size(); i++)
\r
91 * @param i DOCUMENT ME!
\r
93 private void calcSeqNum(int i)
\r
95 String sq = null; // for dumb jbuilder not-inited exception warning
\r
98 if ((i > -1) && (i < sequences.size()))
\r
100 sq = ((SequenceI) sequences.elementAt(i)).getSequence();
\r
102 if (seqNums.size() <= i)
\r
104 seqNums.addElement(new int[sq.length() + 1]);
\r
107 if (sq.hashCode() != ((int[]) seqNums.elementAt(i))[0])
\r
111 seqNumsChanged = true;
\r
112 sq = ((SequenceI) sequences.elementAt(i)).getSequence();
\r
115 if (maxLength < len)
\r
120 sqnum = new int[len + 1]; // better to always make a new array - sequence can change its length
\r
121 sqnum[0] = sq.hashCode();
\r
123 for (j = 1; j <= len; j++)
\r
125 sqnum[j] = ((Integer) jalview.schemes.ResidueProperties.aaHash.get(new String(
\r
126 sq.substring(j - 1, j)))).intValue(); // yuk
\r
129 seqNums.setElementAt(sqnum, i);
\r
134 // JBPNote INFO level debug
\r
135 System.err.println(
\r
136 "ERROR: calcSeqNum called with out of range sequence index for Alignment\n");
\r
141 * Calculates the conservation values for given set of sequences
\r
143 public void calculate()
\r
145 for (int i = start; i <= end; i++)
\r
147 Hashtable resultHash = null;
\r
148 Hashtable residueHash = null;
\r
150 resultHash = new Hashtable();
\r
151 residueHash = new Hashtable();
\r
153 for (int j = 0; j < sequences.size(); j++)
\r
155 // JBPNote - have to make sure elements of the sequences vector
\r
156 // are tested like this everywhere...
\r
157 if (sequences.elementAt(j) instanceof Sequence)
\r
159 Sequence s = (Sequence) sequences.elementAt(j);
\r
161 if (s.getLength() > i)
\r
163 String res = s.getSequence().substring(i, i + 1);
\r
165 if (residueHash.containsKey(res))
\r
167 int count = ((Integer) residueHash.get(res)).intValue();
\r
169 residueHash.put(res, new Integer(count));
\r
173 residueHash.put(res, new Integer(1));
\r
178 if (residueHash.containsKey("-"))
\r
180 int count = ((Integer) residueHash.get("-")).intValue();
\r
182 residueHash.put("-", new Integer(count));
\r
186 residueHash.put("-", new Integer(1));
\r
192 //What is the count threshold to count the residues in residueHash()
\r
193 int thresh = (threshold * (sequences.size())) / 100;
\r
195 //loop over all the found residues
\r
196 Enumeration e = residueHash.keys();
\r
198 while (e.hasMoreElements())
\r
200 String res = (String) e.nextElement();
\r
202 if (((Integer) residueHash.get(res)).intValue() > thresh)
\r
204 //Now loop over the properties
\r
205 Enumeration e2 = propHash.keys();
\r
207 while (e2.hasMoreElements())
\r
209 String type = (String) e2.nextElement();
\r
210 Hashtable ht = (Hashtable) propHash.get(type);
\r
212 //Have we ticked this before?
\r
213 if (!resultHash.containsKey(type))
\r
215 if (ht.containsKey(res))
\r
217 resultHash.put(type, ht.get(res));
\r
221 resultHash.put(type, ht.get("-"));
\r
224 else if (((Integer) resultHash.get(type)).equals(
\r
225 (Integer) ht.get(res)) == false)
\r
227 resultHash.put(type, new Integer(-1));
\r
233 total.addElement(resultHash);
\r
240 * returns gap count in int[0], and conserved residue count in int[1]
\r
242 public int[] countConsNGaps(int j)
\r
247 int[] r = new int[2];
\r
250 for (int i = 0; i < sequences.size(); i++)
\r
252 if (j >= ((Sequence) sequences.elementAt(i)).getSequence().length())
\r
259 char c = ((Sequence) sequences.elementAt(i)).getSequence().charAt(j);
\r
261 if (jalview.util.Comparison.isGap((c)))
\r
281 r[0] = (nres == cons) ? 1 : 0;
\r
288 * Calculates the conservation sequence
\r
290 * @param consflag if true, poitiveve conservation; false calculates negative conservation
\r
291 * @param percentageGaps commonly used value is 25
\r
293 public void verdict(boolean consflag, float percentageGaps)
\r
295 String consString = "";
\r
297 for (int i = start; i <= end; i++)
\r
299 int[] gapcons = countConsNGaps(i);
\r
300 int totGaps = gapcons[1];
\r
301 float pgaps = ((float) totGaps * 100) / (float) sequences.size();
\r
303 // System.out.println("percentage gaps = "+pgaps+"\n");
\r
304 if (percentageGaps > pgaps)
\r
306 Hashtable resultHash = (Hashtable) total.elementAt(i - start);
\r
308 //Now find the verdict
\r
310 Enumeration e3 = resultHash.keys();
\r
312 while (e3.hasMoreElements())
\r
314 String type = (String) e3.nextElement();
\r
315 Integer result = (Integer) resultHash.get(type);
\r
317 //Do we want to count +ve conservation or +ve and -ve cons.?
\r
320 if (result.intValue() == 1)
\r
327 if (result.intValue() != -1)
\r
336 consString = consString + String.valueOf(count); // Conserved props!=Identity
\r
340 consString = consString + ((gapcons[0] == 1) ? "*" : "+");
\r
345 consString = consString + "-";
\r
349 consSequence = new Sequence(name, consString, start, end);
\r
355 * @return Conservation sequence
\r
357 public Sequence getConsSequence()
\r
359 return consSequence;
\r
362 // From Alignment.java in jalview118
\r
363 public void findQuality()
\r
365 findQuality(0, maxLength - 1);
\r
371 private void percentIdentity2()
\r
373 calcSeqNums(); // updates maxLength, too.
\r
375 if ((cons2 == null) || seqNumsChanged)
\r
377 cons2 = new int[maxLength][24];
\r
379 // Initialize the array
\r
380 for (int j = 0; j < 24; j++)
\r
382 for (int i = 0; i < maxLength; i++)
\r
391 while (j < sequences.size())
\r
393 sqnum = (int[]) seqNums.elementAt(j);
\r
395 for (int i = 1; i < sqnum.length; i++)
\r
397 cons2[i - 1][sqnum[i]]++;
\r
400 for (int i = sqnum.length - 1; i < maxLength; i++)
\r
402 cons2[i][23]++; // gap count
\r
410 /* for (int i=start; i <= end; i++) {
\r
415 for (int j=0;j<24;j++) {
\r
416 if (cons2[i][j] > max) {
\r
428 * Calculates the quality of the set of sequences
\r
430 * @param start Start residue
\r
431 * @param end End residue
\r
433 public void findQuality(int start, int end)
\r
435 quality = new Vector();
\r
437 double max = -10000;
\r
439 int[][] BLOSUM62 = jalview.schemes.ResidueProperties.getBLOSUM62();
\r
441 //Loop over columns // JBPNote Profiling info
\r
442 // long ts = System.currentTimeMillis();
\r
443 //long te = System.currentTimeMillis();
\r
444 percentIdentity2();
\r
446 int size = seqNums.size();
\r
447 int[] lengths = new int[size];
\r
449 for (int l = 0; l < size; l++)
\r
450 lengths[l] = ((int[]) seqNums.elementAt(l)).length - 1;
\r
452 for (int j = start; j <= end; j++)
\r
456 // First Xr = depends on column only
\r
457 double[] x = new double[24];
\r
459 for (int ii = 0; ii < 24; ii++)
\r
465 for (int i2 = 0; i2 < 24; i2++)
\r
467 x[ii] += (((double) cons2[j][i2] * BLOSUM62[ii][i2]) +
\r
471 catch (Exception e)
\r
473 System.err.println("Exception during quality calculation.");
\r
474 e.printStackTrace();
\r
477 //System.out.println("X " + ii + " " + x[ii]);
\r
480 //System.out.println("X " + ii + " " + x[ii]);
\r
483 // Now calculate D for each position and sum
\r
484 for (int k = 0; k < size; k++)
\r
487 double[] xx = new double[24];
\r
488 int seqNum = (j < lengths[k])
\r
489 ? ((int[]) seqNums.elementAt(k))[j + 1] : 23; // Sequence, or gap at the end
\r
491 // This is a loop over r
\r
492 for (int i = 0; i < 23; i++)
\r
498 sr = (double) BLOSUM62[i][seqNum] + 4;
\r
500 catch (Exception e)
\r
502 System.out.println("Exception in sr: " + e);
\r
503 e.printStackTrace();
\r
506 //Calculate X with another loop over residues
\r
507 // System.out.println("Xi " + i + " " + x[i] + " " + sr);
\r
510 tot += (xx[i] * xx[i]);
\r
513 bigtot += Math.sqrt(tot);
\r
516 // This is the quality for one column
\r
522 // bigtot = bigtot * (size-cons2[j][23])/size;
\r
523 quality.addElement(new Double(bigtot));
\r
527 // Need to normalize by gaps
\r
530 double newmax = -10000;
\r
532 for (int j = start; j <= end; j++)
\r
534 double tmp = ((Double) quality.elementAt(j)).doubleValue();
\r
535 tmp = ((max - tmp) * (size - cons2[j][23])) / size;
\r
537 // System.out.println(tmp+ " " + j);
\r
538 quality.setElementAt(new Double(tmp), j);
\r
546 // System.out.println("Quality " + s);
\r
547 qualityRange[0] = new Double(0);
\r
548 qualityRange[1] = new Double(newmax);
\r