JAL-1807 includes ?j2sdebug flag and DebugJS._(msg)
[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", "JU.DebugJS"], 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 consString.append ("" + count);\r
206 } else {\r
207 consString.append ((gapcons[0] == 1) ? "*" : "+");\r
208 }} else {\r
209 consString.append ('-');\r
210 }}\r
211 this.consSequence =  new jalview.datamodel.Sequence (this.name, consString.toString (), this.start, this.end);\r
212 }, "~B,~N");\r
213 Clazz.defineMethod (c$, "getConsSequence", \r
214 function () {\r
215 return this.consSequence;\r
216 });\r
217 Clazz.defineMethod (c$, "findQuality", \r
218 function () {\r
219 this.findQuality (0, this.maxLength - 1);\r
220 });\r
221 Clazz.defineMethod (c$, "percentIdentity2", \r
222  function () {\r
223 this.seqNums =  new java.util.Vector ();\r
224 var i = 0;\r
225 var iSize = this.sequences.length;\r
226 for (i = 0; i < iSize; i++) {\r
227 this.calcSeqNum (i);\r
228 }\r
229 if ((this.cons2 == null) || this.seqNumsChanged) {\r
230 this.cons2 =  Clazz.newIntArray (this.maxLength, 24, 0);\r
231 for (var j = 0; j < 24; j++) {\r
232 for (i = 0; i < this.maxLength; i++) {\r
233 this.cons2[i][j] = 0;\r
234 }\r
235 }\r
236 var sqnum;\r
237 var j = 0;\r
238 while (j < this.sequences.length) {\r
239 sqnum = this.seqNums.elementAt (j);\r
240 for (i = 1; i < sqnum.length; i++) {\r
241 this.cons2[i - 1][sqnum[i]]++;\r
242 }\r
243 for (i = sqnum.length - 1; i < this.maxLength; i++) {\r
244 this.cons2[i][23]++;\r
245 }\r
246 j++;\r
247 }\r
248 }});\r
249 Clazz.defineMethod (c$, "findQuality", \r
250 function (start, end) {\r
251 this.quality =  new java.util.Vector ();\r
252 var max = -10000;\r
253 var BLOSUM62 = jalview.schemes.ResidueProperties.getBLOSUM62 ();\r
254 this.percentIdentity2 ();\r
255 var size = this.seqNums.size ();\r
256 var lengths =  Clazz.newIntArray (size, 0);\r
257 var tot;\r
258 var bigtot;\r
259 var sr;\r
260 var tmp;\r
261 var x;\r
262 var xx;\r
263 var l;\r
264 var j;\r
265 var i;\r
266 var ii;\r
267 var i2;\r
268 var k;\r
269 var seqNum;\r
270 for (l = 0; l < size; l++) {\r
271 lengths[l] = (this.seqNums.elementAt (l)).length - 1;\r
272 }\r
273 for (j = start; j <= end; j++) {\r
274 bigtot = 0;\r
275 x =  Clazz.newDoubleArray (24, 0);\r
276 for (ii = 0; ii < 24; ii++) {\r
277 x[ii] = 0;\r
278 for (i2 = 0; i2 < 24; i2++) {\r
279 x[ii] += ((this.cons2[j][i2] * BLOSUM62[ii][i2]) + 4);\r
280 }\r
281 x[ii] /= size;\r
282 }\r
283 for (k = 0; k < size; k++) {\r
284 tot = 0;\r
285 xx =  Clazz.newDoubleArray (24, 0);\r
286 seqNum = (j < lengths[k]) ? (this.seqNums.elementAt (k))[j + 1] : 23;\r
287 for (i = 0; i < 23; i++) {\r
288 sr = 0;\r
289 sr = BLOSUM62[i][seqNum] + 4;\r
290 xx[i] = x[i] - sr;\r
291 tot += (xx[i] * xx[i]);\r
292 }\r
293 bigtot += Math.sqrt (tot);\r
294 }\r
295 if (max < bigtot) {\r
296 max = bigtot;\r
297 }this.quality.addElement ( new Double (bigtot));\r
298 }\r
299 var newmax = -10000;\r
300 for (j = start; j <= end; j++) {\r
301 tmp = (this.quality.elementAt (j)).doubleValue ();\r
302 tmp = ((max - tmp) * (size - this.cons2[j][23])) / size;\r
303 this.quality.setElementAt ( new Double (tmp), j);\r
304 if (tmp > newmax) {\r
305 newmax = tmp;\r
306 }}\r
307 this.qualityRange[0] =  new Double (0);\r
308 this.qualityRange[1] =  new Double (newmax);\r
309 }, "~N,~N");\r
310 Clazz.defineMethod (c$, "completeAnnotations", \r
311 function (conservation, quality2, istart, alWidth) {\r
312 var sequence = this.getConsSequence ().getSequence ();\r
313 var minR;\r
314 var minG;\r
315 var minB;\r
316 var maxR;\r
317 var maxG;\r
318 var maxB;\r
319 minR = 0.3;\r
320 minG = 0.0;\r
321 minB = 0;\r
322 maxR = 1.0 - minR;\r
323 maxG = 0.9 - minG;\r
324 maxB = 0 - minB;\r
325 var min = 0;\r
326 var max = 11;\r
327 var qmin = 0;\r
328 var qmax = 0;\r
329 var c;\r
330 if (conservation.annotations != null && conservation.annotations.length < alWidth) {\r
331 conservation.annotations =  new Array (alWidth);\r
332 }if (quality2 != null) {\r
333 quality2.graphMax = this.qualityRange[1].floatValue ();\r
334 if (quality2.annotations != null && quality2.annotations.length < alWidth) {\r
335 quality2.annotations =  new Array (alWidth);\r
336 }qmin = this.qualityRange[0].floatValue ();\r
337 qmax = this.qualityRange[1].floatValue ();\r
338 }for (var i = 0; i < alWidth; i++) {\r
339 var value = 0;\r
340 c = sequence[i];\r
341 if (Character.isDigit (c)) {\r
342 value = c.charCodeAt (0) - 48;\r
343 } else if (c == '*') {\r
344 value = 11;\r
345 } else if (c == '+') {\r
346 value = 10;\r
347 }var vprop = value - min;\r
348 vprop /= max;\r
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)));\r
350 if (quality2 != null) {\r
351 value = (this.quality.elementAt (i)).floatValue ();\r
352 vprop = value - qmin;\r
353 vprop /= qmax;\r
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)));\r
355 }}\r
356 JU.DebugJS._ ("Conservation");\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