594baf8ac358b897891fd157255a73d06ecd87e7
[jalview.git] / src / jalview / gui / Finder.java
1 package jalview.gui;\r
2 \r
3 import jalview.jbgui.GFinder;\r
4 import jalview.datamodel.*;\r
5 import java.awt.event.*;\r
6 import javax.swing.*;\r
7 import javax.swing.event.*;\r
8 import java.util.*;\r
9 import java.awt.*;\r
10 \r
11 public class Finder extends GFinder\r
12 {\r
13   AlignViewport av;\r
14   AlignmentPanel ap;\r
15   JInternalFrame frame;\r
16   SuperGroup searchGroup;\r
17   Vector searchResults;\r
18 \r
19   int seqIndex = 0;\r
20   int resIndex = 0;\r
21   public Finder(AlignViewport av, AlignmentPanel ap, JInternalFrame f)\r
22   {\r
23     this.av = av;\r
24     this.ap = ap;\r
25     frame = f;\r
26 \r
27     // all a big pain, but we need to wait until the frame is visible before the textfield can\r
28     // obtain the focus/////////////////////////\r
29     frame.addInternalFrameListener(new InternalFrameAdapter()\r
30     {\r
31       public void internalFrameOpened(InternalFrameEvent evt)\r
32       {\r
33         SwingUtilities.invokeLater(new Runnable()\r
34         {\r
35           public void run()\r
36           {   textfield.requestFocus();      }\r
37        });\r
38      }\r
39 \r
40      public void internalFrameClosing(InternalFrameEvent evt)\r
41      {\r
42        cancel_actionPerformed(null);\r
43      }\r
44 });\r
45 \r
46   }\r
47 \r
48   public void textfield_actionPerformed(ActionEvent e)\r
49   {\r
50     doSearch(false);\r
51   }\r
52 \r
53   public void findNext_actionPerformed(ActionEvent e)\r
54   {\r
55     doSearch(false);\r
56   }\r
57 \r
58   public void findAll_actionPerformed(ActionEvent e)\r
59   {\r
60     resIndex=0;\r
61     seqIndex=0;\r
62     doSearch(true);\r
63   }\r
64 \r
65   public void cancel_actionPerformed(ActionEvent e)\r
66   {\r
67     try{\r
68       // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
69       ap.highlightSearchResults( null );\r
70       ap.idPanel.highlightSearchResults( null );\r
71      // frame.setClosed(true);\r
72     }catch(Exception ex){ }\r
73   }\r
74 \r
75 \r
76   public void createNewGroup_actionPerformed(ActionEvent e)\r
77   {\r
78     jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();\r
79    Color [] newColors = new Color[24];\r
80    for(int i=0; i<24; i++)\r
81      newColors[i] = new Color(60,160,115);\r
82 \r
83    ucs.setColourScheme(newColors);\r
84    String searchString = textfield.getText();\r
85 \r
86    searchGroup = new SuperGroup(searchString, ucs , true, true, false);\r
87 \r
88 \r
89     for(int i=0; i<searchResults.size(); i+=3)\r
90     {\r
91       // its possible edits may have occurred since search was performed\r
92       // account for this here\r
93 \r
94       SequenceI seq = av.alignment.getSequenceAt( Integer.parseInt(searchResults.elementAt(i).toString()) );\r
95       int startRes = seq.findIndex( Integer.parseInt(searchResults.elementAt(i+1).toString()) )-1;\r
96       int endRes = seq.findIndex(   Integer.parseInt(searchResults.elementAt(i+2).toString()) )-1;\r
97 \r
98       SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false, startRes , endRes  );\r
99       sg.addSequence(seq);\r
100       av.alignment.addGroup(sg);\r
101       searchGroup.addGroup( sg );\r
102 \r
103     }\r
104 \r
105     ap.av.alignment.addSuperGroup(searchGroup);\r
106     ap.highlightSearchResults( null );\r
107   }\r
108 \r
109 \r
110   void doSearch(boolean findAll)\r
111   {\r
112     createNewGroup.setEnabled(false);\r
113 \r
114     String searchString = textfield.getText().toUpperCase();\r
115 \r
116     com.stevesoft.pat.Regex regex = new  com.stevesoft.pat.Regex(searchString);\r
117 \r
118     searchResults = new Vector();\r
119     int [] allResults = null;\r
120 \r
121     Sequence seq;\r
122     String item=null;\r
123     boolean found = false;\r
124 \r
125     ////// is the searchString a residue number?\r
126     try{\r
127       int res = Integer.parseInt(searchString);\r
128       found = true;\r
129 \r
130       if(av.getSelectionGroup().getSize()>0)\r
131         seq = (Sequence)(av.getSelectionGroup().getSequenceAt(0));\r
132       else\r
133         seq = (Sequence)av.getAlignment().getSequenceAt(0);\r
134 \r
135       searchResults.add( Integer.toString( av.getAlignment().findIndex(seq) ) );\r
136       searchResults.add( Integer.toString( seq.findIndex(res)-1 ) );\r
137       searchResults.add( Integer.toString( seq.findIndex(res)-1 ) );\r
138 \r
139     }catch(NumberFormatException ex){}\r
140     ///////////////////////////////////////////////\r
141 \r
142 \r
143     jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();\r
144     Color [] newColors = new Color[24];\r
145     for(int i=0; i<24; i++)\r
146       newColors[i] = new Color(60,160,115);\r
147 \r
148     ucs.setColourScheme(newColors);\r
149 \r
150     searchGroup = new SuperGroup(searchString, ucs , true, true, false);\r
151 \r
152     int end = av.alignment.getHeight();\r
153 \r
154     SequenceGroup selection = av.getSelectionGroup();\r
155     if(selection!=null)\r
156      if(selection.getSize()<1 || (selection.getEndRes()-selection.getStartRes()<2))\r
157       selection = null;\r
158 \r
159     while( !found && seqIndex<end)\r
160     {\r
161       seq = (Sequence)av.alignment.getSequenceAt(seqIndex);\r
162 \r
163 \r
164       if(selection!=null && !selection.sequences.contains(seq))\r
165       {\r
166         seqIndex++;\r
167         resIndex=0;\r
168         continue;\r
169       }\r
170 \r
171       item = seq.getSequence().toUpperCase();\r
172 \r
173       if(selection!=null && selection.getEndRes()<av.alignment.getWidth())\r
174           item = item.substring(0, selection.getEndRes()+1);\r
175 \r
176       ///Shall we ignore gaps????\r
177       StringBuffer noGaps = new StringBuffer();\r
178       int insertCount=0;\r
179       Vector spaces = new Vector();\r
180 \r
181       for (int j=0; j < item.length(); j++)\r
182       {\r
183 \r
184         if(!jalview.util.Comparison.isGap(item.charAt(j)))\r
185          {\r
186            noGaps.append(item.charAt(j));\r
187            spaces.add(new Integer(insertCount));\r
188          }\r
189         else\r
190            insertCount++;\r
191       }\r
192 \r
193 \r
194 \r
195       for(int r = resIndex; r<noGaps.length(); r++)\r
196       {\r
197 \r
198        if( regex.searchFrom( noGaps.toString(), r ) )\r
199        {\r
200          resIndex = regex.matchedFrom();\r
201          if(selection!=null && ( resIndex+ Integer.parseInt(spaces.get(resIndex).toString()) )<selection.getStartRes())\r
202            continue;\r
203 \r
204          searchResults.add( Integer.toString( seqIndex) );\r
205 \r
206          int sres = seq.findPosition( resIndex+ Integer.parseInt(spaces.elementAt(resIndex).toString()) );\r
207          int eres = seq.findPosition( regex.matchedTo()-1 + Integer.parseInt(spaces.elementAt(regex.matchedTo()-1).toString()) );\r
208 \r
209           searchResults.addElement( sres+"" );\r
210           searchResults.addElement( eres+"" );\r
211 \r
212          if(!findAll)\r
213          {\r
214            // thats enough, break and display the result\r
215            found = true;\r
216            resIndex++;\r
217            break;\r
218          }\r
219 \r
220          r=resIndex;\r
221        }\r
222       }\r
223       if(!found)\r
224       {\r
225         seqIndex++;\r
226         resIndex = 0;\r
227       }\r
228     }\r
229 \r
230 \r
231     Vector idMatch = new Vector();\r
232     for(int id = 0; id<av.alignment.getHeight(); id++)\r
233     {\r
234       if(   regex.search( av.alignment.getSequenceAt(id).getName() ) )\r
235         idMatch.add( av.alignment.getSequenceAt(id) );\r
236     }\r
237 \r
238     if(searchResults.size()==0 && idMatch.size()>0)\r
239       ap.idPanel.highlightSearchResults( idMatch );\r
240 \r
241 \r
242     if(searchResults.size()>0)\r
243     {\r
244       allResults = new int[searchResults.size()];\r
245       for(int i=0; i<searchResults.size(); i++)\r
246         allResults[i] = Integer.parseInt(searchResults.get(i).toString());\r
247 \r
248       createNewGroup.setEnabled(true);\r
249     }\r
250     else\r
251     {\r
252       JOptionPane.showInternalMessageDialog(this, "Finished searching", null, JOptionPane.INFORMATION_MESSAGE);\r
253       resIndex=0;\r
254       seqIndex=0;\r
255     }\r
256 \r
257     // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
258     ap.highlightSearchResults( allResults );\r
259 \r
260     if (findAll)\r
261     {\r
262       String message =  (searchResults.size()/3) + " matches found.";\r
263       JOptionPane.showInternalMessageDialog(this, message, null,\r
264                                             JOptionPane.INFORMATION_MESSAGE);\r
265     }\r
266 \r
267   }\r
268 \r
269 }\r