JAL-1807 test
[jalviewjs.git] / bin / jalview / analysis / AAFrequency.js
1 Clazz.declarePackage ("jalview.analysis");
2 Clazz.load (null, "jalview.analysis.AAFrequency", ["jalview.analysis.CodingUtils", "jalview.datamodel.Annotation", "jalview.util.Format", "$.MappingUtils", "$.QuickSort", "java.lang.Float", "$.StringBuilder", "java.util.Arrays", "$.Hashtable"], function () {
3 c$ = Clazz.declareType (jalview.analysis, "AAFrequency");
4 c$.calculate = Clazz.defineMethod (c$, "calculate", 
5 function (list, start, end) {
6 return jalview.analysis.AAFrequency.calculate (list, start, end, false);
7 }, "java.util.List,~N,~N");
8 c$.calculate = Clazz.defineMethod (c$, "calculate", 
9 function (sequences, start, end, profile) {
10 var seqs =  new Array (sequences.size ());
11 var width = 0;
12 {
13 for (var i = 0; i < sequences.size (); i++) {
14 seqs[i] = sequences.get (i);
15 if (seqs[i].getLength () > width) {
16 width = seqs[i].getLength ();
17 }}
18 var reply =  new Array (width);
19 if (end >= width) {
20 end = width;
21 }jalview.analysis.AAFrequency.calculate (seqs, start, end, reply, profile);
22 return reply;
23 }}, "java.util.List,~N,~N,~B");
24 c$.calculate = Clazz.defineMethod (c$, "calculate", 
25 function (sequences, start, end, result, profile) {
26 var residueHash;
27 var maxCount;
28 var nongap;
29 var i;
30 var j;
31 var v;
32 var jSize = sequences.length;
33 var maxResidue;
34 var c = '-';
35 var percentage;
36 var values =  Clazz.newIntArray (255, 0);
37 var seq;
38 for (i = start; i < end; i++) {
39 residueHash =  new java.util.Hashtable ();
40 maxCount = 0;
41 maxResidue = "";
42 nongap = 0;
43 values =  Clazz.newIntArray (255, 0);
44 for (j = 0; j < jSize; j++) {
45 if (sequences[j] == null) {
46 System.err.println ("WARNING: Consensus skipping null sequence - possible race condition.");
47 continue;
48 }seq = sequences[j].getSequence ();
49 if (seq.length > i) {
50 c = seq[i];
51 if (c == '.' || c == ' ') {
52 c = '-';
53 }if (c == '-') {
54 values['-'.charCodeAt (0)]++;
55 continue;
56 } else if ('a' <= c && c <= 'z') {
57 c = String.fromCharCode (c.charCodeAt (0) + -32);
58 }nongap++;
59 values[c.charCodeAt (0)]++;
60 } else {
61 values['-'.charCodeAt (0)]++;
62 }}
63 if (jSize == 1) {
64 maxResidue = String.valueOf (c);
65 maxCount = 1;
66 } else {
67 for (v = 65; v <= 90; v++) {
68 if (values[v] < 1 || values[v] < maxCount) {
69 continue;
70 }if (values[v] > maxCount) {
71 maxResidue = jalview.analysis.AAFrequency.CHARS[v - 65];
72 } else if (values[v] == maxCount) {
73 maxResidue += jalview.analysis.AAFrequency.CHARS[v - 65];
74 }maxCount = values[v];
75 }
76 }if (maxResidue.length == 0) {
77 maxResidue = "-";
78 }if (profile) {
79 residueHash.put ("P",  Clazz.newArray (-1, [values,  Clazz.newIntArray (-1, [jSize, nongap])]));
80 }residueHash.put ("C",  new Integer (maxCount));
81 residueHash.put ("R", maxResidue);
82 percentage = (maxCount * 100) / jSize;
83 residueHash.put ("G",  new Float (percentage));
84 if (nongap > 0) {
85 percentage = (maxCount * 100) / nongap;
86 }residueHash.put ("N",  new Float (percentage));
87 result[i] = residueHash;
88 }
89 }, "~A,~N,~N,~A,~B");
90 c$.completeConsensus = Clazz.defineMethod (c$, "completeConsensus", 
91 function (consensus, hconsensus, iStart, width, ignoreGapsInConsensusCalculation, includeAllConsSymbols, nseq) {
92 jalview.analysis.AAFrequency.completeConsensus (consensus, hconsensus, iStart, width, ignoreGapsInConsensusCalculation, includeAllConsSymbols, null, nseq);
93 }, "jalview.datamodel.AlignmentAnnotation,~A,~N,~N,~B,~B,~N");
94 c$.completeConsensus = Clazz.defineMethod (c$, "completeConsensus", 
95 function (consensus, hconsensus, iStart, width, ignoreGapsInConsensusCalculation, includeAllConsSymbols, alphabet, nseq) {
96 if (consensus == null || consensus.annotations == null || consensus.annotations.length < width) {
97 return;
98 }var fmt = jalview.analysis.AAFrequency.getPercentageFormat (nseq);
99 for (var i = iStart; i < width; i++) {
100 var hci;
101 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null)) {
102 consensus.annotations[i] = null;
103 continue;
104 }var fv = hci.get (ignoreGapsInConsensusCalculation ? "N" : "G");
105 if (fv == null) {
106 consensus.annotations[i] = null;
107 continue;
108 }var value = fv.floatValue ();
109 var maxRes = hci.get ("R").toString ();
110 var mouseOver =  new StringBuilder (64);
111 if (maxRes.length > 1) {
112 mouseOver.append ("[").append (maxRes).append ("] ");
113 maxRes = "+";
114 } else {
115 mouseOver.append (hci.get ("R") + " ");
116 }var profile = hci.get ("P");
117 if (profile != null && includeAllConsSymbols) {
118 var sequenceCount = profile[1][0];
119 var nonGappedCount = profile[1][1];
120 var normalisedBy = ignoreGapsInConsensusCalculation ? nonGappedCount : sequenceCount;
121 mouseOver.setLength (0);
122 if (alphabet != null) {
123 for (var c = 0; c < alphabet.length; c++) {
124 var tval = profile[0][alphabet[c].charCodeAt (0)] * 100 / normalisedBy;
125 mouseOver.append (((c == 0) ? "" : "; ")).append (alphabet[c]).append (" ").append (((fmt != null) ? fmt.formDouble (tval) : (Clazz.floatToInt (tval)))).append ("%");
126 }
127 } else {
128 var ca =  Clazz.newCharArray (profile[0].length, '\0');
129 var vl =  Clazz.newFloatArray (profile[0].length, 0);
130 for (var c = 0; c < ca.length; c++) {
131 ca[c] = String.fromCharCode (c);
132 vl[c] = profile[0][c];
133 }
134 jalview.util.QuickSort.sortFloatChar (vl, ca);
135 for (var p = 0, c = ca.length - 1; profile[0][ca[c].charCodeAt (0)] > 0; c--) {
136 var residue = ca[c];
137 if (residue != '-') {
138 var tval = profile[0][residue.charCodeAt (0)] * 100 / normalisedBy;
139 mouseOver.append ((((p == 0) ? "" : "; "))).append (residue).append (" ").append (((fmt != null) ? fmt.formDouble (tval) : (Clazz.floatToInt (tval)))).append ("%");
140 p++;
141 }}
142 }} else {
143 mouseOver.append ((((fmt != null) ? fmt.formDouble (value) : (Clazz.floatToInt (value))))).append ("%");
144 }consensus.annotations[i] =  new jalview.datamodel.Annotation (maxRes, mouseOver.toString (), ' ', value);
145 }
146 }, "jalview.datamodel.AlignmentAnnotation,~A,~N,~N,~B,~B,~A,~N");
147 c$.getPercentageFormat = Clazz.defineMethod (c$, "getPercentageFormat", 
148 function (nseq) {
149 var scale = 0;
150 while (nseq >= 10) {
151 scale++;
152 nseq /= 10;
153 }
154 return scale <= 1 ? null :  new jalview.util.Format ("%3." + (scale - 1) + "f");
155 }, "~N");
156 c$.extractProfile = Clazz.defineMethod (c$, "extractProfile", 
157 function (hconsensus, ignoreGaps) {
158 var rtnval =  Clazz.newIntArray (64, 0);
159 var profile = hconsensus.get ("P");
160 if (profile == null) {
161 return null;
162 }var ca =  Clazz.newCharArray (profile[0].length, '\0');
163 var vl =  Clazz.newFloatArray (profile[0].length, 0);
164 for (var c = 0; c < ca.length; c++) {
165 ca[c] = String.fromCharCode (c);
166 vl[c] = profile[0][c];
167 }
168 jalview.util.QuickSort.sortFloatChar (vl, ca);
169 var nextArrayPos = 2;
170 var totalPercentage = 0;
171 var distinctValuesCount = 0;
172 var divisor = profile[1][ignoreGaps ? 1 : 0];
173 for (var c = ca.length - 1; profile[0][ca[c].charCodeAt (0)] > 0; c--) {
174 if (ca[c] != '-') {
175 rtnval[nextArrayPos++] = (ca[c]).charCodeAt (0);
176 var percentage = Clazz.floatToInt (profile[0][ca[c].charCodeAt (0)] * 100 / divisor);
177 rtnval[nextArrayPos++] = percentage;
178 totalPercentage += percentage;
179 distinctValuesCount++;
180 }}
181 rtnval[0] = distinctValuesCount;
182 rtnval[1] = totalPercentage;
183 var result =  Clazz.newIntArray (rtnval.length + 1, 0);
184 result[0] = 0;
185 System.arraycopy (rtnval, 0, result, 1, rtnval.length);
186 return result;
187 }, "java.util.Hashtable,~B");
188 c$.extractCdnaProfile = Clazz.defineMethod (c$, "extractCdnaProfile", 
189 function (hashtable, ignoreGaps) {
190 var codonCounts = hashtable.get ("P");
191 var sortedCounts =  Clazz.newIntArray (codonCounts.length - 2, 0);
192 System.arraycopy (codonCounts, 2, sortedCounts, 0, codonCounts.length - 2);
193 var result =  Clazz.newIntArray (3 + 2 * sortedCounts.length, 0);
194 result[0] = 2;
195 var codons =  Clazz.newCharArray (sortedCounts.length, '\0');
196 for (var i = 0; i < codons.length; i++) {
197 codons[i] = String.fromCharCode (i);
198 }
199 jalview.util.QuickSort.sortIntChar (sortedCounts, codons);
200 var totalPercentage = 0;
201 var distinctValuesCount = 0;
202 var j = 3;
203 var divisor = ignoreGaps ? codonCounts[1] : codonCounts[0];
204 for (var i = codons.length - 1; i >= 0; i--) {
205 var codonCount = sortedCounts[i];
206 if (codonCount == 0) {
207 break;
208 }distinctValuesCount++;
209 result[j++] = (codons[i]).charCodeAt (0);
210 var percentage = Clazz.doubleToInt (codonCount * 100 / divisor);
211 result[j++] = percentage;
212 totalPercentage += percentage;
213 }
214 result[2] = totalPercentage;
215 result[1] = distinctValuesCount;
216 return java.util.Arrays.copyOfRange (result, 0, j);
217 }, "java.util.Hashtable,~B");
218 c$.calculateCdna = Clazz.defineMethod (c$, "calculateCdna", 
219 function (alignment, mappings, hconsensus, col0, col1) {
220 var gapCharacter = alignment.getGapCharacter ();
221 for (var col = col0; col < col1; col++) {
222 var columnHash =  new java.util.Hashtable ();
223 var codonCounts =  Clazz.newIntArray (66, 0);
224 codonCounts[0] = alignment.getSequences ().size ();
225 var ungappedCount = 0;
226 for (var seq, $seq = alignment.getSequences ().iterator (); $seq.hasNext () && ((seq = $seq.next ()) || true);) {
227 if (seq.getCharAt (col) == gapCharacter) {
228 continue;
229 }var codon = jalview.util.MappingUtils.findCodonFor (seq, col, mappings);
230 var codonEncoded = jalview.analysis.CodingUtils.encodeCodon (codon);
231 if (codonEncoded >= 0) {
232 codonCounts[codonEncoded + 2]++;
233 ungappedCount++;
234 }}
235 codonCounts[1] = ungappedCount;
236 columnHash.put ("P", codonCounts);
237 hconsensus[col] = columnHash;
238 }
239 }, "jalview.datamodel.AlignmentI,java.util.Set,~A,~N,~N");
240 c$.completeCdnaConsensus = Clazz.defineMethod (c$, "completeCdnaConsensus", 
241 function (consensusAnnotation, consensusData, showProfileLogo, nseqs) {
242 if (consensusAnnotation == null || consensusAnnotation.annotations == null || consensusAnnotation.annotations.length < consensusData.length) {
243 return;
244 }consensusAnnotation.scaleColLabel = true;
245 for (var col = 0; col < consensusData.length; col++) {
246 var hci = consensusData[col];
247 if (hci == null) {
248 continue;
249 }var codonCounts = hci.get ("P");
250 var totalCount = 0;
251 var codons =  Clazz.newCharArray (codonCounts.length - 2, '\0');
252 for (var j = 2; j < codonCounts.length; j++) {
253 var codonCount = codonCounts[j];
254 codons[j - 2] = String.fromCharCode (j - 2);
255 totalCount += codonCount;
256 }
257 var sortedCodonCounts =  Clazz.newIntArray (codonCounts.length - 2, 0);
258 System.arraycopy (codonCounts, 2, sortedCodonCounts, 0, codonCounts.length - 2);
259 jalview.util.QuickSort.sortIntChar (sortedCodonCounts, codons);
260 var modalCodonEncoded = (codons[codons.length - 1]).charCodeAt (0);
261 var modalCodonCount = sortedCodonCounts[codons.length - 1];
262 var modalCodon = String.valueOf (jalview.analysis.CodingUtils.decodeCodon (modalCodonEncoded));
263 if (sortedCodonCounts.length > 1 && sortedCodonCounts[codons.length - 2] == modalCodonEncoded) {
264 modalCodon = "+";
265 }var pid = sortedCodonCounts[sortedCodonCounts.length - 1] * 100 / totalCount;
266 var mouseOver =  new StringBuilder (32);
267 var samePercent =  new StringBuilder ();
268 var percent = null;
269 var lastPercent = null;
270 var fmt = jalview.analysis.AAFrequency.getPercentageFormat (nseqs);
271 for (var j = codons.length - 1; j >= 0; j--) {
272 var codonCount = sortedCodonCounts[j];
273 if (codonCount == 0) {
274 if (samePercent.length () > 0) {
275 mouseOver.append (samePercent).append (": ").append (percent).append ("% ");
276 }break;
277 }var codonEncoded = (codons[j]).charCodeAt (0);
278 var pct = Clazz.doubleToInt (codonCount * 100 / totalCount);
279 var codon = String.valueOf (jalview.analysis.CodingUtils.decodeCodon (codonEncoded));
280 percent = fmt == null ? Integer.toString (pct) : fmt.formLong (pct);
281 if (showProfileLogo || codonCount == modalCodonCount) {
282 if (percent.equals (lastPercent) && j > 0) {
283 samePercent.append (samePercent.length () == 0 ? "" : ", ");
284 samePercent.append (codon);
285 } else {
286 if (samePercent.length () > 0) {
287 mouseOver.append (samePercent).append (": ").append (lastPercent).append ("% ");
288 }samePercent.setLength (0);
289 samePercent.append (codon);
290 }lastPercent = percent;
291 }}
292 consensusAnnotation.annotations[col] =  new jalview.datamodel.Annotation (modalCodon, mouseOver.toString (), ' ', pid);
293 }
294 }, "jalview.datamodel.AlignmentAnnotation,~A,~B,~N");
295 Clazz.defineStatics (c$,
296 "TO_UPPER_CASE", -32,
297 "MAXCOUNT", "C",
298 "MAXRESIDUE", "R",
299 "PID_GAPS", "G",
300 "PID_NOGAPS", "N",
301 "PROFILE", "P",
302 "ENCODED_CHARS", "E");
303 c$.CHARS = c$.prototype.CHARS =  new Array (26);
304 {
305 for (var c = 'A'; c <= 'Z'; c = String.fromCharCode (c.charCodeAt (0) + 1)) {
306 jalview.analysis.AAFrequency.CHARS[c.charCodeAt (0) - 65] = String.valueOf (c);
307 }
308 }});