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