From fc90b2a19f4827d0389c578c56105dc7e69e59ea Mon Sep 17 00:00:00 2001 From: gmungoc Date: Wed, 2 Dec 2020 19:59:33 +0000 Subject: [PATCH] JAL-3761 helper method reinstated to avoid code duplication --- src/jalview/util/MapList.java | 55 ++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/jalview/util/MapList.java b/src/jalview/util/MapList.java index 198066d..745f367 100644 --- a/src/jalview/util/MapList.java +++ b/src/jalview/util/MapList.java @@ -1155,27 +1155,8 @@ public class MapList */ public int[] locateInFrom(int start, int end) { - if (end < start) - { - int tmp = end; - end = start; - start = tmp; - } - - /* - * traverse toShifts and mark offsets in fromShifts - * of any positions that lie in [start, end] - */ - BitSet offsets = getMappedOffsetsForPositions(start, end, toShifts, + return mapPositions(start, end, toShifts, fromShifts, toRatio, fromRatio); - - /* - * traverse fromShifts and collect positions at the marked offsets - */ - List mapped = getPositionsForOffsets(fromShifts, offsets); - - // TODO: or just return the List and adjust calling code to match - return mapped.isEmpty() ? null : MappingUtils.rangeListToArray(mapped); } /** @@ -1190,6 +1171,29 @@ public class MapList */ public int[] locateInTo(int start, int end) { + return mapPositions(start, end, fromShifts, toShifts, + fromRatio, toRatio); + } + + /** + * Helper method that returns the [start1, end1, start2, end2, ...] positions + * in {@code targetRange} that map to positions between {@code start} and + * {@code end} in {@code sourceRange}. 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 + * @param sourceRange + * @param targetRange + * @param sourceWordLength + * @param targetWordLength + * @return + */ + final static int[] mapPositions(int start, int end, + List sourceRange, List targetRange, + int sourceWordLength, int targetWordLength) + { if (end < start) { int tmp = end; @@ -1198,17 +1202,18 @@ public class MapList } /* - * traverse fromShifts and mark offsets in toShifts + * traverse sourceRange and mark offsets in targetRange * of any positions that lie in [start, end] */ - BitSet offsets = getMappedOffsetsForPositions(start, end, fromShifts, - fromRatio, toRatio); + BitSet offsets = getMappedOffsetsForPositions(start, end, sourceRange, + sourceWordLength, targetWordLength); /* - * traverse toShifts and collect positions at the marked offsets + * traverse targetRange and collect positions at the marked offsets */ - List mapped = getPositionsForOffsets(toShifts, offsets); + List mapped = getPositionsForOffsets(targetRange, offsets); + // TODO: or just return the List and adjust calling code to match return mapped.isEmpty() ? null : MappingUtils.rangeListToArray(mapped); } -- 1.7.10.2