+ /**
+ * Calculates and saves the sequence string to search. The string is
+ * restricted to the current selection region if there is one, and is saved
+ * with all gaps removed.
+ * <p>
+ * If there are hidden columns, and option {@ignoreHidden} is selected, then
+ * only visible positions of the sequence are included, and a mapping is also
+ * constructed from the returned string positions to the true sequence
+ * positions.
+ * <p>
+ * Note we have to do this each time {@code findNext} or {@code findAll} is
+ * called, in case the alignment, selection group or hidden columns have
+ * changed. In particular, if the sequence at offset {@code sequenceIndex} in
+ * the alignment is (no longer) in the selection group, search is advanced to
+ * the next sequence that is.
+ * <p>
+ * Sets sequence string to the empty string if there are no more sequences (in
+ * selection group if any) at or after {@code sequenceIndex}.
+ * <p>
+ * Returns true if a sequence could be found, false if end of alignment was
+ * reached
+ *
+ * @param ignoreHidden
+ * @return
+ */
+ private boolean getSequence(boolean ignoreHidden)
+ {
+ AlignmentI alignment = viewport.getAlignment();
+ if (sequenceIndex >= alignment.getHeight())
+ {
+ seqToSearch = "";
+ return false;
+ }
+ SequenceI seq = alignment.getSequenceAt(sequenceIndex);
+ SequenceGroup selection = viewport.getSelectionGroup();
+ if (selection != null && !selection.contains(seq))
+ {
+ if (!nextSequence(ignoreHidden))
+ {
+ return false;
+ }
+ seq = alignment.getSequenceAt(sequenceIndex);
+ }
+
+ String seqString = null;
+ if (ignoreHidden)
+ {
+ seqString = getVisibleSequence(seq);
+ this.searchedSequenceStartPosition = 1;
+ }
+ else
+ {
+ int startCol = 0;
+ int endCol = seq.getLength() - 1;
+ this.searchedSequenceStartPosition = seq.getStart();
+ if (selection != null)
+ {
+ startCol = selection.getStartRes();
+ endCol = Math.min(endCol, selection.getEndRes());
+ this.searchedSequenceStartPosition = seq.findPosition(startCol);
+ }
+ seqString = seq.getSequenceAsString(startCol, endCol + 1);
+ }
+
+ /*
+ * remove gaps; note that even if this leaves an empty string, we 'search'
+ * the sequence anyway (for possible match on name or description)
+ */
+ String ungapped = AlignSeq.extractGaps(Comparison.GapChars, seqString);
+ this.seqToSearch = ungapped;
+
+ return true;
+ }
+
+ /**
+ * Returns a string consisting of only the visible residues of {@code seq}
+ * from alignment column {@ fromColumn}, restricted to the current selection
+ * region if there is one.
+ * <p>
+ * As a side-effect, also computes the mapping from the true sequence
+ * positions to the positions (1, 2, ...) of the returned sequence. This is to
+ * allow search matches in the visible sequence to be converted to sequence
+ * positions.
+ *
+ * @param seq
+ * @return
+ */
+ private String getVisibleSequence(SequenceI seq)
+ {
+ /*
+ * get start / end columns of sequence and convert to base 0
+ * (so as to match the visible column ranges)
+ */
+ int seqStartCol = seq.findIndex(seq.getStart()) - 1;
+ int seqEndCol = seq.findIndex(seq.getStart() + seq.getLength() - 1) - 1;
+ Iterator<int[]> visibleColumns = viewport.getViewAsVisibleContigs(true);
+ StringBuilder visibleSeq = new StringBuilder(seqEndCol - seqStartCol);
+ List<int[]> fromRanges = new ArrayList<>();
+
+ while (visibleColumns.hasNext())
+ {
+ int[] range = visibleColumns.next();
+ if (range[0] > seqEndCol)
+ {
+ // beyond the end of the sequence
+ break;
+ }
+ if (range[1] < seqStartCol)
+ {
+ // before the start of the sequence
+ continue;
+ }
+ String subseq = seq.getSequenceAsString(range[0], range[1] + 1);
+ String ungapped = AlignSeq.extractGaps(Comparison.GapChars, subseq);
+ visibleSeq.append(ungapped);
+ if (!ungapped.isEmpty())
+ {
+ /*
+ * visible region includes at least one non-gap character,
+ * so add the range to the mapping being constructed
+ */
+ int seqResFrom = seq.findPosition(range[0]);
+ int seqResTo = seqResFrom + ungapped.length() - 1;
+ fromRanges.add(new int[] { seqResFrom, seqResTo });
+ }
+ }
+
+ /*
+ * construct the mapping
+ * from: visible sequence positions 1..length
+ * to: true residue positions of the alignment sequence
+ */
+ List<int[]> toRange = Arrays
+ .asList(new int[]
+ { 1, visibleSeq.length() });
+ searchedSequenceMap = new MapList(fromRanges, toRange, 1, 1);
+
+ return visibleSeq.toString();
+ }
+
+ /**
+ * Advances the search to the next sequence in the alignment. Sequences not in
+ * the current selection group (if there is one) are skipped. The
+ * (sub-)sequence to be searched is extracted, gaps removed, and saved, or set
+ * to null if there are no more sequences to search.
+ * <p>
+ * Returns true if a sequence could be found, false if end of alignment was
+ * reached
+ *
+ * @param ignoreHidden
+ */
+ private boolean nextSequence(boolean ignoreHidden)
+ {
+ sequenceIndex++;
+ residueIndex = -1;