2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
25 import jalview.datamodel.*;
30 * Implements the search algorithms for the Find dialog box.
32 SearchResults searchResults;
36 jalview.datamodel.SequenceGroup selection = null;
38 Vector idMatch = null;
40 boolean caseSensitive = false;
42 boolean findAll = false;
44 com.stevesoft.pat.Regex regex = null;
47 * hold's last-searched position between calles to find(false)
49 int seqIndex = 0, resIndex = -1;
51 public Finder(AlignmentI alignment, SequenceGroup selection)
53 this.alignment = alignment;
54 this.selection = selection;
58 * restart search at given sequence and residue on alignment and (optionally)
59 * contained in selection
62 * @param selectionGroup
66 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
67 int seqIndex, int resIndex)
69 this(alignment, selectionGroup);
70 this.seqIndex = seqIndex;
71 this.resIndex = resIndex;
74 public boolean find(String searchString)
76 boolean hasResults = false;
79 searchString = searchString.toUpperCase();
81 regex = new com.stevesoft.pat.Regex(searchString);
82 regex.setIgnoreCase(!caseSensitive);
83 searchResults = new SearchResults();
84 idMatch = new Vector();
87 boolean found = false;
88 int end = alignment.getHeight();
90 // /////////////////////////////////////////////
92 if (selection != null)
94 if ((selection.getSize() < 1)
95 || ((selection.getEndRes() - selection.getStartRes()) < 2))
101 while (!found && (seqIndex < end))
103 seq = (Sequence) alignment.getSequenceAt(seqIndex);
105 if ((selection != null && selection.getSize() > 0)
106 && !selection.getSequences(null).contains(seq))
116 // test for one off matches - sequence position and sequence ID
117 // //// is the searchString a residue number?
120 int res = Integer.parseInt(searchString);
121 // possibly a residue number - check if valid for seq
122 if (seq.getEnd() >= res)
124 searchResults.addResult(seq, res, res);
126 // resIndex=seq.getLength();
134 } catch (NumberFormatException ex)
138 if (regex.search(seq.getName()))
140 idMatch.addElement(seq);
144 // stop and return the match
150 item = seq.getSequenceAsString();
152 if ((selection != null)
153 && (selection.getEndRes() < alignment.getWidth() - 1))
155 item = item.substring(0, selection.getEndRes() + 1);
158 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
159 StringBuffer noGapsSB = new StringBuffer();
161 Vector spaces = new Vector();
163 for (int j = 0; j < item.length(); j++)
165 if (!jalview.util.Comparison.isGap(item.charAt(j)))
167 noGapsSB.append(item.charAt(j));
168 spaces.addElement(new Integer(insertCount));
176 String noGaps = noGapsSB.toString();
178 for (int r = resIndex; r < noGaps.length(); r++)
181 if (regex.searchFrom(noGaps, r))
183 resIndex = regex.matchedFrom();
185 if ((selection != null && selection.getSize() > 0)
186 && ((resIndex + Integer.parseInt(spaces.elementAt(
187 resIndex).toString())) < selection.getStartRes()))
191 // if invalid string used, then regex has no matched to/from
193 .findPosition(resIndex
194 + Integer.parseInt(spaces.elementAt(resIndex)
196 int eres = seq.findPosition(regex.matchedTo()
198 + Integer.parseInt(spaces
199 .elementAt(regex.matchedTo() - 1).toString()));
201 searchResults.addResult(seq, sres, eres);
205 // thats enough, break and display the result
228 * We now search the Id string in the main search loop. for (int id = 0; id
229 * < alignment.getHeight(); id++) { if
230 * (regex.search(alignment.getSequenceAt(id).getName())) {
231 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
237 * @return the alignment
239 public AlignmentI getAlignment()
246 * the alignment to set
248 public void setAlignment(AlignmentI alignment)
250 this.alignment = alignment;
254 * @return the caseSensitive
256 public boolean isCaseSensitive()
258 return caseSensitive;
262 * @param caseSensitive
263 * the caseSensitive to set
265 public void setCaseSensitive(boolean caseSensitive)
267 this.caseSensitive = caseSensitive;
271 * @return the findAll
273 public boolean isFindAll()
282 public void setFindAll(boolean findAll)
284 this.findAll = findAll;
288 * @return the selection
290 public jalview.datamodel.SequenceGroup getSelection()
297 * the selection to set
299 public void setSelection(jalview.datamodel.SequenceGroup selection)
301 this.selection = selection;
305 * @return the idMatch
307 public Vector getIdMatch()
315 public com.stevesoft.pat.Regex getRegex()
321 * @return the searchResults
323 public SearchResults getSearchResults()
325 return searchResults;
329 * @return the resIndex
331 public int getResIndex()
338 * the resIndex to set
340 public void setResIndex(int resIndex)
342 this.resIndex = resIndex;
346 * @return the seqIndex
348 public int getSeqIndex()
355 * the seqIndex to set
357 public void setSeqIndex(int seqIndex)
359 this.seqIndex = seqIndex;