JAL-2526 Sequence.findPositions to get residue positions for column
[jalview.git] / src / jalview / datamodel / Sequence.java
index d7ac7a7..dd7c24a 100755 (executable)
@@ -662,7 +662,7 @@ public class Sequence extends ASequence implements SequenceI
     // Rely on end being at least as long as the length of the sequence.
     while ((i < sequence.length) && (j <= end) && (j <= pos))
     {
-      if (!jalview.util.Comparison.isGap(sequence[i]))
+      if (!Comparison.isGap(sequence[i]))
       {
         j++;
       }
@@ -688,7 +688,7 @@ public class Sequence extends ASequence implements SequenceI
     int seqlen = sequence.length;
     while ((j < i) && (j < seqlen))
     {
-      if (!jalview.util.Comparison.isGap(sequence[j]))
+      if (!Comparison.isGap(sequence[j]))
       {
         pos++;
       }
@@ -700,6 +700,68 @@ public class Sequence extends ASequence implements SequenceI
   }
 
   /**
+   * {@inheritDoc}
+   */
+  @Override
+  public Range findPositions(int fromCol, int toCol)
+  {
+    /*
+     * count residues before fromCol
+     */
+    int j = 0;
+    int count = 0;
+    int seqlen = sequence.length;
+    while (j < fromCol && j < seqlen)
+    {
+      if (!Comparison.isGap(sequence[j]))
+      {
+        count++;
+      }
+      j++;
+    }
+
+    /*
+     * find first and last residues between fromCol and toCol
+     */
+    int firstPos = 0;
+    int lastPos = 0;
+    int firstPosCol = 0;
+    boolean foundFirst = false;
+    
+    while (j <= toCol && j < seqlen)
+    {
+      if (!Comparison.isGap(sequence[j]))
+      {
+        count++;
+        if (!foundFirst)
+        {
+          firstPos = count;
+          firstPosCol = j;
+          foundFirst = true;
+        }
+        lastPos = count;
+      }
+      j++;
+    }
+
+    if (firstPos == 0)
+    {
+      /*
+       * no residues in this range
+       */
+      return null;
+    }
+
+    /*
+     * adjust for sequence start coordinate
+     */
+    firstPos += start - 1;
+    lastPos += start - 1;
+
+    return new Range(firstPos, lastPos);
+  }
+
+  /**
    * Returns an int array where indices correspond to each residue in the
    * sequence and the element value gives its position in the alignment
    *