+ profile.setCounts(residueCounts);
+ }
+
+ result[column] = profile;
+ }
+ // long elapsed = System.currentTimeMillis() - now;
+ // System.out.println(elapsed);
+ }
+
+ /**
+ * Make an estimate of the profile size we are going to compute i.e. how many
+ * different characters may be present in it. Overestimating has a cost of
+ * using more memory than necessary. Underestimating has a cost of needing to
+ * extend the SparseIntArray holding the profile counts.
+ *
+ * @param profileSizes
+ * counts of sizes of profiles so far encountered
+ * @return
+ */
+ static int estimateProfileSize(SparseIntArray profileSizes)
+ {
+ if (profileSizes.size() == 0)
+ {
+ return 4;
+ }
+
+ /*
+ * could do a statistical heuristic here e.g. 75%ile
+ * for now just return the largest value
+ */
+ return profileSizes.keyAt(profileSizes.size() - 1);
+ }
+
+ /**
+ * Derive the consensus annotations to be added to the alignment for display.
+ * This does not recompute the raw data, but may be called on a change in
+ * display options, such as 'ignore gaps', which may in turn result in a
+ * change in the derived values.
+ *
+ * @param consensus
+ * the annotation row to add annotations to
+ * @param profiles
+ * the source consensus data
+ * @param iStart
+ * start column
+ * @param width
+ * end column
+ * @param ignoreGaps
+ * if true, normalise residue percentages ignoring gaps
+ * @param showSequenceLogo
+ * if true include all consensus symbols, else just show modal
+ * residue
+ * @param nseq
+ * number of sequences
+ */
+ public static void completeConsensus(AlignmentAnnotation consensus,
+ ProfileI[] profiles, int iStart, int width, boolean ignoreGaps,
+ boolean showSequenceLogo, long nseq)
+ {
+ // long now = System.currentTimeMillis();
+ if (consensus == null || consensus.annotations == null
+ || consensus.annotations.length < width)
+ {
+ /*
+ * called with a bad alignment annotation row
+ * wait for it to be initialised properly
+ */
+ return;
+ }
+
+ final int dp = getPercentageDp(nseq);
+
+ for (int i = iStart; i < width; i++)
+ {
+ ProfileI profile;
+ if (i >= profiles.length || ((profile = profiles[i]) == null))
+ {
+ /*
+ * happens if sequences calculated over were
+ * shorter than alignment width
+ */
+ consensus.annotations[i] = null;
+ continue;
+ }
+
+ float value = profile.getPercentageIdentity(ignoreGaps);
+
+ String description = getTooltip(profile, value, showSequenceLogo,
+ ignoreGaps, dp);
+
+ String modalResidue = profile.getModalResidue();
+ if ("".equals(modalResidue))
+ {
+ modalResidue = "-";
+ }
+ else if (modalResidue.length() > 1)
+ {
+ modalResidue = "+";
+ }
+ consensus.annotations[i] = new Annotation(modalResidue,
+ description, ' ', value);
+ }
+ // long elapsed = System.currentTimeMillis() - now;
+ // System.out.println(-elapsed);
+ }
+
+ /**
+ * Returns a tooltip showing either
+ * <ul>
+ * <li>the full profile (percentages of all residues present), if
+ * showSequenceLogo is true, or</li>
+ * <li>just the modal (most common) residue(s), if showSequenceLogo is false</li>
+ * </ul>
+ * Percentages are as a fraction of all sequence, or only ungapped sequences
+ * if ignoreGaps is true.
+ *
+ * @param profile
+ * @param pid
+ * @param showSequenceLogo
+ * @param ignoreGaps
+ * @param dp
+ * the number of decimal places to format percentages to
+ * @return
+ */
+ static String getTooltip(ProfileI profile, float pid,
+ boolean showSequenceLogo, boolean ignoreGaps, int dp)
+ {
+ ResidueCount counts = profile.getCounts();
+
+ String description = null;
+ if (counts != null && showSequenceLogo)
+ {
+ int normaliseBy = ignoreGaps ? profile.getNonGapped() : profile
+ .getHeight();
+ description = counts.getTooltip(normaliseBy, dp);
+ }
+ else
+ {
+ StringBuilder sb = new StringBuilder(64);
+ String maxRes = profile.getModalResidue();
+ if (maxRes.length() > 1)
+ {
+ sb.append("[").append(maxRes).append("]");
+ }
+ else
+ {
+ sb.append(maxRes);
+ }
+ if (maxRes.length() > 0)
+ {
+ sb.append(" ");
+ Format.appendPercentage(sb, pid, dp);
+ sb.append("%");
+ }
+ description = sb.toString();
+ }
+ return description;
+ }
+
+ /**
+ * Returns the sorted profile for the given consensus data. The returned array
+ * contains
+ *
+ * <pre>
+ * [profileType, numberOfValues, nonGapCount, charValue1, percentage1, charValue2, percentage2, ...]
+ * in descending order of percentage value
+ * </pre>
+ *
+ * @param profile
+ * the data object from which to extract and sort values
+ * @param ignoreGaps
+ * if true, only non-gapped values are included in percentage
+ * calculations
+ * @return
+ */
+ public static int[] extractProfile(ProfileI profile,
+ boolean ignoreGaps)
+ {
+ int[] rtnval = new int[64];
+ ResidueCount counts = profile.getCounts();
+ if (counts == null)
+ {
+ return null;
+ }
+
+ SymbolCounts symbolCounts = counts.getSymbolCounts();
+ char[] symbols = symbolCounts.symbols;
+ int[] values = symbolCounts.values;
+ QuickSort.sort(values, symbols);
+ int nextArrayPos = 2;
+ int totalPercentage = 0;
+ final int divisor = ignoreGaps ? profile.getNonGapped() : profile
+ .getHeight();