2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.analysis;
22 import jalview.datamodel.*;
27 * Implements the search algorithms for the Find dialog box.
29 SearchResults searchResults;
33 jalview.datamodel.SequenceGroup selection = null;
35 Vector idMatch = null;
37 boolean caseSensitive = false;
39 boolean findAll = false;
41 com.stevesoft.pat.Regex regex = null;
44 * hold's last-searched position between calles to find(false)
46 int seqIndex = 0, resIndex = -1;
48 public Finder(AlignmentI alignment, SequenceGroup selection)
50 this.alignment = alignment;
51 this.selection = selection;
55 * restart search at given sequence and residue on alignment and (optionally)
56 * contained in selection
59 * @param selectionGroup
63 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
64 int seqIndex, int resIndex)
66 this(alignment, selectionGroup);
67 this.seqIndex = seqIndex;
68 this.resIndex = resIndex;
71 public boolean find(String searchString)
73 boolean hasResults = false;
76 searchString = searchString.toUpperCase();
78 regex = new com.stevesoft.pat.Regex(searchString);
79 regex.setIgnoreCase(!caseSensitive);
80 searchResults = new SearchResults();
81 idMatch = new Vector();
84 boolean found = false;
86 // //// is the searchString a residue number?
89 int res = Integer.parseInt(searchString);
91 if (selection == null || selection.getSize() < 1)
93 seq = (Sequence) alignment.getSequenceAt(0);
97 seq = (Sequence) (selection.getSequenceAt(0));
100 searchResults.addResult(seq, res, res);
102 } catch (NumberFormatException ex)
106 // /////////////////////////////////////////////
108 int end = alignment.getHeight();
110 if (selection != null)
112 if ((selection.getSize() < 1)
113 || ((selection.getEndRes() - selection.getStartRes()) < 2))
119 while (!found && (seqIndex < end))
121 seq = (Sequence) alignment.getSequenceAt(seqIndex);
123 if ((selection != null && selection.getSize() > 0)
124 && !selection.getSequences(null).contains(seq))
134 if (regex.search(seq.getName()))
136 idMatch.addElement(seq);
140 // stop and return the match
146 item = seq.getSequenceAsString();
148 if ((selection != null)
149 && (selection.getEndRes() < alignment.getWidth() - 1))
151 item = item.substring(0, selection.getEndRes() + 1);
154 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
155 StringBuffer noGapsSB = new StringBuffer();
157 Vector spaces = new Vector();
159 for (int j = 0; j < item.length(); j++)
161 if (!jalview.util.Comparison.isGap(item.charAt(j)))
163 noGapsSB.append(item.charAt(j));
164 spaces.addElement(new Integer(insertCount));
172 String noGaps = noGapsSB.toString();
174 for (int r = resIndex; r < noGaps.length(); r++)
177 if (regex.searchFrom(noGaps, r))
179 resIndex = regex.matchedFrom();
181 if ((selection != null && selection.getSize() > 0)
182 && ((resIndex + Integer.parseInt(spaces.elementAt(
183 resIndex).toString())) < selection.getStartRes()))
189 .findPosition(resIndex
190 + Integer.parseInt(spaces.elementAt(resIndex)
192 int eres = seq.findPosition(regex.matchedTo()
194 + Integer.parseInt(spaces
195 .elementAt(regex.matchedTo() - 1).toString()));
197 searchResults.addResult(seq, sres, eres);
201 // thats enough, break and display the result
224 * We now search the Id string in the main search loop. for (int id = 0; id
225 * < alignment.getHeight(); id++) { if
226 * (regex.search(alignment.getSequenceAt(id).getName())) {
227 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
233 * @return the alignment
235 public AlignmentI getAlignment()
242 * the alignment to set
244 public void setAlignment(AlignmentI alignment)
246 this.alignment = alignment;
250 * @return the caseSensitive
252 public boolean isCaseSensitive()
254 return caseSensitive;
258 * @param caseSensitive
259 * the caseSensitive to set
261 public void setCaseSensitive(boolean caseSensitive)
263 this.caseSensitive = caseSensitive;
267 * @return the findAll
269 public boolean isFindAll()
278 public void setFindAll(boolean findAll)
280 this.findAll = findAll;
284 * @return the selection
286 public jalview.datamodel.SequenceGroup getSelection()
293 * the selection to set
295 public void setSelection(jalview.datamodel.SequenceGroup selection)
297 this.selection = selection;
301 * @return the idMatch
303 public Vector getIdMatch()
311 public com.stevesoft.pat.Regex getRegex()
317 * @return the searchResults
319 public SearchResults getSearchResults()
321 return searchResults;
325 * @return the resIndex
327 public int getResIndex()
334 * the resIndex to set
336 public void setResIndex(int resIndex)
338 this.resIndex = resIndex;
342 * @return the seqIndex
344 public int getSeqIndex()
351 * the seqIndex to set
353 public void setSeqIndex(int seqIndex)
355 this.seqIndex = seqIndex;