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
19 package jalview.gui;
\r
24 import java.awt.event.*;
\r
25 import javax.swing.*;
\r
26 import javax.swing.event.*;
\r
28 import jalview.datamodel.*;
\r
29 import jalview.jbgui.*;
\r
36 JInternalFrame frame;
\r
37 SuperGroup searchGroup;
\r
38 Vector searchResults;
\r
42 public Finder(AlignViewport av, AlignmentPanel ap, JInternalFrame f)
\r
48 // all a big pain, but we need to wait until the frame is visible before the textfield can
\r
49 // obtain the focus/////////////////////////
\r
50 frame.addInternalFrameListener(new InternalFrameAdapter()
\r
52 public void internalFrameOpened(InternalFrameEvent evt)
\r
54 SwingUtilities.invokeLater(new Runnable()
\r
58 textfield.requestFocus();
\r
63 public void internalFrameClosing(InternalFrameEvent evt)
\r
65 cancel_actionPerformed(null);
\r
70 public void textfield_actionPerformed(ActionEvent e)
\r
75 public void findNext_actionPerformed(ActionEvent e)
\r
80 public void findAll_actionPerformed(ActionEvent e)
\r
87 public void cancel_actionPerformed(ActionEvent e)
\r
91 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
92 ap.highlightSearchResults(null);
\r
93 ap.idPanel.highlightSearchResults(null);
\r
95 // frame.setClosed(true);
\r
97 catch (Exception ex)
\r
102 public void createNewGroup_actionPerformed(ActionEvent e)
\r
104 Color[] newColors = new Color[24];
\r
106 for (int i = 0; i < 24; i++)
\r
108 newColors[i] = new Color(60, 160, 115);
\r
111 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
\r
113 String searchString = textfield.getText();
\r
115 searchGroup = new SuperGroup(searchString, ucs, true, true, false);
\r
117 for (int i = 0; i < searchResults.size(); i += 3)
\r
119 // its possible edits may have occurred since search was performed
\r
120 // account for this here
\r
121 SequenceI seq = av.alignment.getSequenceAt(Integer.parseInt(
\r
122 searchResults.elementAt(i).toString()));
\r
123 int startRes = seq.findIndex(Integer.parseInt(
\r
124 searchResults.elementAt(i + 1).toString())) - 1;
\r
125 int endRes = seq.findIndex(Integer.parseInt(
\r
126 searchResults.elementAt(i + 2).toString())) - 1;
\r
128 SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true,
\r
129 false, startRes, endRes);
\r
130 sg.addSequence(seq, false);
\r
131 av.alignment.addGroup(sg);
\r
132 searchGroup.addGroup(sg);
\r
135 ap.av.alignment.addSuperGroup(searchGroup);
\r
136 ap.highlightSearchResults(null);
\r
139 void doSearch(boolean findAll)
\r
141 createNewGroup.setEnabled(false);
\r
143 String searchString = textfield.getText().toUpperCase();
\r
145 com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString);
\r
147 searchResults = new Vector();
\r
149 int[] allResults = null;
\r
152 String item = null;
\r
153 boolean found = false;
\r
155 ////// is the searchString a residue number?
\r
158 int res = Integer.parseInt(searchString);
\r
161 if (av.getSelectionGroup().getSize() > 0)
\r
163 seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0));
\r
167 seq = (Sequence) av.getAlignment().getSequenceAt(0);
\r
170 searchResults.add(Integer.toString(av.getAlignment().findIndex(seq)));
\r
171 searchResults.add(Integer.toString(seq.findIndex(res) - 1));
\r
172 searchResults.add(Integer.toString(seq.findIndex(res) - 1));
\r
174 catch (NumberFormatException ex)
\r
178 ///////////////////////////////////////////////
\r
179 Color[] newColors = new Color[24];
\r
181 for (int i = 0; i < 24; i++)
\r
183 newColors[i] = new Color(60, 160, 115);
\r
186 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
\r
189 searchGroup = new SuperGroup(searchString, ucs, true, true, false);
\r
191 int end = av.alignment.getHeight();
\r
193 SequenceGroup selection = av.getSelectionGroup();
\r
195 if (selection != null)
\r
197 if ( (selection.getSize() < 1) ||
\r
198 ( (selection.getEndRes() - selection.getStartRes()) < 2))
\r
204 while (!found && (seqIndex < end))
\r
206 seq = (Sequence) av.alignment.getSequenceAt(seqIndex);
\r
208 if ( (selection != null) && !selection.sequences.contains(seq))
\r
216 item = seq.getSequence().toUpperCase();
\r
218 if ( (selection != null) &&
\r
219 (selection.getEndRes() < av.alignment.getWidth()))
\r
221 item = item.substring(0, selection.getEndRes() + 1);
\r
224 ///Shall we ignore gaps????
\r
225 StringBuffer noGaps = new StringBuffer();
\r
226 int insertCount = 0;
\r
227 Vector spaces = new Vector();
\r
229 for (int j = 0; j < item.length(); j++)
\r
231 if (!jalview.util.Comparison.isGap(item.charAt(j)))
\r
233 noGaps.append(item.charAt(j));
\r
234 spaces.add(new Integer(insertCount));
\r
242 for (int r = resIndex; r < noGaps.length(); r++)
\r
244 if (regex.searchFrom(noGaps.toString(), r))
\r
246 resIndex = regex.matchedFrom();
\r
248 if ( (selection != null) &&
\r
250 Integer.parseInt(spaces.get(resIndex).toString())) <
\r
251 selection.getStartRes()))
\r
256 searchResults.add(Integer.toString(seqIndex));
\r
258 int sres = seq.findPosition(resIndex +
\r
259 Integer.parseInt(spaces.elementAt(
\r
262 int eres = seq.findPosition(regex.matchedTo() - 1 +
\r
263 Integer.parseInt(spaces.elementAt(regex.
\r
267 searchResults.addElement(sres + "");
\r
268 searchResults.addElement(eres + "");
\r
272 // thats enough, break and display the result
\r
290 Vector idMatch = new Vector();
\r
292 for (int id = 0; id < av.alignment.getHeight(); id++)
\r
294 if (regex.search(av.alignment.getSequenceAt(id).getName()))
\r
296 idMatch.add(av.alignment.getSequenceAt(id));
\r
300 if ( (searchResults.size() == 0) && (idMatch.size() > 0))
\r
302 ap.idPanel.highlightSearchResults(idMatch);
\r
305 if (searchResults.size() > 0)
\r
307 allResults = new int[searchResults.size()];
\r
309 for (int i = 0; i < searchResults.size(); i++)
\r
311 allResults[i] = Integer.parseInt(searchResults.get(i).toString());
\r
314 createNewGroup.setEnabled(true);
\r
318 JOptionPane.showInternalMessageDialog(this, "Finished searching",
\r
320 JOptionPane.INFORMATION_MESSAGE);
\r
325 // if allResults is null, this effectively switches displaySearch flag in seqCanvas
\r
326 ap.highlightSearchResults(allResults);
\r
330 String message = (searchResults.size() / 3) + " matches found.";
\r
331 JOptionPane.showInternalMessageDialog(this, message, null,
\r
332 JOptionPane.INFORMATION_MESSAGE);
\r