2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
20 package jalview.appletgui;
\r
22 import jalview.jbappletgui.GFinder;
\r
23 import jalview.datamodel.*;
\r
24 import java.awt.event.*;
\r
28 public class Finder extends GFinder
\r
33 SuperGroup searchGroup;
\r
34 Vector searchResults;
\r
38 public Finder(final AlignmentPanel ap)
\r
42 frame = new Frame();
\r
44 jalview.bin.JalviewLite.addFrame(frame, "Find", 340,120);
\r
46 frame.addWindowListener(new WindowAdapter()
\r
47 {public void windowClosing(WindowEvent evt)
\r
48 { ap.highlightSearchResults( null ); }
\r
52 public void textfield_actionPerformed(ActionEvent e)
\r
57 public void findNext_actionPerformed(ActionEvent e)
\r
62 public void findAll_actionPerformed(ActionEvent e)
\r
69 public void cancel_actionPerformed(ActionEvent e)
\r
72 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
73 ap.highlightSearchResults( null );
\r
74 ap.idPanel.highlightSearchResults( null );
\r
75 // frame.setClosed(true);
\r
76 }catch(Exception ex){ }
\r
80 public void createNewGroup_actionPerformed(ActionEvent e)
\r
82 Color [] newColors = new Color[24];
\r
83 for(int i=0; i<24; i++)
\r
84 newColors[i] = new Color(60,160,115);
\r
86 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(newColors);
\r
87 String searchString = textfield.getText();
\r
89 searchGroup = new SuperGroup(searchString, ucs , true, true, false);
\r
92 for(int i=0; i<searchResults.size(); i+=3)
\r
94 // its possible edits may have occurred since search was performed
\r
95 // account for this here
\r
97 SequenceI seq = av.alignment.getSequenceAt( Integer.parseInt(searchResults.elementAt(i).toString()) );
\r
98 int startRes = seq.findIndex( Integer.parseInt(searchResults.elementAt(i+1).toString()) )-1;
\r
99 int endRes = seq.findIndex( Integer.parseInt(searchResults.elementAt(i+2).toString()) )-1;
\r
101 SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false, startRes , endRes );
\r
102 sg.addSequence(seq);
\r
103 av.alignment.addGroup(sg);
\r
104 searchGroup.addGroup( sg );
\r
108 ap.av.alignment.addSuperGroup(searchGroup);
\r
109 ap.highlightSearchResults( null );
\r
113 void doSearch(boolean findAll)
\r
115 createNewGroup.setEnabled(false);
\r
117 String searchString = textfield.getText().toUpperCase();
\r
119 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
\r
121 searchResults = new Vector();
\r
122 int [] allResults = null;
\r
126 boolean found = false;
\r
128 ////// is the searchString a residue number?
\r
130 int res = Integer.parseInt(searchString);
\r
133 if(av.getSelectionGroup().getSize()>0)
\r
134 seq = (Sequence)(av.getSelectionGroup().getSequenceAt(0));
\r
136 seq = (Sequence)av.getAlignment().getSequenceAt(0);
\r
138 searchResults.addElement( Integer.toString( av.getAlignment().findIndex(seq) ) );
\r
139 searchResults.addElement( Integer.toString( seq.findIndex(res)-1 ) );
\r
140 searchResults.addElement( Integer.toString( seq.findIndex(res)-1 ) );
\r
142 }catch(NumberFormatException ex){}
\r
143 ///////////////////////////////////////////////
\r
146 int end = av.alignment.getHeight();
\r
148 SequenceGroup selection = av.getSelectionGroup();
\r
149 if(selection!=null)
\r
150 if(selection.getSize()<1 || (selection.getEndRes()-selection.getStartRes()<2))
\r
153 while( !found && seqIndex<end)
\r
155 seq = (Sequence)av.alignment.getSequenceAt(seqIndex);
\r
158 if(selection!=null && !selection.sequences.contains(seq))
\r
165 item = seq.getSequence().toUpperCase();
\r
167 if(selection!=null && selection.getEndRes()<av.alignment.getWidth())
\r
168 item = item.substring(0, selection.getEndRes()+1);
\r
170 ///Shall we ignore gaps????
\r
171 StringBuffer noGaps = new StringBuffer();
\r
173 Vector spaces = new Vector();
\r
175 for (int j=0; j < item.length(); j++)
\r
178 if(!jalview.util.Comparison.isGap(item.charAt(j)))
\r
180 noGaps.append(item.charAt(j));
\r
181 spaces.addElement(new Integer(insertCount));
\r
189 for(int r = resIndex; r<noGaps.length(); r++)
\r
192 if( regex.searchFrom( noGaps.toString(), r ) )
\r
194 resIndex = regex.matchedFrom();
\r
195 if(selection!=null && ( resIndex+ Integer.parseInt(spaces.elementAt(resIndex).toString()) )<selection.getStartRes())
\r
198 searchResults.addElement( Integer.toString( seqIndex) );
\r
199 int sres = seq.findPosition( resIndex+ Integer.parseInt(spaces.elementAt(resIndex).toString()) );
\r
200 int eres = seq.findPosition( regex.matchedTo()-1 + Integer.parseInt(spaces.elementAt(regex.matchedTo()-1).toString()) );
\r
201 searchResults.addElement( sres+"" );
\r
202 searchResults.addElement( eres+"" );
\r
207 // thats enough, break and display the result
\r
224 Vector idMatch = new Vector();
\r
225 for(int id = 0; id<av.alignment.getHeight(); id++)
\r
227 if( regex.search( av.alignment.getSequenceAt(id).getName() ) )
\r
228 idMatch.addElement( av.alignment.getSequenceAt(id) );
\r
231 if(searchResults.size()==0 && idMatch.size()>0)
\r
232 ap.idPanel.highlightSearchResults( idMatch );
\r
235 if(searchResults.size()>0)
\r
237 allResults = new int[searchResults.size()];
\r
238 for(int i=0; i<searchResults.size(); i++)
\r
239 allResults[i] = Integer.parseInt(searchResults.elementAt(i).toString());
\r
241 createNewGroup.setEnabled(true);
\r
249 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
250 ap.highlightSearchResults( allResults );
\r
254 String message = (searchResults.size()/3) + " matches found.";
\r
255 System.out.println(message);
\r