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