Merge branch 'develop' into features/mchmmer
[jalview.git] / src / jalview / datamodel / Sequence.java
index 441d8d0..7f200da 100755 (executable)
@@ -55,12 +55,20 @@ public class Sequence extends ASequence implements SequenceI
 
   private char[] sequence;
 
+  int previousPosition;
+
   String description;
 
   int start;
 
   int end;
 
+  boolean hasInfo;
+
+  HiddenMarkovModel hmm;
+
+  boolean isHMMConsensusSequence = false;
+
   Vector<PDBEntry> pdbIds;
 
   String vamsasId;
@@ -337,6 +345,15 @@ public class Sequence extends ASequence implements SequenceI
         this.addPDBId(new PDBEntry(pdb));
       }
     }
+    if (seq.isHMMConsensusSequence())
+    {
+      this.isHMMConsensusSequence = true;
+    }
+    if (seq.getHMM() != null)
+    {
+      this.hmm = new HiddenMarkovModel(seq.getHMM());
+    }
+
   }
 
   @Override
@@ -408,7 +425,7 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (pdbIds == null)
     {
-      pdbIds = new Vector<PDBEntry>();
+      pdbIds = new Vector<>();
       pdbIds.add(entry);
       return true;
     }
@@ -444,7 +461,7 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public Vector<PDBEntry> getAllPDBEntries()
   {
-    return pdbIds == null ? new Vector<PDBEntry>() : pdbIds;
+    return pdbIds == null ? new Vector<>() : pdbIds;
   }
 
   /**
@@ -1151,7 +1168,7 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public List<int[]> getInsertions()
   {
-    ArrayList<int[]> map = new ArrayList<int[]>();
+    ArrayList<int[]> map = new ArrayList<>();
     int lastj = -1, j = 0;
     int pos = start;
     int seqlen = sequence.length;
@@ -1462,7 +1479,7 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (this.annotation == null)
     {
-      this.annotation = new Vector<AlignmentAnnotation>();
+      this.annotation = new Vector<>();
     }
     if (!this.annotation.contains(annotation))
     {
@@ -1629,7 +1646,7 @@ public class Sequence extends ASequence implements SequenceI
       return null;
     }
 
-    Vector<AlignmentAnnotation> subset = new Vector<AlignmentAnnotation>();
+    Vector<AlignmentAnnotation> subset = new Vector<>();
     Enumeration<AlignmentAnnotation> e = annotation.elements();
     while (e.hasMoreElements())
     {
@@ -1763,7 +1780,7 @@ public class Sequence extends ASequence implements SequenceI
   public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
           String label)
   {
-    List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
+    List<AlignmentAnnotation> result = new ArrayList<>();
     if (this.annotation != null)
     {
       for (AlignmentAnnotation ann : annotation)
@@ -1819,7 +1836,7 @@ public class Sequence extends ASequence implements SequenceI
     }
     synchronized (dbrefs)
     {
-      List<DBRefEntry> primaries = new ArrayList<DBRefEntry>();
+      List<DBRefEntry> primaries = new ArrayList<>();
       DBRefEntry[] tmp = new DBRefEntry[1];
       for (DBRefEntry ref : dbrefs)
       {
@@ -1866,6 +1883,106 @@ public class Sequence extends ASequence implements SequenceI
     }
   }
 
+  @Override
+  public HiddenMarkovModel getHMM()
+  {
+    return hmm;
+  }
+
+  @Override
+  public void setHMM(HiddenMarkovModel hmm)
+  {
+    this.hmm = hmm;
+  }
+
+  @Override
+  public void updateHMMMapping()
+  {
+    int node = 1;
+    int column = 0;
+    hmm.emptyNodeLookup();
+    for (char residue : sequence)
+    {
+      if (!Comparison.isGap(residue))
+      {
+        hmm.setAlignmentColumn(node, column);
+        node++;
+      }
+      column++;
+    }
+  }
+
+  /**
+   * Maps the HMM sequence to the reference annotation.
+   * 
+   * @param rf
+   */
+  @Override
+  public void mapToReference(AlignmentAnnotation rf)
+  {
+    if (this.isHMMConsensusSequence)
+    {
+      int node = 1;
+      hmm.emptyNodeLookup();
+      for (int i = 0; i < getLength(); i++)
+      {
+        if (rf.annotations[i].displayCharacter.equals("x")
+                || rf.annotations[i].displayCharacter.equals("X"))
+        {
+          if (i < hmm.getNodeAlignmentColumn(node))
+          {
+            this.deleteChars(i, hmm.getNodeAlignmentColumn(node));
+            updateHMMMapping();
+          }
+          else if (i > hmm.getNodeAlignmentColumn(node))
+          {
+            int length = i - hmm.getNodeAlignmentColumn(node);
+            this.insertCharAt(hmm.getNodeAlignmentColumn(node), length,
+                    '-');
+            updateHMMMapping();
+          }
+          node++;
+        }
+      }
+    }
+  }
+
+  @Override
+  public boolean isHMMConsensusSequence()
+  {
+    return isHMMConsensusSequence;
+  }
+
+  @Override
+  public void setIsHMMConsensusSequence(boolean isHMMConsensusSequence)
+  {
+    this.isHMMConsensusSequence = isHMMConsensusSequence;
+  }
+
+  @Override
+  public boolean hasHMMAnnotation()
+  {
+    return hasInfo;
+  }
+
+  @Override
+  public void setHasInfo(boolean status)
+  {
+    hasInfo = true;
+  }
+
+  @Override
+  public int getPreviousPosition()
+  {
+    return previousPosition;
+  }
+
+  @Override
+  public void setPreviousPosition(int previousPosition)
+  {
+    this.previousPosition = previousPosition;
+  }
+
   /**
    * {@inheritDoc}
    */