Formatting changes
[jalview.git] / src / jalview / gui / Finder.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.gui;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import jalview.jbgui.*;\r
24 \r
25 import java.awt.*;\r
26 import java.awt.event.*;\r
27 \r
28 import java.util.*;\r
29 \r
30 import javax.swing.*;\r
31 import javax.swing.event.*;\r
32 \r
33 \r
34 /**\r
35  * DOCUMENT ME!\r
36  *\r
37  * @author $author$\r
38  * @version $Revision$\r
39  */\r
40 public class Finder extends GFinder\r
41 {\r
42     AlignViewport av;\r
43     AlignmentPanel ap;\r
44     JInternalFrame frame;\r
45     SuperGroup searchGroup;\r
46     Vector searchResults;\r
47     int seqIndex = 0;\r
48     int resIndex = 0;\r
49 \r
50     /**\r
51      * Creates a new Finder object.\r
52      *\r
53      * @param av DOCUMENT ME!\r
54      * @param ap DOCUMENT ME!\r
55      * @param f DOCUMENT ME!\r
56      */\r
57     public Finder(AlignViewport av, AlignmentPanel ap, JInternalFrame f)\r
58     {\r
59         this.av = av;\r
60         this.ap = ap;\r
61         frame = f;\r
62 \r
63         // all a big pain, but we need to wait until the frame is visible before the textfield can\r
64         // obtain the focus/////////////////////////\r
65         frame.addInternalFrameListener(new InternalFrameAdapter()\r
66             {\r
67                 public void internalFrameOpened(InternalFrameEvent evt)\r
68                 {\r
69                     SwingUtilities.invokeLater(new Runnable()\r
70                         {\r
71                             public void run()\r
72                             {\r
73                                 textfield.requestFocus();\r
74                             }\r
75                         });\r
76                 }\r
77 \r
78                 /**\r
79                  * DOCUMENT ME!\r
80                  *\r
81                  * @param evt DOCUMENT ME!\r
82                  */\r
83                 public void internalFrameClosing(InternalFrameEvent evt)\r
84                 {\r
85                     cancel_actionPerformed(null);\r
86                 }\r
87             });\r
88     }\r
89 \r
90     /**\r
91      * DOCUMENT ME!\r
92      *\r
93      * @param e DOCUMENT ME!\r
94      */\r
95     public void textfield_actionPerformed(ActionEvent e)\r
96     {\r
97         doSearch(false);\r
98     }\r
99 \r
100     /**\r
101      * DOCUMENT ME!\r
102      *\r
103      * @param e DOCUMENT ME!\r
104      */\r
105     public void findNext_actionPerformed(ActionEvent e)\r
106     {\r
107         doSearch(false);\r
108     }\r
109 \r
110     /**\r
111      * DOCUMENT ME!\r
112      *\r
113      * @param e DOCUMENT ME!\r
114      */\r
115     public void findAll_actionPerformed(ActionEvent e)\r
116     {\r
117         resIndex = 0;\r
118         seqIndex = 0;\r
119         doSearch(true);\r
120     }\r
121 \r
122     /**\r
123      * DOCUMENT ME!\r
124      *\r
125      * @param e DOCUMENT ME!\r
126      */\r
127     public void cancel_actionPerformed(ActionEvent e)\r
128     {\r
129         try\r
130         {\r
131             // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
132             ap.highlightSearchResults(null);\r
133             ap.idPanel.highlightSearchResults(null);\r
134 \r
135             // frame.setClosed(true);\r
136         }\r
137         catch (Exception ex)\r
138         {\r
139         }\r
140     }\r
141 \r
142     /**\r
143      * DOCUMENT ME!\r
144      *\r
145      * @param e DOCUMENT ME!\r
146      */\r
147     public void createNewGroup_actionPerformed(ActionEvent e)\r
148     {\r
149         Color[] newColors = new Color[24];\r
150 \r
151         for (int i = 0; i < 24; i++)\r
152         {\r
153             newColors[i] = new Color(60, 160, 115);\r
154         }\r
155 \r
156         jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(newColors);\r
157         String searchString = textfield.getText();\r
158 \r
159         searchGroup = new SuperGroup(searchString, ucs, true, true, false);\r
160 \r
161         for (int i = 0; i < searchResults.size(); i += 3)\r
162         {\r
163             // its possible edits may have occurred since search was performed\r
164             // account for this here\r
165             SequenceI seq = av.alignment.getSequenceAt(Integer.parseInt(\r
166                         searchResults.elementAt(i).toString()));\r
167             int startRes = seq.findIndex(Integer.parseInt(\r
168                         searchResults.elementAt(i + 1).toString())) - 1;\r
169             int endRes = seq.findIndex(Integer.parseInt(\r
170                         searchResults.elementAt(i + 2).toString())) - 1;\r
171 \r
172             SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true,\r
173                     false, startRes, endRes);\r
174             sg.addSequence(seq, false);\r
175             av.alignment.addGroup(sg);\r
176             searchGroup.addGroup(sg);\r
177         }\r
178 \r
179         ap.av.alignment.addSuperGroup(searchGroup);\r
180         ap.highlightSearchResults(null);\r
181     }\r
182 \r
183     /**\r
184      * DOCUMENT ME!\r
185      *\r
186      * @param findAll DOCUMENT ME!\r
187      */\r
188     void doSearch(boolean findAll)\r
189     {\r
190         createNewGroup.setEnabled(false);\r
191 \r
192         String searchString = textfield.getText().toUpperCase();\r
193 \r
194         com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);\r
195 \r
196         searchResults = new Vector();\r
197 \r
198         int[] allResults = null;\r
199 \r
200         Sequence seq;\r
201         String item = null;\r
202         boolean found = false;\r
203 \r
204         ////// is the searchString a residue number?\r
205         try\r
206         {\r
207             int res = Integer.parseInt(searchString);\r
208             found = true;\r
209 \r
210             if (av.getSelectionGroup().getSize() > 0)\r
211             {\r
212                 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));\r
213             }\r
214             else\r
215             {\r
216                 seq = (Sequence) av.getAlignment().getSequenceAt(0);\r
217             }\r
218 \r
219             searchResults.add(Integer.toString(av.getAlignment().findIndex(seq)));\r
220             searchResults.add(Integer.toString(seq.findIndex(res) - 1));\r
221             searchResults.add(Integer.toString(seq.findIndex(res) - 1));\r
222         }\r
223         catch (NumberFormatException ex)\r
224         {\r
225         }\r
226 \r
227         ///////////////////////////////////////////////\r
228         Color[] newColors = new Color[24];\r
229 \r
230         for (int i = 0; i < 24; i++)\r
231         {\r
232             newColors[i] = new Color(60, 160, 115);\r
233         }\r
234 \r
235         jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(newColors);\r
236 \r
237         searchGroup = new SuperGroup(searchString, ucs, true, true, false);\r
238 \r
239         int end = av.alignment.getHeight();\r
240 \r
241         SequenceGroup selection = av.getSelectionGroup();\r
242 \r
243         if (selection != null)\r
244         {\r
245             if ((selection.getSize() < 1) ||\r
246                     ((selection.getEndRes() - selection.getStartRes()) < 2))\r
247             {\r
248                 selection = null;\r
249             }\r
250         }\r
251 \r
252         while (!found && (seqIndex < end))\r
253         {\r
254             seq = (Sequence) av.alignment.getSequenceAt(seqIndex);\r
255 \r
256             if ((selection != null) && !selection.sequences.contains(seq))\r
257             {\r
258                 seqIndex++;\r
259                 resIndex = 0;\r
260 \r
261                 continue;\r
262             }\r
263 \r
264             item = seq.getSequence().toUpperCase();\r
265 \r
266             if ((selection != null) &&\r
267                     (selection.getEndRes() < av.alignment.getWidth()))\r
268             {\r
269                 item = item.substring(0, selection.getEndRes() + 1);\r
270             }\r
271 \r
272             ///Shall we ignore gaps????\r
273             StringBuffer noGaps = new StringBuffer();\r
274             int insertCount = 0;\r
275             Vector spaces = new Vector();\r
276 \r
277             for (int j = 0; j < item.length(); j++)\r
278             {\r
279                 if (!jalview.util.Comparison.isGap(item.charAt(j)))\r
280                 {\r
281                     noGaps.append(item.charAt(j));\r
282                     spaces.add(new Integer(insertCount));\r
283                 }\r
284                 else\r
285                 {\r
286                     insertCount++;\r
287                 }\r
288             }\r
289 \r
290             for (int r = resIndex; r < noGaps.length(); r++)\r
291             {\r
292                 if (regex.searchFrom(noGaps.toString(), r))\r
293                 {\r
294                     resIndex = regex.matchedFrom();\r
295 \r
296                     if ((selection != null) &&\r
297                             ((resIndex +\r
298                             Integer.parseInt(spaces.get(resIndex).toString())) < selection.getStartRes()))\r
299                     {\r
300                         continue;\r
301                     }\r
302 \r
303                     searchResults.add(Integer.toString(seqIndex));\r
304 \r
305                     int sres = seq.findPosition(resIndex +\r
306                             Integer.parseInt(spaces.elementAt(resIndex)\r
307                                                    .toString()));\r
308                     int eres = seq.findPosition(regex.matchedTo() - 1 +\r
309                             Integer.parseInt(spaces.elementAt(regex.matchedTo() -\r
310                                     1).toString()));\r
311 \r
312                     searchResults.addElement(sres + "");\r
313                     searchResults.addElement(eres + "");\r
314 \r
315                     if (!findAll)\r
316                     {\r
317                         // thats enough, break and display the result\r
318                         found = true;\r
319                         resIndex++;\r
320 \r
321                         break;\r
322                     }\r
323 \r
324                     r = resIndex;\r
325                 }\r
326             }\r
327 \r
328             if (!found)\r
329             {\r
330                 seqIndex++;\r
331                 resIndex = 0;\r
332             }\r
333         }\r
334 \r
335         Vector idMatch = new Vector();\r
336 \r
337         for (int id = 0; id < av.alignment.getHeight(); id++)\r
338         {\r
339             if (regex.search(av.alignment.getSequenceAt(id).getName()))\r
340             {\r
341                 idMatch.add(av.alignment.getSequenceAt(id));\r
342             }\r
343         }\r
344 \r
345         if ((searchResults.size() == 0) && (idMatch.size() > 0))\r
346         {\r
347             ap.idPanel.highlightSearchResults(idMatch);\r
348         }\r
349 \r
350         if (searchResults.size() > 0)\r
351         {\r
352             allResults = new int[searchResults.size()];\r
353 \r
354             for (int i = 0; i < searchResults.size(); i++)\r
355             {\r
356                 allResults[i] = Integer.parseInt(searchResults.get(i).toString());\r
357             }\r
358 \r
359             createNewGroup.setEnabled(true);\r
360         }\r
361         else\r
362         {\r
363             JOptionPane.showInternalMessageDialog(this, "Finished searching",\r
364                 null, JOptionPane.INFORMATION_MESSAGE);\r
365             resIndex = 0;\r
366             seqIndex = 0;\r
367         }\r
368 \r
369         // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
370         ap.highlightSearchResults(allResults);\r
371 \r
372         if (findAll)\r
373         {\r
374             String message = (searchResults.size() / 3) + " matches found.";\r
375             JOptionPane.showInternalMessageDialog(this, message, null,\r
376                 JOptionPane.INFORMATION_MESSAGE);\r
377         }\r
378     }\r
379 }\r