GPL license added
[jalview.git] / src / jalview / gui / Finder.java
index 8567451..d20cb65 100755 (executable)
@@ -1,3 +1,22 @@
+/*\r
+* Jalview - A Sequence Alignment Editor and Viewer\r
+* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+*\r
+* This program is free software; you can redistribute it and/or\r
+* modify it under the terms of the GNU General Public License\r
+* as published by the Free Software Foundation; either version 2\r
+* of the License, or (at your option) any later version.\r
+*\r
+* This program is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with this program; if not, write to the Free Software\r
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+*/\r
+\r
 package jalview.gui;\r
 \r
 import jalview.jbgui.GFinder;\r
@@ -13,8 +32,8 @@ public class Finder extends GFinder
   AlignViewport av;\r
   AlignmentPanel ap;\r
   JInternalFrame frame;\r
-  String searchString;\r
   SuperGroup searchGroup;\r
+  Vector searchResults;\r
 \r
   int seqIndex = 0;\r
   int resIndex = 0;\r
@@ -67,6 +86,7 @@ public class Finder extends GFinder
     try{\r
       // if allResults is null, this effectively switches displaySearch flag in seqCanvas\r
       ap.highlightSearchResults( null );\r
+      ap.idPanel.highlightSearchResults( null );\r
      // frame.setClosed(true);\r
     }catch(Exception ex){ }\r
   }\r
@@ -74,8 +94,32 @@ public class Finder extends GFinder
 \r
   public void createNewGroup_actionPerformed(ActionEvent e)\r
   {\r
-    for(int i=0; i<searchGroup.getSize(); i++)\r
-      av.alignment.addGroup( searchGroup.getGroupeAt(i));\r
+\r
+   Color [] newColors = new Color[24];\r
+   for(int i=0; i<24; i++)\r
+     newColors[i] = new Color(60,160,115);\r
+\r
+   jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(newColors);\r
+   String searchString = textfield.getText();\r
+\r
+   searchGroup = new SuperGroup(searchString, ucs , true, true, false);\r
+\r
+\r
+    for(int i=0; i<searchResults.size(); i+=3)\r
+    {\r
+      // its possible edits may have occurred since search was performed\r
+      // account for this here\r
+\r
+      SequenceI seq = av.alignment.getSequenceAt( Integer.parseInt(searchResults.elementAt(i).toString()) );\r
+      int startRes = seq.findIndex( Integer.parseInt(searchResults.elementAt(i+1).toString()) )-1;\r
+      int endRes = seq.findIndex(   Integer.parseInt(searchResults.elementAt(i+2).toString()) )-1;\r
+\r
+      SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false, startRes , endRes  );\r
+      sg.addSequence(seq);\r
+      av.alignment.addGroup(sg);\r
+      searchGroup.addGroup( sg );\r
+\r
+    }\r
 \r
     ap.av.alignment.addSuperGroup(searchGroup);\r
     ap.highlightSearchResults( null );\r
