}
/**
- * Converts a series of [start, end] ranges into an array of individual
- * positions.
+ * Converts a series of [start, end] range pairs into an array of individual
+ * positions. This also caters for 'reverse strand' (start > end) cases.
*
* @param ranges
* @return
int count = 0;
for (int i = 0; i < ranges.length - 1; i += 2)
{
- count += ranges[i + 1] - ranges[i] + 1;
+ count += Math.abs(ranges[i + 1] - ranges[i]) + 1;
}
int[] result = new int[count];
int k = 0;
for (int i = 0; i < ranges.length - 1; i += 2)
{
- for (int j = ranges[i]; j <= ranges[i + 1]; j++)
+ int from = ranges[i];
+ final int to = ranges[i + 1];
+ int step = from <= to ? 1 : -1;
+ do
{
- result[k++] = j;
- }
+ result[k++] = from;
+ from += step;
+ } while (from != to + step);
}
return result;
}
"[1, 2, 3, 4, 7, 8, 9, 12]",
Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
9, 12, 12 })));
- // unpaired start position is ignored:
+ // trailing unpaired start position is ignored:
assertEquals(
"[1, 2, 3, 4, 7, 8, 9, 12]",
Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
assertEquals(12, e.getPosition());
assertEquals(6, e.getNumber());
}
+
+ /**
+ * Tests for the method that converts a series of [start, end] ranges to
+ * single positions, where the mapping is to a reverse strand i.e. start is
+ * greater than end point mapped to
+ */
+ @Test(groups = { "Functional" })
+ public void testFlattenRanges_reverseStrand()
+ {
+ assertEquals("[4, 3, 2, 1]",
+ Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 1 })));
+ assertEquals(
+ "[4, 3, 2, 1]",
+ Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 3, 2,
+ 1 })));
+ assertEquals(
+ "[4, 3, 2, 1]",
+ Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 4, 3,
+ 3, 2, 2, 1, 1 })));
+ assertEquals(
+ "[12, 9, 8, 7, 4, 3, 2, 1]",
+ Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
+ 9, 7, 4, 1 })));
+ // forwards and backwards anyone?
+ assertEquals(
+ "[4, 5, 6, 3, 2, 1]",
+ Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 6, 3,
+ 1 })));
+ // backwards and forwards
+ assertEquals(
+ "[3, 2, 1, 4, 5, 6]",
+ Arrays.toString(MappingUtils.flattenRanges(new int[] { 3, 1, 4,
+ 6 })));
+ // trailing unpaired start position is ignored:
+ assertEquals(
+ "[12, 9, 8, 7, 4, 3, 2]",
+ Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
+ 9, 7, 4, 2, 1 })));
+ }
}