2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
23 import jalview.datamodel.*;
26 * Calculates conservation values for a given set of sequences
31 public class Conservation
33 SequenceI[] sequences;
36 Vector seqNums; // vector of int vectors where first is sequence checksum
37 int maxLength = 0; // used by quality calcs
38 boolean seqNumsChanged = false; // updated after any change via calcSeqNum;
41 /** Stores calculated quality values */
42 public Vector quality;
44 /** Stores maximum and minimum values of quality values */
45 public Double[] qualityRange = new Double[2];
46 String consString = "";
47 Sequence consSequence;
54 * Creates a new Conservation object.
56 * @param name Name of conservation
57 * @param propHash DOCUMENT ME!
58 * @param threshold to count the residues in residueHash(). commonly used value is 3
59 * @param sequences sequences to be used in calculation
60 * @param start start residue position
61 * @param end end residue position
63 public Conservation(String name, Hashtable propHash, int threshold,
64 Vector sequences, int start, int end)
68 this.propHash = propHash;
69 this.threshold = threshold;
73 maxLength = end - start + 1; // default width includes bounds of calculation
75 int s, sSize = sequences.size();
76 SequenceI[] sarray = new SequenceI[sSize];
77 this.sequences = sarray;
79 for (s = 0; s < sSize; s++)
81 sarray[s] = (SequenceI) sequences.elementAt(s);
82 if (sarray[s].getLength() > maxLength)
84 maxLength = sarray[s].getLength();
92 * @param i DOCUMENT ME!
94 private void calcSeqNum(int i)
96 String sq = null; // for dumb jbuilder not-inited exception warning
99 int sSize = sequences.length;
101 if ( (i > -1) && (i < sSize))
103 sq = sequences[i].getSequenceAsString();
105 if (seqNums.size() <= i)
107 seqNums.addElement(new int[sq.length() + 1]);
110 if (sq.hashCode() != ( (int[]) seqNums.elementAt(i))[0])
114 seqNumsChanged = true;
122 sqnum = new int[len + 1]; // better to always make a new array - sequence can change its length
123 sqnum[0] = sq.hashCode();
125 for (j = 1; j <= len; j++)
127 sqnum[j] = jalview.schemes.ResidueProperties.aaIndex[sq.charAt(j - 1)];
130 seqNums.setElementAt(sqnum, i);
134 System.out.println("SEQUENCE HAS BEEN DELETED!!!");
139 // JBPNote INFO level debug
141 "ERROR: calcSeqNum called with out of range sequence index for Alignment\n");
146 * Calculates the conservation values for given set of sequences
148 public void calculate()
150 Hashtable resultHash, ht;
151 int thresh, j, jSize = sequences.length;
152 int[] values; // Replaces residueHash
153 String type, res = null;
155 Enumeration enumeration2;
157 total = new Hashtable[maxLength];
159 for (int i = start; i <= end; i++)
161 values = new int[255];
163 for (j = 0; j < jSize; j++)
165 if (sequences[j].getLength() > i)
167 c = sequences[j].getCharAt(i);
169 // No need to check if its a '-'
170 if (c == '.' || c == ' ')
175 if ('a' <= c && c <= 'z')
177 c -= (32); // 32 = 'a' - 'A'
188 //What is the count threshold to count the residues in residueHash()
189 thresh = (threshold * (jSize)) / 100;
191 //loop over all the found residues
192 resultHash = new Hashtable();
193 for (char v = '-'; v < 'Z'; v++)
196 if (values[v] > thresh)
198 res = String.valueOf(v);
200 //Now loop over the properties
201 enumeration2 = propHash.keys();
203 while (enumeration2.hasMoreElements())
205 type = (String) enumeration2.nextElement();
206 ht = (Hashtable) propHash.get(type);
208 //Have we ticked this before?
209 if (!resultHash.containsKey(type))
211 if (ht.containsKey(res))
213 resultHash.put(type, ht.get(res));
217 resultHash.put(type, ht.get("-"));
220 else if ( ( (Integer) resultHash.get(type)).equals(
221 (Integer) ht.get(res)) == false)
223 resultHash.put(type, new Integer( -1));
229 total[i - start] = resultHash;
235 * returns gap count in int[0], and conserved residue count in int[1]
237 public int[] countConsNGaps(int j)
242 int[] r = new int[2];
244 int i, iSize = sequences.length;
247 for (i = 0; i < iSize; i++)
249 if (j >= sequences[i].getLength())
255 c = sequences[i].getCharAt(j); // gaps do not have upper/lower case
257 if (jalview.util.Comparison.isGap( (c)))
277 r[0] = (nres == cons) ? 1 : 0;
284 * Calculates the conservation sequence
286 * @param consflag if true, poitiveve conservation; false calculates negative conservation
287 * @param percentageGaps commonly used value is 25
289 public void verdict(boolean consflag, float percentageGaps)
291 StringBuffer consString = new StringBuffer();
297 Hashtable resultHash;
298 Enumeration enumeration;
300 //NOTE THIS SHOULD CHECK IF THE CONSEQUENCE ALREADY
301 //EXISTS AND NOT OVERWRITE WITH '-', BUT THIS CASE
302 //DOES NOT EXIST IN JALVIEW 2.1.2
303 for (int i = 0; i < start; i++)
305 consString.append('-');
308 for (int i = start; i <= end; i++)
310 gapcons = countConsNGaps(i);
311 totGaps = gapcons[1];
312 pgaps = ( (float) totGaps * 100) / (float) sequences.length;
314 if (percentageGaps > pgaps)
316 resultHash = total[i - start];
318 //Now find the verdict
320 enumeration = resultHash.keys();
322 while (enumeration.hasMoreElements())
324 type = (String) enumeration.nextElement();
325 result = (Integer) resultHash.get(type);
327 //Do we want to count +ve conservation or +ve and -ve cons.?
330 if (result.intValue() == 1)
337 if (result.intValue() != -1)
346 consString.append(count); // Conserved props!=Identity
350 consString.append( (gapcons[0] == 1) ? "*" : "+");
355 consString.append('-');
359 consSequence = new Sequence(name, consString.toString(), start, end);
365 * @return Conservation sequence
367 public Sequence getConsSequence()
372 // From Alignment.java in jalview118
373 public void findQuality()
375 findQuality(0, maxLength - 1);
381 private void percentIdentity2()
383 seqNums = new Vector();
385 int i = 0, iSize = sequences.length;
386 //Do we need to calculate this again?
387 for (i = 0; i < iSize; i++)
392 if ( (cons2 == null) || seqNumsChanged)
394 cons2 = new int[maxLength][24];
396 // Initialize the array
397 for (int j = 0; j < 24; j++)
399 for (i = 0; i < maxLength; i++)
408 while (j < sequences.length)
410 sqnum = (int[]) seqNums.elementAt(j);
412 for (i = 1; i < sqnum.length; i++)
414 cons2[i - 1][sqnum[i]]++;
417 for (i = sqnum.length - 1; i < maxLength; i++)
419 cons2[i][23]++; // gap count
427 /* for (int i=start; i <= end; i++) {
432 for (int j=0;j<24;j++) {
433 if (cons2[i][j] > max) {
445 * Calculates the quality of the set of sequences
447 * @param start Start residue
448 * @param end End residue
450 public void findQuality(int start, int end)
452 quality = new Vector();
455 int[][] BLOSUM62 = jalview.schemes.ResidueProperties.getBLOSUM62();
457 //Loop over columns // JBPNote Profiling info
458 //long ts = System.currentTimeMillis();
459 //long te = System.currentTimeMillis();
462 int size = seqNums.size();
463 int[] lengths = new int[size];
464 double tot, bigtot, sr, tmp;
466 int l, j, i, ii, i2, k, seqNum;
468 for (l = 0; l < size; l++)
470 lengths[l] = ( (int[]) seqNums.elementAt(l)).length - 1;
473 for (j = start; j <= end; j++)
477 // First Xr = depends on column only
480 for (ii = 0; ii < 24; ii++)
484 for (i2 = 0; i2 < 24; i2++)
486 x[ii] += ( ( (double) cons2[j][i2] * BLOSUM62[ii][i2]) +
493 // Now calculate D for each position and sum
494 for (k = 0; k < size; k++)
498 seqNum = (j < lengths[k])
499 ? ( (int[]) seqNums.elementAt(k))[j + 1] : 23; // Sequence, or gap at the end
501 // This is a loop over r
502 for (i = 0; i < 23; i++)
506 sr = (double) BLOSUM62[i][seqNum] + 4;
508 //Calculate X with another loop over residues
509 // System.out.println("Xi " + i + " " + x[i] + " " + sr);
512 tot += (xx[i] * xx[i]);
515 bigtot += Math.sqrt(tot);
518 // This is the quality for one column
524 // bigtot = bigtot * (size-cons2[j][23])/size;
525 quality.addElement(new Double(bigtot));
527 // Need to normalize by gaps
530 double newmax = -10000;
532 for (j = start; j <= end; j++)
534 tmp = ( (Double) quality.elementAt(j)).doubleValue();
535 tmp = ( (max - tmp) * (size - cons2[j][23])) / size;
537 // System.out.println(tmp+ " " + j);
538 quality.setElementAt(new Double(tmp), j);
546 // System.out.println("Quality " + s);
547 qualityRange[0] = new Double(0);
548 qualityRange[1] = new Double(newmax);