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