ensure lastSeq is refreshed if new feature added
[jalview.git] / src / jalview / gui / Finder.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 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     int seqIndex = 0;\r
46     int resIndex = 0;\r
47 \r
48     SearchResults searchResults;\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()\r
58     {\r
59       frame = new JInternalFrame();\r
60       frame.setContentPane(this);\r
61       frame.setLayer(JLayeredPane.PALETTE_LAYER);\r
62       Desktop.addInternalFrame(frame, "Find", 340, 110);\r
63 \r
64       textfield.requestFocus();\r
65     }\r
66 \r
67 \r
68     /**\r
69      * DOCUMENT ME!\r
70      *\r
71      * @param e DOCUMENT ME!\r
72      */\r
73     public void findNext_actionPerformed(ActionEvent e)\r
74     {\r
75       if (getFocusedViewport())\r
76         doSearch(false);\r
77     }\r
78 \r
79     /**\r
80      * DOCUMENT ME!\r
81      *\r
82      * @param e DOCUMENT ME!\r
83      */\r
84     public void findAll_actionPerformed(ActionEvent e)\r
85     {\r
86       if(getFocusedViewport())\r
87       {\r
88         resIndex = 0;\r
89         seqIndex = 0;\r
90         doSearch(true);\r
91       }\r
92     }\r
93 \r
94     boolean getFocusedViewport()\r
95     {\r
96       JInternalFrame frame = Desktop.desktop.getAllFrames()[1];\r
97 \r
98       if(frame!=null && frame instanceof AlignFrame)\r
99       {\r
100         av = ((AlignFrame)frame).viewport;\r
101         ap = ((AlignFrame)frame).alignPanel;\r
102         return true;\r
103       }\r
104       return false;\r
105     }\r
106 \r
107 \r
108     /**\r
109      * DOCUMENT ME!\r
110      *\r
111      * @param e DOCUMENT ME!\r
112      */\r
113     public void createNewGroup_actionPerformed(ActionEvent e)\r
114     {\r
115         JLabel label = new JLabel("Enter name of new sequence feature");\r
116         JTextField textinput = new JTextField(textfield.getText());\r
117         JPanel panel = new JPanel(new BorderLayout());\r
118         panel.add(label, BorderLayout.NORTH);\r
119         panel.add(textinput, BorderLayout.SOUTH);\r
120 \r
121 \r
122         SequenceI [] seqs = new SequenceI[searchResults.getSize()];\r
123         SequenceFeature [] features = new SequenceFeature[searchResults.getSize()];\r
124 \r
125 \r
126         for (int i = 0; i < searchResults.getSize(); i ++ )\r
127         {\r
128             seqs[i] = searchResults.getResultSequence(i).getDatasetSequence();\r
129 \r
130             features[i] = new SequenceFeature(textinput.getText(),\r
131                 "Search Results", null,\r
132                 searchResults.getResultStart(i),\r
133                 searchResults.getResultEnd(i),\r
134                "Search Results");\r
135         }\r
136 \r
137         if( ap.seqPanel.seqCanvas.getFeatureRenderer()\r
138             .createNewFeatures(seqs, features))\r
139         {\r
140           ap.alignFrame.showSeqFeatures.setSelected(true);\r
141           av.setShowSequenceFeatures(true);\r
142           ap.highlightSearchResults(null);\r
143         }\r
144     }\r
145 \r
146     /**\r
147      * DOCUMENT ME!\r
148      *\r
149      * @param findAll DOCUMENT ME!\r
150      */\r
151     void doSearch(boolean findAll)\r
152     {\r
153         createNewGroup.setEnabled(false);\r
154 \r
155         String searchString = textfield.getText().trim();\r
156         if(!caseSensitive.isSelected())\r
157           searchString = searchString.toUpperCase();\r
158 \r
159         if(searchString.length()<1)\r
160           return;\r
161 \r
162         com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);\r
163 \r
164         searchResults = new SearchResults();\r
165 \r
166         Sequence seq;\r
167         String item = null;\r
168         boolean found = false;\r
169 \r
170         ////// is the searchString a residue number?\r
171         try\r
172         {\r
173             int res = Integer.parseInt(searchString);\r
174             found = true;\r
175             if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize(false) < 1)\r
176             {\r
177               seq = (Sequence) av.getAlignment().getSequenceAt(0);\r
178             }\r
179             else\r
180             {\r
181               seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));\r
182             }\r
183 \r
184             searchResults.addResult(seq, res, res);\r
185         }\r
186         catch (NumberFormatException ex)\r
187         {\r
188         }\r
189 \r
190         ///////////////////////////////////////////////\r
191 \r
192         int end = av.alignment.getHeight();\r
193 \r
194         SequenceGroup selection = av.getSelectionGroup();\r
195 \r
196         if (selection != null)\r
197         {\r
198             if ((selection.getSize(false) < 1) ||\r
199                     ((selection.getEndRes() - selection.getStartRes()) < 2))\r
200             {\r
201                 selection = null;\r
202             }\r
203         }\r
204 \r
205         while (!found && (seqIndex < end))\r
206         {\r
207             seq = (Sequence) av.alignment.getSequenceAt(seqIndex);\r
208 \r
209             if ((selection != null) && !selection.getSequences(false).contains(seq))\r
210             {\r
211                 seqIndex++;\r
212                 resIndex = 0;\r
213 \r
214                 continue;\r
215             }\r
216 \r
217             item = seq.getSequenceAsString();\r
218             if(!caseSensitive.isSelected())\r
219               item = item.toUpperCase();\r
220 \r
221             if ((selection != null) &&\r
222                     (selection.getEndRes() < av.alignment.getWidth()-1))\r
223             {\r
224                 item = item.substring(0, selection.getEndRes() + 1);\r
225             }\r
226 \r
227             ///Shall we ignore gaps????\r
228             StringBuffer noGapsSB = new StringBuffer();\r
229             int insertCount = 0;\r
230             Vector spaces = new Vector();\r
231 \r
232             for (int j = 0; j < item.length(); j++)\r
233             {\r
234                 if (!jalview.util.Comparison.isGap(item.charAt(j)))\r
235                 {\r
236                     noGapsSB.append(item.charAt(j));\r
237                     spaces.add(new Integer(insertCount));\r
238                 }\r
239                 else\r
240                 {\r
241                     insertCount++;\r
242                 }\r
243             }\r
244 \r
245             String noGaps = noGapsSB.toString();\r
246 \r
247             for (int r = resIndex; r < noGaps.length(); r++)\r
248             {\r
249 \r
250                 if (regex.searchFrom(noGaps, r))\r
251                 {\r
252                     resIndex = regex.matchedFrom();\r
253 \r
254                     if ((selection != null) &&\r
255                             ((resIndex +\r
256                             Integer.parseInt(spaces.get(resIndex).toString())) < selection.getStartRes()))\r
257                     {\r
258                         continue;\r
259                     }\r
260 \r
261 \r
262                     int sres = seq.findPosition(resIndex +\r
263                             Integer.parseInt(spaces.elementAt(resIndex)\r
264                                                    .toString()));\r
265                     int eres = seq.findPosition(regex.matchedTo() - 1 +\r
266                             Integer.parseInt(spaces.elementAt(regex.matchedTo() -\r
267                                     1).toString()));\r
268 \r
269                     searchResults.addResult(seq, sres, eres);\r
270 \r
271                     if (!findAll)\r
272                     {\r
273                         // thats enough, break and display the result\r
274                         found = true;\r
275                         resIndex++;\r
276 \r
277                         break;\r
278                     }\r
279 \r
280                     r = resIndex;\r
281                 }\r
282                 else\r
283                 {\r
284                   break;\r
285                 }\r
286             }\r
287 \r
288             if (!found)\r
289             {\r
290                 seqIndex++;\r
291                 resIndex = 0;\r
292             }\r
293         }\r
294 \r
295         Vector idMatch = new Vector();\r
296 \r
297         for (int id = 0; id < av.alignment.getHeight(); id++)\r
298         {\r
299             if (regex.search(av.alignment.getSequenceAt(id).getName()))\r
300             {\r
301                 idMatch.add(av.alignment.getSequenceAt(id));\r
302             }\r
303         }\r
304 \r
305         if ((searchResults.getSize() == 0) && (idMatch.size() > 0))\r
306         {\r
307             ap.idPanel.highlightSearchResults(idMatch);\r
308         }\r
309 \r
310 \r
311         int resultSize = searchResults.getSize();\r
312 \r
313         if (searchResults.getSize() > 0)\r
314           createNewGroup.setEnabled(true);\r
315         else\r
316           searchResults = null;\r
317 \r
318         // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
319         ap.highlightSearchResults(searchResults);\r
320 \r
321         if(!findAll && resultSize==0)\r
322         {\r
323             JOptionPane.showInternalMessageDialog(this, "Finished searching",\r
324                 null, JOptionPane.INFORMATION_MESSAGE);\r
325             resIndex = 0;\r
326             seqIndex = 0;\r
327         }\r
328 \r
329         if (findAll)\r
330         {\r
331           String message = resultSize + " matches found.";\r
332           JOptionPane.showInternalMessageDialog(this, message, null,\r
333                                                 JOptionPane.INFORMATION_MESSAGE);\r
334         }\r
335 \r
336     }\r
337 }\r