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