Formatting
[jalview.git] / src / jalview / analysis / Finder.java
1 package jalview.analysis;\r
2 \r
3 import java.util.*;\r
4 \r
5 import jalview.datamodel.*;\r
6 \r
7 public class Finder\r
8 {\r
9   /**\r
10    * Implements the search algorithms for the Find dialog box.\r
11    */\r
12   SearchResults searchResults;\r
13   AlignmentI alignment;\r
14   jalview.datamodel.SequenceGroup selection = null;\r
15   Vector idMatch = null;\r
16   boolean caseSensitive = false;\r
17   boolean findAll = false;\r
18   com.stevesoft.pat.Regex regex = null;\r
19   /**\r
20    * hold's last-searched position between calles to find(false)\r
21    */\r
22   int seqIndex = 0, resIndex = 0;\r
23   public Finder(AlignmentI alignment, SequenceGroup selection)\r
24   {\r
25     this.alignment = alignment;\r
26     this.selection = selection;\r
27   }\r
28 \r
29   public Finder(AlignmentI alignment, SequenceGroup selectionGroup,\r
30                 int seqIndex, int resIndex)\r
31   {\r
32     this(alignment, selectionGroup);\r
33     this.seqIndex = seqIndex;\r
34     this.resIndex = resIndex;\r
35   }\r
36 \r
37   public boolean find(String searchString)\r
38   {\r
39     boolean hasResults = false;\r
40     if (!caseSensitive)\r
41     {\r
42       searchString = searchString.toUpperCase();\r
43     }\r
44     regex = new com.stevesoft.pat.Regex(searchString);\r
45     searchResults = new SearchResults();\r
46     idMatch = new Vector();\r
47     Sequence seq;\r
48     String item = null;\r
49     boolean found = false;\r
50 \r
51     ////// is the searchString a residue number?\r
52     try\r
53     {\r
54       int res = Integer.parseInt(searchString);\r
55       found = true;\r
56       if (selection == null || selection.getSize() < 1)\r
57       {\r
58         seq = (Sequence) alignment.getSequenceAt(0);\r
59       }\r
60       else\r
61       {\r
62         seq = (Sequence) (selection.getSequenceAt(0));\r
63       }\r
64 \r
65       searchResults.addResult(seq, res, res);\r
66       hasResults = true;\r
67     }\r
68     catch (NumberFormatException ex)\r
69     {\r
70     }\r
71 \r
72     ///////////////////////////////////////////////\r
73 \r
74     int end = alignment.getHeight();\r
75 \r
76     if (selection != null)\r
77     {\r
78       if ( (selection.getSize() < 1) ||\r
79           ( (selection.getEndRes() - selection.getStartRes()) < 2))\r
80       {\r
81         selection = null;\r
82       }\r
83     }\r
84 \r
85     while (!found && (seqIndex < end))\r
86     {\r
87       seq = (Sequence) alignment.getSequenceAt(seqIndex);\r
88 \r
89       if ( (selection != null) && !selection.getSequences(null).contains(seq))\r
90       {\r
91         seqIndex++;\r
92         resIndex = 0;\r
93 \r
94         continue;\r
95       }\r
96 \r
97       item = seq.getSequenceAsString();\r
98       // JBPNote - check if this toUpper which is present in the application implementation makes a difference\r
99       //if(!caseSensitive)\r
100       //  item = item.toUpperCase();\r
101 \r
102       if ( (selection != null) &&\r
103           (selection.getEndRes() < alignment.getWidth() - 1))\r
104       {\r
105         item = item.substring(0, selection.getEndRes() + 1);\r
106       }\r
107 \r
108       ///Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not\r
109       StringBuffer noGapsSB = new StringBuffer();\r
110       int insertCount = 0;\r
111       Vector spaces = new Vector();\r
112 \r
113       for (int j = 0; j < item.length(); j++)\r
114       {\r
115         if (!jalview.util.Comparison.isGap(item.charAt(j)))\r
116         {\r
117           noGapsSB.append(item.charAt(j));\r
118           spaces.addElement(new Integer(insertCount));\r
119         }\r
120         else\r
121         {\r
122           insertCount++;\r
123         }\r
124       }\r
125 \r
126       String noGaps = noGapsSB.toString();\r
127 \r
128       for (int r = resIndex; r < noGaps.length(); r++)\r
129       {\r
130 \r
131         if (regex.searchFrom(noGaps, r))\r
132         {\r
133           resIndex = regex.matchedFrom();\r
134 \r
135           if ( (selection != null) &&\r
136               ( (resIndex +\r
137                  Integer.parseInt(spaces.elementAt(resIndex).toString())) <\r
138                selection.getStartRes()))\r
139           {\r
140             continue;\r
141           }\r
142 \r
143           int sres = seq.findPosition(resIndex +\r
144                                       Integer.parseInt(spaces.elementAt(\r
145               resIndex)\r
146               .toString()));\r
147           int eres = seq.findPosition(regex.matchedTo() - 1 +\r
148                                       Integer.parseInt(spaces.elementAt(regex.\r
149               matchedTo() -\r
150               1).toString()));\r
151 \r
152           searchResults.addResult(seq, sres, eres);\r
153           hasResults = true;\r
154           if (!findAll)\r
155           {\r
156             // thats enough, break and display the result\r
157             found = true;\r
158             resIndex++;\r
159 \r
160             break;\r
161           }\r
162 \r
163           r = resIndex;\r
164         }\r
165         else\r
166         {\r
167           break;\r
168         }\r
169       }\r
170 \r
171       if (!found)\r
172       {\r
173         seqIndex++;\r
174         resIndex = 0;\r
175       }\r
176     }\r
177 \r
178     for (int id = 0; id < alignment.getHeight(); id++)\r
179     {\r
180       if (regex.search(alignment.getSequenceAt(id).getName()))\r
181       {\r
182         idMatch.addElement(alignment.getSequenceAt(id));\r
183         hasResults = true;\r
184       }\r
185     }\r
186     return hasResults;\r
187   }\r
188 \r
189   /**\r
190    * @return the alignment\r
191    */\r
192   public AlignmentI getAlignment()\r
193   {\r
194     return alignment;\r
195   }\r
196 \r
197   /**\r
198    * @param alignment the alignment to set\r
199    */\r
200   public void setAlignment(AlignmentI alignment)\r
201   {\r
202     this.alignment = alignment;\r
203   }\r
204 \r
205   /**\r
206    * @return the caseSensitive\r
207    */\r
208   public boolean isCaseSensitive()\r
209   {\r
210     return caseSensitive;\r
211   }\r
212 \r
213   /**\r
214    * @param caseSensitive the caseSensitive to set\r
215    */\r
216   public void setCaseSensitive(boolean caseSensitive)\r
217   {\r
218     this.caseSensitive = caseSensitive;\r
219   }\r
220 \r
221   /**\r
222    * @return the findAll\r
223    */\r
224   public boolean isFindAll()\r
225   {\r
226     return findAll;\r
227   }\r
228 \r
229   /**\r
230    * @param findAll the findAll to set\r
231    */\r
232   public void setFindAll(boolean findAll)\r
233   {\r
234     this.findAll = findAll;\r
235   }\r
236 \r
237   /**\r
238    * @return the selection\r
239    */\r
240   public jalview.datamodel.SequenceGroup getSelection()\r
241   {\r
242     return selection;\r
243   }\r
244 \r
245   /**\r
246    * @param selection the selection to set\r
247    */\r
248   public void setSelection(jalview.datamodel.SequenceGroup selection)\r
249   {\r
250     this.selection = selection;\r
251   }\r
252 \r
253   /**\r
254    * @return the idMatch\r
255    */\r
256   public Vector getIdMatch()\r
257   {\r
258     return idMatch;\r
259   }\r
260 \r
261   /**\r
262    * @return the regex\r
263    */\r
264   public com.stevesoft.pat.Regex getRegex()\r
265   {\r
266     return regex;\r
267   }\r
268 \r
269   /**\r
270    * @return the searchResults\r
271    */\r
272   public SearchResults getSearchResults()\r
273   {\r
274     return searchResults;\r
275   }\r
276 \r
277   /**\r
278    * @return the resIndex\r
279    */\r
280   public int getResIndex()\r
281   {\r
282     return resIndex;\r
283   }\r
284 \r
285   /**\r
286    * @param resIndex the resIndex to set\r
287    */\r
288   public void setResIndex(int resIndex)\r
289   {\r
290     this.resIndex = resIndex;\r
291   }\r
292 \r
293   /**\r
294    * @return the seqIndex\r
295    */\r
296   public int getSeqIndex()\r
297   {\r
298     return seqIndex;\r
299   }\r
300 \r
301   /**\r
302    * @param seqIndex the seqIndex to set\r
303    */\r
304   public void setSeqIndex(int seqIndex)\r
305   {\r
306     this.seqIndex = seqIndex;\r
307   }\r
308 }\r