+ codonCounts[1] = ungappedCount;
+ // todo: sort values here, save counts and codons?
+ columnHash.put(PROFILE, codonCounts);
+ hconsensus[col] = columnHash;
+ }
+ }
+
+ /**
+ * Derive displayable cDNA consensus annotation from computed consensus data.
+ *
+ * @param consensusAnnotation
+ * the annotation row to be populated for display
+ * @param consensusData
+ * the computed consensus data
+ * @param showProfileLogo
+ * if true show all symbols present at each position, else only the
+ * modal value
+ * @param nseqs
+ * the number of sequences in the alignment
+ */
+ public static void completeCdnaConsensus(
+ AlignmentAnnotation consensusAnnotation,
+ Hashtable[] consensusData, boolean showProfileLogo, int nseqs)
+ {
+ if (consensusAnnotation == null
+ || consensusAnnotation.annotations == null
+ || consensusAnnotation.annotations.length < consensusData.length)
+ {
+ // called with a bad alignment annotation row - wait for it to be
+ // initialised properly
+ return;
+ }
+
+ // ensure codon triplet scales with font size
+ consensusAnnotation.scaleColLabel = true;
+ for (int col = 0; col < consensusData.length; col++)
+ {
+ Hashtable hci = consensusData[col];
+ if (hci == null)
+ {
+ // gapped protein column?
+ continue;
+ }
+ // array holds #seqs, #ungapped, then codon counts indexed by codon
+ final int[] codonCounts = (int[]) hci.get(PROFILE);
+ int totalCount = 0;
+
+ /*
+ * First pass - get total count and find the highest
+ */
+ final char[] codons = new char[codonCounts.length - 2];
+ for (int j = 2; j < codonCounts.length; j++)
+ {
+ final int codonCount = codonCounts[j];
+ codons[j - 2] = (char) (j - 2);
+ totalCount += codonCount;
+ }
+
+ /*
+ * Sort array of encoded codons by count ascending - so the modal value
+ * goes to the end; start by copying the count (dropping the first value)
+ */
+ int[] sortedCodonCounts = new int[codonCounts.length - 2];
+ System.arraycopy(codonCounts, 2, sortedCodonCounts, 0,
+ codonCounts.length - 2);
+ QuickSort.sort(sortedCodonCounts, codons);
+
+ int modalCodonEncoded = codons[codons.length - 1];
+ int modalCodonCount = sortedCodonCounts[codons.length - 1];
+ String modalCodon = String.valueOf(CodingUtils
+ .decodeCodon(modalCodonEncoded));
+ if (sortedCodonCounts.length > 1
+ && sortedCodonCounts[codons.length - 2] == sortedCodonCounts[codons.length - 1])
+ {
+ /*
+ * two or more codons share the modal count
+ */
+ modalCodon = "+";
+ }
+ float pid = sortedCodonCounts[sortedCodonCounts.length - 1] * 100
+ / (float) totalCount;
+
+ /*
+ * todo ? Replace consensus hashtable with sorted arrays of codons and
+ * counts (non-zero only). Include total count in count array [0].
+ */
+
+ /*
+ * Scan sorted array backwards for most frequent values first. Show
+ * repeated values compactly.
+ */
+ StringBuilder mouseOver = new StringBuilder(32);
+ StringBuilder samePercent = new StringBuilder();
+ String percent = null;
+ String lastPercent = null;
+ Format fmt = getPercentageFormat(nseqs);
+
+ for (int j = codons.length - 1; j >= 0; j--)
+ {
+ int codonCount = sortedCodonCounts[j];
+ if (codonCount == 0)
+ {
+ /*
+ * remaining codons are 0% - ignore, but finish off the last one if
+ * necessary
+ */
+ if (samePercent.length() > 0)
+ {
+ mouseOver.append(samePercent).append(": ").append(percent)
+ .append("% ");
+ }
+ break;
+ }
+ int codonEncoded = codons[j];
+ final int pct = codonCount * 100 / totalCount;
+ String codon = String
+ .valueOf(CodingUtils.decodeCodon(codonEncoded));
+ percent = fmt == null ? Integer.toString(pct) : fmt.form(pct);
+ if (showProfileLogo || codonCount == modalCodonCount)
+ {
+ if (percent.equals(lastPercent) && j > 0)
+ {
+ samePercent.append(samePercent.length() == 0 ? "" : ", ");
+ samePercent.append(codon);
+ }
+ else
+ {
+ if (samePercent.length() > 0)
+ {
+ mouseOver.append(samePercent).append(": ")
+ .append(lastPercent).append("% ");
+ }
+ samePercent.setLength(0);
+ samePercent.append(codon);
+ }
+ lastPercent = percent;
+ }
+ }
+
+ consensusAnnotation.annotations[col] = new Annotation(modalCodon,
+ mouseOver.toString(), ' ', pid);