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.analysis;
\r
23 import jalview.datamodel.*;
\r
28 * <p>Description: </p>
\r
29 * Routine which does approximate Sequence Id resolution by name using
\r
30 * string containment (on word boundaries) rather than equivalence
\r
31 * <p>Copyright: Copyright (c) 2004</p>
\r
33 * <p>Company: Dundee University</p>
\r
35 * @author not attributable
\r
38 public class SequenceIdMatcher
\r
40 private Hashtable names;
\r
42 public SequenceIdMatcher(SequenceI[] seqs)
\r
44 names = new Hashtable();
\r
45 for (int i = 0; i < seqs.length; i++)
\r
47 names.put(new SeqIdName(seqs[i].getName()), seqs[i]);
\r
51 SequenceI findIdMatch(SequenceI seq)
\r
53 SeqIdName nam = new SeqIdName(seq.getName());
\r
55 if (names.containsKey(nam))
\r
57 return (SequenceI) names.get(nam);
\r
63 SequenceI findIdMatch(String seqnam)
\r
65 SeqIdName nam = new SeqIdName(seqnam);
\r
67 if (names.containsKey(nam))
\r
69 return (SequenceI) names.get(nam);
\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
81 * @param seqs SequenceI[]
\r
82 * @return SequenceI[]
\r
84 SequenceI[] findIdMatch(SequenceI[] seqs)
\r
86 SequenceI[] namedseqs = null;
\r
90 if (seqs.length > 0)
\r
92 namedseqs = new SequenceI[seqs.length];
\r
95 nam = new SeqIdName(seqs[i].getName());
\r
97 if (names.containsKey(nam))
\r
99 namedseqs[i] = (SequenceI) names.get(nam);
\r
103 namedseqs[i] = null;
\r
106 while (++i < seqs.length);
\r
112 private class SeqIdName
\r
116 SeqIdName(String s)
\r
118 id = new String(s);
\r
121 public int hashCode()
\r
123 return (id.substring(0, 4).hashCode());
\r
126 public boolean equals(Object s)
\r
128 if (s instanceof SeqIdName)
\r
130 return this.equals( (SeqIdName) s);
\r
134 if (s instanceof String)
\r
136 return this.equals( (String) s);
\r
144 * matches if one ID properly contains another at a whitespace boundary.
\r
145 * TODO: (JBPNote) These are not efficient. should use char[] for speed
\r
146 * @param s SeqIdName
\r
149 public boolean equals(SeqIdName s)
\r
151 if (id.length()>s.id.length()) {
\r
152 return id.startsWith(s.id) ?
\r
153 (id.equals(s.id) ? true : id.startsWith(s.id+" "))
\r
156 return s.id.startsWith(id) ?
\r
157 (s.id.equals(id) ? true : s.id.startsWith(id+" "))
\r
161 public boolean equals(String s)
\r
163 if (id.length()>s.length()) {
\r
164 return id.startsWith(s) ?
\r
165 (id.equals(s) ? true : id.startsWith(s+" "))
\r
168 return s.startsWith(id) ?
\r
169 (s.equals(id) ? true : s.startsWith(id+" "))
\r