Merge branch 'merge/JAL-3285_mchmmer_with_211_develop' into alpha/JAL-3362_Jalview_21...
[jalview.git] / src / jalview / datamodel / Sequence.java
index e798525..efcf6ae 100755 (executable)
@@ -28,12 +28,14 @@ import jalview.util.Comparison;
 import jalview.util.DBRefUtils;
 import jalview.util.MapList;
 import jalview.util.StringUtils;
+import jalview.workers.InformationThread;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Vector;
@@ -42,10 +44,7 @@ import fr.orsay.lri.varna.models.rna.RNA;
 
 /**
  * 
- * Implements the SequenceI interface for a char[] based sequence object.
- * 
- * @author $author$
- * @version $Revision$
+ * Implements the SequenceI interface for a char[] based sequence object
  */
 public class Sequence extends ASequence implements SequenceI
 {
@@ -61,8 +60,6 @@ public class Sequence extends ASequence implements SequenceI
 
   int end;
 
-  boolean hasInfo;
-
   HiddenMarkovModel hmm;
 
   boolean isHMMConsensusSequence = false;
@@ -343,13 +340,9 @@ 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());
+      this.hmm = new HiddenMarkovModel(seq.getHMM(), this);
     }
 
   }
@@ -687,8 +680,8 @@ public class Sequence extends ASequence implements SequenceI
   public void setGeneLoci(String speciesId, String assemblyId,
           String chromosomeId, MapList map)
   {
-    addDBRef(new DBRefEntry(speciesId, assemblyId, DBRefEntry.CHROMOSOME
-            + ":" + chromosomeId, new Mapping(map)));
+    addDBRef(new GeneLocus(speciesId, assemblyId, chromosomeId,
+            new Mapping(map)));
   }
 
   /**
@@ -704,36 +697,9 @@ public class Sequence extends ASequence implements SequenceI
     {
       for (final DBRefEntry ref : refs)
       {
-        if (ref.isChromosome())
+        if (ref instanceof GeneLociI)
         {
-          return new GeneLociI()
-          {
-            @Override
-            public String getSpeciesId()
-            {
-              return ref.getSource();
-            }
-
-            @Override
-            public String getAssemblyId()
-            {
-              return ref.getVersion();
-            }
-
-            @Override
-            public String getChromosomeId()
-            {
-              // strip off "chromosome:" prefix to chrId
-              return ref.getAccessionId().substring(
-                      DBRefEntry.CHROMOSOME.length() + 1);
-            }
-
-            @Override
-            public MapList getMap()
-            {
-              return ref.getMap().getMap();
-            }
-          };
+          return (GeneLociI) ref;
         }
       }
     }
@@ -811,6 +777,7 @@ public class Sequence extends ASequence implements SequenceI
      * preserve end residue column provided cursor was valid
      */
     int endColumn = isValidCursor(cursor) ? cursor.lastColumnPosition : 0;
