JAL-1807 test
[jalviewjs.git] / bin / jalview / analysis / StructureFrequency.js
1 Clazz.declarePackage ("jalview.analysis");
2 Clazz.load (null, "jalview.analysis.StructureFrequency", ["jalview.datamodel.Annotation", "jalview.util.Format", "$.QuickSort", "java.lang.Float", "java.util.Hashtable", "javajs.util.AU"], function () {
3 c$ = Clazz.declareType (jalview.analysis, "StructureFrequency");
4 c$.findPair = Clazz.defineMethod (c$, "findPair", 
5 function (pairs, indice) {
6 for (var i = 0; i < pairs.length; i++) {
7 if (pairs[i].getBegin () == indice) {
8 return pairs[i].getEnd ();
9 }}
10 return -1;
11 }, "~A,~N");
12 c$.calculate = Clazz.defineMethod (c$, "calculate", 
13 function (sequences, start, end, result, profile, rnaStruc) {
14 var residueHash;
15 var maxResidue;
16 var struc = rnaStruc.getRNAStruc ().toCharArray ();
17 var rna = rnaStruc._rnasecstr;
18 var c;
19 var s;
20 var cEnd;
21 var count = 0;
22 var nonGap = 0;
23 var i;
24 var bpEnd = -1;
25 var j;
26 var jSize = sequences.length;
27 var values;
28 var pairs;
29 var percentage;
30 var wooble = true;
31 for (i = start; i < end; i++) {
32 residueHash =  new java.util.Hashtable ();
33 maxResidue = "-";
34 values =  Clazz.newIntArray (255, 0);
35 pairs =  Clazz.newIntArray (255, 255, 0);
36 bpEnd = -1;
37 if (i < struc.length) {
38 s = struc[i];
39 } else {
40 s = '-';
41 }if (s == '.' || s == ' ') {
42 s = '-';
43 }if (s != '(' && s != '[') {
44 if (s == '-') {
45 values['-'.charCodeAt (0)]++;
46 }} else {
47 bpEnd = jalview.analysis.StructureFrequency.findPair (rna, i);
48 if (bpEnd > -1) {
49 for (j = 0; j < jSize; j++) {
50 if (sequences[j] == null) {
51 System.err.println ("WARNING: Consensus skipping null sequence - possible race condition.");
52 continue;
53 }c = sequences[j].getCharAt (i);
54 if (c == '.' || c == ' ') {
55 c = '-';
56 }if (c == '-') {
57 values['-'.charCodeAt (0)]++;
58 continue;
59 }cEnd = sequences[j].getCharAt (bpEnd);
60 if (jalview.analysis.StructureFrequency.checkBpType (c, cEnd) == true) {
61 values['('.charCodeAt (0)]++;
62 maxResidue = "(";
63 wooble = true;
64 }if (jalview.analysis.StructureFrequency.checkBpType (c, cEnd) == false) {
65 wooble = false;
66 values['['.charCodeAt (0)]++;
67 maxResidue = "[";
68 }pairs[c.charCodeAt (0)][cEnd.charCodeAt (0)]++;
69 }
70 }}if (profile) {
71 residueHash.put ("P",  Clazz.newArray (-1, [values,  Clazz.newIntArray (-1, [jSize, (jSize - values['-'.charCodeAt (0)])])]));
72 residueHash.put ("B", pairs);
73 }if (wooble == true) {
74 count = values['('.charCodeAt (0)];
75 }if (wooble == false) {
76 count = values['['.charCodeAt (0)];
77 }residueHash.put ("C",  new Integer (count));
78 residueHash.put ("R", maxResidue);
79 percentage = (count * 100) / jSize;
80 residueHash.put ("G",  new Float (percentage));
81 if (result[i] == null) {
82 result[i] = residueHash;
83 }if (bpEnd > 0) {
84 values[')'.charCodeAt (0)] = values['('.charCodeAt (0)];
85 values[']'.charCodeAt (0)] = values['['.charCodeAt (0)];
86 values['('.charCodeAt (0)] = 0;
87 values['['.charCodeAt (0)] = 0;
88 residueHash =  new java.util.Hashtable ();
89 if (wooble == true) {
90 maxResidue = ")";
91 }if (wooble == false) {
92 maxResidue = "]";
93 }if (profile) {
94 residueHash.put ("P",  Clazz.newArray (-1, [values,  Clazz.newIntArray (-1, [jSize, (jSize - values['-'.charCodeAt (0)])])]));
95 residueHash.put ("B", pairs);
96 }residueHash.put ("C",  new Integer (count));
97 residueHash.put ("R", maxResidue);
98 percentage = (count * 100) / jSize;
99 residueHash.put ("G",  new Float (percentage));
100 result[bpEnd] = residueHash;
101 }}
102 }, "~A,~N,~N,~A,~B,jalview.datamodel.AlignmentAnnotation");
103 c$.checkBpType = Clazz.defineMethod (c$, "checkBpType", 
104 function (up, down) {
105 if (up > 'Z') {
106 up = String.fromCharCode (up.charCodeAt (0) - 32);
107 }if (down > 'Z') {
108 down = String.fromCharCode (down.charCodeAt (0) - 32);
109 }switch (up) {
110 case 'A':
111 switch (down) {
112 case 'T':
113 return true;
114 case 'U':
115 return true;
116 }
117 break;
118 case 'C':
119 switch (down) {
120 case 'G':
121 return true;
122 }
123 break;
124 case 'T':
125 switch (down) {
126 case 'A':
127 return true;
128 case 'G':
129 return true;
130 }
131 break;
132 case 'G':
133 switch (down) {
134 case 'C':
135 return true;
136 case 'T':
137 return true;
138 case 'U':
139 return true;
140 }
141 break;
142 case 'U':
143 switch (down) {
144 case 'A':
145 return true;
146 case 'G':
147 return true;
148 }
149 break;
150 }
151 return false;
152 }, "~S,~S");
153 c$.completeConsensus = Clazz.defineMethod (c$, "completeConsensus", 
154 function (consensus, hconsensus, iStart, width, ignoreGapsInConsensusCalculation, includeAllConsSymbols, nseq) {
155 var tval;
156 var value;
157 if (consensus == null || consensus.annotations == null || consensus.annotations.length < width) {
158 return;
159 }var fmtstr = "%3.1f";
160 var precision = 2;
161 while (nseq > 100) {
162 precision++;
163 nseq /= 10;
164 }
165 if (precision > 2) {
166 fmtstr = "%" + (2 + precision) + "." + precision + "f";
167 }var fmt =  new jalview.util.Format (fmtstr);
168 for (var i = iStart; i < width; i++) {
169 var hci;
170 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null)) {
171 consensus.annotations[i] = null;
172 continue;
173 }value = 0;
174 var fv;
175 if (ignoreGapsInConsensusCalculation) {
176 fv = hci.get ("N");
177 } else {
178 fv = hci.get ("G");
179 }if (fv == null) {
180 consensus.annotations[i] = null;
181 continue;
182 }value = fv.floatValue ();
183 var maxRes = hci.get ("R").toString ();
184 var mouseOver = hci.get ("R") + " ";
185 if (maxRes.length > 1) {
186 mouseOver = "[" + maxRes + "] ";
187 maxRes = "+";
188 }var profile = hci.get ("P");
189 var pairs = hci.get ("B");
190 if (pairs != null && includeAllConsSymbols) {
191 mouseOver = "";
192 var ca = javajs.util.AU.newInt2 (625);
193 var vl =  Clazz.newFloatArray (625, 0);
194 var x = 0;
195 for (var c = 65; c < 90; c++) {
196 for (var d = 65; d < 90; d++) {
197 ca[x] =  Clazz.newIntArray (-1, [c, d]);
198 vl[x] = pairs[c][d];
199 x++;
200 }
201 }
202 jalview.util.QuickSort.sortFloatObject (vl, ca);
203 var p = 0;
204 var divisor = profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
205 for (var c = 624; c > 0; c--) {
206 if (vl[c] > 0) {
207 tval = (vl[c] * 100 / divisor);
208 mouseOver += ((p == 0) ? "" : "; ") + String.fromCharCode (ca[c][0]) + String.fromCharCode (ca[c][1]) + " " + fmt.formDouble (tval) + "%";
209 p++;
210 }}
211 } else {
212 mouseOver += (fmt.formDouble (value) + "%");
213 }consensus.annotations[i] =  new jalview.datamodel.Annotation (maxRes, mouseOver, ' ', value);
214 }
215 }, "jalview.datamodel.AlignmentAnnotation,~A,~N,~N,~B,~B,~N");
216 c$.extractProfile = Clazz.defineMethod (c$, "extractProfile", 
217 function (hconsensus, ignoreGapsInConsensusCalculation) {
218 var rtnval =  Clazz.newIntArray (74, 0);
219 var profile = hconsensus.get ("P");
220 var pairs = hconsensus.get ("B");
221 if (profile == null) {
222 return null;
223 }var ca = javajs.util.AU.newInt2 (625);
224 var vl =  Clazz.newFloatArray (625, 0);
225 var x = 0;
226 for (var c = 65; c < 90; c++) {
227 for (var d = 65; d < 90; d++) {
228 ca[x] =  Clazz.newIntArray (-1, [c, d]);
229 vl[x] = pairs[c][d];
230 x++;
231 }
232 }
233 jalview.util.QuickSort.sortFloatObject (vl, ca);
234 var valuesCount = 0;
235 rtnval[1] = 0;
236 var offset = 2;
237 var divisor = profile[1][ignoreGapsInConsensusCalculation ? 1 : 0];
238 for (var c = 624; c > 0; c--) {
239 if (vl[c] > 0) {
240 rtnval[offset++] = ca[c][0];
241 rtnval[offset++] = ca[c][1];
242 rtnval[offset] = Clazz.floatToInt (vl[c] * 100 / divisor);
243 rtnval[1] += rtnval[offset++];
244 valuesCount++;
245 }}
246 rtnval[0] = valuesCount;
247 var result =  Clazz.newIntArray (rtnval.length + 1, 0);
248 result[0] = 1;
249 System.arraycopy (rtnval, 0, result, 1, rtnval.length);
250 return result;
251 }, "java.util.Hashtable,~B");
252 Clazz.defineStatics (c$,
253 "STRUCTURE_PROFILE_LENGTH", 74,
254 "MAXCOUNT", "C",
255 "MAXRESIDUE", "R",
256 "PID_GAPS", "G",
257 "PID_NOGAPS", "N",
258 "PROFILE", "P",
259 "PAIRPROFILE", "B");
260 });