Merge commit 'alpha/update_2_12_for_2_11_2_series_merge^2' into HEAD
[jalview.git] / src / jalview / util / MapList.java
index 745f367..d310991 100644 (file)
@@ -427,8 +427,8 @@ public class MapList
    * @return int[] { from, to pos in range }, int[range.to-range.from+1]
    *         returning mapped position
    */
-  private int[][] posMap(List<int[]> shiftTo, int ratio,
-          List<int[]> shiftFrom, int toRatio)
+  private int[][] posMap(List<int[]> shiftTo, int sourceRatio,
+          List<int[]> shiftFrom, int targetRatio)
   {
     // TODO only used for test - remove??
     int iv = 0, ivSize = shiftTo.size();
@@ -467,7 +467,7 @@ public class MapList
     int mp[][] = new int[to - from + 2][];
     for (int i = 0; i < mp.length; i++)
     {
-      int[] m = shift(i + from, shiftTo, ratio, shiftFrom, toRatio);
+      int[] m = shift(i + from, shiftTo, sourceRatio, shiftFrom, targetRatio);
       if (m != null)
       {
         if (i == 0)
@@ -929,70 +929,15 @@ public class MapList
 
     for (int[] range : map.getFromRanges())
     {
-      addRange(range, fromShifts);
+      MappingUtils.addRange(range, fromShifts);
     }
     for (int[] range : map.getToRanges())
     {
-      addRange(range, toShifts);
+      MappingUtils.addRange(range, toShifts);
     }
   }
 
   /**
-   * Adds the given range to a list of ranges. If the new range just extends
-   * existing ranges, the current endpoint is updated instead.
-   * 
-   * @param range
-   * @param addTo
-   */
-  static void addRange(int[] range, List<int[]> addTo)
-  {
-    /*
-     * list is empty - add to it!
-     */
-    if (addTo.size() == 0)
-    {
-      addTo.add(range);
-      return;
-    }
-
-    int[] last = addTo.get(addTo.size() - 1);
-    boolean lastForward = last[1] >= last[0];
-    boolean newForward = range[1] >= range[0];
-
-    /*
-     * contiguous range in the same direction - just update endpoint
-     */
-    if (lastForward == newForward && last[1] == range[0])
-    {
-      last[1] = range[1];
-      return;
-    }
-
-    /*
-     * next range starts at +1 in forward sense - update endpoint
-     */
-    if (lastForward && newForward && range[0] == last[1] + 1)
-    {
-      last[1] = range[1];
-      return;
-    }
-
-    /*
-     * next range starts at -1 in reverse sense - update endpoint
-     */
-    if (!lastForward && !newForward && range[0] == last[1] - 1)
-    {
-      last[1] = range[1];
-      return;
-    }
-
-    /*
-     * just add the new range
-     */
-    addTo.add(range);
-  }
-
-  /**
    * Returns true if mapping is from forward strand, false if from reverse
    * strand. Result is just based on the first 'from' range that is not a single
    * position. Default is true unless proven to be false. Behaviour is not well
@@ -1244,20 +1189,20 @@ public class MapList
    * 
    * @param start
    * @param end
-   * @param ranges
-   * @param wordLengthFrom
-   * @param wordLengthTo
+   * @param sourceRange
+   * @param sourceWordLength
+   * @param targetWordLength
    * @return
    */
   protected final static BitSet getMappedOffsetsForPositions(int start,
-          int end, List<int[]> ranges, int wordLengthFrom, int wordLengthTo)
+          int end, List<int[]> sourceRange, int sourceWordLength, int targetWordLength)
   {
     BitSet overlaps = new BitSet();
     int offset = 0;
-    final int s1 = ranges.size();
+    final int s1 = sourceRange.size();
     for (int i = 0; i < s1; i++)
     {
-      int[] range = ranges.get(i);
+      int[] range = sourceRange.get(i);
       final int offset1 = offset;
       int overlapStartOffset = -1;
       int overlapEndOffset = -1;
@@ -1300,24 +1245,24 @@ public class MapList
         /*
          * found an overlap
          */
-        if (wordLengthFrom != wordLengthTo)
+        if (sourceWordLength != targetWordLength)
         {
           /*
            * convert any overlap found to whole words in the target range
            * (e.g. treat any partial codon overlap as if the whole codon)
            */
-          overlapStartOffset -= overlapStartOffset % wordLengthFrom;
-          overlapStartOffset = overlapStartOffset / wordLengthFrom
-                  * wordLengthTo;
+          overlapStartOffset -= overlapStartOffset % sourceWordLength;
+          overlapStartOffset = overlapStartOffset / sourceWordLength
+                  * targetWordLength;
 
           /*
            * similar calculation for range end, adding 
            * (wordLength2 - 1) for end of mapped word
            */
-          overlapEndOffset -= overlapEndOffset % wordLengthFrom;
-          overlapEndOffset = overlapEndOffset / wordLengthFrom
-                  * wordLengthTo;
-          overlapEndOffset += wordLengthTo - 1;
+          overlapEndOffset -= overlapEndOffset % sourceWordLength;
+          overlapEndOffset = overlapEndOffset / sourceWordLength
+                  * targetWordLength;
+          overlapEndOffset += targetWordLength - 1;
         }
         overlaps.set(overlapStartOffset, overlapEndOffset + 1);
       }
@@ -1328,15 +1273,15 @@ public class MapList
 
   /**
    * Returns a (possibly empty) list of the [start-end] values (positions) at
-   * offsets in the {@code ranges} list that are marked by 'on' bits in the
+   * offsets in the {@code targetRange} list that are marked by 'on' bits in the
    * {@code offsets} bitset.
    * 
-   * @param ranges
+   * @param targetRange
    * @param offsets
    * @return
    */
   protected final static List<int[]> getPositionsForOffsets(
-          List<int[]> ranges, BitSet offsets)
+          List<int[]> targetRange, BitSet offsets)
   {
     List<int[]> mapped = new ArrayList<>();
     if (offsets.isEmpty())
@@ -1356,10 +1301,10 @@ public class MapList
      */
     final int toAdd = offsets.cardinality();
     int added = 0;
-    final int s2 = ranges.size();
+    final int s2 = targetRange.size();
     for (int i = 0; added < toAdd && i < s2; i++)
     {
-      int[] range = ranges.get(i);
+      int[] range = targetRange.get(i);
       added += addOffsetPositions(mapped, traversed, range, offsets);
       traversed += Math.abs(range[1] - range[0]) + 1;
     }