1 package jalview.analysis;
2 import java.util.Vector;
4 import jalview.datamodel.*;
8 * Implements the search algorithms for the Find dialog box.
10 SearchResults searchResults;
12 jalview.datamodel.SequenceGroup selection=null;
14 boolean caseSensitive=false;
15 boolean findAll=false;
16 com.stevesoft.pat.Regex regex=null;
17 public Finder(AlignmentI alignment, SequenceGroup selection) {
18 this.alignment=alignment;
19 this.selection = selection;
21 public boolean find(String searchString) {
22 boolean hasResults=false;
24 searchString = searchString.toUpperCase();
25 regex = new com.stevesoft.pat.Regex(searchString);
26 searchResults = new SearchResults();
27 idMatch = new Vector();
29 int seqIndex=0,resIndex=0;
31 boolean found = false;
33 ////// is the searchString a residue number?
36 int res = Integer.parseInt(searchString);
38 if (selection == null || selection.getSize(false) < 1)
40 seq = (Sequence) alignment.getSequenceAt(0);
44 seq = (Sequence) (selection.getSequenceAt(0));
47 searchResults.addResult(seq, res, res);
50 catch (NumberFormatException ex)
54 ///////////////////////////////////////////////
56 int end = alignment.getHeight();
59 if (selection != null)
61 if ((selection.getSize(false) < 1) ||
62 ((selection.getEndRes() - selection.getStartRes()) < 2))
68 while (!found && (seqIndex < end))
70 seq = (Sequence) alignment.getSequenceAt(seqIndex);
72 if ((selection != null) && !selection.getSequences(false).contains(seq))
80 item = seq.getSequenceAsString();
81 // JBPNote - check if this toUpper which is present in the application implementation makes a difference
83 // item = item.toUpperCase();
85 if ((selection != null) &&
86 (selection.getEndRes() < alignment.getWidth()-1))
88 item = item.substring(0, selection.getEndRes() + 1);
91 ///Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
92 StringBuffer noGapsSB = new StringBuffer();
94 Vector spaces = new Vector();
96 for (int j = 0; j < item.length(); j++)
98 if (!jalview.util.Comparison.isGap(item.charAt(j)))
100 noGapsSB.append(item.charAt(j));
101 spaces.add(new Integer(insertCount));
109 String noGaps = noGapsSB.toString();
111 for (int r = resIndex; r < noGaps.length(); r++)
114 if (regex.searchFrom(noGaps, r))
116 resIndex = regex.matchedFrom();
118 if ((selection != null) &&
120 Integer.parseInt(spaces.get(resIndex).toString())) < selection.getStartRes()))
126 int sres = seq.findPosition(resIndex +
127 Integer.parseInt(spaces.elementAt(resIndex)
129 int eres = seq.findPosition(regex.matchedTo() - 1 +
130 Integer.parseInt(spaces.elementAt(regex.matchedTo() -
133 searchResults.addResult(seq, sres, eres);
137 // thats enough, break and display the result
159 for (int id = 0; id < alignment.getHeight(); id++)
161 if (regex.search(alignment.getSequenceAt(id).getName()))
163 idMatch.add(alignment.getSequenceAt(id));
170 * @return the alignment
172 public AlignmentI getAlignment() {
176 * @param alignment the alignment to set
178 public void setAlignment(AlignmentI alignment) {
179 this.alignment = alignment;
182 * @return the caseSensitive
184 public boolean isCaseSensitive() {
185 return caseSensitive;
188 * @param caseSensitive the caseSensitive to set
190 public void setCaseSensitive(boolean caseSensitive) {
191 this.caseSensitive = caseSensitive;
194 * @return the findAll
196 public boolean isFindAll() {
200 * @param findAll the findAll to set
202 public void setFindAll(boolean findAll) {
203 this.findAll = findAll;
206 * @return the selection
208 public jalview.datamodel.SequenceGroup getSelection() {
212 * @param selection the selection to set
214 public void setSelection(jalview.datamodel.SequenceGroup selection) {
215 this.selection = selection;
218 * @return the idMatch
220 public Vector getIdMatch() {
226 public com.stevesoft.pat.Regex getRegex() {
230 * @return the searchResults
232 public SearchResults getSearchResults() {
233 return searchResults;