JAL-3706 handle e.g. mapped exon feature extending beyond mapped range
[jalview.git] / src / jalview / datamodel / MappedFeatures.java
index d652a97..2bc82bc 100644 (file)
@@ -276,7 +276,8 @@ public class MappedFeatures
 
   /**
    * Answers the mapped ranges (as one or more [start, end] positions) which
-   * correspond to the given [begin, end] range of the linked sequence.
+   * correspond to the given [begin, end] range of (some feature on) the linked
+   * sequence.
    * 
    * <pre>
    * Example: MappedFeatures with CDS features mapped to peptide 
@@ -293,9 +294,36 @@ public class MappedFeatures
    */
   public int[] getMappedPositions(int begin, int end)
   {
+    int[] result = null;
     MapList map = mapping.getMap();
-    return mapping.to == featureSequence ? map.locateInFrom(begin, end)
-            : map.locateInTo(begin, end);
+    
+    /*
+     * mapping may be in either direction, so handle either case;
+     * limit feature extent to the range of the mapping if it is greater
+     * (e.g. an overlapping exon); if that still fails, try reducing by
+     * 3 positions (to omit a mapped stop codon)
+     */
+    if (mapping.to == featureSequence)
+    {
+      begin = Math.max(begin, map.getToLowest());
+      end = Math.min(end,  map.getToHighest());
+      result = map.locateInFrom(begin, end);
+      if (result == null)
+      {
+        result = map.locateInFrom(begin, end-3);
+      }
+    }
+    else
+    {
+      begin = Math.max(begin, map.getFromLowest());
+      end = Math.min(end,  map.getFromHighest());
+      result = map.locateInTo(begin, end);
+      if (result == null)
+      {
+        result = map.locateInTo(begin, end-3);
+      }
+    }
+    return result;
   }
 
   /**