New implementation for holding search results
[jalview.git] / src / jalview / appletgui / 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 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 \r
27 import jalview.datamodel.*;\r
28 \r
29 public class Finder extends Panel implements ActionListener\r
30 {\r
31   AlignViewport av;\r
32   AlignmentPanel ap;\r
33   Frame frame;\r
34   SuperGroup searchGroup;\r
35 \r
36   SearchResults searchResults;\r
37 \r
38   int seqIndex = 0;\r
39   int resIndex = 0;\r
40   public Finder(final AlignmentPanel ap)\r
41   {\r
42     try\r
43     {\r
44       jbInit();\r
45     }\r
46     catch (Exception e)\r
47     {\r
48       e.printStackTrace();\r
49     }\r
50 \r
51     this.av = ap.av;\r
52     this.ap = ap;\r
53     frame = new Frame();\r
54     frame.add(this);\r
55     jalview.bin.JalviewLite.addFrame(frame, "Find", 340, 120);\r
56     frame.repaint();\r
57     frame.addWindowListener(new WindowAdapter()\r
58     {\r
59       public void windowClosing(WindowEvent evt)\r
60       {\r
61         ap.highlightSearchResults(null);\r
62       }\r
63     });\r
64   }\r
65 \r
66   public void actionPerformed(ActionEvent evt)\r
67   {\r
68     if (evt.getSource() == textfield)\r
69       doSearch(false);\r
70 \r
71     else if (evt.getSource() == findNext)\r
72       doSearch(false);\r
73 \r
74     else if (evt.getSource() == findAll)\r
75     {\r
76       resIndex = 0;\r
77       seqIndex = 0;\r
78       doSearch(true);\r
79     }\r
80     else if(evt.getSource() == createNewGroup)\r
81       createNewGroup_actionPerformed();\r
82   }\r
83 \r
84 \r
85   public void createNewGroup_actionPerformed()\r
86   {\r
87     Color[] newColors = new Color[24];\r
88     for (int i = 0; i < 24; i++)\r
89     {\r
90       newColors[i] = new Color(60, 160, 115);\r
91     }\r
92 \r
93     jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(\r
94         newColors);\r
95     String searchString = textfield.getText();\r
96 \r
97     searchGroup = new SuperGroup(searchString, ucs, true, true, false);\r
98 \r
99     for (int i = 0; i < searchResults.getSize(); i ++ )\r
100     {\r
101       // its possible edits may have occurred since search was performed\r
102       // account for this here\r
103 \r
104       SequenceI seq = searchResults.getResultSequence(i);\r
105       int startRes = seq.findIndex(searchResults.getResultStart(i)) - 1;\r
106             int endRes = seq.findIndex(searchResults.getResultEnd(i)) - 1;\r
107 \r
108       SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false,\r
109                                            startRes, endRes);\r
110       sg.addSequence(seq, false);\r
111       av.alignment.addGroup(sg);\r
112       searchGroup.addGroup(sg);\r
113 \r
114     }\r
115 \r
116     ap.av.alignment.addSuperGroup(searchGroup);\r
117     ap.highlightSearchResults(null);\r
118   }\r
119 \r
120   void doSearch(boolean findAll)\r
121   {\r
122     createNewGroup.setEnabled(false);\r
123 \r
124     String searchString = textfield.getText().toUpperCase();\r
125 \r
126     com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);\r
127 \r
128     searchResults = new SearchResults();\r
129 \r
130     Sequence seq;\r
131     String item = null;\r
132     boolean found = false;\r
133 \r
134     ////// is the searchString a residue number?\r
135     try\r
136     {\r
137       int res = Integer.parseInt(searchString);\r
138       found = true;\r
139 \r
140       if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize() < 1)\r
141       {\r
142         seq = (Sequence) av.getAlignment().getSequenceAt(0);\r
143       }\r
144       else\r
145       {\r
146         seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));\r
147       }\r
148 \r
149 \r
150       searchResults.addResult(seq, res, res);\r
151 \r
152     }\r
153     catch (NumberFormatException ex)\r
154     {}\r
155     ///////////////////////////////////////////////\r
156 \r
157 \r
158     int end = av.alignment.getHeight();\r
159 \r
160     SequenceGroup selection = av.getSelectionGroup();\r
161     if (selection != null)\r
162     {\r
163       if (selection.getSize() < 1 ||\r
164           (selection.getEndRes() - selection.getStartRes() < 2))\r
165       {\r
166         selection = null;\r
167       }\r
168     }\r
169 \r
170     while (!found && seqIndex < end)\r
171     {\r
172       seq = (Sequence) av.alignment.getSequenceAt(seqIndex);\r
173 \r
174       if (selection != null && !selection.sequences.contains(seq))\r
175       {\r
176         seqIndex++;\r
177         resIndex = 0;\r
178         continue;\r
179       }\r
180 \r
181       item = seq.getSequence().toUpperCase();\r
182 \r
183       if (selection != null && selection.getEndRes() < av.alignment.getWidth())\r
184       {\r
185         item = item.substring(0, selection.getEndRes() + 1);\r
186       }\r
187 \r
188       ///Shall we ignore gaps????\r
189       StringBuffer noGaps = new StringBuffer();\r
190       int insertCount = 0;\r
191       Vector spaces = new Vector();\r
192 \r
193       for (int j = 0; j < item.length(); j++)\r
194       {\r
195 \r
196         if (!jalview.util.Comparison.isGap(item.charAt(j)))\r
197         {\r
198           noGaps.append(item.charAt(j));\r
199           spaces.addElement(new Integer(insertCount));\r
200         }\r
201         else\r
202         {\r
203           insertCount++;\r
204         }\r
205       }\r
206 \r
207       for (int r = resIndex; r < noGaps.length(); r++)\r
208       {\r
209 \r
210         if (regex.searchFrom(noGaps.toString(), r))\r
211         {\r
212           resIndex = regex.matchedFrom();\r
213           if (selection != null &&\r
214               (resIndex + Integer.parseInt(spaces.elementAt(resIndex).toString())) <\r
215               selection.getStartRes())\r
216           {\r
217             continue;\r
218           }\r
219 \r
220 \r
221           int sres = seq.findPosition(resIndex +\r
222                                       Integer.parseInt(spaces.\r
223               elementAt(resIndex).toString()));\r
224           int eres = seq.findPosition(regex.matchedTo() - 1 +\r
225                                       Integer.parseInt(\r
226                                       spaces.elementAt(regex.matchedTo() - 1).\r
227                                       toString()));\r
228 \r
229           searchResults.addResult(seq, sres, eres);\r
230 \r
231           if (!findAll)\r
232           {\r
233             // thats enough, break and display the result\r
234             found = true;\r
235             resIndex++;\r
236             break;\r
237           }\r
238 \r
239           r = resIndex;\r
240         }\r
241         else\r
242           break;\r
243       }\r
244       if (!found)\r
245       {\r
246         seqIndex++;\r
247         resIndex = 0;\r
248       }\r
249     }\r
250 \r
251     Vector idMatch = new Vector();\r
252     for (int id = 0; id < av.alignment.getHeight(); id++)\r
253     {\r
254       if (regex.search(av.alignment.getSequenceAt(id).getName()))\r
255       {\r
256         idMatch.addElement(av.alignment.getSequenceAt(id));\r
257       }\r
258     }\r
259 \r
260     if (searchResults.getSize() == 0 && idMatch.size() > 0)\r
261     {\r
262       ap.idPanel.highlightSearchResults(idMatch);\r
263     }\r
264 \r
265     if (searchResults.getSize() > 0)\r
266     {\r
267       createNewGroup.setEnabled(true);\r
268     }\r
269     else\r
270     {\r
271       searchResults = null;\r
272       resIndex = 0;\r
273       seqIndex = 0;\r
274     }\r
275 \r
276     // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
277     ap.highlightSearchResults(searchResults);\r
278 \r
279     if (findAll)\r
280     {\r
281       String message = (searchResults.getSize()) + " matches found.";\r
282       System.out.println(message);\r
283     }\r
284 \r
285   }\r
286 \r
287   Label jLabel1 = new Label();\r
288   protected TextField textfield = new TextField();\r
289   protected Button findAll = new Button();\r
290   protected Button findNext = new Button();\r
291   Panel jPanel1 = new Panel();\r
292   GridLayout gridLayout1 = new GridLayout();\r
293   protected Button createNewGroup = new Button();\r
294 \r
295 \r
296   private void jbInit() throws Exception {\r
297       jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));\r
298       jLabel1.setText("Find");\r
299       jLabel1.setBounds(new Rectangle(3, 30, 34, 15));\r
300       this.setLayout(null);\r
301       textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
302       textfield.setText("");\r
303       textfield.setBounds(new Rectangle(40, 27, 133, 21));\r
304       textfield.addKeyListener(new java.awt.event.KeyAdapter() {\r
305               public void keyTyped(KeyEvent e) {\r
306                   textfield_keyTyped(e);\r
307               }\r
308           });\r
309       textfield.addActionListener(this);\r
310       findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
311       findAll.setLabel("Find all");\r
312       findAll.addActionListener(this);\r
313       findNext.setEnabled(false);\r
314       findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
315       findNext.setLabel("Find Next");\r
316       findNext.addActionListener(this);\r
317       jPanel1.setBounds(new Rectangle(180, 5, 141, 64));\r
318       jPanel1.setLayout(gridLayout1);\r
319       gridLayout1.setHgap(0);\r
320       gridLayout1.setRows(3);\r
321       gridLayout1.setVgap(2);\r
322       createNewGroup.setEnabled(false);\r
323       createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
324       createNewGroup.setLabel("Create new group");\r
325       createNewGroup.addActionListener(this);\r
326       jPanel1.add(findNext, null);\r
327       jPanel1.add(findAll, null);\r
328       jPanel1.add(createNewGroup, null);\r
329       this.add(textfield, null);\r
330       this.add(jLabel1, null);\r
331       this.add(jPanel1, null);\r
332   }\r
333 \r
334   void textfield_keyTyped(KeyEvent e) {\r
335       findNext.setEnabled(true);\r
336   }\r
337 \r
338 }\r