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.SearchResultMatchI;
25 import jalview.datamodel.SearchResults;
26 import jalview.datamodel.SearchResultsI;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceGroup;
30 import java.util.Vector;
35 * Implements the search algorithms for the Find dialog box.
37 SearchResultsI searchResults;
41 jalview.datamodel.SequenceGroup selection = null;
43 Vector idMatch = null;
45 boolean caseSensitive = false;
47 private boolean includeDescription = false;
49 boolean findAll = false;
51 com.stevesoft.pat.Regex regex = null;
54 * hold's last-searched position between calles to find(false)
56 int seqIndex = 0, resIndex = -1;
58 public Finder(AlignmentI alignment, SequenceGroup selection)
60 this.alignment = alignment;
61 this.selection = selection;
65 * restart search at given sequence and residue on alignment and (optionally)
66 * contained in selection
69 * @param selectionGroup
73 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
74 int seqIndex, int resIndex)
76 this(alignment, selectionGroup);
77 this.seqIndex = seqIndex;
78 this.resIndex = resIndex;
81 public boolean find(String searchString)
83 boolean hasResults = false;
86 searchString = searchString.toUpperCase();
88 regex = new com.stevesoft.pat.Regex(searchString);
89 regex.setIgnoreCase(!caseSensitive);
90 searchResults = new SearchResults();
91 idMatch = new Vector();
94 boolean found = false;
95 int end = alignment.getHeight();
97 // /////////////////////////////////////////////
99 if (selection != null)
101 if ((selection.getSize() < 1)
102 || ((selection.getEndRes() - selection.getStartRes()) < 2))
107 SearchResultMatchI lastm = null;
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 (!jalview.util.Comparison.isGap(item.charAt(j)))
188 noGapsSB.append(item.charAt(j));
189 spaces.addElement(new Integer(insertCount));
197 String noGaps = noGapsSB.toString();
198 for (int r = resIndex; r < noGaps.length(); r++)
201 if (regex.searchFrom(noGaps, r))
203 resIndex = regex.matchedFrom();
205 if ((selection != null && selection.getSize() > 0)
206 && ((resIndex + Integer.parseInt(spaces.elementAt(
207 resIndex).toString())) < selection.getStartRes()))
211 // if invalid string used, then regex has no matched to/from
213 .findPosition(resIndex
214 + Integer.parseInt(spaces.elementAt(resIndex)
216 int eres = seq.findPosition(regex.matchedTo()
218 + Integer.parseInt(spaces
219 .elementAt(regex.matchedTo() - 1).toString()));
220 // only add result if not contained in previous result
222 || (lastm.getSequence() != seq || (!(lastm.getStart() <= sres && lastm
223 .getEnd() >= eres))))
225 lastm = searchResults.addResult(seq, sres, eres);
230 // thats enough, break and display the result
253 * We now search the Id string in the main search loop. for (int id = 0; id
254 * < alignment.getHeight(); id++) { if
255 * (regex.search(alignment.getSequenceAt(id).getName())) {
256 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
262 * @return the alignment
264 public AlignmentI getAlignment()
271 * the alignment to set
273 public void setAlignment(AlignmentI alignment)
275 this.alignment = alignment;
279 * @return the caseSensitive
281 public boolean isCaseSensitive()
283 return caseSensitive;
287 * @param caseSensitive
288 * the caseSensitive to set
290 public void setCaseSensitive(boolean caseSensitive)
292 this.caseSensitive = caseSensitive;
296 * @return the findAll
298 public boolean isFindAll()
307 public void setFindAll(boolean findAll)
309 this.findAll = findAll;
313 * @return the selection
315 public jalview.datamodel.SequenceGroup getSelection()
322 * the selection to set
324 public void setSelection(jalview.datamodel.SequenceGroup selection)
326 this.selection = selection;
330 * @return the idMatch
332 public Vector getIdMatch()
340 public com.stevesoft.pat.Regex getRegex()
346 * @return the searchResults
348 public SearchResultsI getSearchResults()
350 return searchResults;
354 * @return the resIndex
356 public int getResIndex()
363 * the resIndex to set
365 public void setResIndex(int resIndex)
367 this.resIndex = resIndex;
371 * @return the seqIndex
373 public int getSeqIndex()
380 * the seqIndex to set
382 public void setSeqIndex(int seqIndex)
384 this.seqIndex = seqIndex;
387 public boolean isIncludeDescription()
389 return includeDescription;
392 public void setIncludeDescription(boolean includeDescription)
394 this.includeDescription = includeDescription;