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
25 import java.awt.event.*;
\r
27 import jalview.datamodel.*;
\r
28 import jalview.jbappletgui.*;
\r
36 SuperGroup searchGroup;
\r
37 Vector searchResults;
\r
41 public Finder(final AlignmentPanel ap)
\r
45 frame = new Frame();
\r
47 jalview.bin.JalviewLite.addFrame(frame, "Find", 340, 120);
\r
49 frame.addWindowListener(new WindowAdapter()
\r
51 public void windowClosing(WindowEvent evt)
\r
53 ap.highlightSearchResults(null);
\r
58 public void textfield_actionPerformed(ActionEvent e)
\r
63 public void findNext_actionPerformed(ActionEvent e)
\r
68 public void findAll_actionPerformed(ActionEvent e)
\r
75 public void cancel_actionPerformed(ActionEvent e)
\r
79 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
80 ap.highlightSearchResults(null);
\r
81 ap.idPanel.highlightSearchResults(null);
\r
82 // frame.setClosed(true);
\r
84 catch (Exception ex)
\r
88 public void createNewGroup_actionPerformed(ActionEvent e)
\r
90 Color[] newColors = new Color[24];
\r
91 for (int i = 0; i < 24; i++)
\r
93 newColors[i] = new Color(60, 160, 115);
\r
96 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
\r
98 String searchString = textfield.getText();
\r
100 searchGroup = new SuperGroup(searchString, ucs, true, true, false);
\r
102 for (int i = 0; i < searchResults.size(); i += 3)
\r
104 // its possible edits may have occurred since search was performed
\r
105 // account for this here
\r
107 SequenceI seq = av.alignment.getSequenceAt(Integer.parseInt(searchResults.
\r
108 elementAt(i).toString()));
\r
109 int startRes = seq.findIndex(Integer.parseInt(searchResults.elementAt(i +
\r
110 1).toString())) - 1;
\r
111 int endRes = seq.findIndex(Integer.parseInt(searchResults.elementAt(i + 2).
\r
114 SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false,
\r
116 sg.addSequence(seq, false);
\r
117 av.alignment.addGroup(sg);
\r
118 searchGroup.addGroup(sg);
\r
122 ap.av.alignment.addSuperGroup(searchGroup);
\r
123 ap.highlightSearchResults(null);
\r
126 void doSearch(boolean findAll)
\r
128 createNewGroup.setEnabled(false);
\r
130 String searchString = textfield.getText().toUpperCase();
\r
132 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
\r
134 searchResults = new Vector();
\r
135 int[] allResults = null;
\r
138 String item = null;
\r
139 boolean found = false;
\r
141 ////// is the searchString a residue number?
\r
144 int res = Integer.parseInt(searchString);
\r
147 if (av.getSelectionGroup().getSize() > 0)
\r
149 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));
\r
153 seq = (Sequence) av.getAlignment().getSequenceAt(0);
\r
156 searchResults.addElement(Integer.toString(av.getAlignment().findIndex(seq)));
\r
157 searchResults.addElement(Integer.toString(seq.findIndex(res) - 1));
\r
158 searchResults.addElement(Integer.toString(seq.findIndex(res) - 1));
\r
161 catch (NumberFormatException ex)
\r
163 ///////////////////////////////////////////////
\r
166 int end = av.alignment.getHeight();
\r
168 SequenceGroup selection = av.getSelectionGroup();
\r
169 if (selection != null)
\r
171 if (selection.getSize() < 1 ||
\r
172 (selection.getEndRes() - selection.getStartRes() < 2))
\r
178 while (!found && seqIndex < end)
\r
180 seq = (Sequence) av.alignment.getSequenceAt(seqIndex);
\r
182 if (selection != null && !selection.sequences.contains(seq))
\r
189 item = seq.getSequence().toUpperCase();
\r
191 if (selection != null && selection.getEndRes() < av.alignment.getWidth())
\r
193 item = item.substring(0, selection.getEndRes() + 1);
\r
196 ///Shall we ignore gaps????
\r
197 StringBuffer noGaps = new StringBuffer();
\r
198 int insertCount = 0;
\r
199 Vector spaces = new Vector();
\r
201 for (int j = 0; j < item.length(); j++)
\r
204 if (!jalview.util.Comparison.isGap(item.charAt(j)))
\r
206 noGaps.append(item.charAt(j));
\r
207 spaces.addElement(new Integer(insertCount));
\r
215 for (int r = resIndex; r < noGaps.length(); r++)
\r
218 if (regex.searchFrom(noGaps.toString(), r))
\r
220 resIndex = regex.matchedFrom();
\r
221 if (selection != null &&
\r
222 (resIndex + Integer.parseInt(spaces.elementAt(resIndex).toString())) <
\r
223 selection.getStartRes())
\r
228 searchResults.addElement(Integer.toString(seqIndex));
\r
229 int sres = seq.findPosition(resIndex +
\r
230 Integer.parseInt(spaces.
\r
231 elementAt(resIndex).toString()));
\r
232 int eres = seq.findPosition(regex.matchedTo() - 1 +
\r
234 spaces.elementAt(regex.matchedTo() - 1).
\r
236 searchResults.addElement(sres + "");
\r
237 searchResults.addElement(eres + "");
\r
241 // thats enough, break and display the result
\r
257 Vector idMatch = new Vector();
\r
258 for (int id = 0; id < av.alignment.getHeight(); id++)
\r
260 if (regex.search(av.alignment.getSequenceAt(id).getName()))
\r
262 idMatch.addElement(av.alignment.getSequenceAt(id));
\r
266 if (searchResults.size() == 0 && idMatch.size() > 0)
\r
268 ap.idPanel.highlightSearchResults(idMatch);
\r
271 if (searchResults.size() > 0)
\r
273 allResults = new int[searchResults.size()];
\r
274 for (int i = 0; i < searchResults.size(); i++)
\r
276 allResults[i] = Integer.parseInt(searchResults.elementAt(i).toString());
\r
279 createNewGroup.setEnabled(true);
\r
287 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
288 ap.highlightSearchResults(allResults);
\r
292 String message = (searchResults.size() / 3) + " matches found.";
\r
293 System.out.println(message);
\r