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.Sequence;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.jsdev.RegExp;
28 import jalview.jsdev.api.RegExpInterface;
29 import jalview.util.Comparison;
31 import java.util.Vector;
33 //import com.stevesoft.pat.Regex;
38 * Implements the search algorithms for the Find dialog box.
40 SearchResults searchResults;
44 SequenceGroup selection = null;
46 Vector idMatch = null;
48 boolean caseSensitive = false;
50 private boolean includeDescription = false;
52 boolean findAll = false;
54 RegExpInterface regex = null;
57 * hold's last-searched position between calles to find(false)
59 int seqIndex = 0, resIndex = -1;
61 public Finder(AlignmentI alignment, SequenceGroup selection)
63 this.alignment = alignment;
64 this.selection = selection;
68 * restart search at given sequence and residue on alignment and (optionally)
69 * contained in selection
72 * @param selectionGroup
76 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
77 int seqIndex, int resIndex)
79 this(alignment, selectionGroup);
80 this.seqIndex = seqIndex;
81 this.resIndex = resIndex;
84 public boolean find(String searchString)
86 boolean hasResults = false;
89 searchString = searchString.toUpperCase();
91 regex = RegExp.newRegex(searchString);
92 regex.setIgnoreCase(!caseSensitive);
93 searchResults = new SearchResults();
94 idMatch = new Vector();
97 boolean found = false;
98 int end = alignment.getHeight();
100 // /////////////////////////////////////////////
102 if (selection != null)
104 if ((selection.getSize() < 1)
105 || ((selection.getEndRes() - selection.getStartRes()) < 2))
111 while (!found && (seqIndex < end))
113 seq = (Sequence) alignment.getSequenceAt(seqIndex);
115 if ((selection != null && selection.getSize() > 0)
116 && !selection.getSequences(null).contains(seq))
126 // test for one off matches - sequence position and sequence ID
127 // //// is the searchString a residue number?
130 int res = Integer.parseInt(searchString);
131 // possibly a residue number - check if valid for seq
132 if (seq.getEnd() >= res)
134 searchResults.addResult(seq, res, res);
136 // resIndex=seq.getLength();
144 } catch (NumberFormatException ex)
148 if (regex.search(seq.getName()))
150 idMatch.addElement(seq);
154 // stop and return the match
160 if (isIncludeDescription() && seq.getDescription() != null
161 && regex.search(seq.getDescription()))
163 idMatch.addElement(seq);
167 // stop and return the match
173 item = seq.getSequenceAsString();
175 if ((selection != null)
176 && (selection.getEndRes() < alignment.getWidth() - 1))
178 item = item.substring(0, selection.getEndRes() + 1);
181 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
182 StringBuffer noGapsSB = new StringBuffer();
184 Vector spaces = new Vector();
186 for (int j = 0; j < item.length(); j++)
188 if (!Comparison.isGap(item.charAt(j)))
190 noGapsSB.append(item.charAt(j));
191 spaces.addElement(new Integer(insertCount));
199 String noGaps = noGapsSB.toString();
201 for (int r = resIndex; r < noGaps.length(); r++)
204 if (regex.searchFrom(noGaps, r))
206 resIndex = regex.matchedFrom();
208 if ((selection != null && selection.getSize() > 0)
209 && ((resIndex + Integer.parseInt(spaces.elementAt(
210 resIndex).toString())) < selection.getStartRes()))
214 // if invalid string used, then regex has no matched to/from
216 .findPosition(resIndex
217 + Integer.parseInt(spaces.elementAt(resIndex)
219 int eres = seq.findPosition(regex.matchedTo()
221 + Integer.parseInt(spaces
222 .elementAt(regex.matchedTo() - 1).toString()));
224 searchResults.addResult(seq, sres, eres);
228 // thats enough, break and display the result
251 * We now search the Id string in the main search loop. for (int id = 0; id
252 * < alignment.getHeight(); id++) { if
253 * (regex.search(alignment.getSequenceAt(id).getName())) {
254 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
260 * @return the alignment
262 public AlignmentI getAlignment()
269 * the alignment to set
271 public void setAlignment(AlignmentI alignment)
273 this.alignment = alignment;
277 * @return the caseSensitive
279 public boolean isCaseSensitive()
281 return caseSensitive;
285 * @param caseSensitive
286 * the caseSensitive to set
288 public void setCaseSensitive(boolean caseSensitive)
290 this.caseSensitive = caseSensitive;
294 * @return the findAll
296 public boolean isFindAll()
305 public void setFindAll(boolean findAll)
307 this.findAll = findAll;
311 * @return the selection
313 public SequenceGroup getSelection()
320 * the selection to set
322 public void setSelection(SequenceGroup selection)
324 this.selection = selection;
328 * @return the idMatch
330 public Vector getIdMatch()
338 public RegExpInterface getRegex()
344 * @return the searchResults
346 public SearchResults getSearchResults()
348 return searchResults;
352 * @return the resIndex
354 public int getResIndex()
361 * the resIndex to set
363 public void setResIndex(int resIndex)
365 this.resIndex = resIndex;
369 * @return the seqIndex
371 public int getSeqIndex()
378 * the seqIndex to set
380 public void setSeqIndex(int seqIndex)
382 this.seqIndex = seqIndex;
385 public boolean isIncludeDescription()
387 return includeDescription;
390 public void setIncludeDescription(boolean includeDescription)
392 this.includeDescription = includeDescription;