Formatted source
[jalview.git] / src / jalview / analysis / SequenceIdMatcher.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */\r
19 package jalview.analysis;\r
20 \r
21 import jalview.datamodel.SequenceI;\r
22 \r
23 import java.util.Hashtable;\r
24 import java.util.Vector;\r
25 \r
26 \r
27 /**
28  * <p>Title: </p>
29  * SequenceIdMatcher
30  * <p>Description: </p>
31  * Routine which does approximate Sequence Id resolution by name using string containment rather than equivalence
32  * <p>Copyright: Copyright (c) 2004</p>
33  *
34  * <p>Company: Dundee University</p>
35  *
36  * @author not attributable
37  * @version 1.0
38  */\r
39 public class SequenceIdMatcher {\r
40     private Hashtable names;\r
41 \r
42     public SequenceIdMatcher(SequenceI[] seqs) {\r
43         names = new Hashtable();\r
44 \r
45         for (int i = 0; i < seqs.length; i++) {\r
46             names.put(new SeqIdName(seqs[i].getName()), seqs[i]);\r
47         }\r
48     }\r
49 \r
50     SequenceI findIdMatch(SequenceI seq) {\r
51         SeqIdName nam = new SeqIdName(seq.getName());\r
52 \r
53         if (names.containsKey(nam)) {\r
54             return (SequenceI) names.get(nam);\r
55         }\r
56 \r
57         return null;\r
58     }\r
59 \r
60     SequenceI findIdMatch(String seqnam) {\r
61         SeqIdName nam = new SeqIdName(seqnam);\r
62 \r
63         if (names.containsKey(nam)) {\r
64             return (SequenceI) names.get(nam);\r
65         }\r
66 \r
67         return null;\r
68     }\r
69 \r
70     /**
71  * findIdMatch
72  *
73  * Return pointers to sequences (or sequence object containers)
74  * which have same Id as a given set of different sequence objects
75  *
76  * @param seqs SequenceI[]
77  * @return SequenceI[]
78  */\r
79     SequenceI[] findIdMatch(SequenceI[] seqs) {\r
80         SequenceI[] namedseqs = new SequenceI[seqs.length];\r
81 \r
82         int i = 0;\r
83         SeqIdName nam;\r
84 \r
85         if (seqs.length > 0) {\r
86             do {\r
87                 nam = new SeqIdName(seqs[i].getName());\r
88 \r
89                 if (names.containsKey(nam)) {\r
90                     namedseqs[i] = (SequenceI) names.get(nam);\r
91                 } else {\r
92                     namedseqs[i] = null;\r
93                 }\r
94             } while (i++ < seqs.length);\r
95         }\r
96 \r
97         return namedseqs;\r
98     }\r
99 \r
100     private class SeqIdName {\r
101         String id;\r
102 \r
103         SeqIdName(String s) {\r
104             id = new String(s);\r
105         }\r
106 \r
107         public int hashCode() {\r
108             return (id.substring(0, 4).hashCode());\r
109         }\r
110 \r
111         public boolean equals(Object s) {\r
112             if (s instanceof SeqIdName) {\r
113                 return this.equals((SeqIdName) s);\r
114             } else {\r
115                 if (s instanceof String) {\r
116                     return this.equals((String) s);\r
117                 }\r
118             }\r
119 \r
120             return false;\r
121         }\r
122 \r
123         public boolean equals(SeqIdName s) {\r
124             if (id.startsWith(s.id) || s.id.startsWith(id)) {\r
125                 return true;\r
126             }\r
127 \r
128             return false;\r
129         }\r
130 \r
131         public boolean equals(String s) {\r
132             if (id.startsWith(s) || s.startsWith(id)) {\r
133                 return true;\r
134             }\r
135 \r
136             return false;\r
137         }\r
138     }\r
139 }\r