JAL-2629 tidy tests, refactor hmmer node mapping slightly
[jalview.git] / src / jalview / datamodel / Sequence.java
index 441d8d0..e798525 100755 (executable)
@@ -61,6 +61,12 @@ public class Sequence extends ASequence implements SequenceI
 
   int end;
 
+  boolean hasInfo;
+
+  HiddenMarkovModel hmm;
+
+  boolean isHMMConsensusSequence = false;
+
   Vector<PDBEntry> pdbIds;
 
   String vamsasId;
@@ -337,6 +343,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 +423,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 +459,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 +1166,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 +1477,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 +1644,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,12 +1778,13 @@ 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)
       {
-        if (ann.calcId != null && ann.calcId.equals(calcId)
+        String id = ann.getCalcId();
+        if (id != null && id.equals(calcId)
                 && ann.label != null && ann.label.equals(label))
         {
           result.add(ann);
@@ -1819,7 +1835,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 +1882,87 @@ 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()
+  {
+    if (hmm == null)
+    {
+      return;
+    }
+    hmm.updateMapping(sequence);
+  }
+
+  /**
+   * Maps the HMM sequence to the reference annotation.
+   * 
+   * @param rf
+   */
+  @Override
+  public void mapToReference(AlignmentAnnotation rf)
+  {
+    if (this.isHMMConsensusSequence)
+    {
+      int node = 1;
+      hmm.clearNodeLookup();
+      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;
+  }
+
   /**
    * {@inheritDoc}
    */