1 /* Jalview - a java multiple alignment editor
\r
2 * Copyright (C) 1998 Michele Clamp
\r
4 * This program is free software; you can redistribute it and/or
\r
5 * modify it under the terms of the GNU General Public License
\r
6 * as published by the Free Software Foundation; either version 2
\r
7 * of the License, or (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program; if not, write to the Free Software
\r
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
18 package jalview.analysis;
\r
22 import jalview.gui.*;
\r
23 import jalview.datamodel.*;
\r
26 public class Conservation {
\r
30 Vector seqNums; // vector of int vectors where first is sequence checksum
\r
31 int maxLength=0; // used by quality calcs
\r
32 boolean seqNumsChanged = false; // updated after any change via calcSeqNum;
\r
33 private void calcSeqNums() {
\r
34 for (int i=0; i<sequences.size(); i++) {
\r
38 private void calcSeqNum(int i) {
\r
39 String sq=null; // for dumb jbuilder not-inited exception warning
\r
41 if (i>-1 && i<sequences.size()) {
\r
42 sq = ((SequenceI)sequences.elementAt(i)).getSequence();
\r
43 if (seqNums.size()<=i) {
\r
44 seqNums.add(new int[sq.length()+1]);
\r
46 if (sq.hashCode()!=((int[])seqNums.elementAt(i))[0])
\r
49 seqNumsChanged = true;
\r
50 sq = ((SequenceI) sequences.elementAt(i)).getSequence();
\r
52 if (maxLength < len)
\r
54 sqnum = new int[len + 1]; // better to always make a new array - sequence can change its length
\r
55 sqnum[0] = sq.hashCode();
\r
56 for (j = 1; j <= len; j++)
\r
58 sqnum[j] = ( (Integer) jalview.schemes.ResidueProperties.aaHash.get(new
\r
59 String(sq.substring(j - 1, j)))).intValue(); // yuk
\r
61 seqNums.set(i, sqnum);
\r
64 // JBPNote INFO level debug
\r
65 System.out.println("calcSeqNum called with out of range sequence index for Alignment\n");
\r
68 Vector total = new Vector();
\r
69 public Vector quality;
\r
70 public Double[] qualityRange = new Double[2];
\r
71 String consString = "";
\r
73 Sequence consSequence;
\r
79 public Conservation(String name,Hashtable propHash, int threshold, Vector sequences, int start, int end) {
\r
81 this.propHash = propHash;
\r
82 this.threshold = threshold;
\r
83 this.sequences = sequences;
\r
86 seqNums = new Vector(sequences.size());
\r
91 public void calculate() {
\r
93 for (int i = start;i <= end; i++) {
\r
94 Hashtable resultHash = null;
\r
95 Hashtable residueHash = null;
\r
97 resultHash = new Hashtable();
\r
98 residueHash = new Hashtable();
\r
100 for (int j=0; j < sequences.size(); j++) {
\r
102 if (sequences.elementAt(j) instanceof Sequence) {
\r
103 Sequence s = (Sequence)sequences.elementAt(j);
\r
105 if (s.getLength() > i) {
\r
106 String res = s.getSequence().substring(i,i+1);
\r
108 if (residueHash.containsKey(res)) {
\r
109 int count = ((Integer)residueHash.get(res)).intValue() ;
\r
111 residueHash.put(res,new Integer(count));
\r
113 residueHash.put(res,new Integer(1));
\r
116 if (residueHash.containsKey("-")) {
\r
117 int count = ((Integer)residueHash.get("-")).intValue() ;
\r
119 residueHash.put("-",new Integer(count));
\r
121 residueHash.put("-",new Integer(1));
\r
127 //What is the count threshold to count the residues in residueHash()
\r
128 int thresh = threshold*(sequences.size())/100;
\r
130 //loop over all the found residues
\r
131 Enumeration e = residueHash.keys();
\r
133 while (e.hasMoreElements()) {
\r
135 String res = (String)e.nextElement();
\r
136 if (((Integer)residueHash.get(res)).intValue() > thresh) {
\r
138 //Now loop over the properties
\r
139 Enumeration e2 = propHash.keys();
\r
141 while (e2.hasMoreElements()) {
\r
142 String type = (String)e2.nextElement();
\r
143 Hashtable ht = (Hashtable)propHash.get(type);
\r
145 //Have we ticked this before?
\r
146 if (! resultHash.containsKey(type)) {
\r
147 if (ht.containsKey(res)) {
\r
148 resultHash.put(type,ht.get(res));
\r
150 resultHash.put(type,ht.get("-"));
\r
152 } else if ( ((Integer)resultHash.get(type)).equals((Integer)ht.get(res)) == false) {
\r
153 resultHash.put(type,new Integer(-1));
\r
158 total.addElement(resultHash);
\r
162 public int countGaps(int j)
\r
166 for (int i = 0; i < sequences.size();i++)
\r
168 if( j+1 > ((Sequence)sequences.elementAt(i)).getSequence().length())
\r
169 { count++; continue;}
\r
171 char c = ((Sequence)sequences.elementAt(i)).getSequence().charAt(j);
\r
172 if (jalview.util.Comparison.isGap((c)))
\r
179 public void verdict(boolean consflag, float percentageGaps) {
\r
180 String consString = "";
\r
182 for (int i=start; i <= end; i++) {
\r
183 int totGaps = countGaps(i);
\r
184 float pgaps = (float)totGaps*100/(float)sequences.size();
\r
186 if (percentageGaps > pgaps)
\r
188 Hashtable resultHash = (Hashtable)total.elementAt(i-start);
\r
190 //Now find the verdict
\r
192 Enumeration e3 = resultHash.keys();
\r
194 while (e3.hasMoreElements())
\r
196 String type = (String)e3.nextElement();
\r
197 Integer result = (Integer)resultHash.get(type);
\r
199 //Do we want to count +ve conservation or +ve and -ve cons.?
\r
203 if (result.intValue() == 1)
\r
208 if (result.intValue() != -1)
\r
214 consString = consString + String.valueOf(count);
\r
216 consString = consString + "*";
\r
221 consString = consString + "-";
\r
225 consSequence = new Sequence(name,consString,start,end);
\r
228 public Sequence getConsSequence() {
\r
229 return consSequence;
\r
232 // From Alignment.java in jalview118
\r
234 public void findQuality() {
\r
235 findQuality(0,maxLength-1);
\r
240 private void percentIdentity2() {
\r
241 calcSeqNums(); // updates maxLength, too.
\r
242 if (cons2==null || seqNumsChanged) {
\r
243 cons2 = new int[maxLength][24];
\r
244 // Initialize the array
\r
245 for (int j=0;j<24;j++) {
\r
246 for (int i=0; i < maxLength;i++) {
\r
253 while(j < sequences.size()) {
\r
254 sqnum=(int[])seqNums.elementAt(j);
\r
255 for (int i = 1; i < sqnum.length; i++) {
\r
256 cons2[i-1][sqnum[i]]++;
\r
258 for (int i=sqnum.length-1; i<maxLength; i++) {
\r
259 cons2[i][23]++; // gap count
\r
265 /* for (int i=start; i <= end; i++) {
\r
270 for (int j=0;j<24;j++) {
\r
271 if (cons2[i][j] > max) {
\r
284 public void findQuality(int start, int end) {
\r
285 quality = new Vector();
\r
286 double max = -10000;
\r
288 int[][] BLOSUM62 = jalview.schemes.ResidueProperties.getBLOSUM62();
\r
289 //Loop over columns // JBPNote Profiling info
\r
290 // long ts = System.currentTimeMillis();
\r
291 //long te = System.currentTimeMillis();
\r
292 percentIdentity2();
\r
294 int size = seqNums.size();
\r
295 int[] lengths = new int[size];
\r
297 for (int l = 0; l < size; l++)
\r
298 lengths[l] = ((int[]) seqNums.get(l)).length-1;
\r
300 for (int j=start; j <= end; j++) {
\r
303 // First Xr = depends on column only
\r
304 double x[] = new double[24];
\r
306 for (int ii=0; ii < 24; ii++) {
\r
309 for (int i2=0; i2 < 24; i2++) {
\r
310 x[ii] += (double)cons2[j][i2] * BLOSUM62[ii][i2]+4;
\r
312 } catch (Exception e) {
\r
313 System.out.println("Exception : " + e);
\r
315 //System.out.println("X " + ii + " " + x[ii]);
\r
317 //System.out.println("X " + ii + " " + x[ii]);
\r
319 // Now calculate D for each position and sum
\r
320 for (int k=0; k < size; k++) {
\r
322 double[] xx = new double[24];
\r
325 ? ((int[]) seqNums.get(k))[j+1]
\r
326 : 23; // Sequence, or gap at the end
\r
328 // This is a loop over r
\r
329 for (int i=0; i < 23; i++) {
\r
332 sr = (double)BLOSUM62[i][seqNum]+4;
\r
333 } catch (Exception e) {
\r
334 System.out.println("Exception in sr " + e);
\r
336 //Calculate X with another loop over residues
\r
338 // System.out.println("Xi " + i + " " + x[i] + " " + sr);
\r
341 tot += xx[i]*xx[i];
\r
343 bigtot += Math.sqrt(tot);
\r
345 // This is the quality for one column
\r
346 if (max < bigtot) {max = bigtot;}
\r
347 // bigtot = bigtot * (size-cons2[j][23])/size;
\r
349 quality.addElement(new Double(bigtot));
\r
353 // Need to normalize by gaps
\r
355 double newmax=-10000;
\r
356 for (int j=start; j <= end; j++) {
\r
357 double tmp = ((Double)quality.elementAt(j)).doubleValue();
\r
358 tmp = (max - tmp)*(size-cons2[j][23])/size;
\r
359 // System.out.println(tmp+ " " + j);
\r
360 quality.setElementAt(new Double(tmp),j);
\r
364 // System.out.println("Quality " + s);
\r
365 qualityRange[0] = new Double(0);
\r
366 qualityRange[1] = new Double(newmax);
\r