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
29 public class Finder extends Panel implements ActionListener
\r
34 SuperGroup searchGroup;
\r
36 SearchResults searchResults;
\r
40 public Finder(final AlignmentPanel ap)
\r
48 e.printStackTrace();
\r
53 frame = new Frame();
\r
55 jalview.bin.JalviewLite.addFrame(frame, "Find", 340, 120);
\r
57 frame.addWindowListener(new WindowAdapter()
\r
59 public void windowClosing(WindowEvent evt)
\r
61 ap.highlightSearchResults(null);
\r
66 public void actionPerformed(ActionEvent evt)
\r
68 if (evt.getSource() == textfield)
\r
71 else if (evt.getSource() == findNext)
\r
74 else if (evt.getSource() == findAll)
\r
80 else if(evt.getSource() == createNewGroup)
\r
81 createNewGroup_actionPerformed();
\r
85 public void createNewGroup_actionPerformed()
\r
87 Color[] newColors = new Color[24];
\r
88 for (int i = 0; i < 24; i++)
\r
90 newColors[i] = new Color(60, 160, 115);
\r
93 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
\r
95 String searchString = textfield.getText();
\r
97 searchGroup = new SuperGroup(searchString, ucs, true, true, false);
\r
99 for (int i = 0; i < searchResults.getSize(); i ++ )
\r
101 // its possible edits may have occurred since search was performed
\r
102 // account for this here
\r
104 SequenceI seq = searchResults.getResultSequence(i);
\r
105 int startRes = seq.findIndex(searchResults.getResultStart(i)) - 1;
\r
106 int endRes = seq.findIndex(searchResults.getResultEnd(i)) - 1;
\r
108 SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false,
\r
110 sg.addSequence(seq, false);
\r
111 av.alignment.addGroup(sg);
\r
112 searchGroup.addGroup(sg);
\r
116 ap.av.alignment.addSuperGroup(searchGroup);
\r
117 ap.highlightSearchResults(null);
\r
120 void doSearch(boolean findAll)
\r
122 createNewGroup.setEnabled(false);
\r
124 String searchString = textfield.getText().toUpperCase();
\r
126 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
\r
128 searchResults = new SearchResults();
\r
131 String item = null;
\r
132 boolean found = false;
\r
134 ////// is the searchString a residue number?
\r
137 int res = Integer.parseInt(searchString);
\r
140 if (av.getSelectionGroup() == null || av.getSelectionGroup().getSize() < 1)
\r
142 seq = (Sequence) av.getAlignment().getSequenceAt(0);
\r
146 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));
\r
150 searchResults.addResult(seq, res, res);
\r
153 catch (NumberFormatException ex)
\r
155 ///////////////////////////////////////////////
\r
158 int end = av.alignment.getHeight();
\r
160 SequenceGroup selection = av.getSelectionGroup();
\r
161 if (selection != null)
\r
163 if (selection.getSize() < 1 ||
\r
164 (selection.getEndRes() - selection.getStartRes() < 2))
\r
170 while (!found && seqIndex < end)
\r
172 seq = (Sequence) av.alignment.getSequenceAt(seqIndex);
\r
174 if (selection != null && !selection.sequences.contains(seq))
\r
181 item = seq.getSequence().toUpperCase();
\r
183 if (selection != null && selection.getEndRes() < av.alignment.getWidth())
\r
185 item = item.substring(0, selection.getEndRes() + 1);
\r
188 ///Shall we ignore gaps????
\r
189 StringBuffer noGaps = new StringBuffer();
\r
190 int insertCount = 0;
\r
191 Vector spaces = new Vector();
\r
193 for (int j = 0; j < item.length(); j++)
\r
196 if (!jalview.util.Comparison.isGap(item.charAt(j)))
\r
198 noGaps.append(item.charAt(j));
\r
199 spaces.addElement(new Integer(insertCount));
\r
207 for (int r = resIndex; r < noGaps.length(); r++)
\r
210 if (regex.searchFrom(noGaps.toString(), r))
\r
212 resIndex = regex.matchedFrom();
\r
213 if (selection != null &&
\r
214 (resIndex + Integer.parseInt(spaces.elementAt(resIndex).toString())) <
\r
215 selection.getStartRes())
\r
221 int sres = seq.findPosition(resIndex +
\r
222 Integer.parseInt(spaces.
\r
223 elementAt(resIndex).toString()));
\r
224 int eres = seq.findPosition(regex.matchedTo() - 1 +
\r
226 spaces.elementAt(regex.matchedTo() - 1).
\r
229 searchResults.addResult(seq, sres, eres);
\r
233 // thats enough, break and display the result
\r
251 Vector idMatch = new Vector();
\r
252 for (int id = 0; id < av.alignment.getHeight(); id++)
\r
254 if (regex.search(av.alignment.getSequenceAt(id).getName()))
\r
256 idMatch.addElement(av.alignment.getSequenceAt(id));
\r
260 if (searchResults.getSize() == 0 && idMatch.size() > 0)
\r
262 ap.idPanel.highlightSearchResults(idMatch);
\r
265 if (searchResults.getSize() > 0)
\r
267 createNewGroup.setEnabled(true);
\r
271 searchResults = null;
\r
276 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
277 ap.highlightSearchResults(searchResults);
\r
281 String message = (searchResults.getSize()) + " matches found.";
\r
282 System.out.println(message);
\r
287 Label jLabel1 = new Label();
\r
288 protected TextField textfield = new TextField();
\r
289 protected Button findAll = new Button();
\r
290 protected Button findNext = new Button();
\r
291 Panel jPanel1 = new Panel();
\r
292 GridLayout gridLayout1 = new GridLayout();
\r
293 protected Button createNewGroup = new Button();
\r
296 private void jbInit() throws Exception {
\r
297 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
\r
298 jLabel1.setText("Find");
\r
299 jLabel1.setBounds(new Rectangle(3, 30, 34, 15));
\r
300 this.setLayout(null);
\r
301 textfield.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
302 textfield.setText("");
\r
303 textfield.setBounds(new Rectangle(40, 27, 133, 21));
\r
304 textfield.addKeyListener(new java.awt.event.KeyAdapter() {
\r
305 public void keyTyped(KeyEvent e) {
\r
306 textfield_keyTyped(e);
\r
309 textfield.addActionListener(this);
\r
310 findAll.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
311 findAll.setLabel("Find all");
\r
312 findAll.addActionListener(this);
\r
313 findNext.setEnabled(false);
\r
314 findNext.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
315 findNext.setLabel("Find Next");
\r
316 findNext.addActionListener(this);
\r
317 jPanel1.setBounds(new Rectangle(180, 5, 141, 64));
\r
318 jPanel1.setLayout(gridLayout1);
\r
319 gridLayout1.setHgap(0);
\r
320 gridLayout1.setRows(3);
\r
321 gridLayout1.setVgap(2);
\r
322 createNewGroup.setEnabled(false);
\r
323 createNewGroup.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
\r
324 createNewGroup.setLabel("Create new group");
\r
325 createNewGroup.addActionListener(this);
\r
326 jPanel1.add(findNext, null);
\r
327 jPanel1.add(findAll, null);
\r
328 jPanel1.add(createNewGroup, null);
\r
329 this.add(textfield, null);
\r
330 this.add(jLabel1, null);
\r
331 this.add(jPanel1, null);
\r
334 void textfield_keyTyped(KeyEvent e) {
\r
335 findNext.setEnabled(true);
\r