2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 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. It also
\r
31 * attempts to resolve ties where no exact match is available by picking the
\r
32 * the id closest to the query.
\r
33 * <p>Copyright: Copyright (c) 2004</p>
\r
35 * <p>Company: Dundee University</p>
\r
37 * @author not attributable
\r
40 public class SequenceIdMatcher
\r
42 private Hashtable names;
\r
44 public SequenceIdMatcher(SequenceI[] seqs)
\r
46 names = new Hashtable();
\r
47 for (int i = 0; i < seqs.length; i++)
\r
49 names.put(new SeqIdName(seqs[i].getName()), seqs[i]);
\r
54 * returns the closest SequenceI in matches to SeqIdName and returns all the matches
\r
55 * to the names hash.
\r
56 * @param candName SeqIdName
\r
57 * @param matches Vector of SequenceI objects
\r
58 * @return SequenceI closest SequenceI to SeqIdName
\r
60 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
\r
62 SequenceI match = null;
\r
63 if (candName == null || matches == null || matches.size() == 0)
\r
67 match = (SequenceI) matches.elementAt(0);
\r
68 matches.removeElementAt(0);
\r
69 names.put(new SeqIdName(match.getName()), match);
\r
70 int matchlen = match.getName().length();
\r
71 int namlen = candName.id.length();
\r
72 while (matches.size() > 0)
\r
74 // look through for a better one.
\r
75 SequenceI cand = (SequenceI) matches.elementAt(0);
\r
76 names.put(new SeqIdName(cand.getName()), cand);
\r
77 int candlen = cand.getName().length();
\r
78 // keep the one with an id 'closer' to the given seqnam string
\r
79 if (Math.abs(matchlen - namlen) > Math.abs(candlen - namlen) &&
\r
90 * get SequenceI with closest SequenceI.getName() to seq.getName()
\r
91 * @param seq SequenceI
\r
94 SequenceI findIdMatch(SequenceI seq)
\r
96 SeqIdName nam = new SeqIdName(seq.getName());
\r
97 return findIdMatch(nam);
\r
100 SequenceI findIdMatch(String seqnam)
\r
102 SeqIdName nam = new SeqIdName(seqnam);
\r
103 return findIdMatch(nam);
\r
109 * Return pointers to sequences (or sequence object containers)
\r
110 * which have same Id as a given set of different sequence objects
\r
112 * @param seqs SequenceI[]
\r
113 * @return SequenceI[]
\r
115 SequenceI[] findIdMatch(SequenceI[] seqs)
\r
117 SequenceI[] namedseqs = null;
\r
121 if (seqs.length > 0)
\r
123 namedseqs = new SequenceI[seqs.length];
\r
126 nam = new SeqIdName(seqs[i].getName());
\r
128 if (names.containsKey(nam))
\r
130 namedseqs[i] = findIdMatch(nam);
\r
134 namedseqs[i] = null;
\r
137 while (++i < seqs.length);
\r
144 * core findIdMatch search method
\r
145 * @param nam SeqIdName
\r
146 * @return SequenceI
\r
148 private SequenceI findIdMatch(jalview.analysis.SequenceIdMatcher.SeqIdName
\r
151 Vector matches = new Vector();
\r
152 while (names.containsKey(nam))
\r
154 matches.addElement(names.remove(nam));
\r
156 return pickbestMatch(nam, matches);
\r
159 private class SeqIdName
\r
163 SeqIdName(String s)
\r
167 id = new String(s);
\r
175 public int hashCode()
\r
177 return ( (id.length() >= 4) ? id.substring(0, 4).hashCode() : id.hashCode());
\r
180 public boolean equals(Object s)
\r
182 if (s instanceof SeqIdName)
\r
184 return this.equals( (SeqIdName) s);
\r
188 if (s instanceof String)
\r
190 return this.equals( (String) s);
\r
198 * Characters that define the end of a unique sequence ID at
\r
199 * the beginning of an arbitrary ID string
\r
200 * JBPNote: This is a heuristic that will fail for arbritrarily extended sequence id's
\r
201 * (like portions of an aligned set of repeats from one sequence)
\r
203 private String WORD_SEP = "~. |#\\/<>!\"£$%^*)}[@',?_";
\r
206 * matches if one ID properly contains another at a whitespace boundary.
\r
207 * TODO: (JBPNote) These are not efficient. should use char[] for speed
\r
208 * todo: (JBPNote) Set separator characters appropriately
\r
209 * @param s SeqIdName
\r
212 public boolean equals(SeqIdName s)
\r
214 if (id.length() > s.id.length())
\r
216 return id.startsWith(s.id) ?
\r
217 (WORD_SEP.indexOf(id.charAt(s.id.length())) > -1)
\r
222 return s.id.startsWith(id) ?
\r
223 (s.id.equals(id) ? true :
\r
224 (WORD_SEP.indexOf(s.id.charAt(id.length())) > -1))
\r
229 public boolean equals(String s)
\r
231 if (id.length() > s.length())
\r
233 return id.startsWith(s) ?
\r
234 (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
\r
239 return s.startsWith(id) ?
\r
240 (s.equals(id) ? true :
\r
241 (WORD_SEP.indexOf(s.charAt(id.length())) > -1))
\r