JAL-1807 - Bob's last(?) before leaving Dundee -- adds fast file loading
[jalviewjs.git] / site / j2s / jalview / analysis / Conservation.js
1 Clazz.declarePackage ("jalview.analysis");
2 Clazz.load (null, "jalview.analysis.Conservation", ["jalview.datamodel.Annotation", "$.Sequence", "jalview.schemes.ResidueProperties", "jalview.util.Comparison", "java.awt.Color", "java.lang.Character", "$.Double", "$.StringBuffer", "java.util.Hashtable", "$.Vector"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.sequences = null;
5 this.start = 0;
6 this.end = 0;
7 this.seqNums = null;
8 this.maxLength = 0;
9 this.seqNumsChanged = false;
10 this.total = null;
11 this.canonicaliseAa = true;
12 this.quality = null;
13 this.qualityRange = null;
14 this.consString = "";
15 this.consSequence = null;
16 this.propHash = null;
17 this.threshold = 0;
18 this.name = "";
19 this.cons2 = null;
20 this.consSymbs = null;
21 Clazz.instantialize (this, arguments);
22 }, jalview.analysis, "Conservation");
23 Clazz.prepareFields (c$, function () {
24 this.qualityRange =  new Array (2);
25 });
26 Clazz.makeConstructor (c$, 
27 function (name, propHash, threshold, sequences, start, end) {
28 this.name = name;
29 this.propHash = propHash;
30 this.threshold = threshold;
31 this.start = start;
32 this.end = end;
33 this.maxLength = end - start + 1;
34 var s;
35 var sSize = sequences.size ();
36 var sarray =  new Array (sSize);
37 this.sequences = sarray;
38 try {
39 for (s = 0; s < sSize; s++) {
40 sarray[s] = sequences.get (s);
41 if (sarray[s].getLength () > this.maxLength) {
42 this.maxLength = sarray[s].getLength ();
43 }}
44 } catch (ex) {
45 if (Clazz.exceptionOf (ex, ArrayIndexOutOfBoundsException)) {
46 this.sequences =  new Array (0);
47 this.maxLength = 0;
48 } else {
49 throw ex;
50 }
51 }
52 }, "~S,java.util.Hashtable,~N,java.util.List,~N,~N");
53 Clazz.defineMethod (c$, "calcSeqNum", 
54  function (i) {
55 var sq = null;
56 var sqnum = null;
57 var sSize = this.sequences.length;
58 if ((i > -1) && (i < sSize)) {
59 sq = this.sequences[i].getSequenceAsString ();
60 if (this.seqNums.size () <= i) {
61 this.seqNums.addElement ( Clazz.newIntArray (sq.length + 1, 0));
62 }if (sq.hashCode () != (this.seqNums.elementAt (i))[0]) {
63 var j;
64 var len;
65 this.seqNumsChanged = true;
66 len = sq.length;
67 if (this.maxLength < len) {
68 this.maxLength = len;
69 }sqnum =  Clazz.newIntArray (len + 1, 0);
70 sqnum[0] = sq.hashCode ();
71 for (j = 1; j <= len; j++) {
72 sqnum[j] = jalview.schemes.ResidueProperties.aaIndex[sq.charCodeAt (j - 1)];
73 }
74 this.seqNums.setElementAt (sqnum, i);
75 } else {
76 System.out.println ("SEQUENCE HAS BEEN DELETED!!!");
77 }} else {
78 System.err.println ("ERROR: calcSeqNum called with out of range sequence index for Alignment\n");
79 }}, "~N");
80 Clazz.defineMethod (c$, "calculate", 
81 function () {
82 var resultHash;
83 var ht;
84 var thresh;
85 var j;
86 var jSize = this.sequences.length;
87 var values;
88 var type;
89 var res = null;
90 var c;
91 var enumeration2;
92 this.total =  new Array (this.maxLength);
93 for (var i = this.start; i <= this.end; i++) {
94 values =  Clazz.newIntArray (255, 0);
95 for (j = 0; j < jSize; j++) {
96 if (this.sequences[j].getLength () > i) {
97 c = this.sequences[j].getCharAt (i);
98 if (this.canonicaliseAa) {
99 c = String.fromCharCode (jalview.schemes.ResidueProperties.aaIndex[this.sequences[j].getCharAt (i).charCodeAt (0)]);
100 if (c.charCodeAt (0) > 20) {
101 c = '-';
102 } else {
103 c = jalview.schemes.ResidueProperties.aa[c.charCodeAt (0)].charAt (0);
104 }} else {
105 if (c == '.' || c == ' ') {
106 c = '-';
107 }if (!this.canonicaliseAa && 'a' <= c && c <= 'z') {
108 c = String.fromCharCode (c.charCodeAt (0) - (32));
109 }}values[c.charCodeAt (0)]++;
110 } else {
111 values['-'.charCodeAt (0)]++;
112 }}
113 thresh = Clazz.doubleToInt ((this.threshold * (jSize)) / 100);
114 resultHash =  new java.util.Hashtable ();
115 for (var v = '-'; v < 'Z'; v = String.fromCharCode (v.charCodeAt (0) + 1)) {
116 if (values[v.charCodeAt (0)] > thresh) {
117 res = String.valueOf (v);
118 enumeration2 = this.propHash.keys ();
119 while (enumeration2.hasMoreElements ()) {
120 type = enumeration2.nextElement ();
121 ht = this.propHash.get (type);
122 if (!resultHash.containsKey (type)) {
123 if (ht.containsKey (res)) {
124 resultHash.put (type, ht.get (res));
125 } else {
126 resultHash.put (type, ht.get ("-"));
127 }} else if ((resultHash.get (type)).equals (ht.get (res)) == false) {
128 resultHash.put (type,  new Integer (-1));
129 }}
130 }}
131 if (this.total.length > 0) {
132 this.total[i - this.start] = resultHash;
133 }}
134 });
135 Clazz.defineMethod (c$, "countConsNGaps", 
136 function (j) {
137 var count = 0;
138 var cons = 0;
139 var nres = 0;
140 var r =  Clazz.newIntArray (2, 0);
141 var f = '$';
142 var i;
143 var iSize = this.sequences.length;
144 var c;
145 for (i = 0; i < iSize; i++) {
146 if (j >= this.sequences[i].getLength ()) {
147 count++;
148 continue;
149 }c = this.sequences[i].getCharAt (j);
150 if (jalview.util.Comparison.isGap ((c))) {
151 count++;
152 } else {
153 nres++;
154 if (nres == 1) {
155 f = c;
156 cons++;
157 } else if (f == c) {
158 cons++;
159 }}}
160 r[0] = (nres == cons) ? 1 : 0;
161 r[1] = count;
162 return r;
163 }, "~N");
164 Clazz.defineMethod (c$, "verdict", 
165 function (consflag, percentageGaps) {
166 var consString =  new StringBuffer ();
167 var type;
168 var result;
169 var gapcons;
170 var totGaps;
171 var count;
172 var pgaps;
173 var resultHash;
174 var enumeration;
175 for (var i = 0; i < this.start; i++) {
176 consString.append ('-');
177 }
178 this.consSymbs =  new Array (this.end - this.start + 1);
179 for (var i = this.start; i <= this.end; i++) {
180 gapcons = this.countConsNGaps (i);
181 totGaps = gapcons[1];
182 pgaps = (totGaps * 100) / this.sequences.length;
183 this.consSymbs[i - this.start] =  String.instantialize ();
184 if (percentageGaps > pgaps) {
185 resultHash = this.total[i - this.start];
186 count = 0;
187 enumeration = resultHash.keys ();
188 while (enumeration.hasMoreElements ()) {
189 type = enumeration.nextElement ();
190 result = resultHash.get (type);
191 if (consflag) {
192 if (result.intValue () == 1) {
193 this.consSymbs[i - this.start] = type + " " + this.consSymbs[i - this.start];
194 count++;
195 }} else {
196 if (result.intValue () != -1) {
197 {
198 if (result.intValue () == 0) {
199 this.consSymbs[i - this.start] = this.consSymbs[i - this.start] + " !" + type;
200 } else {
201 this.consSymbs[i - this.start] = type + " " + this.consSymbs[i - this.start];
202 }}count++;
203 }}}
204 if (count < 10) {
205 consString.append ("" + count);
206 } else {
207 consString.append ((gapcons[0] == 1) ? "*" : "+");
208 }} else {
209 consString.append ('-');
210 }}
211 this.consSequence =  new jalview.datamodel.Sequence (this.name, consString.toString (), this.start, this.end);
212 }, "~B,~N");
213 Clazz.defineMethod (c$, "getConsSequence", 
214 function () {
215 return this.consSequence;
216 });
217 Clazz.defineMethod (c$, "findQuality", 
218 function () {
219 this.findQuality (0, this.maxLength - 1);
220 });
221 Clazz.defineMethod (c$, "percentIdentity2", 
222  function () {
223 this.seqNums =  new java.util.Vector ();
224 var i = 0;
225 var iSize = this.sequences.length;
226 for (i = 0; i < iSize; i++) {
227 this.calcSeqNum (i);
228 }
229 if ((this.cons2 == null) || this.seqNumsChanged) {
230 this.cons2 =  Clazz.newIntArray (this.maxLength, 24, 0);
231 for (var j = 0; j < 24; j++) {
232 for (i = 0; i < this.maxLength; i++) {
233 this.cons2[i][j] = 0;
234 }
235 }
236 var sqnum;
237 var j = 0;
238 while (j < this.sequences.length) {
239 sqnum = this.seqNums.elementAt (j);
240 for (i = 1; i < sqnum.length; i++) {
241 this.cons2[i - 1][sqnum[i]]++;
242 }
243 for (i = sqnum.length - 1; i < this.maxLength; i++) {
244 this.cons2[i][23]++;
245 }
246 j++;
247 }
248 }});
249 Clazz.defineMethod (c$, "findQuality", 
250 function (start, end) {
251 this.quality =  new java.util.Vector ();
252 var max = -10000;
253 var BLOSUM62 = jalview.schemes.ResidueProperties.getBLOSUM62 ();
254 this.percentIdentity2 ();
255 var size = this.seqNums.size ();
256 var lengths =  Clazz.newIntArray (size, 0);
257 var tot;
258 var bigtot;
259 var sr;
260 var tmp;
261 var x;
262 var xx;
263 var l;
264 var j;
265 var i;
266 var ii;
267 var i2;
268 var k;
269 var seqNum;
270 for (l = 0; l < size; l++) {
271 lengths[l] = (this.seqNums.elementAt (l)).length - 1;
272 }
273 for (j = start; j <= end; j++) {
274 bigtot = 0;
275 x =  Clazz.newDoubleArray (24, 0);
276 for (ii = 0; ii < 24; ii++) {
277 x[ii] = 0;
278 for (i2 = 0; i2 < 24; i2++) {
279 x[ii] += ((this.cons2[j][i2] * BLOSUM62[ii][i2]) + 4);
280 }
281 x[ii] /= size;
282 }
283 for (k = 0; k < size; k++) {
284 tot = 0;
285 xx =  Clazz.newDoubleArray (24, 0);
286 seqNum = (j < lengths[k]) ? (this.seqNums.elementAt (k))[j + 1] : 23;
287 for (i = 0; i < 23; i++) {
288 sr = 0;
289 sr = BLOSUM62[i][seqNum] + 4;
290 xx[i] = x[i] - sr;
291 tot += (xx[i] * xx[i]);
292 }
293 bigtot += Math.sqrt (tot);
294 }
295 if (max < bigtot) {
296 max = bigtot;
297 }this.quality.addElement ( new Double (bigtot));
298 }
299 var newmax = -10000;
300 for (j = start; j <= end; j++) {
301 tmp = (this.quality.elementAt (j)).doubleValue ();
302 tmp = ((max - tmp) * (size - this.cons2[j][23])) / size;
303 this.quality.setElementAt ( new Double (tmp), j);
304 if (tmp > newmax) {
305 newmax = tmp;
306 }}
307 this.qualityRange[0] =  new Double (0);
308 this.qualityRange[1] =  new Double (newmax);
309 }, "~N,~N");
310 Clazz.defineMethod (c$, "completeAnnotations", 
311 function (conservation, quality2, istart, alWidth) {
312 var sequence = this.getConsSequence ().getSequence ();
313 var minR;
314 var minG;
315 var minB;
316 var maxR;
317 var maxG;
318 var maxB;
319 minR = 0.3;
320 minG = 0.0;
321 minB = 0;
322 maxR = 1.0 - minR;
323 maxG = 0.9 - minG;
324 maxB = 0 - minB;
325 var min = 0;
326 var max = 11;
327 var qmin = 0;
328 var qmax = 0;
329 var c;
330 if (conservation.annotations != null && conservation.annotations.length < alWidth) {
331 conservation.annotations =  new Array (alWidth);
332 }if (quality2 != null) {
333 quality2.graphMax = this.qualityRange[1].floatValue ();
334 if (quality2.annotations != null && quality2.annotations.length < alWidth) {
335 quality2.annotations =  new Array (alWidth);
336 }qmin = this.qualityRange[0].floatValue ();
337 qmax = this.qualityRange[1].floatValue ();
338 }for (var i = 0; i < alWidth; i++) {
339 var value = 0;
340 c = sequence[i];
341 if (Character.isDigit (c)) {
342 value = c.charCodeAt (0) - 48;
343 } else if (c == '*') {
344 value = 11;
345 } else if (c == '+') {
346 value = 10;
347 }var vprop = value - min;
348 vprop /= max;
349 conservation.annotations[i] =  new jalview.datamodel.Annotation (String.valueOf (c), this.consSymbs[i - this.start], ' ', value,  new java.awt.Color (minR + (maxR * vprop), minG + (maxG * vprop), minB + (maxB * vprop)));
350 if (quality2 != null) {
351 value = (this.quality.elementAt (i)).floatValue ();
352 vprop = value - qmin;
353 vprop /= qmax;
354 quality2.annotations[i] =  new jalview.datamodel.Annotation (" ", String.valueOf (value), ' ', value,  new java.awt.Color (minR + (maxR * vprop), minG + (maxG * vprop), minB + (maxB * vprop)));
355 }}
356 }, "jalview.datamodel.AlignmentAnnotation,jalview.datamodel.AlignmentAnnotation,~N,~N");
357 c$.calculateConservation = Clazz.defineMethod (c$, "calculateConservation", 
358 function (name, consHash, threshold, seqs, start, end, posOrNeg, consPercGaps, calcQuality) {
359 var cons =  new jalview.analysis.Conservation (name, consHash, threshold, seqs, start, end);
360 return jalview.analysis.Conservation.calculateConservation (cons, posOrNeg, consPercGaps, calcQuality);
361 }, "~S,java.util.Hashtable,~N,java.util.List,~N,~N,~B,~N,~B");
362 c$.calculateConservation = Clazz.defineMethod (c$, "calculateConservation", 
363 function (cons, b, consPercGaps, calcQuality) {
364 cons.calculate ();
365 cons.verdict (b, consPercGaps);
366 if (calcQuality) {
367 cons.findQuality ();
368 }return cons;
369 }, "jalview.analysis.Conservation,~B,~N,~B");
370 });