2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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 regex.setIgnoreCase(!caseSensitive);
72 searchResults = new SearchResults();
73 idMatch = new Vector();
76 boolean found = false;
78 // //// is the searchString a residue number?
81 int res = Integer.parseInt(searchString);
83 if (selection == null || selection.getSize() < 1)
85 seq = (Sequence) alignment.getSequenceAt(0);
89 seq = (Sequence) (selection.getSequenceAt(0));
92 searchResults.addResult(seq, res, res);
94 } catch (NumberFormatException ex)
98 // /////////////////////////////////////////////
100 int end = alignment.getHeight();
102 if (selection != null)
104 if ((selection.getSize() < 1)
105 || ((selection.getEndRes() - selection.getStartRes()) < 2))
111 while (!found && (seqIndex < end))
113 seq = (Sequence) alignment.getSequenceAt(seqIndex);
115 if ((selection != null && selection.getSize()>0)
116 && !selection.getSequences(null).contains(seq))
124 item = seq.getSequenceAsString();
126 if ((selection != null)
127 && (selection.getEndRes() < alignment.getWidth() - 1))
129 item = item.substring(0, selection.getEndRes() + 1);
132 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
133 StringBuffer noGapsSB = new StringBuffer();
135 Vector spaces = new Vector();
137 for (int j = 0; j < item.length(); j++)
139 if (!jalview.util.Comparison.isGap(item.charAt(j)))
141 noGapsSB.append(item.charAt(j));
142 spaces.addElement(new Integer(insertCount));
150 String noGaps = noGapsSB.toString();
152 for (int r = resIndex; r < noGaps.length(); r++)
155 if (regex.searchFrom(noGaps, r))
157 resIndex = regex.matchedFrom();
159 if ((selection != null && selection.getSize()>0)
160 && ((resIndex + Integer.parseInt(spaces.elementAt(
161 resIndex).toString())) < selection.getStartRes()))
167 .findPosition(resIndex
168 + Integer.parseInt(spaces.elementAt(resIndex)
170 int eres = seq.findPosition(regex.matchedTo()
172 + Integer.parseInt(spaces
173 .elementAt(regex.matchedTo() - 1).toString()));
175 searchResults.addResult(seq, sres, eres);
179 // thats enough, break and display the result
201 for (int id = 0; id < alignment.getHeight(); id++)
203 if (regex.search(alignment.getSequenceAt(id).getName()))
205 idMatch.addElement(alignment.getSequenceAt(id));
213 * @return the alignment
215 public AlignmentI getAlignment()
222 * the alignment to set
224 public void setAlignment(AlignmentI alignment)
226 this.alignment = alignment;
230 * @return the caseSensitive
232 public boolean isCaseSensitive()
234 return caseSensitive;
238 * @param caseSensitive
239 * the caseSensitive to set
241 public void setCaseSensitive(boolean caseSensitive)
243 this.caseSensitive = caseSensitive;
247 * @return the findAll
249 public boolean isFindAll()
258 public void setFindAll(boolean findAll)
260 this.findAll = findAll;
264 * @return the selection
266 public jalview.datamodel.SequenceGroup getSelection()
273 * the selection to set
275 public void setSelection(jalview.datamodel.SequenceGroup selection)
277 this.selection = selection;
281 * @return the idMatch
283 public Vector getIdMatch()
291 public com.stevesoft.pat.Regex getRegex()
297 * @return the searchResults
299 public SearchResults getSearchResults()
301 return searchResults;
305 * @return the resIndex
307 public int getResIndex()
314 * the resIndex to set
316 public void setResIndex(int resIndex)
318 this.resIndex = resIndex;
322 * @return the seqIndex
324 public int getSeqIndex()
331 * the seqIndex to set
333 public void setSeqIndex(int seqIndex)
335 this.seqIndex = seqIndex;