Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:56:54 GMT
  2. Package jalview.analysis

File AAFrequency.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
12% of files have more coverage

Code metrics

152
318
18
1
976
620
112
0.35
17.67
18
6.22

Classes

Class Line # Actions
AAFrequency 55 318 112
0.866803386.7%
 

Contributing tests

This file is covered by 192 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.analysis;
22   
23    import jalview.datamodel.AlignedCodonFrame;
24    import jalview.datamodel.AlignmentAnnotation;
25    import jalview.datamodel.AlignmentI;
26    import jalview.datamodel.Annotation;
27    import jalview.datamodel.Profile;
28    import jalview.datamodel.ProfileI;
29    import jalview.datamodel.Profiles;
30    import jalview.datamodel.ProfilesI;
31    import jalview.datamodel.ResidueCount;
32    import jalview.datamodel.ResidueCount.SymbolCounts;
33    import jalview.datamodel.SecondaryStructureCount;
34    import jalview.datamodel.SequenceI;
35    import jalview.ext.android.SparseIntArray;
36    import jalview.util.Comparison;
37    import jalview.util.Format;
38    import jalview.util.MappingUtils;
39    import jalview.util.QuickSort;
40   
41    import java.awt.Color;
42    import java.util.Arrays;
43    import java.util.Hashtable;
44    import java.util.List;
45   
46    /**
47    * Takes in a vector or array of sequences and column start and column end and
48    * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
49    * This class is used extensively in calculating alignment colourschemes that
50    * depend on the amount of conservation in each alignment column.
51    *
52    * @author $author$
53    * @version $Revision$
54    */
 
