X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FMappingUtils.java;fp=src%2Fjalview%2Futil%2FMappingUtils.java;h=fe95f4b3f367727bc7031cf3c864554e3074f80c;hb=304e64fb34b32659be1bbfd39fb4e15b2f79586e;hp=4e07a08ba6c739402fa7f10389b05a4889ca3a28;hpb=61ff8fb4efa315c35149c9d11850d99e3d00c441;p=jalview.git diff --git a/src/jalview/util/MappingUtils.java b/src/jalview/util/MappingUtils.java index 4e07a08..fe95f4b 100644 --- a/src/jalview/util/MappingUtils.java +++ b/src/jalview/util/MappingUtils.java @@ -20,13 +20,6 @@ */ package jalview.util; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - import jalview.analysis.AlignmentSorter; import jalview.api.AlignViewportI; import jalview.commands.CommandI; @@ -46,6 +39,13 @@ import jalview.datamodel.Sequence; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + /** * Helper methods for manipulations involving sequence mappings. * @@ -546,7 +546,8 @@ public final class MappingUtils while (regions.hasNext()) { mapHiddenColumns(regions.next(), codonFrames, newHidden, - fromSequences, toSequences, fromGapChar); + fromSequences, + toSequences, fromGapChar); } return; // mappedColumns; } @@ -964,7 +965,7 @@ public final class MappingUtils int min = Math.min(range[0], range[1]); int max = Math.max(range[0], range[1]); - + return (min <= queryRange[0] && max >= queryRange[0] && min <= queryRange[1] && max >= queryRange[1]); } @@ -979,7 +980,8 @@ public final class MappingUtils * a list of (single) [start, end] ranges * @return */ - public static void removeEndPositions(int positions, List ranges) + public static void removeEndPositions(int positions, + List ranges) { int toRemove = positions; Iterator it = new ReverseListIterator<>(ranges); @@ -991,8 +993,8 @@ public final class MappingUtils /* * not coded for [start1, end1, start2, end2, ...] */ - System.err.println( - "MappingUtils.removeEndPositions doesn't handle multiple ranges"); + System.err + .println("MappingUtils.removeEndPositions doesn't handle multiple ranges"); return; } @@ -1002,8 +1004,8 @@ public final class MappingUtils /* * not coded for a reverse strand range (end < start) */ - System.err.println( - "MappingUtils.removeEndPositions doesn't handle reverse strand"); + System.err + .println("MappingUtils.removeEndPositions doesn't handle reverse strand"); return; } if (length > toRemove) @@ -1018,6 +1020,60 @@ public final class MappingUtils } } } + /** + * 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 + */ + public static void addRange(int[] range, List 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); + } /** * Converts a list of {@code start-end} ranges to a single array of