JAL-3725 helper methods for computing mapped feature range overlap
[jalview.git] / src / jalview / util / MapList.java
index 3555e52..8efe42b 100644 (file)
@@ -25,6 +25,8 @@ import java.util.Arrays;
 import java.util.BitSet;
 import java.util.List;
 
+import jalview.bin.Cache;
+
 /**
  * A simple way of bijectively mapping a non-contiguous linear range to another
  * non-contiguous linear range.
@@ -307,7 +309,7 @@ public class MapList
       if (range.length != 2)
       {
         // throw new IllegalArgumentException(range);
-        System.err.println("Invalid format for fromRange "
+        Cache.log.error("Invalid format for fromRange "
                 + Arrays.toString(range) + " may cause errors");
       }
       fromLowest = Math.min(fromLowest, Math.min(range[0], range[1]));
@@ -321,7 +323,7 @@ public class MapList
       if (range.length != 2)
       {
         // throw new IllegalArgumentException(range);
-        System.err.println("Invalid format for toRange "
+        Cache.log.error("Invalid format for toRange "
                 + Arrays.toString(range) + " may cause errors");
       }
       toLowest = Math.min(toLowest, Math.min(range[0], range[1]));
@@ -467,7 +469,8 @@ public class MapList
     int mp[][] = new int[to - from + 2][];
     for (int i = 0; i < mp.length; i++)
     {
-      int[] m = shift(i + from, shiftTo, sourceRatio, shiftFrom, targetRatio);
+      int[] m = shift(i + from, shiftTo, sourceRatio, shiftFrom,
+              targetRatio);
       if (m != null)
       {
         if (i == 0)
@@ -1144,10 +1147,11 @@ public class MapList
   }
 
   /**
-   * Returns the [start1, end1, start2, end2, ...] positions in the 'from' range
-   * that map to positions between {@code start} and {@code end} in the 'to'
-   * range. Note that for a reverse strand mapping this will return ranges with
-   * end < start. Returns null if no mapped positions are found in start-end.
+   * <<<<<<< HEAD Returns the [start1, end1, start2, end2, ...] positions in the
+   * 'from' range that map to positions between {@code start} and {@code end} in
+   * the 'to' range. Note that for a reverse strand mapping this will return
+   * ranges with end < start. Returns null if no mapped positions are found in
+   * start-end.
    * 
    * @param start
    * @param end
@@ -1155,8 +1159,8 @@ public class MapList
    */
   public int[] locateInFrom(int start, int end)
   {
-    return mapPositions(start, end, toShifts, fromShifts,
-            toRatio, fromRatio);
+    return mapPositions(start, end, toShifts, fromShifts, toRatio,
+            fromRatio);
   }
 
   /**
@@ -1171,8 +1175,8 @@ public class MapList
    */
   public int[] locateInTo(int start, int end)
   {
-    return mapPositions(start, end, fromShifts, toShifts,
-            fromRatio, toRatio);
+    return mapPositions(start, end, fromShifts, toShifts, fromRatio,
+            toRatio);
   }
 
   /**
@@ -1250,7 +1254,8 @@ public class MapList
    * @return
    */
   protected final static BitSet getMappedOffsetsForPositions(int start,
-          int end, List<int[]> sourceRange, int sourceWordLength, int targetWordLength)
+          int end, List<int[]> sourceRange, int sourceWordLength,
+          int targetWordLength)
   {
     BitSet overlaps = new BitSet();
     int offset = 0;
@@ -1419,4 +1424,36 @@ public class MapList
 
     return added;
   }
+
+  /*
+   * Returns the [start, end...] positions in the range mapped from, that are
+   * mapped to by part or all of the given begin-end of the range mapped to.
+   * Returns null if begin-end does not overlap any position mapped to.
+   * 
+   * @param begin
+   * @param end
+   * @return
+   */
+  public int[] getOverlapsInFrom(final int begin, final int end)
+  {
+    int[] overlaps = MappingUtils.findOverlap(toShifts, begin, end);
+
+    return overlaps == null ? null : locateInFrom(overlaps[0], overlaps[1]);
+  }
+
+  /**
+   * Returns the [start, end...] positions in the range mapped to, that are
+   * mapped to by part or all of the given begin-end of the range mapped from.
+   * Returns null if begin-end does not overlap any position mapped from.
+   * 
+   * @param begin
+   * @param end
+   * @return
+   */
+  public int[] getOverlapsInTo(final int begin, final int end)
+  {
+    int[] overlaps = MappingUtils.findOverlap(fromShifts, begin, end);
+
+    return overlaps == null ? null : locateInTo(overlaps[0], overlaps[1]);
+  }
 }