Formatting changes
[jalview.git] / src / jalview / analysis / SequenceIdMatcher.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\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
9  *\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
14  *\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
18  */\r
19 package jalview.analysis;\r
20 \r
21 import java.util.*;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 /**\r
26  * <p>Title: </p>\r
27  * SequenceIdMatcher\r
28  * <p>Description: </p>\r
29  * Routine which does approximate Sequence Id resolution by name using string containment rather than equivalence\r
30  * <p>Copyright: Copyright (c) 2004</p>\r
31  *\r
32  * <p>Company: Dundee University</p>\r
33  *\r
34  * @author not attributable\r
35  * @version 1.0\r
36  */\r
37 public class SequenceIdMatcher\r
38 {\r
39   private Hashtable names;\r
40 \r
41   public SequenceIdMatcher(SequenceI[] seqs)\r
42   {\r
43     names = new Hashtable();\r
44 \r
45     for (int i = 0; i < seqs.length; i++)\r
46     {\r
47       names.put(new SeqIdName(seqs[i].getName()), seqs[i]);\r
48     }\r
49   }\r
50 \r
51   SequenceI findIdMatch(SequenceI seq)\r
52   {\r
53     SeqIdName nam = new SeqIdName(seq.getName());\r
54 \r
55     if (names.containsKey(nam))\r
56     {\r
57       return (SequenceI) names.get(nam);\r
58     }\r
59 \r
60     return null;\r
61   }\r
62 \r
63   SequenceI findIdMatch(String seqnam)\r
64   {\r
65     SeqIdName nam = new SeqIdName(seqnam);\r
66 \r
67     if (names.containsKey(nam))\r
68     {\r
69       return (SequenceI) names.get(nam);\r
70     }\r
71 \r
72     return null;\r
73   }\r
74 \r
75   /**\r
76    * findIdMatch\r
77    *\r
78    * Return pointers to sequences (or sequence object containers)\r
79    * which have same Id as a given set of different sequence objects\r
80    *\r
81    * @param seqs SequenceI[]\r
82    * @return SequenceI[]\r
83    */\r
84   SequenceI[] findIdMatch(SequenceI[] seqs)\r
85   {\r
86     SequenceI[] namedseqs = new SequenceI[seqs.length];\r
87 \r
88     int i = 0;\r
89     SeqIdName nam;\r
90 \r
91     if (seqs.length > 0)\r
92     {\r
93       do\r
94       {\r
95         nam = new SeqIdName(seqs[i].getName());\r
96 \r
97         if (names.containsKey(nam))\r
98         {\r
99           namedseqs[i] = (SequenceI) names.get(nam);\r
100         }\r
101         else\r
102         {\r
103           namedseqs[i] = null;\r
104         }\r
105       }\r
106       while (i++ < seqs.length);\r
107     }\r
108 \r
109     return namedseqs;\r
110   }\r
111 \r
112   private class SeqIdName\r
113   {\r
114     String id;\r
115 \r
116     SeqIdName(String s)\r
117     {\r
118       id = new String(s);\r
119     }\r
120 \r
121     public int hashCode()\r
122     {\r
123       return (id.substring(0, 4).hashCode());\r
124     }\r
125 \r
126     public boolean equals(Object s)\r
127     {\r
128       if (s instanceof SeqIdName)\r
129       {\r
130         return this.equals( (SeqIdName) s);\r
131       }\r
132       else\r
133       {\r
134         if (s instanceof String)\r
135         {\r
136           return this.equals( (String) s);\r
137         }\r
138       }\r
139 \r
140       return false;\r
141     }\r
142 \r
143     public boolean equals(SeqIdName s)\r
144     {\r
145       if (id.startsWith(s.id) || s.id.startsWith(id))\r
146       {\r
147         return true;\r
148       }\r
149 \r
150       return false;\r
151     }\r
152 \r
153     public boolean equals(String s)\r
154     {\r
155       if (id.startsWith(s) || s.startsWith(id))\r
156       {\r
157         return true;\r
158       }\r
159 \r
160       return false;\r
161     }\r
162   }\r
163 }\r