Add new feature from seqrch 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 \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     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, null);\r
88     Dialog dialog = new Dialog(ap.alignFrame, "Enter New Feature Name", true);\r
89     dialog.add(cap);\r
90 \r
91     cap.setText(textfield.getText());\r
92 \r
93     dialog.setBounds( frame.getLocation().x+frame.getSize().width+5,\r
94                       frame.getLocation().y+20,300,100);\r
95     dialog.show();\r
96 \r
97 \r
98     String featureName = cap.getText().trim();\r
99     if(featureName.length()<1)\r
100       return;\r
101 \r
102 \r
103     for (int i = 0; i < searchResults.getSize(); i ++ )\r
104     {\r
105         SequenceI seq = searchResults.getResultSequence(i);\r
106 \r
107         SequenceFeature sf = new SequenceFeature(featureName,\r
108             null, null,\r
109             searchResults.getResultStart(i),\r
110            searchResults.getResultEnd(i));\r
111 \r
112         ap.seqPanel.seqCanvas.getFeatureRenderer().addNewFeature(\r
113             featureName, new Color(60,160,115));\r
114         seq.addSequenceFeature(sf);\r
115     }\r
116 \r
117     ap.seqPanel.seqCanvas.getFeatureRenderer().findAllFeatures();\r
118     ap.alignFrame.sequenceFeatures.setState(true);\r
119     av.showSequenceFeatures(true);\r
120     ap.highlightSearchResults(null);\r
121   }\r
122 \r
123   void doSearch(boolean findAll)\r
124   {\r
125     createNewGroup.setEnabled(false);\r
126 \r
127     String searchString = textfield.getText().toUpperCase();\r
128 \r
129     com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);\r
130 \r
131     searchResults = new SearchResults();\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 \r
153       searchResults.addResult(seq, res, res);\r
154 \r
155     }\r
156     catch (NumberFormatException ex)\r
157     {}\r
158     ///////////////////////////////////////////////\r
159 \r
160 \r
161     int end = av.alignment.getHeight();\r
162 \r
163     SequenceGroup selection = av.getSelectionGroup();\r
164     if (selection != null)\r
165     {\r
166       if (selection.getSize() < 1 ||\r
167           (selection.getEndRes() - selection.getStartRes() < 2))\r
168       {\r
169         selection = null;\r
170       }\r
171     }\r
172 \r
173     while (!found && seqIndex < end)\r
174     {\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 noGapsSB = 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           noGapsSB.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       String noGaps = noGapsSB.toString();\r
212 \r
213       for (int r = resIndex; r < noGaps.length(); r++)\r
214       {\r
215 \r
216         if (regex.searchFrom(noGaps, r))\r
217         {\r
218           resIndex = regex.matchedFrom();\r
219           if (selection != null &&\r
220               (resIndex + Integer.parseInt(spaces.elementAt(resIndex).toString())) <\r
221               selection.getStartRes())\r
222           {\r
223             continue;\r
224           }\r
225 \r
226 \r
227           int sres = seq.findPosition(resIndex +\r
228                                       Integer.parseInt(spaces.\r
229               elementAt(resIndex).toString()));\r
230           int eres = seq.findPosition(regex.matchedTo() - 1 +\r
231                                       Integer.parseInt(\r
232                                       spaces.elementAt(regex.matchedTo() - 1).\r
233                                       toString()));\r
234 \r
235           searchResults.addResult(seq, sres, eres);\r
236 \r
237           if (!findAll)\r
238           {\r
239             // thats enough, break and display the result\r
240             found = true;\r
241             resIndex++;\r
242             break;\r
243           }\r
244 \r
245           r = resIndex;\r
246         }\r
247         else\r
248           break;\r
249       }\r
250       if (!found)\r
251       {\r
252         seqIndex++;\r
253         resIndex = 0;\r
254       }\r
255     }\r
256 \r
257     Vector idMatch = new Vector();\r
258     for (int id = 0; id < av.alignment.getHeight(); id++)\r
259     {\r
260       if (regex.search(av.alignment.getSequenceAt(id).getName()))\r
261       {\r
262         idMatch.addElement(av.alignment.getSequenceAt(id));\r
263       }\r
264     }\r
265 \r
266     if (searchResults.getSize() == 0 && idMatch.size() > 0)\r
267     {\r
268       ap.idPanel.highlightSearchResults(idMatch);\r
269     }\r
270 \r
271     if (searchResults.getSize() > 0)\r
272     {\r
273       createNewGroup.setEnabled(true);\r
274     }\r
275     else\r
276     {\r
277       searchResults = null;\r
278       resIndex = 0;\r
279       seqIndex = 0;\r
280     }\r
281 \r
282     // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
283     ap.highlightSearchResults(searchResults);\r
284 \r
285     if (findAll)\r
286     {\r
287       String message = (searchResults==null?0 : searchResults.getSize()) + " matches found.";\r
288       ap.alignFrame.statusBar.setText("Search results: "+searchString+" : "+message);\r
289     }\r
290 \r
291   }\r
292 \r
293   Label jLabel1 = new Label();\r
294   protected TextField textfield = new TextField();\r
295   protected Button findAll = new Button();\r
296   protected Button findNext = new Button();\r
297   Panel jPanel1 = new Panel();\r
298   GridLayout gridLayout1 = new GridLayout();\r
299   protected Button createNewGroup = new Button();\r
300 \r
301 \r
302   private void jbInit() throws Exception {\r
303       jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));\r
304       jLabel1.setText("Find");\r
305       jLabel1.setBounds(new Rectangle(3, 30, 34, 15));\r
306       this.setLayout(null);\r
307       textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
308       textfield.setText("");\r
309       textfield.setBounds(new Rectangle(40, 27, 133, 21));\r
310       textfield.addKeyListener(new java.awt.event.KeyAdapter() {\r
311               public void keyTyped(KeyEvent e) {\r
312                   textfield_keyTyped(e);\r
313               }\r
314           });\r
315       textfield.addActionListener(this);\r
316       findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
317       findAll.setLabel("Find all");\r
318       findAll.addActionListener(this);\r
319       findNext.setEnabled(false);\r
320       findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
321       findNext.setLabel("Find Next");\r
322       findNext.addActionListener(this);\r
323       jPanel1.setBounds(new Rectangle(180, 5, 141, 64));\r
324       jPanel1.setLayout(gridLayout1);\r
325       gridLayout1.setHgap(0);\r
326       gridLayout1.setRows(3);\r
327       gridLayout1.setVgap(2);\r
328       createNewGroup.setEnabled(false);\r
329       createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
330       createNewGroup.setLabel("New Feature");\r
331       createNewGroup.addActionListener(this);\r
332       jPanel1.add(findNext, null);\r
333       jPanel1.add(findAll, null);\r
334       jPanel1.add(createNewGroup, null);\r
335       this.add(textfield, null);\r
336       this.add(jLabel1, null);\r
337       this.add(jPanel1, null);\r
338   }\r
339 \r
340   void textfield_keyTyped(KeyEvent e) {\r
341       findNext.setEnabled(true);\r
342   }\r
343 \r
344 }\r