JAL-2629 add basic parameter adjustment to hmmsearch/align
[jalview.git] / src / jalview / datamodel / Sequence.java
index b0faf21..701ad7d 100755 (executable)
@@ -29,6 +29,7 @@ import jalview.util.StringUtils;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.BitSet;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
@@ -51,12 +52,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;
@@ -78,7 +87,9 @@ public class Sequence extends ASequence implements SequenceI
    */
   int index = -1;
 
-  /** array of sequence features - may not be null for a valid sequence object */
+  /**
+   * array of sequence features - may not be null for a valid sequence object
+   */
   public SequenceFeature[] sequenceFeatures;
 
   /**
@@ -133,8 +144,8 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (name == null)
     {
-      System.err
-              .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
+      System.err.println(
+              "POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
       name = "";
     }
     // Does sequence have the /start-end signature?
@@ -290,6 +301,10 @@ public class Sequence extends ASequence implements SequenceI
         this.addPDBId(new PDBEntry(pdb));
       }
     }
+    if (seq.getHMM() != null)
+    {
+      this.hmm = new HiddenMarkovModel(seq.getHMM());
+    }
   }
 
   @Override
@@ -307,7 +322,7 @@ public class Sequence extends ASequence implements SequenceI
       {
         new Exception(
                 "Warning: JAL-2046 side effect ? Possible implementation error: overwriting dataset sequence features by setting sequence features on alignment")
-                .printStackTrace();
+                        .printStackTrace();
       }
       datasetSequence.setSequenceFeatures(features);
     }
@@ -333,7 +348,8 @@ public class Sequence extends ASequence implements SequenceI
       }
     }
 
-    SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
+    SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length
+            + 1];
     System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
     temp[sequenceFeatures.length] = sf;
 
@@ -416,7 +432,7 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (pdbIds == null)
     {
-      pdbIds = new Vector<PDBEntry>();
+      pdbIds = new Vector<>();
       pdbIds.add(entry);
       return true;
     }
@@ -787,7 +803,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;
@@ -819,6 +835,40 @@ public class Sequence extends ASequence implements SequenceI
   }
 
   @Override
+  public BitSet getInsertionsAsBits()
+  {
+    BitSet map = new BitSet();
+    int lastj = -1, j = 0;
+    int pos = start;
+    int seqlen = sequence.length;
+    while ((j < seqlen))
+    {
+      if (jalview.util.Comparison.isGap(sequence[j]))
+      {
+        if (lastj == -1)
+        {
+          lastj = j;
+        }
+      }
+      else
+      {
+        if (lastj != -1)
+        {
+          map.set(lastj, j);
+          lastj = -1;
+        }
+      }
+      j++;
+    }
+    if (lastj != -1)
+    {
+      map.set(lastj, j);
+      lastj = -1;
+    }
+    return map;
+  }
+
+  @Override
   public void deleteChars(int i, int j)
   {
     int newstart = start, newend = end;
@@ -1033,8 +1083,9 @@ public class Sequence extends ASequence implements SequenceI
   @Override
   public AlignmentAnnotation[] getAnnotation()
   {
-    return annotation == null ? null : annotation
-            .toArray(new AlignmentAnnotation[annotation.size()]);
+    return annotation == null ? null
+            : annotation
+                    .toArray(new AlignmentAnnotation[annotation.size()]);
   }
 
   @Override
@@ -1048,7 +1099,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))
     {
@@ -1146,8 +1197,9 @@ public class Sequence extends ASequence implements SequenceI
   {
     if (datasetSequence == null)
     {
-      Sequence dsseq = new Sequence(getName(), AlignSeq.extractGaps(
-              jalview.util.Comparison.GapChars, getSequenceAsString()),
+      Sequence dsseq = new Sequence(getName(),
+              AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
+                      getSequenceAsString()),
               getStart(), getEnd());
 
       datasetSequence = dsseq;
@@ -1290,7 +1342,8 @@ public class Sequence extends ASequence implements SequenceI
       for (int si = 0; si < sfs.length; si++)
       {
         SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
-                : new SequenceFeature[] { new SequenceFeature(sfs[si]) };
+                : new SequenceFeature[]
+                { new SequenceFeature(sfs[si]) };
         if (sf != null && sf.length > 0)
         {
           for (int sfi = 0; sfi < sf.length; sfi++)
@@ -1372,7 +1425,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)
@@ -1428,7 +1481,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)
       {
@@ -1446,8 +1499,8 @@ public class Sequence extends ASequence implements SequenceI
           }
         }
         // whilst it looks like it is a primary ref, we also sanity check type
-        if (DBRefUtils.getCanonicalName(DBRefSource.PDB).equals(
-                DBRefUtils.getCanonicalName(ref.getSource())))
+        if (DBRefUtils.getCanonicalName(DBRefSource.PDB)
+                .equals(DBRefUtils.getCanonicalName(ref.getSource())))
         {
           // PDB dbrefs imply there should be a PDBEntry associated
           // TODO: tighten PDB dbrefs
@@ -1475,4 +1528,89 @@ 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;
+    for (char residue : sequence)
+    {
+      if (!Comparison.isGap(residue))
+      {
+        hmm.setAlignmentColumn(node, column);
+        hmm.getNodeLookup().put(column, node);
+        node++;
+      }
+      else
+      {
+        hmm.getNodeLookup().remove(column);
+      }
+      column++;
+    }
+
+  }
+
+  @Override
+  public boolean isHMMConsensusSequence()
+  {
+    return isHMMConsensusSequence;
+  }
+
+  @Override
+  public void setIsHMMConsensusSequence(boolean isHMMConsensusSequence)
+  {
+    this.isHMMConsensusSequence = isHMMConsensusSequence;
+  }
+
+  @Override
+  public boolean hasHMMAnnotation()
+  {
+    return hasInfo;
+    /*
+    if (annotation == null)
+    {
+      return false;
+    }
+    
+    for (AlignmentAnnotation annot : annotation)
+    {
+      if (annot.label.contains("_HMM"))
+      {
+        return true;
+      }
+    }
+    return false;
+    */
+  }
+
+  @Override
+  public void setHasInfo(boolean status)
+  {
+    hasInfo = true;
+  }
+
+  @Override
+  public int getPreviousPosition()
+  {
+    return previousPosition;
+  }
+
+  @Override
+  public void setPreviousPosition(int previousPosition)
+  {
+    this.previousPosition = previousPosition;
+  }
+
 }