JAL-2480 clarify that findOverlaps excludes spanning contact features
[jalview.git] / src / jalview / datamodel / Sequence.java
index 96747e4..cdfde5c 100755 (executable)
@@ -44,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
 {
@@ -723,7 +720,10 @@ public class Sequence extends ASequence implements SequenceI
    */
   protected void updateCursor(int residuePos, int column, int startColumn)
   {
-    int endColumn = cursor == null ? 0 : cursor.lastColumnPosition;
+    /*
+     * preserve end residue column provided cursor was valid
+     */
+    int endColumn = isValidCursor(cursor) ? cursor.lastResidueColumn : 0;
     if (residuePos == this.end)
     {
       endColumn = column;
@@ -760,8 +760,7 @@ 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;
 
@@ -779,7 +778,7 @@ public class Sequence extends ASequence implements SequenceI
     }
 
     col++; // convert back to base 1
-    updateCursor(pos, col, curs.firstColumnPosition);
+    updateCursor(pos, col, curs.firstResidueColumn);
 
     return col;
   }
@@ -788,6 +787,53 @@ public class Sequence extends ASequence implements SequenceI
    * {@inheritDoc}
    */
   @Override
+  public Range findPositions(int fromColumn, int toColumn)
+  {
+    if (toColumn < fromColumn || fromColumn < 1)
+    {
+      return null;
+    }
+
+    /*
+     * find the first non-gapped position, if any
+     */
+    int firstPosition = 0;
+    int col = fromColumn - 1;
+    int length = sequence.length;
+    while (col < length && col < toColumn)
+    {
+      if (!Comparison.isGap(sequence[col]))
+      {
+        firstPosition = findPosition(col++);
+        break;
+      }
+      col++;
+    }
+
+    if (firstPosition == 0)
+    {
+      return null;
+    }
+
+    /*
+     * find the last non-gapped position
+     */
+    int lastPosition = firstPosition;
+    while (col < length && col < toColumn)
+    {
+      if (!Comparison.isGap(sequence[col++]))
+      {
+        lastPosition++;
+      }
+    }
+
+    return new Range(firstPosition, lastPosition);
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
   public int findPosition(final int column)
   {
     /*
@@ -910,7 +956,7 @@ public class Sequence extends ASequence implements SequenceI
       return curs.residuePosition; // easy case :-)
     }
 
-    if (curs.lastColumnPosition > 0 && curs.lastColumnPosition < col)
+    if (curs.lastResidueColumn > 0 && curs.lastResidueColumn < col)
     {
       /*
        * sequence lies entirely to the left of col
@@ -919,7 +965,7 @@ public class Sequence extends ASequence implements SequenceI
       return end + 1;
     }
 
-    if (curs.firstColumnPosition > 0 && curs.firstColumnPosition > col)
+    if (curs.firstResidueColumn > 0 && curs.firstResidueColumn > col)
     {
       /*
        * sequence lies entirely to the right of col
@@ -934,7 +980,7 @@ public class Sequence extends ASequence implements SequenceI
     /*
      * move left or right to find pos from cursor position
      */
-    int firstResidueColumn = curs.firstColumnPosition;
+    int firstResidueColumn = curs.firstResidueColumn;
     int column = curs.columnPosition - 1; // to base 0
     int newPos = curs.residuePosition;
     int delta = curs.columnPosition > col ? -1 : 1;
@@ -1112,9 +1158,9 @@ public class Sequence extends ASequence implements SequenceI
     // the very large sequence case
     int eindex = -1, sindex = -1;
     boolean ecalc = false, scalc = false;
-    for (int s = i; s < j; s++)
+    for (int s = i; s < j && s < sequence.length; s++)
     {
-      if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
+      if (!Comparison.isGap(sequence[s]))
       {
         if (createNewDs)
         {
@@ -1765,12 +1811,7 @@ public class Sequence extends ASequence implements SequenceI
   {
     int startPos = findPosition(fromColumn - 1); // convert base 1 to base 0
     int endPos = findPosition(toColumn - 1);
-    // to trace / debug behaviour:
-    // System.out
-    // .println(String
-    // .format("%s.findFeatures columns [%d-%d] positions [%d-%d] leaves cursor %s",
-    // getName(), fromColumn, toColumn, startPos,
-    // endPos, cursor));
+
     List<SequenceFeature> result = new ArrayList<>();
     if (datasetSequence != null)
     {
@@ -1785,7 +1826,7 @@ public class Sequence extends ASequence implements SequenceI
     /*
      * if the start or end column is gapped, startPos or endPos may be to the 
      * left or right, and we may have included adjacent or enclosing features;
-     * remove any that are not enclosing, non-contact features
+     * remove any that are not enclosing features
      */
     if (endPos > this.end || Comparison.isGap(sequence[fromColumn - 1])
             || Comparison.isGap(sequence[toColumn - 1]))
@@ -1796,18 +1837,8 @@ public class Sequence extends ASequence implements SequenceI
         SequenceFeature sf = it.next();
         int featureStartColumn = findIndex(sf.getBegin());
         int featureEndColumn = findIndex(sf.getEnd());
-        boolean noOverlap = featureStartColumn > toColumn
-                        || featureEndColumn < fromColumn;
-
-        /*
-         * reject an 'enclosing' feature if it is actually a contact feature
-         */
-        if (sf.isContactFeature() && featureStartColumn < fromColumn
-                && featureEndColumn > toColumn)
-        {
-          noOverlap = true;
-        }
-        if (noOverlap)
+        if (featureStartColumn > toColumn
+                        || featureEndColumn < fromColumn)
         {
           it.remove();
         }