sequence is char []
[jalview.git] / src / jalview / appletgui / Finder.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 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 import java.awt.Rectangle;\r
29 \r
30 public class Finder extends Panel implements ActionListener\r
31 {\r
32   AlignViewport av;\r
33   AlignmentPanel ap;\r
34   Frame frame;\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 \r
88     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, null);\r
89     cap.ok.setLabel("Accept");\r
90     Dialog dialog = new Dialog(ap.alignFrame, "Enter New Feature Name", true);\r
91     dialog.add(cap);\r
92 \r
93     cap.setText(textfield.getText());\r
94 \r
95     dialog.setBounds( frame.getLocation().x+frame.getSize().width+5,\r
96                       frame.getLocation().y+20,300,100);\r
97     dialog.show();\r
98 \r
99 \r
100     String featureName = cap.getText().trim();\r
101     if(featureName.length()<1)\r
102       return;\r
103 \r
104 \r
105     for (int i = 0; i < searchResults.getSize(); i ++ )\r
106     {\r
107         SequenceI seq = searchResults.getResultSequence(i);\r
108 \r
109         SequenceFeature sf = new SequenceFeature(featureName,\r
110             null, null,\r
111             searchResults.getResultStart(i),\r
112            searchResults.getResultEnd(i), "Search Results");\r
113 \r
114         ap.seqPanel.seqCanvas.getFeatureRenderer().addNewFeature(\r
115             featureName, new Color(60,160,115));\r
116         seq.addSequenceFeature(sf);\r
117     }\r
118 \r
119     ap.seqPanel.seqCanvas.getFeatureRenderer().findAllFeatures();\r
120     ap.alignFrame.sequenceFeatures.setState(true);\r
121     av.showSequenceFeatures(true);\r
122     ap.highlightSearchResults(null);\r
123   }\r
124 \r
125   void doSearch(boolean findAll)\r
126   {\r
127     if(jalview.bin.JalviewLite.currentAlignFrame!=null)\r
128     {\r
129       ap = jalview.bin.JalviewLite.currentAlignFrame.alignPanel;\r
130       av = ap.av;\r
131     }\r
132     createNewGroup.setEnabled(false);\r
133 \r
134     String searchString = textfield.getText();\r
135     if(!caseSensitive.getState())\r
136       searchString = searchString.toUpperCase();\r
137 \r
138     com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);\r
139 \r
140     searchResults = new SearchResults();\r
141 \r
142     Sequence seq;\r
143     String item = null;\r
144     boolean found = false;\r
145 \r
146     ////// is the searchString a residue number?\r
147     try\r
148     {\r
149       int res = Integer.parseInt(searchString);\r
150       found = true;\r
151 \r
152       if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize(false) < 1)\r
153       {\r
154         seq = (Sequence) av.getAlignment().getSequenceAt(0);\r
155       }\r
156       else\r
157       {\r
158         seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));\r
159       }\r
160 \r
161 \r
162       searchResults.addResult(seq, res, res);\r
163 \r
164     }\r
165     catch (NumberFormatException ex)\r
166     {}\r
167     ///////////////////////////////////////////////\r
168 \r
169 \r
170     int end = av.alignment.getHeight();\r
171 \r
172     SequenceGroup selection = av.getSelectionGroup();\r
173     if (selection != null)\r
174     {\r
175       if (selection.getSize(false) < 1 ||\r
176           (selection.getEndRes() - selection.getStartRes() < 2))\r
177       {\r
178         selection = null;\r
179       }\r
180     }\r
181 \r
182     while (!found && seqIndex < end)\r
183     {\r
184 \r
185       seq = (Sequence) av.alignment.getSequenceAt(seqIndex);\r
186 \r
187       if (selection != null && !selection.getSequences(false).contains(seq))\r
188       {\r
189         seqIndex++;\r
190         resIndex = 0;\r
191         continue;\r
192       }\r
193 \r
194       item = seq.getSequenceAsString();\r
195       if(!caseSensitive.getState())\r
196         item = item.toUpperCase();\r
197 \r
198       if (selection != null && selection.getEndRes() < av.alignment.getWidth())\r
199       {\r
200         item = item.substring(0, selection.getEndRes() + 1);\r
201       }\r
202 \r
203       ///Shall we ignore gaps????\r
204       StringBuffer noGapsSB = new StringBuffer();\r
205       int insertCount = 0;\r
206       Vector spaces = new Vector();\r
207 \r
208       for (int j = 0; j < item.length(); j++)\r
209       {\r
210 \r
211         if (!jalview.util.Comparison.isGap(item.charAt(j)))\r
212         {\r
213           noGapsSB.append(item.charAt(j));\r
214           spaces.addElement(new Integer(insertCount));\r
215         }\r
216         else\r
217         {\r
218           insertCount++;\r
219         }\r
220       }\r
221 \r
222       String noGaps = noGapsSB.toString();\r
223 \r
224       for (int r = resIndex; r < noGaps.length(); r++)\r
225       {\r
226 \r
227         if (regex.searchFrom(noGaps, r))\r
228         {\r
229           resIndex = regex.matchedFrom();\r
230           if (selection != null &&\r
231               (resIndex + Integer.parseInt(spaces.elementAt(resIndex).toString())) <\r
232               selection.getStartRes())\r
233           {\r
234             continue;\r
235           }\r
236 \r
237 \r
238           int sres = seq.findPosition(resIndex +\r
239                                       Integer.parseInt(spaces.\r
240               elementAt(resIndex).toString()));\r
241           int eres = seq.findPosition(regex.matchedTo() - 1 +\r
242                                       Integer.parseInt(\r
243                                       spaces.elementAt(regex.matchedTo() - 1).\r
244                                       toString()));\r
245 \r
246           searchResults.addResult(seq, sres, eres);\r
247 \r
248           if (!findAll)\r
249           {\r
250             // thats enough, break and display the result\r
251             found = true;\r
252             resIndex++;\r
253             break;\r
254           }\r
255 \r
256           r = resIndex;\r
257         }\r
258         else\r
259           break;\r
260       }\r
261       if (!found)\r
262       {\r
263         seqIndex++;\r
264         resIndex = 0;\r
265       }\r
266     }\r
267 \r
268     Vector idMatch = new Vector();\r
269     for (int id = 0; id < av.alignment.getHeight(); id++)\r
270     {\r
271       if (regex.search(av.alignment.getSequenceAt(id).getName()))\r
272       {\r
273         idMatch.addElement(av.alignment.getSequenceAt(id));\r
274       }\r
275     }\r
276 \r
277     if (searchResults.getSize() == 0 && idMatch.size() > 0)\r
278     {\r
279       ap.idPanel.highlightSearchResults(idMatch);\r
280     }\r
281 \r
282     if (searchResults.getSize() > 0)\r
283     {\r
284       createNewGroup.setEnabled(true);\r
285     }\r
286     else\r
287     {\r
288       searchResults = null;\r
289       resIndex = 0;\r
290       seqIndex = 0;\r
291     }\r
292 \r
293     // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
294     ap.highlightSearchResults(searchResults);\r
295 \r
296     if (findAll)\r
297     {\r
298       String message = (searchResults==null?0 : searchResults.getSize()) + " matches found.";\r
299       ap.alignFrame.statusBar.setText("Search results: "+searchString+" : "+message);\r
300     }\r
301 \r
302   }\r
303 \r
304   Label jLabel1 = new Label();\r
305   protected TextField textfield = new TextField();\r
306   protected Button findAll = new Button();\r
307   protected Button findNext = new Button();\r
308   Panel jPanel1 = new Panel();\r
309   GridLayout gridLayout1 = new GridLayout();\r
310   protected Button createNewGroup = new Button();\r
311   Checkbox caseSensitive = new Checkbox();\r
312 \r
313   private void jbInit() throws Exception {\r
314       jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));\r
315       jLabel1.setText("Find");\r
316       jLabel1.setBounds(new Rectangle(3, 30, 34, 15));\r
317       this.setLayout(null);\r
318       textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
319       textfield.setText("");\r
320       textfield.setBounds(new Rectangle(40, 27, 133, 21));\r
321       textfield.addKeyListener(new java.awt.event.KeyAdapter() {\r
322               public void keyTyped(KeyEvent e) {\r
323                   textfield_keyTyped(e);\r
324               }\r
325           });\r
326       textfield.addActionListener(this);\r
327       findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
328       findAll.setLabel("Find all");\r
329       findAll.addActionListener(this);\r
330       findNext.setEnabled(false);\r
331       findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
332       findNext.setLabel("Find Next");\r
333       findNext.addActionListener(this);\r
334       jPanel1.setBounds(new Rectangle(180, 5, 141, 64));\r
335       jPanel1.setLayout(gridLayout1);\r
336       gridLayout1.setHgap(0);\r
337       gridLayout1.setRows(3);\r
338       gridLayout1.setVgap(2);\r
339       createNewGroup.setEnabled(false);\r
340       createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));\r
341       createNewGroup.setLabel("New Feature");\r
342       createNewGroup.addActionListener(this);\r
343     caseSensitive.setLabel("Match Case");\r
344     caseSensitive.setBounds(new Rectangle(40, 49, 126, 23));\r
345     jPanel1.add(findNext, null);\r
346       jPanel1.add(findAll, null);\r
347       jPanel1.add(createNewGroup, null);\r
348     this.add(caseSensitive);\r
349     this.add(textfield, null);\r
350       this.add(jLabel1, null);\r
351       this.add(jPanel1, null);\r
352   }\r
353 \r
354   void textfield_keyTyped(KeyEvent e) {\r
355       findNext.setEnabled(true);\r
356   }\r
357 \r
358 }\r