2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4.0.b2)
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 = 0;
49 public Finder(AlignmentI alignment, SequenceGroup selection)
51 this.alignment = alignment;
52 this.selection = selection;
55 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
56 int seqIndex, int resIndex)
58 this(alignment, selectionGroup);
59 this.seqIndex = seqIndex;
60 this.resIndex = resIndex;
63 public boolean find(String searchString)
65 boolean hasResults = false;
68 searchString = searchString.toUpperCase();
70 regex = new com.stevesoft.pat.Regex(searchString);
71 searchResults = new SearchResults();
72 idMatch = new Vector();
75 boolean found = false;
77 // //// is the searchString a residue number?
80 int res = Integer.parseInt(searchString);
82 if (selection == null || selection.getSize() < 1)
84 seq = (Sequence) alignment.getSequenceAt(0);
88 seq = (Sequence) (selection.getSequenceAt(0));
91 searchResults.addResult(seq, res, res);
93 } catch (NumberFormatException ex)
97 // /////////////////////////////////////////////
99 int end = alignment.getHeight();
101 if (selection != null)
103 if ((selection.getSize() < 1)
104 || ((selection.getEndRes() - selection.getStartRes()) < 2))
110 while (!found && (seqIndex < end))
112 seq = (Sequence) alignment.getSequenceAt(seqIndex);
114 if ((selection != null)
115 && !selection.getSequences(null).contains(seq))
123 item = seq.getSequenceAsString();
125 item = item.toUpperCase();
127 if ((selection != null)
128 && (selection.getEndRes() < alignment.getWidth() - 1))
130 item = item.substring(0, selection.getEndRes() + 1);
133 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
134 StringBuffer noGapsSB = new StringBuffer();
136 Vector spaces = new Vector();
138 for (int j = 0; j < item.length(); j++)
140 if (!jalview.util.Comparison.isGap(item.charAt(j)))
142 noGapsSB.append(item.charAt(j));
143 spaces.addElement(new Integer(insertCount));
151 String noGaps = noGapsSB.toString();
153 for (int r = resIndex; r < noGaps.length(); r++)
156 if (regex.searchFrom(noGaps, r))
158 resIndex = regex.matchedFrom();
160 if ((selection != null)
161 && ((resIndex + Integer.parseInt(spaces.elementAt(
162 resIndex).toString())) < selection.getStartRes()))
168 .findPosition(resIndex
169 + Integer.parseInt(spaces.elementAt(resIndex)
171 int eres = seq.findPosition(regex.matchedTo()
173 + Integer.parseInt(spaces
174 .elementAt(regex.matchedTo() - 1).toString()));
176 searchResults.addResult(seq, sres, eres);
180 // thats enough, break and display the result
202 for (int id = 0; id < alignment.getHeight(); id++)
204 if (regex.search(alignment.getSequenceAt(id).getName()))
206 idMatch.addElement(alignment.getSequenceAt(id));
214 * @return the alignment
216 public AlignmentI getAlignment()
223 * the alignment to set
225 public void setAlignment(AlignmentI alignment)
227 this.alignment = alignment;
231 * @return the caseSensitive
233 public boolean isCaseSensitive()
235 return caseSensitive;
239 * @param caseSensitive
240 * the caseSensitive to set
242 public void setCaseSensitive(boolean caseSensitive)
244 this.caseSensitive = caseSensitive;
248 * @return the findAll
250 public boolean isFindAll()
259 public void setFindAll(boolean findAll)
261 this.findAll = findAll;
265 * @return the selection
267 public jalview.datamodel.SequenceGroup getSelection()
274 * the selection to set
276 public void setSelection(jalview.datamodel.SequenceGroup selection)
278 this.selection = selection;
282 * @return the idMatch
284 public Vector getIdMatch()
292 public com.stevesoft.pat.Regex getRegex()
298 * @return the searchResults
300 public SearchResults getSearchResults()
302 return searchResults;
306 * @return the resIndex
308 public int getResIndex()
315 * the resIndex to set
317 public void setResIndex(int resIndex)
319 this.resIndex = resIndex;
323 * @return the seqIndex
325 public int getSeqIndex()
332 * the seqIndex to set
334 public void setSeqIndex(int seqIndex)
336 this.seqIndex = seqIndex;