+
+ /**
+ * Returns the sorted HMM profile for the given column of the alignment. The
+ * returned array contains
+ *
+ * <pre>
+ * [profileType=0, numberOfValues, 100, charValue1, percentage1, charValue2, percentage2, ...]
+ * in descending order of percentage value
+ * </pre>
+ *
+ * @param hmm
+ * @param column
+ * @param removeBelowBackground
+ * if true, ignores residues with probability less than their
+ * background frequency
+ * @param infoHeight
+ * if true, uses the log ratio 'information' measure to scale the
+ * value
+ * @return
+ */
+ public static int[] extractHMMProfile(HiddenMarkovModel hmm, int column,
+ boolean removeBelowBackground, boolean infoHeight)
+ {
+ if (hmm == null)
+ {
+ return null;
+ }
+ String alphabet = hmm.getSymbols();
+ int size = alphabet.length();
+ char symbols[] = new char[size];
+ int values[] = new int[size];
+ int totalCount = 0;
+
+ for (int i = 0; i < size; i++)
+ {
+ char symbol = alphabet.charAt(i);
+ symbols[i] = symbol;
+ int value = getAnalogueCount(hmm, column, symbol,
+ removeBelowBackground, infoHeight);
+ values[i] = value;
+ totalCount += value;
+ }
+
+ /*
+ * sort symbols by increasing emission probability
+ */
+ QuickSort.sort(values, symbols);
+
+ int[] profile = new int[3 + size * 2];
+
+ profile[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
+ profile[1] = size;
+ profile[2] = 100;
+
+ /*
+ * order symbol/count profile by decreasing emission probability
+ */
+ if (totalCount != 0)
+ {
+ int arrayPos = 3;
+ for (int k = size - 1; k >= 0; k--)
+ {
+ Float percentage;
+ int value = values[k];
+ if (removeBelowBackground)
+ {
+ percentage = ((float) value) / totalCount * 100f;
+ }
+ else
+ {
+ percentage = value / 100f;
+ }
+ int intPercent = Math.round(percentage);
+ profile[arrayPos] = symbols[k];
+ profile[arrayPos + 1] = intPercent;
+ arrayPos += 2;
+ }
+ }
+ return profile;
+ }
+
+ /**
+ * Converts the emission probability of a residue at a column in the alignment
+ * to a 'count', suitable for rendering as an annotation value
+ *
+ * @param hmm
+ * @param column
+ * @param symbol
+ * @param removeBelowBackground
+ * if true, returns 0 for any symbol with a match emission
+ * probability less than the background frequency
+ * @infoHeight if true, uses the log ratio 'information content' to scale the
+ * value
+ * @return
+ */
+ static int getAnalogueCount(HiddenMarkovModel hmm, int column,
+ char symbol, boolean removeBelowBackground, boolean infoHeight)
+ {
+ double value = hmm.getMatchEmissionProbability(column, symbol);
+ double freq = ResidueProperties.backgroundFrequencies
+ .get(hmm.getAlphabetType()).get(symbol);
+ if (value < freq && removeBelowBackground)
+ {
+ return 0;
+ }
+
+ if (infoHeight)
+ {
+ value = value * (Math.log(value / freq) / LOG2);
+ }
+
+ value = value * 10000d;
+ return Math.round((float) value);
+ }