2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SearchResults;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceGroup;
28 import java.util.Vector;
33 * Implements the search algorithms for the Find dialog box.
35 SearchResults searchResults;
39 jalview.datamodel.SequenceGroup selection = null;
41 Vector idMatch = null;
43 boolean caseSensitive = false;
45 boolean findAll = false;
47 com.stevesoft.pat.Regex regex = null;
50 * hold's last-searched position between calles to find(false)
52 int seqIndex = 0, resIndex = -1;
54 public Finder(AlignmentI alignment, SequenceGroup selection)
56 this.alignment = alignment;
57 this.selection = selection;
61 * restart search at given sequence and residue on alignment and (optionally)
62 * contained in selection
65 * @param selectionGroup
69 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
70 int seqIndex, int resIndex)
72 this(alignment, selectionGroup);
73 this.seqIndex = seqIndex;
74 this.resIndex = resIndex;
77 public boolean find(String searchString)
79 boolean hasResults = false;
82 searchString = searchString.toUpperCase();
84 regex = new com.stevesoft.pat.Regex(searchString);
85 regex.setIgnoreCase(!caseSensitive);
86 searchResults = new SearchResults();
87 idMatch = new Vector();
90 boolean found = false;
91 int end = alignment.getHeight();
93 // /////////////////////////////////////////////
95 if (selection != null)
97 if ((selection.getSize() < 1)
98 || ((selection.getEndRes() - selection.getStartRes()) < 2))
104 while (!found && (seqIndex < end))
106 seq = (Sequence) alignment.getSequenceAt(seqIndex);
108 if ((selection != null && selection.getSize() > 0)
109 && !selection.getSequences(null).contains(seq))
119 // test for one off matches - sequence position and sequence ID
120 // //// is the searchString a residue number?
123 int res = Integer.parseInt(searchString);
124 // possibly a residue number - check if valid for seq
125 if (seq.getEnd() >= res)
127 searchResults.addResult(seq, res, res);
129 // resIndex=seq.getLength();
137 } catch (NumberFormatException ex)
141 if (regex.search(seq.getName()))
143 idMatch.addElement(seq);
147 // stop and return the match
153 if (regex.search(seq.getDescription()))
155 idMatch.addElement(seq);
159 // stop and return the match
165 item = seq.getSequenceAsString();
167 if ((selection != null)
168 && (selection.getEndRes() < alignment.getWidth() - 1))
170 item = item.substring(0, selection.getEndRes() + 1);
173 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
174 StringBuffer noGapsSB = new StringBuffer();
176 Vector spaces = new Vector();
178 for (int j = 0; j < item.length(); j++)
180 if (!jalview.util.Comparison.isGap(item.charAt(j)))
182 noGapsSB.append(item.charAt(j));
183 spaces.addElement(new Integer(insertCount));
191 String noGaps = noGapsSB.toString();
193 for (int r = resIndex; r < noGaps.length(); r++)
196 if (regex.searchFrom(noGaps, r))
198 resIndex = regex.matchedFrom();
200 if ((selection != null && selection.getSize() > 0)
201 && ((resIndex + Integer.parseInt(spaces.elementAt(
202 resIndex).toString())) < selection.getStartRes()))
206 // if invalid string used, then regex has no matched to/from
208 .findPosition(resIndex
209 + Integer.parseInt(spaces.elementAt(resIndex)
211 int eres = seq.findPosition(regex.matchedTo()
213 + Integer.parseInt(spaces
214 .elementAt(regex.matchedTo() - 1).toString()));
216 searchResults.addResult(seq, sres, eres);
220 // thats enough, break and display the result
243 * We now search the Id string in the main search loop. for (int id = 0; id
244 * < alignment.getHeight(); id++) { if
245 * (regex.search(alignment.getSequenceAt(id).getName())) {
246 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
252 * @return the alignment
254 public AlignmentI getAlignment()
261 * the alignment to set
263 public void setAlignment(AlignmentI alignment)
265 this.alignment = alignment;
269 * @return the caseSensitive
271 public boolean isCaseSensitive()
273 return caseSensitive;
277 * @param caseSensitive
278 * the caseSensitive to set
280 public void setCaseSensitive(boolean caseSensitive)
282 this.caseSensitive = caseSensitive;
286 * @return the findAll
288 public boolean isFindAll()
297 public void setFindAll(boolean findAll)
299 this.findAll = findAll;
303 * @return the selection
305 public jalview.datamodel.SequenceGroup getSelection()
312 * the selection to set
314 public void setSelection(jalview.datamodel.SequenceGroup selection)
316 this.selection = selection;
320 * @return the idMatch
322 public Vector getIdMatch()
330 public com.stevesoft.pat.Regex getRegex()
336 * @return the searchResults
338 public SearchResults getSearchResults()
340 return searchResults;
344 * @return the resIndex
346 public int getResIndex()
353 * the resIndex to set
355 public void setResIndex(int resIndex)
357 this.resIndex = resIndex;
361 * @return the seqIndex
363 public int getSeqIndex()
370 * the seqIndex to set
372 public void setSeqIndex(int seqIndex)
374 this.seqIndex = seqIndex;