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