55    public class AAFrequency
56    {
57    public static final String PROFILE = "P";
58   
59    /*
60    * Quick look-up of String value of char 'A' to 'Z'
61    */
62    private static final String[] CHARS = new String['Z' - 'A' + 1];
63   
 
64  50 toggle static
65    {
66  1350 for (char c = 'A'; c <= 'Z'; c++)
67    {
68  1300 CHARS[c - 'A'] = String.valueOf(c);
69    }
70    }
71   
 
72  3 toggle public static final ProfilesI calculate(List<SequenceI> list, int start,
73    int end)
74    {
75  3 return calculate(list, start, end, false);
76    }
77   
 
78  368 toggle public static final ProfilesI calculate(List<SequenceI> sequences,
79    int start, int end, boolean profile)
80    {
81  368 SequenceI[] seqs = new SequenceI[sequences.size()];
82  368 int width = 0;
83  368 synchronized (sequences)
84    {
85  3012 for (int i = 0; i < sequences.size(); i++)
86    {
87  2644 seqs[i] = sequences.get(i);
88  2644 int length = seqs[i].getLength();
89  2644 if (length > width)
90    {
91  367 width = length;
92    }
93    }
94   
95  368 if (end >= width)
96    {
97  210 end = width;
98    }
99   
100  368 ProfilesI reply = calculate(seqs, width, start, end, profile);
101  368 return reply;
102    }
103    }
104   
105    /**
106    * Calculate the consensus symbol(s) for each column in the given range.
107    *
108    * @param sequences
109    * @param width
110    * the full width of the alignment
111    * @param start
112    * start column (inclusive, base zero)
113    * @param end
114    * end column (exclusive)
115    * @param saveFullProfile
116    * if true, store all symbol counts
117    */
 
118  1309 toggle public static final ProfilesI calculate(final SequenceI[] sequences,
119    int width, int start, int end, boolean saveFullProfile)
120    {
121    // long now = System.currentTimeMillis();
122  1309 int seqCount = sequences.length;
123  1309 boolean nucleotide = false;
124  1309 int nucleotideCount = 0;
125  1309 int peptideCount = 0;
126   
127  1309 ProfileI[] result = new ProfileI[width];
128   
129  607067 for (int column = start; column < end; column++)
130    {
131    /*
132    * Apply a heuristic to detect nucleotide data (which can
133    * be counted in more compact arrays); here we test for
134    * more than 90% nucleotide; recheck every 10 columns in case
135    * of misleading data e.g. highly conserved Alanine in peptide!
136    * Mistakenly guessing nucleotide has a small performance cost,
137    * as it will result in counting in sparse arrays.
138    * Mistakenly guessing peptide has a small space cost,
139    * as it will use a larger than necessary array to hold counts.
140    */
141  605768 if (nucleotideCount > 100 && column % 10 == 0)
142    {
143  54700 nucleotide = (9 * peptideCount < nucleotideCount);
144    }
145  605767 ResidueCount residueCounts = new ResidueCount(nucleotide);
146   
147  11242366 for (int row = 0; row < seqCount; row++)
148    {
149  10636509 if (sequences[row] == null)
150    {
151  0 jalview.bin.Console.errPrintln(
152    "WARNING: Consensus skipping null sequence - possible race condition.");
153  0 continue;
154    }
155  10630722 if (sequences[row].getLength() > column)
156    {
157  10596515 char c = sequences[row].getCharAt(column);
158  10582447 residueCounts.add(c);
159  10619879 if (Comparison.isNucleotide(c))
160    {
161  961773 nucleotideCount++;
162    }
163  9603674 else if (!Comparison.isGap(c))
164    {
165  822263 peptideCount++;
166    }
167    }
168    else
169    {
170    /*
171    * count a gap if the sequence doesn't reach this column
172    */
173  38052 residueCounts.addGap();
174    }
175    }
176   
177  605714 int maxCount = residueCounts.getModalCount();
178  605711 String maxResidue = residueCounts.getResiduesForCount(maxCount);
179  605685 int gapCount = residueCounts.getGapCount();
180  605683 ProfileI profile = new Profile(seqCount, gapCount, maxCount,
181    maxResidue);
182   
183  605746 if (saveFullProfile)
184    {
185  587937 profile.setCounts(residueCounts);
186    }
187   
188  605712 result[column] = profile;
189    }
190  1309 return new Profiles(seqCount, result);
191    // long elapsed = System.currentTimeMillis() - now;
192    // jalview.bin.Console.outPrintln(elapsed);
193    }
194   
 
195  0 toggle public static final ProfilesI calculateSS(List<SequenceI> list, int start,
196    int end, String source)
197    {
198  0 return calculateSS(list, start, end, false, source);
199    }
200   
 
201  365 toggle public static final ProfilesI calculateSS(List<SequenceI> sequences,
202    int start, int end, boolean profile, String source)
203    {
204  365 SequenceI[] seqs = new SequenceI[sequences.size()];
205  365 int width = 0;
206  365 synchronized (sequences)
207    {
208  3006 for (int i = 0; i < sequences.size(); i++)
209    {
210  2641 seqs[i] = sequences.get(i);
211  2641 int length = seqs[i].getLength();
212  2641 if (length > width)
213    {
214  364 width = length;
215    }
216    }
217   
218  365 if (end >= width)
219    {
220  210 end = width;
221    }
222   
223  365 ProfilesI reply = calculateSS(seqs, width, start, end, profile,
224    source);
225  365 return reply;
226    }
227    }
228   
 
229  1364 toggle public static final ProfilesI calculateSS(final SequenceI[] sequences,
230    int width, int start, int end, boolean saveFullProfile,
231    String source)
232    {
233   
234  1364 int seqCount = sequences.length;
235   
236  1364 int seqWithSSCount = 0;
237   
238  1364 ProfileI[] result = new ProfileI[width];
239  1364 int maxSSannotcount=0;
240  623126 for (int column = start; column < end; column++)
241    {
242   
243  621769 int ssCount = 0;
244   
245  621774 SecondaryStructureCount ssCounts = new SecondaryStructureCount();
246   
247  11387151 for (int row = 0; row < seqCount; row++)
248    {
249  10790449 if (sequences[row] == null)
250    {
251  0 jalview.bin.Console.errPrintln(
252    "WARNING: Consensus skipping null sequence - possible race condition.");
253  0 continue;
254    }
255   
256  10804126 char c = sequences[row].getCharAt(column);
257   
258  10699541 List<AlignmentAnnotation> annots = AlignmentUtils.getAlignmentAnnotationForSource(sequences[row], source);
259  10794700 if(annots!=null) {
260  100737 seqWithSSCount++;
261  100737 for (AlignmentAnnotation aa : annots)
262    {
263  112538 if (aa != null)
264    {
265  112538 ssCount++;
266    }
267   
268  112535 if (sequences[row].getLength() > column && !Comparison.isGap(c)
269    && aa != null)
270    {
271   
272  70728 int seqPosition = sequences[row].findPosition(column);
273   
274  70734 char ss = AlignmentUtils
275    .findSSAnnotationForGivenSeqposition(aa, seqPosition);
276  70727 if (ss == '*')
277    {
278  0 continue;
279    }
280  70727 ssCounts.add(ss);
281    }
282  41806 else if (Comparison.isGap(c) && aa != null)
283    {
284  41806 ssCounts.addGap();
285    }
286    }
287    }
288    }
289   
290  621704 int maxSSCount = ssCounts.getModalCount();
291  621626 String maxSS = ssCounts.getSSForCount(maxSSCount);
292  621566 int gapCount = ssCounts.getGapCount();
293  621578 ProfileI profile = new Profile(maxSS, ssCount, gapCount, maxSSCount,
294    seqWithSSCount);
295   
296  621795 if (saveFullProfile)
297    {
298  604016 profile.setSSCounts(ssCounts);
299    }
300   
301  621741 result[column] = profile;
302  621772 maxSSannotcount=Math.max(maxSSannotcount, ssCount);
303    }
304  1364 return new Profiles(maxSSannotcount,result);
305    }
306   
307    /**
308    * Make an estimate of the profile size we are going to compute i.e. how many
309    * different characters may be present in it. Overestimating has a cost of
310    * using more memory than necessary. Underestimating has a cost of needing to
311    * extend the SparseIntArray holding the profile counts.
312    *
313    * @param profileSizes
314    * counts of sizes of profiles so far encountered
315    * @return
316    */
 
317  0 toggle static int estimateProfileSize(SparseIntArray profileSizes)
318    {
319  0 if (profileSizes.size() == 0)
320    {
321  0 return 4;
322    }
323   
324    /*
325    * could do a statistical heuristic here e.g. 75%ile
326    * for now just return the largest value
327    */
328  0 return profileSizes.keyAt(profileSizes.size() - 1);
329    }
330   
331    /**
332    * Derive the consensus annotations to be added to the alignment for display.
333    * This does not recompute the raw data, but may be called on a change in
334    * display options, such as 'ignore gaps', which may in turn result in a
335    * change in the derived values.
336    *
337    * @param consensus
338    * the annotation row to add annotations to
339    * @param profiles
340    * the source consensus data
341    * @param startCol
342    * start column (inclusive)
343    * @param endCol
344    * end column (exclusive)
345    * @param ignoreGaps
346    * if true, normalise residue percentages ignoring gaps
347    * @param showSequenceLogo
348    * if true include all consensus symbols, else just show modal
349    * residue
350    * @param nseq
351    * number of sequences
352    */
 
353  1065 toggle public static void completeConsensus(AlignmentAnnotation consensus,
354    ProfilesI profiles, int startCol, int endCol, boolean ignoreGaps,
355    boolean showSequenceLogo, long nseq)
356    {
357    // long now = System.currentTimeMillis();
358  1065 if (consensus == null || consensus.annotations == null
359    || consensus.annotations.length < endCol)
360    {
361    /*
362    * called with a bad alignment annotation row
363    * wait for it to be initialised properly
364    */
365  0 return;
366    }
367   
368  379870 for (int i = startCol; i < endCol; i++)
369    {
370  378818 ProfileI profile = profiles.get(i);
371  378819 if (profile == null)
372    {
373    /*
374    * happens if sequences calculated over were
375    * shorter than alignment width
376    */
377  0 consensus.annotations[i] = null;
378  0 return;
379    }
380   
381  378818 final int dp = getPercentageDp(nseq);
382   
383  378806 float value = profile.getPercentageIdentity(ignoreGaps);
384   
385  378803 String description = getTooltip(profile, value, showSequenceLogo,
386    ignoreGaps, dp);
387   
388  378817 String modalResidue = profile.getModalResidue();
389  378817 if ("".equals(modalResidue))
390    {
391  5910 modalResidue = "-";
392    }
393  372906 else if (modalResidue.length() > 1)
394    {
395  7685 modalResidue = "+";
396    }
397  378817 consensus.annotations[i] = new Annotation(modalResidue, description,
398    ' ', value);
399    }
400    // long elapsed = System.currentTimeMillis() - now;
401    // jalview.bin.Console.outPrintln(-elapsed);
402    }
403   
 
404  905 toggle public static void completeSSConsensus(AlignmentAnnotation ssConsensus,
405    ProfilesI profiles, int startCol, int endCol, boolean ignoreGaps,
406    boolean showSequenceLogo, long nseq)
407    {
408    // long now = System.currentTimeMillis();
409  905 if (ssConsensus == null || ssConsensus.annotations == null
410    || ssConsensus.annotations.length < endCol)
411    {
412    /*
413    * called with a bad alignment annotation row
414    * wait for it to be initialised properly
415    */
416  3 return;
417    }
418   
419  521192 for (int i = startCol; i < endCol; i++)
420    {
421  520290 ProfileI profile = profiles.get(i);
422  520290 if (profile == null)
423    {
424    /*
425    * happens if sequences calculated over were
426    * shorter than alignment width
427    */
428  0 ssConsensus.annotations[i] = null;
429  0 return;
430    }
431   
432  520290 if (ssConsensus.getNoOfSequencesIncluded() < 0)
433    {
434  0 ssConsensus.setNoOfSequencesIncluded(profile.getSeqWithSSCount());
435    }
436   
437  520290 final int dp = getPercentageDp(nseq);
438   
439  520290 float value = profile.getSSPercentageIdentity(ignoreGaps);
440   
441  520290 String description = getSSTooltip(profile, value, showSequenceLogo,
442    ignoreGaps, dp);
443   
444  520290 String modalSS = profile.getModalSS();
445  520290 if ("".equals(modalSS))
446    {
447  502133 modalSS = "-";
448    }
449  18157 else if (modalSS.length() > 1)
450    {
451  1068 modalSS = "+";
452    }
453  520290 ssConsensus.annotations[i] = new Annotation(modalSS, description,
454    ' ', value);
455    }
456   
457    //Hide consensus with no data to display
458  902 if(ssConsensus.getNoOfSequencesIncluded()<1)
459  816 ssConsensus.visible = false;
460   
461    // long elapsed = System.currentTimeMillis() - now;
462    // jalview.bin.Console.outPrintln(-elapsed);
463    }
464   
465    /**
466    * Derive the gap count annotation row.
467    *
468    * @param gaprow
469    * the annotation row to add annotations to
470    * @param profiles
471    * the source consensus data
472    * @param startCol
473    * start column (inclusive)
474    * @param endCol
475    * end column (exclusive)
476    */
 
477  1835 toggle public static void completeGapAnnot(AlignmentAnnotation gaprow,
478    ProfilesI profiles, int startCol, int endCol, long nseq)
479    {
480  1835 if (gaprow == null || gaprow.annotations == null
481    || gaprow.annotations.length < endCol)
482    {
483    /*
484    * called with a bad alignment annotation row
485    * wait for it to be initialised properly
486    */
487  0 return;
488    }
489    // always set ranges again
490  1835 gaprow.graphMax = nseq;
491  1835 gaprow.graphMin = 0;
492  1835 double scale = 0.8 / nseq;
493  880719 for (int i = startCol; i < endCol; i++)
494    {
495  878890 ProfileI profile = profiles.get(i);
496  878877 if (profile == null)
497    {
498    /*
499    * happens if sequences calculated over were
500    * shorter than alignment width
501    */
502  0 gaprow.annotations[i] = null;
503  0 return;
504    }
505   
506  878885 final int gapped = profile.getNonGapped();
507   
508  878838 String description = "" + gapped;
509   
510  878850 gaprow.annotations[i] = new Annotation("", description, '\0', gapped,
511    jalview.util.ColorUtils.bleachColour(Color.DARK_GRAY,
512    (float) scale * gapped));
513    }
514    }
515   
516    /**
517    * Returns a tooltip showing either
518    * <ul>
519    * <li>the full profile (percentages of all residues present), if
520    * showSequenceLogo is true, or</li>
521    * <li>just the modal (most common) residue(s), if showSequenceLogo is
522    * false</li>
523    * </ul>
524    * Percentages are as a fraction of all sequence, or only ungapped sequences
525    * if ignoreGaps is true.
526    *
527    * @param profile
528    * @param pid
529    * @param showSequenceLogo
530    * @param ignoreGaps
531    * @param dp
532    * the number of decimal places to format percentages to
533    * @return
534    */
 
535  378803 toggle static String getTooltip(ProfileI profile, float pid,
536    boolean showSequenceLogo, boolean ignoreGaps, int dp)
537    {
538  378804 ResidueCount counts = profile.getCounts();
539   
540  378801 String description = null;
541  378810 if (counts != null && showSequenceLogo)
542    {
543  60652 int normaliseBy = ignoreGaps ? profile.getNonGapped()
544    : profile.getHeight();
545  60652 description = counts.getTooltip(normaliseBy, dp);
546    }
547    else
548    {
549  318159 StringBuilder sb = new StringBuilder(64);
550  318160 String maxRes = profile.getModalResidue();
551  318157 if (maxRes.length() > 1)
552    {
553  2744 sb.append("[").append(maxRes).append("]");
554    }
555    else
556    {
557  315412 sb.append(maxRes);
558    }
559  318160 if (maxRes.length() > 0)
560    {
561  315191 sb.append(" ");
562  315197 Format.appendPercentage(sb, pid, dp);
563  315197 sb.append("%");
564    }
565  318165 description = sb.toString();
566    }
567  378817 return description;
568    }
569   
 
570  520290 toggle static String getSSTooltip(ProfileI profile, float pid,
571    boolean showSequenceLogo, boolean ignoreGaps, int dp)
572    {
573  520290 SecondaryStructureCount counts = profile.getSSCounts();
574   
575  520290 String description = null;
576  520290 if (counts != null && showSequenceLogo)
577    {
578  45907 int normaliseBy = ignoreGaps ? profile.getNonGapped()
579    : profile.getHeight();
580  45907 description = counts.getTooltip(normaliseBy, dp);
581    }
582    else
583    {
584  474383 StringBuilder sb = new StringBuilder(64);
585  474383 String maxSS = profile.getModalSS();
586  474383 if (maxSS.length() > 1)
587    {
588  728 sb.append("[").append(maxSS).append("]");
589    }
590    else
591    {
592  473655 sb.append(maxSS);
593    }
594  474383 if (maxSS.length() > 0)
595    {
596  12243 sb.append(" ");
597  12243 Format.appendPercentage(sb, pid, dp);
598  12243 sb.append("%");
599    }
600  474383 description = sb.toString();
601    }
602  520290 return description;
603    }
604   
605    /**
606    * Returns the sorted profile for the given consensus data. The returned array
607    * contains
608    *
609    * <pre>
610    * [profileType, numberOfValues, totalPercent, charValue1, percentage1, charValue2, percentage2, ...]
611    * in descending order of percentage value
612    * </pre>
613    *
614    * @param profile
615    * the data object from which to extract and sort values
616    * @param ignoreGaps
617    * if true, only non-gapped values are included in percentage
618    * calculations
619    * @return
620    */
 
621  100035 toggle public static int[] extractProfile(ProfileI profile, boolean ignoreGaps)
622    {
623  100035 char[] symbols;
624  100035 int[] values;
625   
626  100035 if (profile.getCounts() != null)
627    {
628  100035 ResidueCount counts = profile.getCounts();
629  100035 SymbolCounts symbolCounts = counts.getSymbolCounts();
630  100036 symbols = symbolCounts.symbols;
631  100036 values = symbolCounts.values;
632   
633    }
634  0 else if (profile.getSSCounts() != null)
635    {
636  0 SecondaryStructureCount counts = profile.getSSCounts();
637    // to do
638  0 SecondaryStructureCount.SymbolCounts symbolCounts = counts
639    .getSymbolCounts();
640  0 symbols = symbolCounts.symbols;
641  0 values = symbolCounts.values;
642    }
643    else
644    {
645  0 return null;
646    }
647   
648  100036 QuickSort.sort(values, symbols);
649  100036 int totalPercentage = 0;
650  100036 final int divisor = ignoreGaps ? profile.getNonGapped()
651    : profile.getHeight();
652   
653    /*
654    * traverse the arrays in reverse order (highest counts first)
655    */
656  100036 int[] result = new int[3 + 2 * symbols.length];
657  100036 int nextArrayPos = 3;
658  100036 int nonZeroCount = 0;
659   
660  285005 for (int i = symbols.length - 1; i >= 0; i--)
661    {
662  184970 int theChar = symbols[i];
663  184971 int charCount = values[i];
664  184971 final int percentage = (charCount * 100) / divisor;
665  184971 if (percentage == 0)
666    {
667    /*
668    * this count (and any remaining) round down to 0% - discard
669    */
670  2 break;
671    }
672  184969 nonZeroCount++;
673  184969 result[nextArrayPos++] = theChar;
674  184969 result[nextArrayPos++] = percentage;
675  184969 totalPercentage += percentage;
676    }
677   
678    /*
679    * truncate array if any zero values were discarded
680    */
681  100035 if (nonZeroCount < symbols.length)
682    {
683  2 int[] tmp = new int[3 + 2 * nonZeroCount];
684  2 System.arraycopy(result, 0, tmp, 0, tmp.length);
685  2 result = tmp;
686    }
687   
688    /*
689    * fill in 'header' values
690    */
691  100035 result[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
692  100035 result[1] = nonZeroCount;
693  100035 result[2] = totalPercentage;
694   
695  100036 return result;
696    }
697   
698    /**
699    * Extract a sorted extract of cDNA codon profile data. The returned array
700    * contains
701    *
702    * <pre>
703    * [profileType, numberOfValues, totalPercentage, charValue1, percentage1, charValue2, percentage2, ...]
704    * in descending order of percentage value, where the character values encode codon triplets
705    * </pre>
706    *
707    * @param hashtable
708    * @return
709    */
 
710  2 toggle public static int[] extractCdnaProfile(
711    Hashtable<String, Object> hashtable, boolean ignoreGaps)
712    {
713    // this holds #seqs, #ungapped, and then codon count, indexed by encoded
714    // codon triplet
715  2 int[] codonCounts = (int[]) hashtable.get(PROFILE);
716  2 int[] sortedCounts = new int[codonCounts.length - 2];
717  2 System.arraycopy(codonCounts, 2, sortedCounts, 0,
718    codonCounts.length - 2);
719   
720  2 int[] result = new int[3 + 2 * sortedCounts.length];
721    // first value is just the type of profile data
722  2 result[0] = AlignmentAnnotation.CDNA_PROFILE;
723   
724  2 char[] codons = new char[sortedCounts.length];
725  130 for (int i = 0; i < codons.length; i++)
726    {
727  128 codons[i] = (char) i;
728    }
729  2 QuickSort.sort(sortedCounts, codons);
730  2 int totalPercentage = 0;
731  2 int distinctValuesCount = 0;
732  2 int j = 3;
733  2 int divisor = ignoreGaps ? codonCounts[1] : codonCounts[0];
734  8 for (int i = codons.length - 1; i >= 0; i--)
735    {
736  8 final int codonCount = sortedCounts[i];
737  8 if (codonCount == 0)
738    {
739  0 break; // nothing else of interest here
740    }
741  8 final int percentage = codonCount * 100 / divisor;
742  8 if (percentage == 0)
743    {
744    /*
745    * this (and any remaining) values rounded down to 0 - discard
746    */
747  2 break;
748    }
749  6 distinctValuesCount++;
750  6 result[j++] = codons[i];
751  6 result[j++] = percentage;
752  6 totalPercentage += percentage;
753    }
754  2 result[2] = totalPercentage;
755   
756    /*
757    * Just return the non-zero values
758    */
759    // todo next value is redundant if we limit the array to non-zero counts
760  2 result[1] = distinctValuesCount;
761  2 return Arrays.copyOfRange(result, 0, j);
762    }
763   
764    /**
765    * Compute a consensus for the cDNA coding for a protein alignment.
766    *
767    * @param alignment
768    * the protein alignment (which should hold mappings to cDNA
769    * sequences)
770    * @param hconsensus
771    * the consensus data stores to be populated (one per column)
772    */
 
773  4 toggle public static void calculateCdna(AlignmentI alignment,
774    Hashtable<String, Object>[] hconsensus)
775    {
776  4 final char gapCharacter = alignment.getGapCharacter();
777  4 List<AlignedCodonFrame> mappings = alignment.getCodonFrames();
778  4 if (mappings == null || mappings.isEmpty())
779    {
780  0 return;
781    }
782   
783  4 int cols = alignment.getWidth();
784  1928 for (int col = 0; col < cols; col++)
785    {
786    // todo would prefer a Java bean for consensus data
787  1924 Hashtable<String, Object> columnHash = new Hashtable<>();
788    // #seqs, #ungapped seqs, counts indexed by (codon encoded + 1)
789  1924 int[] codonCounts = new int[66];
790  1924 codonCounts[0] = alignment.getSequences().size();
791  1924 int ungappedCount = 0;
792  1924 for (SequenceI seq : alignment.getSequences())
793    {
794  20870 if (seq.getCharAt(col) == gapCharacter)
795    {
796  10166 continue;
797    }
798  10704 List<char[]> codons = MappingUtils.findCodonsFor(seq, col,
799    mappings);
800  10704 for (char[] codon : codons)
801    {
802  10657 int codonEncoded = CodingUtils.encodeCodon(codon);
803  10657 if (codonEncoded >= 0)
804    {
805  10657 codonCounts[codonEncoded + 2]++;
806  10657 ungappedCount++;
807  10657 break;
808    }
809    }
810    }
811  1924 codonCounts[1] = ungappedCount;
812    // todo: sort values here, save counts and codons?
813  1924 columnHash.put(PROFILE, codonCounts);
814  1924 hconsensus[col] = columnHash;
815    }
816    }
817   
818    /**
819    * Derive displayable cDNA consensus annotation from computed consensus data.
820    *
821    * @param consensusAnnotation
822    * the annotation row to be populated for display
823    * @param consensusData
824    * the computed consensus data
825    * @param showProfileLogo
826    * if true show all symbols present at each position, else only the
827    * modal value
828    * @param nseqs
829    * the number of sequences in the alignment
830    */
 
831  4 toggle public static void completeCdnaConsensus(
832    AlignmentAnnotation consensusAnnotation,
833    Hashtable<String, Object>[] consensusData,
834    boolean showProfileLogo, int nseqs)
835    {
836  4 if (consensusAnnotation == null
837    || consensusAnnotation.annotations == null
838    || consensusAnnotation.annotations.length < consensusData.length)
839    {
840    // called with a bad alignment annotation row - wait for it to be
841    // initialised properly
842  0 return;
843    }
844   
845    // ensure codon triplet scales with font size
846  4 consensusAnnotation.scaleColLabel = true;
847  1928 for (int col = 0; col < consensusData.length; col++)
848    {
849  1924 Hashtable<String, Object> hci = consensusData[col];
850  1924 if (hci == null)
851    {
852    // gapped protein column?
853  0 continue;
854    }
855    // array holds #seqs, #ungapped, then codon counts indexed by codon
856  1924 final int[] codonCounts = (int[]) hci.get(PROFILE);
857  1924 int totalCount = 0;
858   
859    /*
860    * First pass - get total count and find the highest
861    */
862  1924 final char[] codons = new char[codonCounts.length - 2];
863  125060 for (int j = 2; j < codonCounts.length; j++)
864    {
865  123136 final int codonCount = codonCounts[j];
866  123136 codons[j - 2] = (char) (j - 2);
867  123136 totalCount += codonCount;
868    }
869   
870    /*
871    * Sort array of encoded codons by count ascending - so the modal value
872    * goes to the end; start by copying the count (dropping the first value)
873    */
874  1924 int[] sortedCodonCounts = new int[codonCounts.length - 2];
875  1924 System.arraycopy(codonCounts, 2, sortedCodonCounts, 0,
876    codonCounts.length - 2);
877  1924 QuickSort.sort(sortedCodonCounts, codons);
878   
879  1924 int modalCodonEncoded = codons[codons.length - 1];
880  1924 int modalCodonCount = sortedCodonCounts[codons.length - 1];
881  1924 String modalCodon = String
882    .valueOf(CodingUtils.decodeCodon(modalCodonEncoded));
883  1924 if (sortedCodonCounts.length > 1 && sortedCodonCounts[codons.length
884    - 2] == sortedCodonCounts[codons.length - 1])
885    {
886    /*
887    * two or more codons share the modal count
888    */
889  25 modalCodon = "+";
890    }
891  1924 float pid = sortedCodonCounts[sortedCodonCounts.length - 1] * 100
892    / (float) totalCount;
893   
894    /*
895    * todo ? Replace consensus hashtable with sorted arrays of codons and
896    * counts (non-zero only). Include total count in count array [0].
897    */
898   
899    /*
900    * Scan sorted array backwards for most frequent values first. Show
901    * repeated values compactly.
902    */
903  1924 StringBuilder mouseOver = new StringBuilder(32);
904  1924 StringBuilder samePercent = new StringBuilder();
905  1924 String percent = null;
906  1924 String lastPercent = null;
907  1924 int percentDecPl = getPercentageDp(nseqs);
908   
909  3823 for (int j = codons.length - 1; j >= 0; j--)
910    {
911  3823 int codonCount = sortedCodonCounts[j];
912  3823 if (codonCount == 0)
913    {
914    /*
915    * remaining codons are 0% - ignore, but finish off the last one if
916    * necessary
917    */
918  1924 if (samePercent.length() > 0)
919    {
920  1899 mouseOver.append(samePercent).append(": ").append(percent)
921    .append("% ");
922    }
923  1924 break;
924    }
925  1899 int codonEncoded = codons[j];
926  1899 final int pct = codonCount * 100 / totalCount;
927  1899 String codon = String
928    .valueOf(CodingUtils.decodeCodon(codonEncoded));
929  1899 StringBuilder sb = new StringBuilder();
930  1899 Format.appendPercentage(sb, pct, percentDecPl);
931  1899 percent = sb.toString();
932  1899 if (showProfileLogo || codonCount == modalCodonCount)
933    {
934  1899 if (percent.equals(lastPercent) && j > 0)
935    {
936  0 samePercent.append(samePercent.length() == 0 ? "" : ", ");
937  0 samePercent.append(codon);
938    }
939    else
940    {
941  1899 if (samePercent.length() > 0)
942    {
943  0 mouseOver.append(samePercent).append(": ").append(lastPercent)
944    .append("% ");
945    }
946  1899 samePercent.setLength(0);
947  1899 samePercent.append(codon);
948    }
949  1899 lastPercent = percent;
950    }
951    }
952   
953  1924 consensusAnnotation.annotations[col] = new Annotation(modalCodon,
954    mouseOver.toString(), ' ', pid);
955    }
956    }
957   
958    /**
959    * Returns the number of decimal places to show for profile percentages. For
960    * less than 100 sequences, returns zero (the integer percentage value will be
961    * displayed). For 100-999 sequences, returns 1, for 1000-9999 returns 2, etc.
962    *
963    * @param nseq
964    * @return
965    */
 
966  900958 toggle protected static int getPercentageDp(long nseq)
967    {
968  900975 int scale = 0;
969  900999 while (nseq >= 100)
970    {
971  0 scale++;
972  0 nseq /= 10;
973    }
974  900980 return scale;
975    }
976    }