header updated
[jalview.git] / src / jalview / analysis / SequenceIdMatcher.java
index b88a03f..ed90422 100755 (executable)
@@ -1,6 +1,6 @@
 /*\r
  * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ * Copyright (C) 2006 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
@@ -115,12 +115,15 @@ public class SequenceIdMatcher
 \r
     SeqIdName(String s)\r
     {\r
-      id = new String(s);\r
+      if (s!=null)\r
+        id = new String(s);\r
+      else\r
+        id = "";\r
     }\r
 \r
     public int hashCode()\r
     {\r
-      return (id.substring(0, 4).hashCode());\r
+      return ((id.length()>=4) ? id.substring(0, 4).hashCode() : id.hashCode());\r
     }\r
 \r
     public boolean equals(Object s)\r
@@ -141,20 +144,30 @@ public class SequenceIdMatcher
     }\r
 \r
     /**\r
-     * matches if one ID properly contains another at a whitespace boundary.\r
-     * TODO: (JBPNote) These are not efficient. should use char[] for speed\r
-     * @param s SeqIdName\r
-     * @return boolean\r
+     * Characters that define the end of a unique sequence ID at\r
+     * the beginning of an arbitrary ID string\r
+     * JBPNote: This is a heuristic that will fail for arbritrarily extended sequence id's\r
+     * (like portions of an aligned set of repeats from one sequence)\r
      */\r
+    private String WORD_SEP="~. |#\\/<>!\"£$%^*)}[@',?";\r
+\r
+   /**\r
+    * matches if one ID properly contains another at a whitespace boundary.\r
+    * TODO: (JBPNote) These are not efficient. should use char[] for speed\r
+    * todo: (JBPNote) Set separator characters appropriately\r
+    * @param s SeqIdName\r
+    * @return boolean\r
+    */\r
     public boolean equals(SeqIdName s)\r
     {\r
       if (id.length()>s.id.length()) {\r
         return id.startsWith(s.id) ?\r
-            (id.equals(s.id) ? true : id.startsWith(s.id+" "))\r
+            (WORD_SEP.indexOf(id.charAt(s.id.length()))>-1)\r
             : false;\r
       } else\r
         return s.id.startsWith(id) ?\r
-            (s.id.equals(id) ? true : s.id.startsWith(id+" "))\r
+            (s.id.equals(id) ? true :\r
+             (WORD_SEP.indexOf(s.id.charAt(id.length()))>-1))\r
             : false;\r
     }\r
 \r
@@ -162,11 +175,12 @@ public class SequenceIdMatcher
     {\r
       if (id.length()>s.length()) {\r
         return id.startsWith(s) ?\r
-            (id.equals(s) ? true : id.startsWith(s+" "))\r
+            (WORD_SEP.indexOf(id.charAt(s.length()))>-1)\r
             : false;\r
       } else\r
         return s.startsWith(id) ?\r
-            (s.equals(id) ? true : s.startsWith(id+" "))\r
+            (s.equals(id) ? true :\r
+             (WORD_SEP.indexOf(s.charAt(id.length()))>-1))\r
             : false;\r
     }\r
   }\r