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;
32 jalview.datamodel.SequenceGroup selection = null;
33 Vector idMatch = null;
34 boolean caseSensitive = false;
35 boolean findAll = false;
36 com.stevesoft.pat.Regex regex = null;
38 * hold's last-searched position between calles to find(false)
40 int seqIndex = 0, resIndex = 0;
41 public Finder(AlignmentI alignment, SequenceGroup selection)
43 this.alignment = alignment;
44 this.selection = selection;
47 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
48 int seqIndex, int resIndex)
50 this(alignment, selectionGroup);
51 this.seqIndex = seqIndex;
52 this.resIndex = resIndex;
55 public boolean find(String searchString)
57 boolean hasResults = false;
60 searchString = searchString.toUpperCase();
62 regex = new com.stevesoft.pat.Regex(searchString);
63 searchResults = new SearchResults();
64 idMatch = new Vector();
67 boolean found = false;
69 ////// is the searchString a residue number?
72 int res = Integer.parseInt(searchString);
74 if (selection == null || selection.getSize() < 1)
76 seq = (Sequence) alignment.getSequenceAt(0);
80 seq = (Sequence) (selection.getSequenceAt(0));
83 searchResults.addResult(seq, res, res);
86 catch (NumberFormatException ex)
90 ///////////////////////////////////////////////
92 int end = alignment.getHeight();
94 if (selection != null)
96 if ( (selection.getSize() < 1) ||
97 ( (selection.getEndRes() - selection.getStartRes()) < 2))
103 while (!found && (seqIndex < end))
105 seq = (Sequence) alignment.getSequenceAt(seqIndex);
107 if ( (selection != null) && !selection.getSequences(null).contains(seq))
115 item = seq.getSequenceAsString();
117 item = item.toUpperCase();
119 if ( (selection != null) &&
120 (selection.getEndRes() < alignment.getWidth() - 1))
122 item = item.substring(0, selection.getEndRes() + 1);
125 ///Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
126 StringBuffer noGapsSB = new StringBuffer();
128 Vector spaces = new Vector();
130 for (int j = 0; j < item.length(); j++)
132 if (!jalview.util.Comparison.isGap(item.charAt(j)))
134 noGapsSB.append(item.charAt(j));
135 spaces.addElement(new Integer(insertCount));
143 String noGaps = noGapsSB.toString();
145 for (int r = resIndex; r < noGaps.length(); r++)
148 if (regex.searchFrom(noGaps, r))
150 resIndex = regex.matchedFrom();
152 if ( (selection != null) &&
154 Integer.parseInt(spaces.elementAt(resIndex).toString())) <
155 selection.getStartRes()))
160 int sres = seq.findPosition(resIndex +
161 Integer.parseInt(spaces.elementAt(
164 int eres = seq.findPosition(regex.matchedTo() - 1 +
165 Integer.parseInt(spaces.elementAt(regex.
169 searchResults.addResult(seq, sres, eres);
173 // thats enough, break and display the result
195 for (int id = 0; id < alignment.getHeight(); id++)
197 if (regex.search(alignment.getSequenceAt(id).getName()))
199 idMatch.addElement(alignment.getSequenceAt(id));
207 * @return the alignment
209 public AlignmentI getAlignment()
215 * @param alignment the alignment to set
217 public void setAlignment(AlignmentI alignment)
219 this.alignment = alignment;
223 * @return the caseSensitive
225 public boolean isCaseSensitive()
227 return caseSensitive;
231 * @param caseSensitive the caseSensitive to set
233 public void setCaseSensitive(boolean caseSensitive)
235 this.caseSensitive = caseSensitive;
239 * @return the findAll
241 public boolean isFindAll()
247 * @param findAll the findAll to set
249 public void setFindAll(boolean findAll)
251 this.findAll = findAll;
255 * @return the selection
257 public jalview.datamodel.SequenceGroup getSelection()
263 * @param selection the selection to set
265 public void setSelection(jalview.datamodel.SequenceGroup selection)
267 this.selection = selection;
271 * @return the idMatch
273 public Vector getIdMatch()
281 public com.stevesoft.pat.Regex getRegex()
287 * @return the searchResults
289 public SearchResults getSearchResults()
291 return searchResults;
295 * @return the resIndex
297 public int getResIndex()
303 * @param resIndex the resIndex to set
305 public void setResIndex(int resIndex)
307 this.resIndex = resIndex;
311 * @return the seqIndex
313 public int getSeqIndex()
319 * @param seqIndex the seqIndex to set
321 public void setSeqIndex(int seqIndex)
323 this.seqIndex = seqIndex;