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