// 0. Node 0 contains average emission probabilities for each symbol
List<HMMNode> nodes = new ArrayList<>();
- // contains the HMM node for each alignment column
+ // contains the HMM node for each alignment column, alignment columns start at
+ // index 0;
Map<Integer, Integer> nodeLookup = new HashMap<>();
// contains the symbol index for each symbol
public HiddenMarkovModel(HiddenMarkovModel hmm)
{
super();
- this.fileProperties = hmm.fileProperties;
- this.symbols = hmm.symbols;
- this.nodes = hmm.nodes;
- this.nodeLookup = hmm.nodeLookup;
- this.symbolIndexLookup = hmm.symbolIndexLookup;
+ this.fileProperties = new HashMap<>(hmm.fileProperties);
+ this.symbols = new ArrayList<>(hmm.symbols);
+ this.nodes = new ArrayList<>(hmm.nodes);
+ this.nodeLookup = new HashMap<>(hmm.nodeLookup);
+ this.symbolIndexLookup = new HashMap<>(
+ hmm.symbolIndexLookup);
this.numberOfSymbols = hmm.numberOfSymbols;
this.fileHeader = new String(hmm.fileHeader);
}
return 0d;
}
symbolIndex = symbolIndexLookup.get(symbol);
- if (nodeLookup.containsKey(alignColumn + 1))
+ if (nodeLookup.containsKey(alignColumn))
{
- nodeIndex = nodeLookup.get(alignColumn + 1);
+ nodeIndex = nodeLookup.get(alignColumn);
probability = getNode(nodeIndex).getMatchEmissions().get(symbolIndex);
return probability;
}
return 0d;
}
symbolIndex = symbolIndexLookup.get(symbol);
- if (nodeLookup.containsKey(alignColumn + 1))
+ if (nodeLookup.containsKey(alignColumn))
{
- nodeIndex = nodeLookup.get(alignColumn + 1);
+ nodeIndex = nodeLookup.get(alignColumn);
probability = getNode(nodeIndex).getInsertEmissions()
.get(symbolIndex);
return probability;
int transitionIndex;
int nodeIndex;
Double probability;
- if (nodeLookup.containsKey(alignColumn + 1))
+ if (nodeLookup.containsKey(alignColumn))
{
- nodeIndex = nodeLookup.get(alignColumn + 1);
+ nodeIndex = nodeLookup.get(alignColumn);
probability = getNode(nodeIndex).getStateTransitions()
.get(transition);
return probability;
public Integer getNodeAlignmentColumn(int nodeIndex)
{
Integer value = nodes.get(nodeIndex).getAlignmentColumn();
- return value - 1;
+ return value;
}
/**
public Integer findNodeIndex(int alignmentColumn)
{
Integer index;
- index = nodeLookup.get(alignmentColumn + 1);
+ index = nodeLookup.get(alignmentColumn);
return index;
}
* Maps the nodes of the hidden Markov model to the reference annotation and
* then deletes this annotation.
*/
- public void mapToReferenceAnnotation(AlignFrame af)
+ public void mapToReferenceAnnotation(AlignFrame af, SequenceI seq)
{
AlignmentAnnotation annotArray[] = af.getViewport().getAlignment()
.getAlignmentAnnotation();
return;
}
- mapToReferenceAnnotation(reference);
+ 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;
nodeIndex++;
if (nodeIndex < nodes.size())
{
- nodes.get(nodeIndex).setAlignmentColumn(col + 1);
- nodeLookup.put(col + 1, nodeIndex);
+ HMMNode node = nodes.get(nodeIndex);
+ node.setAlignmentColumn(col + 1);
+ nodeLookup.put(col, nodeIndex);
}
else
{
}
else
{
- nodeLookup.remove(col + 1);
+ nodeLookup.remove(col);
}
}