2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.SearchResultMatchI;
25 import jalview.datamodel.SearchResults;
26 import jalview.datamodel.SearchResultsI;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.util.Comparison;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Vector;
35 import com.stevesoft.pat.Regex;
40 * Implements the search algorithms for the Find dialog box.
42 SearchResultsI searchResults;
46 SequenceGroup selection = null;
48 Vector<SequenceI> idMatch = null;
50 boolean caseSensitive = false;
52 private boolean includeDescription = false;
54 boolean findAll = false;
59 * holds last-searched position between calls to find(false)
61 int seqIndex = 0, resIndex = -1;
63 public Finder(AlignmentI alignment, SequenceGroup selection)
65 this.alignment = alignment;
66 this.selection = selection;
70 * restart search at given sequence and residue on alignment and (optionally)
71 * contained in selection
74 * @param selectionGroup
78 public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
79 int seqIndex, int resIndex)
81 this(alignment, selectionGroup);
82 this.seqIndex = seqIndex;
83 this.resIndex = resIndex;
86 public boolean find(String searchString)
88 boolean hasResults = false;
91 searchString = searchString.toUpperCase();
93 regex = new Regex(searchString);
94 regex.setIgnoreCase(!caseSensitive);
95 searchResults = new SearchResults();
96 idMatch = new Vector<SequenceI>();
98 boolean found = false;
99 int end = alignment.getHeight();
101 // /////////////////////////////////////////////
103 if (selection != null)
105 if ((selection.getSize() < 1)
106 || ((selection.getEndRes() - selection.getStartRes()) < 2))
111 SearchResultMatchI lastm = null;
113 while (!found && (seqIndex < end))
115 SequenceI seq = alignment.getSequenceAt(seqIndex);
117 if ((selection != null && selection.getSize() > 0)
118 && !selection.getSequences(null).contains(seq))
128 // test for one off matches - sequence position and sequence ID
129 // //// is the searchString a residue number?
132 int res = Integer.parseInt(searchString);
133 // possibly a residue number - check if valid for seq
134 if (seq.getEnd() >= res)
136 searchResults.addResult(seq, res, res);
138 // resIndex=seq.getLength();
146 } catch (NumberFormatException ex)
150 if (regex.search(seq.getName()) && !idMatch.contains(seq))
152 idMatch.addElement(seq);
156 // stop and return the match
162 if (isIncludeDescription() && seq.getDescription() != null
163 && regex.search(seq.getDescription())
164 && !idMatch.contains(seq))
166 idMatch.addElement(seq);
170 // stop and return the match
176 item = seq.getSequenceAsString();
178 if ((selection != null)
179 && (selection.getEndRes() < alignment.getWidth() - 1))
181 item = item.substring(0, selection.getEndRes() + 1);
184 // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
185 StringBuilder noGapsSB = new StringBuilder();
187 List<Integer> spaces = new ArrayList<Integer>();
189 for (int j = 0; j < item.length(); j++)
191 if (!Comparison.isGap(item.charAt(j)))
193 noGapsSB.append(item.charAt(j));
194 spaces.add(Integer.valueOf(insertCount));
202 String noGaps = noGapsSB.toString();
203 for (int r = resIndex; r < noGaps.length(); r++)
206 if (regex.searchFrom(noGaps, r))
208 resIndex = regex.matchedFrom();
210 if ((selection != null && selection.getSize() > 0)
211 && (resIndex + spaces.get(resIndex) < selection
216 // if invalid string used, then regex has no matched to/from
217 int sres = seq.findPosition(resIndex + spaces.get(resIndex));
218 int eres = seq.findPosition(regex.matchedTo() - 1
219 + (spaces.get(regex.matchedTo() - 1)));
220 // only add result if not contained in previous result
222 || (lastm.getSequence() != seq || (!(lastm.getStart() <= sres && lastm
223 .getEnd() >= eres))))
225 lastm = searchResults.addResult(seq, sres, eres);
230 // thats enough, break and display the result
253 * We now search the Id string in the main search loop. for (int id = 0; id
254 * < alignment.getHeight(); id++) { if
255 * (regex.search(alignment.getSequenceAt(id).getName())) {
256 * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
262 * @return the alignment
264 public AlignmentI getAlignment()
271 * the alignment to set
273 public void setAlignment(AlignmentI alignment)
275 this.alignment = alignment;
279 * @return the caseSensitive
281 public boolean isCaseSensitive()
283 return caseSensitive;
287 * @param caseSensitive
288 * the caseSensitive to set
290 public void setCaseSensitive(boolean caseSensitive)
292 this.caseSensitive = caseSensitive;
296 * @return the findAll
298 public boolean isFindAll()
307 public void setFindAll(boolean findAll)
309 this.findAll = findAll;
313 * @return the selection
315 public jalview.datamodel.SequenceGroup getSelection()
322 * the selection to set
324 public void setSelection(jalview.datamodel.SequenceGroup selection)
326 this.selection = selection;
330 * Returns the (possibly empty) list of matching sequences (when search
331 * includes searching sequence names)
335 public Vector<SequenceI> getIdMatch()
343 public com.stevesoft.pat.Regex getRegex()
349 * @return the searchResults
351 public SearchResultsI getSearchResults()
353 return searchResults;
357 * @return the resIndex
359 public int getResIndex()
366 * the resIndex to set
368 public void setResIndex(int resIndex)
370 this.resIndex = resIndex;
374 * @return the seqIndex
376 public int getSeqIndex()
383 * the seqIndex to set
385 public void setSeqIndex(int seqIndex)
387 this.seqIndex = seqIndex;
390 public boolean isIncludeDescription()
392 return includeDescription;
395 public void setIncludeDescription(boolean includeDescription)
397 this.includeDescription = includeDescription;