+
+
+
+ /**
+ * Returns the consensus sequence based on the most probable symbol at each
+ * position. The sequence is adjusted to match the length of the existing
+ * sequence alignment. Gap characters are used as padding.
+ *
+ * @param length
+ * The length of the longest sequence in the existing alignment.
+ * @return
+ */
+ public Sequence getConsensusSequence()
+ {
+ int start;
+ int end;
+ int modelLength;
+ start = getNodeAlignmentColumn(1);
+ modelLength = getLength();
+ end = getNodeAlignmentColumn(modelLength);
+ char[] sequence = new char[end];
+ for (int index = 0; index < end; index++)
+ {
+ Character character;
+
+ character = getConsensusAtAlignColumn(index);
+
+ if (character == null || character == '-')
+ {
+ sequence[index] = '-';
+ }
+ else
+ {
+ sequence[index] = Character.toUpperCase(character);
+ }
+ }
+
+
+ Sequence seq = new Sequence(getName() + "_HMM", sequence, start, end);
+ return seq;
+ }
+
+
+ /**
+ * Maps the nodes of the hidden Markov model to the reference annotation and
+ * then deletes this annotation.
+ */
+ public void mapToReferenceAnnotation(AlignFrame af, SequenceI seq)
+ {
+ AlignmentAnnotation annotArray[] = af.getViewport().getAlignment()
+ .getAlignmentAnnotation();
+
+ AlignmentAnnotation reference = null;
+ for (AlignmentAnnotation annot : annotArray)
+ {
+ if (annot.label.contains("Reference"))
+ {
+ reference = annot;
+ }
+ }
+
+ if (reference == null)
+ {
+ return;
+ }
+
+ mapToReferenceAnnotation(reference, seq);
+ af.getViewport().getAlignment().deleteAnnotation(reference);
+ }
+
+ public void mapToReferenceAnnotation(AlignmentAnnotation reference,
+ SequenceI seq)
+ {
+ HiddenMarkovModel hmm = seq.getHMM();
+ Annotation[] annots = reference.annotations;
+ {
+ int nodeIndex = 0;
+ for (int col = 0; col < annots.length; col++)
+ {
+ String character = annots[col].displayCharacter;
+ if ("x".equals(character) || "X".equals(character))
+ {
+ nodeIndex++;
+ if (nodeIndex < hmm.getNodes().size())
+ {
+ HMMNode node = hmm.getNode(nodeIndex);
+ int alignPos = getNodeAlignmentColumn(nodeIndex);
+ char seqCharacter = seq.getCharAt(alignPos);
+ if (alignPos >= seq.getLength() || col >= seq.getLength())
+ {
+ seq.insertCharAt(seq.getLength(),
+ (alignPos + 1) - seq.getLength(),
+ '-');
+ }
+ seq.getSequence()[alignPos] = '-';
+ seq.getSequence()[col] = seqCharacter;
+ node.setAlignmentColumn(col);
+ hmm.nodeLookup.put(col, nodeIndex);
+ }
+ else
+ {
+ System.out.println(
+ "The reference annotation contains more consensus columns than the hidden Markov model");
+ break;
+ }
+ }
+ else
+ {
+ hmm.nodeLookup.remove(col);
+ }
+ }
+
+ }
+
+ }
+
+ public void mapToReferenceAnnotation(AlignmentAnnotation reference)
+ {
+ Annotation[] annots = reference.annotations;
+ {
+ int nodeIndex = 0;
+ for (int col = 0; col < annots.length; col++)
+ {
+ String character = annots[col].displayCharacter;
+ if ("x".equals(character) || "X".equals(character))
+ {
+ nodeIndex++;
+ if (nodeIndex < nodes.size())
+ {
+ HMMNode node = nodes.get(nodeIndex);
+ node.setAlignmentColumn(col + 1);
+ nodeLookup.put(col, nodeIndex);
+ }
+ else
+ {
+ System.out.println(
+ "The reference annotation contains more consensus columns than the hidden Markov model");
+ break;
+ }
+ }
+ else
+ {
+ nodeLookup.remove(col);
+ }
+ }
+
+ }
+
+ }
+
+ public SequenceI initHMMSequence()
+ {
+ Sequence consensus = getConsensusSequence();
+ consensus.setIsHMMConsensusSequence(true);
+ consensus.setHMM(this);
+ return consensus;
+ }
+
+