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 private boolean includeDescription = false;
47 boolean findAll = false;
49 com.stevesoft.pat.Regex regex = null;
52 * hold's last-searched position between calles to find(false)
54 int seqIndex = 0, resIndex = -1;
56 public Finder(AlignmentI alignment, SequenceGroup selection)
58 this.alignment = alignment;
59 this.selection = selection;
63 * restart search at given sequence and residue on alignment and (optionally)
64 * contained in selection
67 * @param selectionGroup
71 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
72 int seqIndex, int resIndex)
74 this(alignment, selectionGroup);
75 this.seqIndex = seqIndex;
76 this.resIndex = resIndex;
79 public boolean find(String searchString)
81 boolean hasResults = false;
84 searchString = searchString.toUpperCase();
86 regex = new com.stevesoft.pat.Regex(searchString);
87 regex.setIgnoreCase(!caseSensitive);
88 searchResults = new SearchResults();
89 idMatch = new Vector();
92 boolean found = false;
93 int end = alignment.getHeight();
95 // /////////////////////////////////////////////
97 if (selection != null)
99 if ((selection.getSize() < 1)
100 || ((selection.getEndRes() - selection.getStartRes()) < 2))
106 while (!found && (seqIndex < end))
108 seq = (Sequence) alignment.getSequenceAt(seqIndex);
110 if ((selection != null && selection.getSize() > 0)
111 && !selection.getSequences(null).contains(seq))
121 // test for one off matches - sequence position and sequence ID
122 // //// is the searchString a residue number?
125 int res = Integer.parseInt(searchString);
126 // possibly a residue number - check if valid for seq
127 if (seq.getEnd() >= res)
129 searchResults.addResult(seq, res, res);
131 // resIndex=seq.getLength();
139 } catch (NumberFormatException ex)
143 if (regex.search(seq.getName()))
145 idMatch.addElement(seq);
149 // stop and return the match
155 if (isIncludeDescription() && regex.search(seq.getDescription()))
157 idMatch.addElement(seq);
161 // stop and return the match
167 item = seq.getSequenceAsString();
169 if ((selection != null)
170 && (selection.getEndRes() < alignment.getWidth() - 1))
172 item = item.substring(0, selection.getEndRes() + 1);
175 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
176 StringBuffer noGapsSB = new StringBuffer();
178 Vector spaces = new Vector();
180 for (int j = 0; j < item.length(); j++)
182 if (!jalview.util.Comparison.isGap(item.charAt(j)))
184 noGapsSB.append(item.charAt(j));
185 spaces.addElement(new Integer(insertCount));
193 String noGaps = noGapsSB.toString();
195 for (int r = resIndex; r < noGaps.length(); r++)
198 if (regex.searchFrom(noGaps, r))
200 resIndex = regex.matchedFrom();
202 if ((selection != null && selection.getSize() > 0)
203 && ((resIndex + Integer.parseInt(spaces.elementAt(
204 resIndex).toString())) < selection.getStartRes()))
208 // if invalid string used, then regex has no matched to/from
210 .findPosition(resIndex
211 + Integer.parseInt(spaces.elementAt(resIndex)
213 int eres = seq.findPosition(regex.matchedTo()
215 + Integer.parseInt(spaces
216 .elementAt(regex.matchedTo() - 1).toString()));
218 searchResults.addResult(seq, sres, eres);
222 // thats enough, break and display the result
245 * We now search the Id string in the main search loop. for (int id = 0; id
246 * < alignment.getHeight(); id++) { if
247 * (regex.search(alignment.getSequenceAt(id).getName())) {
248 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
254 * @return the alignment
256 public AlignmentI getAlignment()
263 * the alignment to set
265 public void setAlignment(AlignmentI alignment)
267 this.alignment = alignment;
271 * @return the caseSensitive
273 public boolean isCaseSensitive()
275 return caseSensitive;
279 * @param caseSensitive
280 * the caseSensitive to set
282 public void setCaseSensitive(boolean caseSensitive)
284 this.caseSensitive = caseSensitive;
288 * @return the findAll
290 public boolean isFindAll()
299 public void setFindAll(boolean findAll)
301 this.findAll = findAll;
305 * @return the selection
307 public jalview.datamodel.SequenceGroup getSelection()
314 * the selection to set
316 public void setSelection(jalview.datamodel.SequenceGroup selection)
318 this.selection = selection;
322 * @return the idMatch
324 public Vector getIdMatch()
332 public com.stevesoft.pat.Regex getRegex()
338 * @return the searchResults
340 public SearchResults getSearchResults()
342 return searchResults;
346 * @return the resIndex
348 public int getResIndex()
355 * the resIndex to set
357 public void setResIndex(int resIndex)
359 this.resIndex = resIndex;
363 * @return the seqIndex
365 public int getSeqIndex()
372 * the seqIndex to set
374 public void setSeqIndex(int seqIndex)
376 this.seqIndex = seqIndex;
379 public boolean isIncludeDescription()
381 return includeDescription;
384 public void setIncludeDescription(boolean includeDescription)
386 this.includeDescription = includeDescription;