@@ -85,16 +129,19 @@ public class Finder extends GFinder
   void doSearch(boolean findAll)\r
   {\r
     createNewGroup.setEnabled(false);\r
-    searchString = textfield.getText().toUpperCase();\r
 \r
-    ArrayList searchResults = new ArrayList();\r
+    String searchString = textfield.getText().toUpperCase();\r
+\r
+    com.stevesoft.pat.Regex regex = new  com.stevesoft.pat.Regex(searchString);\r
+\r
+    searchResults = new Vector();\r
     int [] allResults = null;\r
 \r
     Sequence seq;\r
     String item=null;\r
     boolean found = false;\r
 \r
-    /// is the searchString a residue number?\r
+    ////// is the searchString a residue number?\r
     try{\r
       int res = Integer.parseInt(searchString);\r
       found = true;\r
@@ -109,47 +156,46 @@ public class Finder extends GFinder
       searchResults.add( Integer.toString( seq.findIndex(res)-1 ) );\r
 \r
     }catch(NumberFormatException ex){}\r
+    ///////////////////////////////////////////////\r
+\r
+\r
 \r
-    jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();\r
     Color [] newColors = new Color[24];\r
     for(int i=0; i<24; i++)\r
       newColors[i] = new Color(60,160,115);\r
 \r
-    ucs.setColourScheme(newColors);\r
+    jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(newColors);\r
 \r
     searchGroup = new SuperGroup(searchString, ucs , true, true, false);\r
 \r
     int end = av.alignment.getHeight();\r
 \r
-\r
-    SequenceGroup rubberband = av.getSelectionGroup();\r
-    if(rubberband!=null)\r
-      end = rubberband.getEndRes();\r
-\r
-\r
+    SequenceGroup selection = av.getSelectionGroup();\r
+    if(selection!=null)\r
+     if(selection.getSize()<1 || (selection.getEndRes()-selection.getStartRes()<2))\r
+      selection = null;\r
 \r
     while( !found && seqIndex<end)\r
     {\r
-      seq = (Sequence)av.getAlignment().getSequenceAt(seqIndex);\r
+      seq = (Sequence)av.alignment.getSequenceAt(seqIndex);\r
 \r
-      if(   av.getSelectionGroup().getSize()>0 && !av.getSelectionGroup().sequences.contains(seq)\r
-            || (rubberband!=null && !rubberband.sequences.contains(seq))  )\r
+\r
+      if(selection!=null && !selection.sequences.contains(seq))\r
       {\r
-        // if a selection has been made, only search within that selection\r
         seqIndex++;\r
         resIndex=0;\r
         continue;\r
       }\r
 \r
-      item = seq.getSequence();\r
+      item = seq.getSequence().toUpperCase();\r
 \r
-      if(rubberband!=null)\r
-        item = item.substring(0, rubberband.getEndRes()+1);\r
+      if(selection!=null && selection.getEndRes()<av.alignment.getWidth())\r
+          item = item.substring(0, selection.getEndRes()+1);\r
 \r
       ///Shall we ignore gaps????\r
       StringBuffer noGaps = new StringBuffer();\r
       int insertCount=0;\r
-      ArrayList spaces = new ArrayList();\r
+      Vector spaces = new Vector();\r
 \r
       for (int j=0; j < item.length(); j++)\r
       {\r
@@ -164,7 +210,6 @@ public class Finder extends GFinder
       }\r
 \r
 \r
-      com.stevesoft.pat.Regex regex = new  com.stevesoft.pat.Regex(searchString);\r
 \r
       for(int r = resIndex; r<noGaps.length(); r++)\r
       {\r
@@ -172,21 +217,16 @@ public class Finder extends GFinder
        if( regex.searchFrom( noGaps.toString(), r ) )\r
        {\r
          resIndex = regex.matchedFrom();\r
-\r
-\r
-         if(rubberband!=null && ( resIndex+ Integer.parseInt(spaces.get(resIndex).toString()) )<rubberband.getStartRes())\r
+         if(selection!=null && ( resIndex+ Integer.parseInt(spaces.get(resIndex).toString()) )<selection.getStartRes())\r
            continue;\r
 \r
          searchResults.add( Integer.toString( seqIndex) );\r
-         searchResults.add( Integer.toString( resIndex+ Integer.parseInt(spaces.get(resIndex).toString()) ) );\r
-         searchResults.add( Integer.toString( regex.matchedTo()-1 + Integer.parseInt(spaces.get(regex.matchedTo()-1).toString()) ) );\r
 \r
-         SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false,\r
-                                             resIndex+ Integer.parseInt(spaces.get(resIndex).toString()) ,\r
-                                      regex.matchedTo()-1 + Integer.parseInt(spaces.get(regex.matchedTo()-1).toString())\r
-            );\r
-         sg.addSequence((Sequence)av.getAlignment().getSequenceAt(seqIndex));\r
-         searchGroup.addGroup( sg );\r
+         int sres = seq.findPosition( resIndex+ Integer.parseInt(spaces.elementAt(resIndex).toString()) );\r
+         int eres = seq.findPosition( regex.matchedTo()-1 + Integer.parseInt(spaces.elementAt(regex.matchedTo()-1).toString()) );\r
+\r
+          searchResults.addElement( sres+"" );\r
+          searchResults.addElement( eres+"" );\r
 \r
          if(!findAll)\r
          {\r
@@ -206,6 +246,18 @@ public class Finder extends GFinder
       }\r
     }\r
 \r
+\r
+    Vector idMatch = new Vector();\r
+    for(int id = 0; id<av.alignment.getHeight(); id++)\r
+    {\r
+      if(   regex.search( av.alignment.getSequenceAt(id).getName() ) )\r
+        idMatch.add( av.alignment.getSequenceAt(id) );\r
+    }\r
+\r
+    if(searchResults.size()==0 && idMatch.size()>0)\r
+      ap.idPanel.highlightSearchResults( idMatch );\r
+\r
+\r
     if(searchResults.size()>0)\r
     {\r
       allResults = new int[searchResults.size()];\r