2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
23 import jalview.datamodel.*;
28 * Implements the search algorithms for the Find dialog box.
30 SearchResults searchResults;
34 jalview.datamodel.SequenceGroup selection = null;
36 Vector idMatch = null;
38 boolean caseSensitive = false;
40 boolean findAll = false;
42 com.stevesoft.pat.Regex regex = null;
45 * hold's last-searched position between calles to find(false)
47 int seqIndex = 0, resIndex = -1;
49 public Finder(AlignmentI alignment, SequenceGroup selection)
51 this.alignment = alignment;
52 this.selection = selection;
56 * restart search at given sequence and residue on alignment and (optionally) contained in selection
58 * @param selectionGroup
62 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
63 int seqIndex, int resIndex)
65 this(alignment, selectionGroup);
66 this.seqIndex = seqIndex;
67 this.resIndex = resIndex;
70 public boolean find(String searchString)
72 boolean hasResults = false;
75 searchString = searchString.toUpperCase();
77 regex = new com.stevesoft.pat.Regex(searchString);
78 regex.setIgnoreCase(!caseSensitive);
79 searchResults = new SearchResults();
80 idMatch = new Vector();
83 boolean found = false;
85 // //// is the searchString a residue number?
88 int res = Integer.parseInt(searchString);
90 if (selection == null || selection.getSize() < 1)
92 seq = (Sequence) alignment.getSequenceAt(0);
96 seq = (Sequence) (selection.getSequenceAt(0));
99 searchResults.addResult(seq, res, res);
101 } catch (NumberFormatException ex)
105 // /////////////////////////////////////////////
107 int end = alignment.getHeight();
109 if (selection != null)
111 if ((selection.getSize() < 1)
112 || ((selection.getEndRes() - selection.getStartRes()) < 2))
118 while (!found && (seqIndex < end))
120 seq = (Sequence) alignment.getSequenceAt(seqIndex);
122 if ((selection != null && selection.getSize()>0)
123 && !selection.getSequences(null).contains(seq))
132 if (regex.search(seq.getName()))
134 idMatch.addElement(seq);
138 // stop and return the match
144 item = seq.getSequenceAsString();
146 if ((selection != null)
147 && (selection.getEndRes() < alignment.getWidth() - 1))
149 item = item.substring(0, selection.getEndRes() + 1);
152 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
153 StringBuffer noGapsSB = new StringBuffer();
155 Vector spaces = new Vector();
157 for (int j = 0; j < item.length(); j++)
159 if (!jalview.util.Comparison.isGap(item.charAt(j)))
161 noGapsSB.append(item.charAt(j));
162 spaces.addElement(new Integer(insertCount));
170 String noGaps = noGapsSB.toString();
172 for (int r = resIndex; r < noGaps.length(); r++)
175 if (regex.searchFrom(noGaps, r))
177 resIndex = regex.matchedFrom();
179 if ((selection != null && selection.getSize()>0)
180 && ((resIndex + Integer.parseInt(spaces.elementAt(
181 resIndex).toString())) < selection.getStartRes()))
187 .findPosition(resIndex
188 + Integer.parseInt(spaces.elementAt(resIndex)
190 int eres = seq.findPosition(regex.matchedTo()
192 + Integer.parseInt(spaces
193 .elementAt(regex.matchedTo() - 1).toString()));
195 searchResults.addResult(seq, sres, eres);
199 // thats enough, break and display the result
222 * We now search the Id string in the main search loop.
223 * for (int id = 0; id < alignment.getHeight(); id++)
225 if (regex.search(alignment.getSequenceAt(id).getName()))
227 idMatch.addElement(alignment.getSequenceAt(id));
235 * @return the alignment
237 public AlignmentI getAlignment()
244 * the alignment to set
246 public void setAlignment(AlignmentI alignment)
248 this.alignment = alignment;
252 * @return the caseSensitive
254 public boolean isCaseSensitive()
256 return caseSensitive;
260 * @param caseSensitive
261 * the caseSensitive to set
263 public void setCaseSensitive(boolean caseSensitive)
265 this.caseSensitive = caseSensitive;
269 * @return the findAll
271 public boolean isFindAll()
280 public void setFindAll(boolean findAll)
282 this.findAll = findAll;
286 * @return the selection
288 public jalview.datamodel.SequenceGroup getSelection()
295 * the selection to set
297 public void setSelection(jalview.datamodel.SequenceGroup selection)
299 this.selection = selection;
303 * @return the idMatch
305 public Vector getIdMatch()
313 public com.stevesoft.pat.Regex getRegex()
319 * @return the searchResults
321 public SearchResults getSearchResults()
323 return searchResults;
327 * @return the resIndex
329 public int getResIndex()
336 * the resIndex to set
338 public void setResIndex(int resIndex)
340 this.resIndex = resIndex;
344 * @return the seqIndex
346 public int getSeqIndex()
353 * the seqIndex to set
355 public void setSeqIndex(int seqIndex)
357 this.seqIndex = seqIndex;