From: tzvanaalten Date: Mon, 24 Jul 2017 14:46:18 +0000 (+0100) Subject: JAL-2616 add placeholder sequence for HMM, does not yet align properly X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=4c5919d5393ad7fb00139b38cea93d1c270afa84;p=jalview.git JAL-2616 add placeholder sequence for HMM, does not yet align properly --- diff --git a/src/jalview/datamodel/HiddenMarkovModel.java b/src/jalview/datamodel/HiddenMarkovModel.java index 0dcc72e..879968f 100644 --- a/src/jalview/datamodel/HiddenMarkovModel.java +++ b/src/jalview/datamodel/HiddenMarkovModel.java @@ -987,5 +987,72 @@ public class HiddenMarkovModel return informationContent; } + /** + * 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 length) + { + int start; + int end; + int modelLength; + start = getNodeAlignmentColumn(1); + modelLength = getLength(); + end = getNodeAlignmentColumn(modelLength); + char[] sequence = new char[length]; + for (int index = 0; index < length; index++) + { + Character character; + if (consensusResidueIsActive()) + { + character = getConsensusAtAlignColumn(index); + } + else + { + character = findConsensusCharacter(index); + } + if (character == null || character == '-') + { + sequence[index] = '-'; + } + else + { + sequence[index] = Character.toUpperCase(character); + } + } + + + Sequence seq = new Sequence("HMM CONSENSUS", sequence, start, end); + return seq; + } + + /** + * Finds the most probable character at a column in an alignment based on the + * HMM. + * + * @param nodeIndex + * The index of the node. + * @return + */ + Character findConsensusCharacter(int column) + { + Character mostLikely = null; + double highestProb = 0; + for (char character : symbols) + { + Double prob = getMatchEmissionProbability(column, character); + if (prob > highestProb) + { + highestProb = prob; + mostLikely = character; + } + } + return mostLikely; + } } diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 74e7652..9e4f37b 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -4673,6 +4673,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, getViewport().getAlignment().getWidth()); getViewport().getAlignment().addAnnotation(annotation); annotation.setHMM(hmm); + int length = getViewport().getAlignment().getWidth(); + Sequence consensus = hmm.getConsensusSequence(length); + SequenceI[] consensusArr = new Sequence[] { consensus }; + AlignmentI newAlignment = new Alignment(consensusArr); + newAlignment.append(getViewport().getAlignment()); + getViewport().setAlignment(newAlignment); isAnnotation = true; }