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 ());
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 ();
18 var reply = new Array (width);
21 }jalview.analysis.AAFrequency.calculate (seqs, start, end, reply, profile);
23 }}, "java.util.List,~N,~N,~B");
24 c$.calculate = Clazz.defineMethod (c$, "calculate",
25 function (sequences, start, end, result, profile) {
32 var jSize = sequences.length;
36 var values = Clazz.newIntArray (255, 0);
38 for (i = start; i < end; i++) {
39 residueHash = new java.util.Hashtable ();
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.");
48 }seq = sequences[j].getSequence ();
51 if (c == '.' || c == ' ') {
54 values['-'.charCodeAt (0)]++;
56 } else if ('a' <= c && c <= 'z') {
57 c = String.fromCharCode (c.charCodeAt (0) + -32);
59 values[c.charCodeAt (0)]++;
61 values['-'.charCodeAt (0)]++;
64 maxResidue = String.valueOf (c);
67 for (v = 65; v <= 90; v++) {
68 if (values[v] < 1 || values[v] < maxCount) {
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];
76 }if (maxResidue.length == 0) {
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));
85 percentage = (maxCount * 100) / nongap;
86 }residueHash.put ("N", new Float (percentage));
87 result[i] = residueHash;
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) {
98 }var fmt = jalview.analysis.AAFrequency.getPercentageFormat (nseq);
99 for (var i = iStart; i < width; i++) {
101 if (i >= hconsensus.length || ((hci = hconsensus[i]) == null)) {
102 consensus.annotations[i] = null;
104 }var fv = hci.get (ignoreGapsInConsensusCalculation ? "N" : "G");
106 consensus.annotations[i] = null;
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 ("] ");
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 ("%");
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];
134 jalview.util.QuickSort.sortFloatChar (vl, ca);
135 for (var p = 0, c = ca.length - 1; profile[0][ca[c].charCodeAt (0)] > 0; 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 ("%");
143 mouseOver.append ((((fmt != null) ? fmt.formDouble (value) : (Clazz.floatToInt (value))))).append ("%");
144 }consensus.annotations[i] = new jalview.datamodel.Annotation (maxRes, mouseOver.toString (), ' ', value);
146 }, "jalview.datamodel.AlignmentAnnotation,~A,~N,~N,~B,~B,~A,~N");
147 c$.getPercentageFormat = Clazz.defineMethod (c$, "getPercentageFormat",
154 return scale <= 1 ? null : new jalview.util.Format ("%3." + (scale - 1) + "f");
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) {
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];
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--) {
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++;
181 rtnval[0] = distinctValuesCount;
182 rtnval[1] = totalPercentage;
183 var result = Clazz.newIntArray (rtnval.length + 1, 0);
185 System.arraycopy (rtnval, 0, result, 1, rtnval.length);
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);
195 var codons = Clazz.newCharArray (sortedCounts.length, '\0');
196 for (var i = 0; i < codons.length; i++) {
197 codons[i] = String.fromCharCode (i);
199 jalview.util.QuickSort.sortIntChar (sortedCounts, codons);
200 var totalPercentage = 0;
201 var distinctValuesCount = 0;
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) {
208 }distinctValuesCount++;
209 result[j++] = (codons[i]).charCodeAt (0);
210 var percentage = Clazz.doubleToInt (codonCount * 100 / divisor);
211 result[j++] = percentage;
212 totalPercentage += percentage;
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) {
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]++;
235 codonCounts[1] = ungappedCount;
236 columnHash.put ("P", codonCounts);
237 hconsensus[col] = columnHash;
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) {
244 }consensusAnnotation.scaleColLabel = true;
245 for (var col = 0; col < consensusData.length; col++) {
246 var hci = consensusData[col];
249 }var codonCounts = hci.get ("P");
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;
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) {
265 }var pid = sortedCodonCounts[sortedCodonCounts.length - 1] * 100 / totalCount;
266 var mouseOver = new StringBuilder (32);
267 var samePercent = new StringBuilder ();
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 ("% ");
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);
286 if (samePercent.length () > 0) {
287 mouseOver.append (samePercent).append (": ").append (lastPercent).append ("% ");
288 }samePercent.setLength (0);
289 samePercent.append (codon);
290 }lastPercent = percent;
292 consensusAnnotation.annotations[col] = new jalview.datamodel.Annotation (modalCodon, mouseOver.toString (), ' ', pid);
294 }, "jalview.datamodel.AlignmentAnnotation,~A,~B,~N");
295 Clazz.defineStatics (c$,
296 "TO_UPPER_CASE", -32,
302 "ENCODED_CHARS", "E");
303 c$.CHARS = c$.prototype.CHARS = new Array (26);
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);