From 0e6751ef949bc0bad9a44220262b7e0d6e46c563 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 1 Apr 2016 11:44:04 +0100 Subject: [PATCH] JAL-1705 generalised getLength() to handle [start1, end1, start2, end2..] --- src/jalview/util/MappingUtils.java | 15 ++++++++++++--- test/jalview/util/MappingUtilsTest.java | 13 +++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/jalview/util/MappingUtils.java b/src/jalview/util/MappingUtils.java index c2cad1f..ae4e55d 100644 --- a/src/jalview/util/MappingUtils.java +++ b/src/jalview/util/MappingUtils.java @@ -770,7 +770,8 @@ public final class MappingUtils } /** - * Returns the total length of the supplied ranges + * Returns the total length of the supplied ranges, which may be as single + * [start, end] or multiple [start, end, start, end ...] * * @param ranges * @return @@ -784,7 +785,16 @@ public final class MappingUtils int length = 0; for (int[] range : ranges) { - length += Math.abs(range[1] - range[0]) + 1; + if (range.length % 2 != 0) + { + System.err.println("Error unbalance start/end ranges: " + + ranges.toString()); + return 0; + } + for (int i = 0; i < range.length - 1; i += 2) + { + length += Math.abs(range[i + 1] - range[i]) + 1; + } } return length; } @@ -848,7 +858,6 @@ public final class MappingUtils int cdspos = 0; for (int x = 0; x < copy.length && sxpos == -1; x += 2) { - // fixme handle reverse strand cdspos += Math.abs(copy[x + 1] - copy[x]) + 1; if (removeCount < cdspos) { diff --git a/test/jalview/util/MappingUtilsTest.java b/test/jalview/util/MappingUtilsTest.java index b53d513..3c417c3 100644 --- a/test/jalview/util/MappingUtilsTest.java +++ b/test/jalview/util/MappingUtilsTest.java @@ -861,6 +861,10 @@ public class MappingUtilsTest public void testGetLength() { assertEquals(0, MappingUtils.getLength(null)); + + /* + * [start, end] ranges + */ List ranges = new ArrayList(); assertEquals(0, MappingUtils.getLength(ranges)); ranges.add(new int[] { 1, 1 }); @@ -869,6 +873,15 @@ public class MappingUtilsTest assertEquals(10, MappingUtils.getLength(ranges)); ranges.add(new int[] { 20, 10 }); assertEquals(21, MappingUtils.getLength(ranges)); + + /* + * [start, end, start, end...] ranges + */ + ranges.clear(); + ranges.add(new int[] { 1, 5, 8, 4 }); + ranges.add(new int[] { 8, 2 }); + ranges.add(new int[] { 12, 12 }); + assertEquals(18, MappingUtils.getLength(ranges)); } @Test(groups = { "Functional" }) -- 1.7.10.2