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;
28 import java.util.Vector;
33 * Implements the search algorithms for the Find dialog box.
35 SearchResults searchResults;
39 jalview.datamodel.SequenceGroup selection = null;
41 Vector idMatch = null;
43 boolean caseSensitive = false;
45 private boolean includeDescription = false;
47 boolean findAll = false;
49 com.stevesoft.pat.Regex regex = null;
52 * hold's last-searched position between calles to find(false)
54 int seqIndex = 0, resIndex = -1;
56 public Finder(AlignmentI alignment, SequenceGroup selection)
58 this.alignment = alignment;
59 this.selection = selection;
63 * restart search at given sequence and residue on alignment and (optionally)
64 * contained in selection
67 * @param selectionGroup
71 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
72 int seqIndex, int resIndex)
74 this(alignment, selectionGroup);
75 this.seqIndex = seqIndex;
76 this.resIndex = resIndex;
79 public boolean find(String searchString)
81 boolean hasResults = false;
84 searchString = searchString.toUpperCase();
86 regex = new com.stevesoft.pat.Regex(searchString);
87 regex.setIgnoreCase(!caseSensitive);
88 searchResults = new SearchResults();
89 idMatch = new Vector();
92 boolean found = false;
93 int end = alignment.getHeight();
95 // /////////////////////////////////////////////
97 if (selection != null)
99 if ((selection.getSize() < 1)
100 || ((selection.getEndRes() - selection.getStartRes()) < 2))
106 while (!found && (seqIndex < end))
108 seq = (Sequence) alignment.getSequenceAt(seqIndex);
110 if ((selection != null && selection.getSize() > 0)
111 && !selection.getSequences(null).contains(seq))
121 // test for one off matches - sequence position and sequence ID
122 // //// is the searchString a residue number?
125 int res = Integer.parseInt(searchString);
126 // possibly a residue number - check if valid for seq
127 if (seq.getEnd() >= res)
129 searchResults.addResult(seq, res, res);
131 // resIndex=seq.getLength();
139 } catch (NumberFormatException ex)
143 if (regex.search(seq.getName()))
145 idMatch.addElement(seq);
149 // stop and return the match
155 if (isIncludeDescription() && seq.getDescription() != null
156 && regex.search(seq.getDescription()))
158 idMatch.addElement(seq);
162 // stop and return the match
168 item = seq.getSequenceAsString();
170 if ((selection != null)
171 && (selection.getEndRes() < alignment.getWidth() - 1))
173 item = item.substring(0, selection.getEndRes() + 1);
176 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
177 StringBuffer noGapsSB = new StringBuffer();
179 Vector spaces = new Vector();
181 for (int j = 0; j < item.length(); j++)
183 if (!jalview.util.Comparison.isGap(item.charAt(j)))
185 noGapsSB.append(item.charAt(j));
186 spaces.addElement(new Integer(insertCount));
194 String noGaps = noGapsSB.toString();
196 for (int r = resIndex; r < noGaps.length(); r++)
199 if (regex.searchFrom(noGaps, r))
201 resIndex = regex.matchedFrom();
203 if ((selection != null && selection.getSize() > 0)
204 && ((resIndex + Integer.parseInt(spaces.elementAt(
205 resIndex).toString())) < selection.getStartRes()))
209 // if invalid string used, then regex has no matched to/from
211 .findPosition(resIndex
212 + Integer.parseInt(spaces.elementAt(resIndex)
214 int eres = seq.findPosition(regex.matchedTo()
216 + Integer.parseInt(spaces
217 .elementAt(regex.matchedTo() - 1).toString()));
219 searchResults.addResult(seq, sres, eres);
223 // thats enough, break and display the result
246 * We now search the Id string in the main search loop. for (int id = 0; id
247 * < alignment.getHeight(); id++) { if
248 * (regex.search(alignment.getSequenceAt(id).getName())) {
249 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
255 * @return the alignment
257 public AlignmentI getAlignment()
264 * the alignment to set
266 public void setAlignment(AlignmentI alignment)
268 this.alignment = alignment;
272 * @return the caseSensitive
274 public boolean isCaseSensitive()
276 return caseSensitive;
280 * @param caseSensitive
281 * the caseSensitive to set
283 public void setCaseSensitive(boolean caseSensitive)
285 this.caseSensitive = caseSensitive;
289 * @return the findAll
291 public boolean isFindAll()
300 public void setFindAll(boolean findAll)
302 this.findAll = findAll;
306 * @return the selection
308 public jalview.datamodel.SequenceGroup getSelection()
315 * the selection to set
317 public void setSelection(jalview.datamodel.SequenceGroup selection)
319 this.selection = selection;
323 * @return the idMatch
325 public Vector getIdMatch()
333 public com.stevesoft.pat.Regex getRegex()
339 * @return the searchResults
341 public SearchResults getSearchResults()
343 return searchResults;
347 * @return the resIndex
349 public int getResIndex()
356 * the resIndex to set
358 public void setResIndex(int resIndex)
360 this.resIndex = resIndex;
364 * @return the seqIndex
366 public int getSeqIndex()
373 * the seqIndex to set
375 public void setSeqIndex(int seqIndex)
377 this.seqIndex = seqIndex;
380 public boolean isIncludeDescription()
382 return includeDescription;
385 public void setIncludeDescription(boolean includeDescription)
387 this.includeDescription = includeDescription;