From: gmungoc Date: Wed, 14 Mar 2018 09:25:04 +0000 (+0000) Subject: JAL-2839 refactored out getNextVisibleSequenceRegion X-Git-Tag: Release_2_11_0~17^2~67^2~2 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=85b18307da5f85a9cb5c13bb6be97eaf2c7f7965 JAL-2839 refactored out getNextVisibleSequenceRegion --- diff --git a/src/jalview/analysis/Finder.java b/src/jalview/analysis/Finder.java index de57d69..adf7ff6 100644 --- a/src/jalview/analysis/Finder.java +++ b/src/jalview/analysis/Finder.java @@ -21,6 +21,7 @@ package jalview.analysis; import jalview.datamodel.AlignmentI; +import jalview.datamodel.Range; import jalview.datamodel.SearchResultMatchI; import jalview.datamodel.SearchResults; import jalview.datamodel.SearchResultsI; @@ -164,17 +165,55 @@ public class Finder } /** - * Answers the start-end column range of the visible region starting at or - * after the given column. if there are no hidden columns, this just returns - * the remaining width of the alignment. Answers null if there are no visible - * columns at or after column. + * Answers the start-end column range of the visible region of + * sequence starting at or after the given column. + * If there are no hidden columns, this just returns the remaining width of + * the sequence. The range is restricted to the current selection + * if there is one. Answers null if there are no visible columns at or after + * column. */ - protected int[] getNextVisibleRegion(int column) + protected Range getNextVisibleSequenceRegion(SequenceI sequence, + int column) { + int seqColStart = column; + int seqColEnd = sequence.getLength() - 1; + + /* + * restrict search to (next) visible column region, + * in case there are hidden columns + */ VisibleContigsIterator visibleRegions = alignment.getHiddenColumns() .getVisContigsIterator(column, alignment.getWidth(), false); - return visibleRegions.hasNext() ? visibleRegions.next() : null; + int[] visible = visibleRegions.hasNext() ? visibleRegions.next() : null; + if (visible == null) + { + columnIndex = seqColEnd + 1; + return null; + } + seqColStart = Math.max(seqColStart, visible[0]); + seqColEnd = Math.min(seqColEnd, visible[1]); + + /* + * restrict search to selected region if there is one + */ + if (selection != null) + { + int selectionStart = selection.getStartRes(); + int selectionEnd = selection.getEndRes(); + if (selectionStart > seqColEnd || selectionEnd < seqColStart) + { + /* + * sequence region doesn't overlap selection region + */ + columnIndex = seqColEnd + 1; + return null; + } + seqColStart = Math.max(seqColStart, selectionStart); + seqColEnd = Math.min(seqColEnd, selectionEnd); + } + + return new Range(seqColStart, seqColEnd); } /** @@ -237,55 +276,17 @@ public class Finder */ protected boolean searchNextVisibleRegion(SequenceI seq, Regex searchPattern) { - /* - * sequence columns to search (working in absolute column - * positions, base 0, including any hidden columns) - */ - int seqColStart = columnIndex; - int seqColEnd = seq.getLength() - 1; - - /* - * restrict search to (next) visible column region, - * in case there are hidden columns - */ - int[] visible = getNextVisibleRegion(columnIndex); - if (visible != null) - { - seqColStart = Math.max(seqColStart, visible[0]); - seqColEnd = Math.min(seqColEnd, visible[1]); - } - else + Range visible = getNextVisibleSequenceRegion(seq, columnIndex); + if (visible == null) { - columnIndex = seqColEnd + 1; return false; } - - /* - * restrict search to selected region if there is one - */ - if (selection != null) - { - int selectionStart = selection.getStartRes(); - int selectionEnd = selection.getEndRes(); - if (selectionStart > seqColEnd || selectionEnd < seqColStart) - { - /* - * sequence region doesn't overlap selection region - - * no match, advance to next visible region - */ - columnIndex = seqColEnd + 1; - return false; - } - seqColStart = Math.max(seqColStart, selectionStart); - seqColEnd = Math.min(seqColEnd, selectionEnd); - } - - String seqString = seq.getSequenceAsString(seqColStart, seqColEnd + 1); + String seqString = seq.getSequenceAsString(visible.start, visible.end + 1); String noGaps = AlignSeq.extractGaps(Comparison.GapChars, seqString); if (searchPattern.search(noGaps)) { - int sequenceStartPosition = seq.findPosition(seqColStart); + int sequenceStartPosition = seq.findPosition(visible.start); recordMatch(seq, searchPattern, sequenceStartPosition); return true; } @@ -295,7 +296,7 @@ public class Finder * no match - advance columnIndex past this visible region * so the next visible region (if any) is searched next */ - columnIndex = seqColEnd + 1; + columnIndex = visible.end + 1; } return false;