2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
25 import jalview.datamodel.*;
28 * Routines for approximate Sequence Id resolution by name using string
29 * containment (on word boundaries) rather than equivalence. It also attempts to
30 * resolve ties where no exact match is available by picking the the id closest
33 public class SequenceIdMatcher
35 private Hashtable names;
37 public SequenceIdMatcher(SequenceI[] seqs)
39 names = new Hashtable();
40 for (int i = 0; i < seqs.length; i++)
42 // TODO: deal with ID collisions - SequenceI should be appended to list
43 // associated with this key.
44 names.put(new SeqIdName(seqs[i].getDisplayId(true)), seqs[i]);
45 SequenceI dbseq = seqs[i];
46 while (dbseq.getDatasetSequence()!=null)
48 dbseq = dbseq.getDatasetSequence();
50 // add in any interesting identifiers
51 if (dbseq.getDBRef() != null)
53 DBRefEntry dbr[] = dbseq.getDBRef();
55 for (int r = 0; r < dbr.length; r++)
57 sid = new SeqIdName(dbr[r].getAccessionId());
58 if (!names.contains(sid))
60 names.put(sid, seqs[i]);
68 * returns the closest SequenceI in matches to SeqIdName and returns all the
69 * matches to the names hash.
74 * Vector of SequenceI objects
75 * @return SequenceI closest SequenceI to SeqIdName
77 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
79 SequenceI[] st = pickbestMatches(candName, matches);
80 return st == null || st.length == 0 ? null : st[0];
84 * returns the closest SequenceI in matches to SeqIdName and returns all the
85 * matches to the names hash.
90 * Vector of SequenceI objects
91 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
94 private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
96 ArrayList best = new ArrayList();
97 SequenceI match = null;
98 if (candName == null || matches == null || matches.size() == 0)
102 match = (SequenceI) matches.elementAt(0);
103 matches.removeElementAt(0);
105 names.put(new SeqIdName(match.getName()), match);
106 int matchlen = match.getName().length();
107 int namlen = candName.id.length();
108 while (matches.size() > 0)
110 // look through for a better one.
111 SequenceI cand = (SequenceI) matches.elementAt(0);
113 names.put(new SeqIdName(cand.getName()), cand);
114 int q, w, candlen = cand.getName().length();
115 // keep the one with an id 'closer' to the given seqnam string
116 if ((q = Math.abs(matchlen - namlen)) > (w = Math.abs(candlen
118 && candlen > matchlen)
125 if (q == w && candlen == matchlen)
131 if (best.size() == 0)
136 return (SequenceI[]) best.toArray(new SequenceI[0]);
140 * get SequenceI with closest SequenceI.getName() to seq.getName()
146 public SequenceI findIdMatch(SequenceI seq)
148 SeqIdName nam = new SeqIdName(seq.getName());
149 return findIdMatch(nam);
152 public SequenceI findIdMatch(String seqnam)
154 SeqIdName nam = new SeqIdName(seqnam);
155 return findIdMatch(nam);
159 * Find all matches for a given sequence name.
162 * string to query Matcher with.
164 public SequenceI[] findAllIdMatches(String seqnam)
167 SeqIdName nam = new SeqIdName(seqnam);
168 return findAllIdMatches(nam);
174 * Return pointers to sequences (or sequence object containers) which have
175 * same Id as a given set of different sequence objects
179 * @return SequenceI[]
181 public SequenceI[] findIdMatch(SequenceI[] seqs)
183 SequenceI[] namedseqs = null;
189 namedseqs = new SequenceI[seqs.length];
192 nam = new SeqIdName(seqs[i].getName());
194 if (names.containsKey(nam))
196 namedseqs[i] = findIdMatch(nam);
202 } while (++i < seqs.length);
209 * core findIdMatch search method
215 private SequenceI findIdMatch(
216 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
218 Vector matches = new Vector();
219 while (names.containsKey(nam))
221 matches.addElement(names.remove(nam));
223 return pickbestMatch(nam, matches);
227 * core findIdMatch search method for finding all equivalent matches
231 * @return SequenceI[]
233 private SequenceI[] findAllIdMatches(
234 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
236 Vector matches = new Vector();
237 while (names.containsKey(nam))
239 matches.addElement(names.remove(nam));
241 SequenceI[] r = pickbestMatches(nam, matches);
245 private class SeqIdName
261 public int hashCode()
263 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
267 public boolean equals(Object s)
269 if (s instanceof SeqIdName)
271 return this.equals((SeqIdName) s);
275 if (s instanceof String)
277 return this.equals((String) s);
285 * Characters that define the end of a unique sequence ID at the beginning
286 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
287 * arbritrarily extended sequence id's (like portions of an aligned set of
288 * repeats from one sequence)
290 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
294 * matches if one ID properly contains another at a whitespace boundary.
295 * TODO: (JBPNote) These are not efficient. should use char[] for speed
296 * todo: (JBPNote) Set separator characters appropriately
302 public boolean equals(SeqIdName s)
304 // TODO: JAL-732 patch for cases when name includes a list of IDs, and the
305 // match contains one ID flanked
306 if (id.length() > s.id.length())
308 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
309 .length())) > -1) : false;
313 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
314 .indexOf(s.id.charAt(id.length())) > -1)) : false;
318 public boolean equals(String s)
320 if (id.length() > s.length())
322 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
327 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
328 .indexOf(s.charAt(id.length())) > -1)) : false;