+
     if (residuePos == this.end)
     {
       endColumn = column;
@@ -829,7 +796,7 @@ public class Sequence extends ASequence implements SequenceI
    * @param curs
    * @return
    */
-  protected int findIndex(int pos, SequenceCursor curs)
+  protected int findIndex(final int pos, SequenceCursor curs)
   {
     if (!isValidCursor(curs))
     {
@@ -847,16 +814,20 @@ public class Sequence extends ASequence implements SequenceI
     /*
      * move left or right to find pos from hint.position
      */
-    int col = curs.columnPosition - 1; // convert from base 1 to 0-based array
-                                       // index
+    int col = curs.columnPosition - 1; // convert from base 1 to base 0
     int newPos = curs.residuePosition;
     int delta = newPos > pos ? -1 : 1;
 
     while (newPos != pos)
     {
       col += delta; // shift one column left or right
-      if (col < 0 || col == sequence.length)
+      if (col < 0)
+      {
+        break;
+      }
+      if (col == sequence.length)
       {
+        col--; // return last column if we failed to reach pos
         break;
       }
       if (!Comparison.isGap(sequence[col]))
@@ -866,7 +837,14 @@ public class Sequence extends ASequence implements SequenceI
     }
 
     col++; // convert back to base 1
-    updateCursor(pos, col, curs.firstColumnPosition);
+
+    /*
+     * only update cursor if we found the target position
+     */
+    if (newPos == pos)
+    {
+      updateCursor(pos, col, curs.firstColumnPosition);
+    }
 
     return col;
   }
@@ -1071,7 +1049,7 @@ public class Sequence extends ASequence implements SequenceI
    * {@inheritDoc}
    */
   @Override
-  public Range findPositions(int fromColumn, int toColumn)
+  public ContiguousI findPositions(int fromColumn, int toColumn)
   {
     if (toColumn < fromColumn || fromColumn < 1)
     {
@@ -1143,6 +1121,27 @@ public class Sequence extends ASequence implements SequenceI
     return map;
   }
 
+  /**
+   * Build a bitset corresponding to sequence gaps
+   * 
+   * @return a BitSet where set values correspond to gaps in the sequence
+   */
+  @Override
+  public BitSet gapBitset()
+  {
+    BitSet gaps = new BitSet(sequence.length);
+    int j = 0;
+    while (j < sequence.length)
+    {
+      if (jalview.util.Comparison.isGap(sequence[j]))
+      {
+        gaps.set(j);
+      }
+      j++;
+    }
+    return gaps;
+  }
+
   @Override
   public int[] findPositionMap()
   {
@@ -1244,12 +1243,13 @@ public class Sequence extends ASequence implements SequenceI
     boolean createNewDs = false;
     // TODO: take a (second look) at the dataset creation validation method for
     // the very large sequence case
+
     int startIndex = findIndex(start) - 1;
     int endIndex = findIndex(end) - 1;
     int startDeleteColumn = -1; // for dataset sequence deletions
     int deleteCount = 0;
 
-    for (int s = i; s < j; s++)
+    for (int s = i; s < j && s < sequence.length; s++)
     {
       if (Comparison.isGap(sequence[s]))
       {
@@ -1895,72 +1895,19 @@ public class Sequence extends ASequence implements SequenceI
   }
 
   @Override
-  public void updateHMMMapping()
+  public boolean hasHMMAnnotation()
   {
-    if (hmm == null)
-    {
-      return;
+    if (this.annotation == null) {
+      return false;
     }
-    hmm.updateMapping(sequence);
-  }
-
-  /**
-   * Maps the HMM sequence to the reference annotation.
-   * 
-   * @param rf
-   */
-  @Override
-  public void mapToReference(AlignmentAnnotation rf)
-  {
-    if (this.isHMMConsensusSequence)
+    for (AlignmentAnnotation ann : annotation)
     {
-      int node = 1;
-      hmm.clearNodeLookup();
-      for (int i = 0; i < getLength(); i++)
+      if (InformationThread.HMM_CALC_ID.equals(ann.getCalcId()))
       {
-        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++;
-        }
+        return true;
       }
     }
-  }
-
-  @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;
+    return false;
   }
 
   /**
@@ -1976,6 +1923,15 @@ public class Sequence extends ASequence implements SequenceI
 
     List<SequenceFeature> result = getFeatures().findFeatures(startPos,
             endPos, types);
+    if (datasetSequence != null)
+    {
+      result = datasetSequence.getFeatures().findFeatures(startPos, endPos,
+              types);
+    }
+    else
+    {
+      result = sequenceFeatureStore.findFeatures(startPos, endPos, types);
+    }
 
     /*
      * if end column is gapped, endPos may be to the right, 
@@ -2058,4 +2014,79 @@ public class Sequence extends ASequence implements SequenceI
 
     return count;
   }
+
+  @Override
+  public String getSequenceStringFromIterator(Iterator<int[]> it)
+  {
+    StringBuilder newSequence = new StringBuilder();
+    while (it.hasNext())
+    {
+      int[] block = it.next();
+      if (it.hasNext())
+      {
+        newSequence.append(getSequence(block[0], block[1] + 1));
+      }
+      else
+      {
+        newSequence.append(getSequence(block[0], block[1]));
+      }
+    }
+
+    return newSequence.toString();
+  }
+
+  @Override
+  public int firstResidueOutsideIterator(Iterator<int[]> regions)
+  {
+    int start = 0;
+
+    if (!regions.hasNext())
+    {
+      return findIndex(getStart()) - 1;
+    }
+
+    // Simply walk along the sequence whilst watching for region
+    // boundaries
+    int hideStart = getLength();
+    int hideEnd = -1;
+    boolean foundStart = false;
+
+    // step through the non-gapped positions of the sequence
+    for (int i = getStart(); i <= getEnd() && (!foundStart); i++)
+    {
+      // get alignment position of this residue in the sequence
+      int p = findIndex(i) - 1;
+
+      // update region start/end
+      while (hideEnd < p && regions.hasNext())
+      {
+        int[] region = regions.next();
+        hideStart = region[0];
+        hideEnd = region[1];
+      }
+      if (hideEnd < p)
+      {
+        hideStart = getLength();
+      }
+      // update boundary for sequence
+      if (p < hideStart)
+      {
+        start = p;
+        foundStart = true;
+      }
+    }
+
+    if (foundStart)
+    {
+      return start;
+    }
+    // otherwise, sequence was completely hidden
+    return 0;
+  }
+
+  @Override
+  public boolean hasHMMProfile()
+  {
+    return hmm != null;
+  }
 }