2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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;
85 int end = alignment.getHeight();
88 // /////////////////////////////////////////////
90 if (selection != null)
92 if ((selection.getSize() < 1)
93 || ((selection.getEndRes() - selection.getStartRes()) < 2))
99 while (!found && (seqIndex < end))
101 seq = (Sequence) alignment.getSequenceAt(seqIndex);
103 if ((selection != null && selection.getSize() > 0)
104 && !selection.getSequences(null).contains(seq))
114 // test for one off matches - sequence position and sequence ID
115 // //// is the searchString a residue number?
118 int res = Integer.parseInt(searchString);
119 // possibly a residue number - check if valid for seq
120 if (seq.getEnd() >= res)
122 searchResults.addResult(seq, res, res);
124 //resIndex=seq.getLength();
131 } catch (NumberFormatException ex)
135 if (regex.search(seq.getName()))
137 idMatch.addElement(seq);
141 // stop and return the match
147 item = seq.getSequenceAsString();
149 if ((selection != null)
150 && (selection.getEndRes() < alignment.getWidth() - 1))
152 item = item.substring(0, selection.getEndRes() + 1);
155 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
156 StringBuffer noGapsSB = new StringBuffer();
158 Vector spaces = new Vector();
160 for (int j = 0; j < item.length(); j++)
162 if (!jalview.util.Comparison.isGap(item.charAt(j)))
164 noGapsSB.append(item.charAt(j));
165 spaces.addElement(new Integer(insertCount));
173 String noGaps = noGapsSB.toString();
175 for (int r = resIndex; r < noGaps.length(); r++)
178 if (regex.searchFrom(noGaps, r))
180 resIndex = regex.matchedFrom();
182 if ((selection != null && selection.getSize() > 0)
183 && ((resIndex + Integer.parseInt(spaces.elementAt(
184 resIndex).toString())) < selection.getStartRes()))
190 .findPosition(resIndex
191 + Integer.parseInt(spaces.elementAt(resIndex)
193 int eres = seq.findPosition(regex.matchedTo()
195 + Integer.parseInt(spaces
196 .elementAt(regex.matchedTo() - 1).toString()));
198 searchResults.addResult(seq, sres, eres);
202 // thats enough, break and display the result
225 * We now search the Id string in the main search loop. for (int id = 0; id
226 * < alignment.getHeight(); id++) { if
227 * (regex.search(alignment.getSequenceAt(id).getName())) {
228 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
234 * @return the alignment
236 public AlignmentI getAlignment()
243 * the alignment to set
245 public void setAlignment(AlignmentI alignment)
247 this.alignment = alignment;
251 * @return the caseSensitive
253 public boolean isCaseSensitive()
255 return caseSensitive;
259 * @param caseSensitive
260 * the caseSensitive to set
262 public void setCaseSensitive(boolean caseSensitive)
264 this.caseSensitive = caseSensitive;
268 * @return the findAll
270 public boolean isFindAll()
279 public void setFindAll(boolean findAll)
281 this.findAll = findAll;
285 * @return the selection
287 public jalview.datamodel.SequenceGroup getSelection()
294 * the selection to set
296 public void setSelection(jalview.datamodel.SequenceGroup selection)
298 this.selection = selection;
302 * @return the idMatch
304 public Vector getIdMatch()
312 public com.stevesoft.pat.Regex getRegex()
318 * @return the searchResults
320 public SearchResults getSearchResults()
322 return searchResults;
326 * @return the resIndex
328 public int getResIndex()
335 * the resIndex to set
337 public void setResIndex(int resIndex)
339 this.resIndex = resIndex;
343 * @return the seqIndex
345 public int getSeqIndex()
352 * the seqIndex to set
354 public void setSeqIndex(int seqIndex)
356 this.seqIndex = seqIndex;