From c5060a3a565a47414ff8d137642df8dd3e0648b0 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 28 Nov 2017 09:07:51 +0000 Subject: [PATCH] JAL-2850 MapList.isToForwardStrand added --- src/jalview/util/MapList.java | 35 ++++++++++++++++++++++++++++++----- test/jalview/util/MapListTest.java | 33 +++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/jalview/util/MapList.java b/src/jalview/util/MapList.java index 3ce0bb3..c944345 100644 --- a/src/jalview/util/MapList.java +++ b/src/jalview/util/MapList.java @@ -77,8 +77,8 @@ public class MapList */ public MapList() { - fromShifts = new ArrayList(); - toShifts = new ArrayList(); + fromShifts = new ArrayList<>(); + toShifts = new ArrayList<>(); } /** @@ -347,7 +347,7 @@ public class MapList } boolean changed = false; - List merged = new ArrayList(); + List merged = new ArrayList<>(); int[] lastRange = ranges.get(0); int lastDirection = lastRange[1] >= lastRange[0] ? 1 : -1; lastRange = new int[] { lastRange[0], lastRange[1] }; @@ -803,7 +803,7 @@ public class MapList { return null; } - List ranges = new ArrayList(); + List ranges = new ArrayList<>(); if (fs <= fe) { intv = fs; @@ -1094,8 +1094,33 @@ public class MapList */ public boolean isFromForwardStrand() { + return isForwardStrand(getFromRanges()); + } + + /** + * Returns true if mapping is to forward strand, false if to reverse strand. + * Result is just based on the first 'to' range that is not a single position. + * Default is true unless proven to be false. Behaviour is not well defined if + * the mapping has a mixture of forward and reverse ranges. + * + * @return + */ + public boolean isToForwardStrand() + { + return isForwardStrand(getToRanges()); + } + + /** + * A helper method that returns true unless at least one range has start > end. + * Behaviour is undefined for a mixture of forward and reverse ranges. + * + * @param ranges + * @return + */ + private boolean isForwardStrand(List ranges) + { boolean forwardStrand = true; - for (int[] range : getFromRanges()) + for (int[] range : ranges) { if (range[1] > range[0]) { diff --git a/test/jalview/util/MapListTest.java b/test/jalview/util/MapListTest.java index 3fc6fe0..029b681 100644 --- a/test/jalview/util/MapListTest.java +++ b/test/jalview/util/MapListTest.java @@ -426,7 +426,7 @@ public class MapListTest @Test(groups = { "Functional" }) public void testGetRanges() { - List ranges = new ArrayList(); + List ranges = new ArrayList<>(); ranges.add(new int[] { 2, 3 }); ranges.add(new int[] { 5, 6 }); assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges))); @@ -603,7 +603,7 @@ public class MapListTest public void testAddRange() { int[] range = { 1, 5 }; - List ranges = new ArrayList(); + List ranges = new ArrayList<>(); // add to empty list: MapList.addRange(range, ranges); @@ -702,7 +702,7 @@ public class MapListTest public void testCoalesceRanges() { assertNull(MapList.coalesceRanges(null)); - List ranges = new ArrayList(); + List ranges = new ArrayList<>(); assertSame(ranges, MapList.coalesceRanges(ranges)); ranges.add(new int[] { 1, 3 }); assertSame(ranges, MapList.coalesceRanges(ranges)); @@ -763,7 +763,7 @@ public class MapListTest @Test(groups = { "Functional" }) public void testCoalesceRanges_withOverlap() { - List ranges = new ArrayList(); + List ranges = new ArrayList<>(); ranges.add(new int[] { 1, 3 }); ranges.add(new int[] { 2, 5 }); @@ -940,4 +940,29 @@ public class MapListTest compound = ml1.traverse(ml2); assertNull(compound); } + + /** + * Test that method that inspects for the (first) forward or reverse 'to' range. + * Single position ranges are ignored. + */ + @Test(groups = { "Functional" }) + public void testIsToForwardsStrand() + { + // [3-9] declares forward strand + MapList ml = new MapList(new int[] { 20, 11 }, + new int[] + { 2, 2, 3, 9, 12, 11 }, 1, 1); + assertTrue(ml.isToForwardStrand()); + + // [11-5] declares reverse strand ([13-14] is ignored) + ml = new MapList(new int[] { 20, 11 }, + new int[] + { 2, 2, 11, 5, 13, 14 }, 1, 1); + assertFalse(ml.isToForwardStrand()); + + // all single position ranges - defaults to forward strand + ml = new MapList(new int[] { 3, 1 }, new int[] { 2, 2, 4, 4, 6, 6 }, 1, + 1); + assertTrue(ml.isToForwardStrand()); + } } -- 1.7.10.2