2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
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 = -1;
49 public Finder(AlignmentI alignment, SequenceGroup selection)
51 this.alignment = alignment;
52 this.selection = selection;
56 * restart search at given sequence and residue on alignment and (optionally)
57 * contained in selection
60 * @param selectionGroup
64 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
65 int seqIndex, int resIndex)
67 this(alignment, selectionGroup);
68 this.seqIndex = seqIndex;
69 this.resIndex = resIndex;
72 public boolean find(String searchString)
74 boolean hasResults = false;
77 searchString = searchString.toUpperCase();
79 regex = new com.stevesoft.pat.Regex(searchString);
80 regex.setIgnoreCase(!caseSensitive);
81 searchResults = new SearchResults();
82 idMatch = new Vector();
85 boolean found = false;
86 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();
132 } catch (NumberFormatException ex)
136 if (regex.search(seq.getName()))
138 idMatch.addElement(seq);
142 // stop and return the match
148 item = seq.getSequenceAsString();
150 if ((selection != null)
151 && (selection.getEndRes() < alignment.getWidth() - 1))
153 item = item.substring(0, selection.getEndRes() + 1);
156 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
157 StringBuffer noGapsSB = new StringBuffer();
159 Vector spaces = new Vector();
161 for (int j = 0; j < item.length(); j++)
163 if (!jalview.util.Comparison.isGap(item.charAt(j)))
165 noGapsSB.append(item.charAt(j));
166 spaces.addElement(new Integer(insertCount));
174 String noGaps = noGapsSB.toString();
176 for (int r = resIndex; r < noGaps.length(); r++)
179 if (regex.searchFrom(noGaps, r))
181 resIndex = regex.matchedFrom();
183 if ((selection != null && selection.getSize() > 0)
184 && ((resIndex + Integer.parseInt(spaces.elementAt(
185 resIndex).toString())) < selection.getStartRes()))
191 .findPosition(resIndex
192 + Integer.parseInt(spaces.elementAt(resIndex)
194 int eres = seq.findPosition(regex.matchedTo()
196 + Integer.parseInt(spaces
197 .elementAt(regex.matchedTo() - 1).toString()));
199 searchResults.addResult(seq, sres, eres);
203 // thats enough, break and display the result
226 * We now search the Id string in the main search loop. for (int id = 0; id
227 * < alignment.getHeight(); id++) { if
228 * (regex.search(alignment.getSequenceAt(id).getName())) {
229 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
235 * @return the alignment
237 public AlignmentI getAlignment()
244 * the alignment to set
246 public void setAlignment(AlignmentI alignment)
248 this.alignment = alignment;
252 * @return the caseSensitive
254 public boolean isCaseSensitive()
256 return caseSensitive;
260 * @param caseSensitive
261 * the caseSensitive to set
263 public void setCaseSensitive(boolean caseSensitive)
265 this.caseSensitive = caseSensitive;
269 * @return the findAll
271 public boolean isFindAll()
280 public void setFindAll(boolean findAll)
282 this.findAll = findAll;
286 * @return the selection
288 public jalview.datamodel.SequenceGroup getSelection()
295 * the selection to set
297 public void setSelection(jalview.datamodel.SequenceGroup selection)
299 this.selection = selection;
303 * @return the idMatch
305 public Vector getIdMatch()
313 public com.stevesoft.pat.Regex getRegex()
319 * @return the searchResults
321 public SearchResults getSearchResults()
323 return searchResults;
327 * @return the resIndex
329 public int getResIndex()
336 * the resIndex to set
338 public void setResIndex(int resIndex)
340 this.resIndex = resIndex;
344 * @return the seqIndex
346 public int getSeqIndex()
353 * the seqIndex to set
355 public void setSeqIndex(int seqIndex)
357 this.seqIndex = seqIndex;