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;
18 * hold's last-searched position between calles to find(false)
20 int seqIndex=0,resIndex=0;
21 public Finder(AlignmentI alignment, SequenceGroup selection) {
22 this.alignment=alignment;
23 this.selection = selection;
26 public Finder(AlignmentI alignment, SequenceGroup selectionGroup, int seqIndex, int resIndex) {
27 this(alignment, selectionGroup);
28 this.seqIndex=seqIndex;
29 this.resIndex=resIndex;
32 public boolean find(String searchString) {
33 boolean hasResults=false;
35 searchString = searchString.toUpperCase();
36 regex = new com.stevesoft.pat.Regex(searchString);
37 searchResults = new SearchResults();
38 idMatch = new Vector();
41 boolean found = false;
43 ////// is the searchString a residue number?
46 int res = Integer.parseInt(searchString);
48 if (selection == null || selection.getSize(false) < 1)
50 seq = (Sequence) alignment.getSequenceAt(0);
54 seq = (Sequence) (selection.getSequenceAt(0));
57 searchResults.addResult(seq, res, res);
60 catch (NumberFormatException ex)
64 ///////////////////////////////////////////////
66 int end = alignment.getHeight();
69 if (selection != null)
71 if ((selection.getSize(false) < 1) ||
72 ((selection.getEndRes() - selection.getStartRes()) < 2))
78 while (!found && (seqIndex < end))
80 seq = (Sequence) alignment.getSequenceAt(seqIndex);
82 if ((selection != null) && !selection.getSequences(false).contains(seq))
90 item = seq.getSequenceAsString();
91 // JBPNote - check if this toUpper which is present in the application implementation makes a difference
93 // item = item.toUpperCase();
95 if ((selection != null) &&
96 (selection.getEndRes() < alignment.getWidth()-1))
98 item = item.substring(0, selection.getEndRes() + 1);
101 ///Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
102 StringBuffer noGapsSB = new StringBuffer();
104 Vector spaces = new Vector();
106 for (int j = 0; j < item.length(); j++)
108 if (!jalview.util.Comparison.isGap(item.charAt(j)))
110 noGapsSB.append(item.charAt(j));
111 spaces.add(new Integer(insertCount));
119 String noGaps = noGapsSB.toString();
121 for (int r = resIndex; r < noGaps.length(); r++)
124 if (regex.searchFrom(noGaps, r))
126 resIndex = regex.matchedFrom();
128 if ((selection != null) &&
130 Integer.parseInt(spaces.get(resIndex).toString())) < selection.getStartRes()))
136 int sres = seq.findPosition(resIndex +
137 Integer.parseInt(spaces.elementAt(resIndex)
139 int eres = seq.findPosition(regex.matchedTo() - 1 +
140 Integer.parseInt(spaces.elementAt(regex.matchedTo() -
143 searchResults.addResult(seq, sres, eres);
147 // thats enough, break and display the result
169 for (int id = 0; id < alignment.getHeight(); id++)
171 if (regex.search(alignment.getSequenceAt(id).getName()))
173 idMatch.add(alignment.getSequenceAt(id));
180 * @return the alignment
182 public AlignmentI getAlignment() {
186 * @param alignment the alignment to set
188 public void setAlignment(AlignmentI alignment) {
189 this.alignment = alignment;
192 * @return the caseSensitive
194 public boolean isCaseSensitive() {
195 return caseSensitive;
198 * @param caseSensitive the caseSensitive to set
200 public void setCaseSensitive(boolean caseSensitive) {
201 this.caseSensitive = caseSensitive;
204 * @return the findAll
206 public boolean isFindAll() {
210 * @param findAll the findAll to set
212 public void setFindAll(boolean findAll) {
213 this.findAll = findAll;
216 * @return the selection
218 public jalview.datamodel.SequenceGroup getSelection() {
222 * @param selection the selection to set
224 public void setSelection(jalview.datamodel.SequenceGroup selection) {
225 this.selection = selection;
228 * @return the idMatch
230 public Vector getIdMatch() {
236 public com.stevesoft.pat.Regex getRegex() {
240 * @return the searchResults
242 public SearchResults getSearchResults() {
243 return searchResults;
247 * @return the resIndex
249 public int getResIndex() {
254 * @param resIndex the resIndex to set
256 public void setResIndex(int resIndex) {
257 this.resIndex = resIndex;
261 * @return the seqIndex
263 public int getSeqIndex() {
268 * @param seqIndex the seqIndex to set
270 public void setSeqIndex(int seqIndex) {
271 this.seqIndex = seqIndex;