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