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.util.Comparison;
29 import java.util.Vector;
31 import com.stevesoft.pat.Regex;
36 * Implements the search algorithms for the Find dialog box.
38 SearchResults searchResults;
42 SequenceGroup selection = null;
44 Vector idMatch = null;
46 boolean caseSensitive = false;
48 private boolean includeDescription = false;
50 boolean findAll = false;
55 * hold's last-searched position between calles to find(false)
57 int seqIndex = 0, resIndex = -1;
59 public Finder(AlignmentI alignment, SequenceGroup selection)
61 this.alignment = alignment;
62 this.selection = selection;
66 * restart search at given sequence and residue on alignment and (optionally)
67 * contained in selection
70 * @param selectionGroup
74 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
75 int seqIndex, int resIndex)
77 this(alignment, selectionGroup);
78 this.seqIndex = seqIndex;
79 this.resIndex = resIndex;
82 public boolean find(String searchString)
84 boolean hasResults = false;
87 searchString = searchString.toUpperCase();
89 regex = new Regex(searchString);
90 regex.setIgnoreCase(!caseSensitive);
91 searchResults = new SearchResults();
92 idMatch = new Vector();
95 boolean found = false;
96 int end = alignment.getHeight();
98 // /////////////////////////////////////////////
100 if (selection != null)
102 if ((selection.getSize() < 1)
103 || ((selection.getEndRes() - selection.getStartRes()) < 2))
109 while (!found && (seqIndex < end))
111 seq = (Sequence) alignment.getSequenceAt(seqIndex);
113 if ((selection != null && selection.getSize() > 0)
114 && !selection.getSequences(null).contains(seq))
124 // test for one off matches - sequence position and sequence ID
125 // //// is the searchString a residue number?
128 int res = Integer.parseInt(searchString);
129 // possibly a residue number - check if valid for seq
130 if (seq.getEnd() >= res)
132 searchResults.addResult(seq, res, res);
134 // resIndex=seq.getLength();
142 } catch (NumberFormatException ex)
146 if (regex.search(seq.getName()))
148 idMatch.addElement(seq);
152 // stop and return the match
158 if (isIncludeDescription() && seq.getDescription() != null
159 && regex.search(seq.getDescription()))
161 idMatch.addElement(seq);
165 // stop and return the match
171 item = seq.getSequenceAsString();
173 if ((selection != null)
174 && (selection.getEndRes() < alignment.getWidth() - 1))
176 item = item.substring(0, selection.getEndRes() + 1);
179 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
180 StringBuffer noGapsSB = new StringBuffer();
182 Vector spaces = new Vector();
184 for (int j = 0; j < item.length(); j++)
186 if (!Comparison.isGap(item.charAt(j)))
188 noGapsSB.append(item.charAt(j));
189 spaces.addElement(new Integer(insertCount));
197 String noGaps = noGapsSB.toString();
199 for (int r = resIndex; r < noGaps.length(); r++)
202 if (regex.searchFrom(noGaps, r))
204 resIndex = regex.matchedFrom();
206 if ((selection != null && selection.getSize() > 0)
207 && ((resIndex + Integer.parseInt(spaces.elementAt(
208 resIndex).toString())) < selection.getStartRes()))
212 // if invalid string used, then regex has no matched to/from
214 .findPosition(resIndex
215 + Integer.parseInt(spaces.elementAt(resIndex)
217 int eres = seq.findPosition(regex.matchedTo()
219 + Integer.parseInt(spaces
220 .elementAt(regex.matchedTo() - 1).toString()));
222 searchResults.addResult(seq, sres, eres);
226 // thats enough, break and display the result
249 * We now search the Id string in the main search loop. for (int id = 0; id
250 * < alignment.getHeight(); id++) { if
251 * (regex.search(alignment.getSequenceAt(id).getName())) {
252 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
258 * @return the alignment
260 public AlignmentI getAlignment()
267 * the alignment to set
269 public void setAlignment(AlignmentI alignment)
271 this.alignment = alignment;
275 * @return the caseSensitive
277 public boolean isCaseSensitive()
279 return caseSensitive;
283 * @param caseSensitive
284 * the caseSensitive to set
286 public void setCaseSensitive(boolean caseSensitive)
288 this.caseSensitive = caseSensitive;
292 * @return the findAll
294 public boolean isFindAll()
303 public void setFindAll(boolean findAll)
305 this.findAll = findAll;
309 * @return the selection
311 public SequenceGroup getSelection()
318 * the selection to set
320 public void setSelection(SequenceGroup selection)
322 this.selection = selection;
326 * @return the idMatch
328 public Vector getIdMatch()
336 public Regex getRegex()
342 * @return the searchResults
344 public SearchResults getSearchResults()
346 return searchResults;
350 * @return the resIndex
352 public int getResIndex()
359 * the resIndex to set
361 public void setResIndex(int resIndex)
363 this.resIndex = resIndex;
367 * @return the seqIndex
369 public int getSeqIndex()
376 * the seqIndex to set
378 public void setSeqIndex(int seqIndex)
380 this.seqIndex = seqIndex;
383 public boolean isIncludeDescription()
385 return includeDescription;
388 public void setIncludeDescription(boolean includeDescription)
390 this.includeDescription = includeDescription;