2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SearchResults;
25 import jalview.datamodel.SearchResultsI;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceGroup;
29 import java.util.Vector;
34 * Implements the search algorithms for the Find dialog box.
36 SearchResultsI searchResults;
40 jalview.datamodel.SequenceGroup selection = null;
42 Vector idMatch = null;
44 boolean caseSensitive = false;
46 private boolean includeDescription = false;
48 boolean findAll = false;
50 com.stevesoft.pat.Regex regex = null;
53 * hold's last-searched position between calles to find(false)
55 int seqIndex = 0, resIndex = -1;
57 public Finder(AlignmentI alignment, SequenceGroup selection)
59 this.alignment = alignment;
60 this.selection = selection;
64 * restart search at given sequence and residue on alignment and (optionally)
65 * contained in selection
68 * @param selectionGroup
72 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
73 int seqIndex, int resIndex)
75 this(alignment, selectionGroup);
76 this.seqIndex = seqIndex;
77 this.resIndex = resIndex;
80 public boolean find(String searchString)
82 boolean hasResults = false;
85 searchString = searchString.toUpperCase();
87 regex = new com.stevesoft.pat.Regex(searchString);
88 regex.setIgnoreCase(!caseSensitive);
89 searchResults = new SearchResults();
90 idMatch = new Vector();
93 boolean found = false;
94 int end = alignment.getHeight();
96 // /////////////////////////////////////////////
98 if (selection != null)
100 if ((selection.getSize() < 1)
101 || ((selection.getEndRes() - selection.getStartRes()) < 2))
107 while (!found && (seqIndex < end))
109 seq = (Sequence) alignment.getSequenceAt(seqIndex);
111 if ((selection != null && selection.getSize() > 0)
112 && !selection.getSequences(null).contains(seq))
122 // test for one off matches - sequence position and sequence ID
123 // //// is the searchString a residue number?
126 int res = Integer.parseInt(searchString);
127 // possibly a residue number - check if valid for seq
128 if (seq.getEnd() >= res)
130 searchResults.addResult(seq, res, res);
132 // resIndex=seq.getLength();
140 } catch (NumberFormatException ex)
144 if (regex.search(seq.getName()))
146 idMatch.addElement(seq);
150 // stop and return the match
156 if (isIncludeDescription() && seq.getDescription() != null
157 && regex.search(seq.getDescription()))
159 idMatch.addElement(seq);
163 // stop and return the match
169 item = seq.getSequenceAsString();
171 if ((selection != null)
172 && (selection.getEndRes() < alignment.getWidth() - 1))
174 item = item.substring(0, selection.getEndRes() + 1);
177 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
178 StringBuffer noGapsSB = new StringBuffer();
180 Vector spaces = new Vector();
182 for (int j = 0; j < item.length(); j++)
184 if (!jalview.util.Comparison.isGap(item.charAt(j)))
186 noGapsSB.append(item.charAt(j));
187 spaces.addElement(new Integer(insertCount));
195 String noGaps = noGapsSB.toString();
197 for (int r = resIndex; r < noGaps.length(); r++)
200 if (regex.searchFrom(noGaps, r))
202 resIndex = regex.matchedFrom();
204 if ((selection != null && selection.getSize() > 0)
205 && ((resIndex + Integer.parseInt(spaces.elementAt(
206 resIndex).toString())) < selection.getStartRes()))
210 // if invalid string used, then regex has no matched to/from
212 .findPosition(resIndex
213 + Integer.parseInt(spaces.elementAt(resIndex)
215 int eres = seq.findPosition(regex.matchedTo()
217 + Integer.parseInt(spaces
218 .elementAt(regex.matchedTo() - 1).toString()));
220 searchResults.addResult(seq, sres, eres);
224 // thats enough, break and display the result
247 * We now search the Id string in the main search loop. for (int id = 0; id
248 * < alignment.getHeight(); id++) { if
249 * (regex.search(alignment.getSequenceAt(id).getName())) {
250 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
256 * @return the alignment
258 public AlignmentI getAlignment()
265 * the alignment to set
267 public void setAlignment(AlignmentI alignment)
269 this.alignment = alignment;
273 * @return the caseSensitive
275 public boolean isCaseSensitive()
277 return caseSensitive;
281 * @param caseSensitive
282 * the caseSensitive to set
284 public void setCaseSensitive(boolean caseSensitive)
286 this.caseSensitive = caseSensitive;
290 * @return the findAll
292 public boolean isFindAll()
301 public void setFindAll(boolean findAll)
303 this.findAll = findAll;
307 * @return the selection
309 public jalview.datamodel.SequenceGroup getSelection()
316 * the selection to set
318 public void setSelection(jalview.datamodel.SequenceGroup selection)
320 this.selection = selection;
324 * @return the idMatch
326 public Vector getIdMatch()
334 public com.stevesoft.pat.Regex getRegex()
340 * @return the searchResults
342 public SearchResultsI getSearchResults()
344 return searchResults;
348 * @return the resIndex
350 public int getResIndex()
357 * the resIndex to set
359 public void setResIndex(int resIndex)
361 this.resIndex = resIndex;
365 * @return the seqIndex
367 public int getSeqIndex()
374 * the seqIndex to set
376 public void setSeqIndex(int seqIndex)
378 this.seqIndex = seqIndex;
381 public boolean isIncludeDescription()
383 return includeDescription;
386 public void setIncludeDescription(boolean includeDescription)
388 this.includeDescription = includeDescription;