2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.analysis;
22 import jalview.datamodel.*;
25 * Routines for approximate Sequence Id resolution by name using string
26 * containment (on word boundaries) rather than equivalence. It also attempts to
27 * resolve ties where no exact match is available by picking the the id closest
30 public class SequenceIdMatcher
32 private Hashtable names;
34 public SequenceIdMatcher(SequenceI[] seqs)
36 names = new Hashtable();
37 for (int i = 0; i < seqs.length; i++)
39 // TODO: deal with ID collisions - SequenceI should be appended to list
40 // associated with this key.
41 names.put(new SeqIdName(seqs[i].getDisplayId(true)), seqs[i]);
42 // add in any interesting identifiers
43 if (seqs[i].getDBRef() != null)
45 DBRefEntry dbr[] = seqs[i].getDBRef();
47 for (int r = 0; r < dbr.length; r++)
49 sid = new SeqIdName(dbr[r].getAccessionId());
50 if (!names.contains(sid))
52 names.put(sid, seqs[i]);
60 * returns the closest SequenceI in matches to SeqIdName and returns all the
61 * matches to the names hash.
66 * Vector of SequenceI objects
67 * @return SequenceI closest SequenceI to SeqIdName
69 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
71 SequenceI[] st = pickbestMatches(candName, matches);
72 return st == null || st.length == 0 ? null : st[0];
76 * returns the closest SequenceI in matches to SeqIdName and returns all the
77 * matches to the names hash.
82 * Vector of SequenceI objects
83 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
86 private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
88 ArrayList best = new ArrayList();
89 SequenceI match = null;
90 if (candName == null || matches == null || matches.size() == 0)
94 match = (SequenceI) matches.elementAt(0);
95 matches.removeElementAt(0);
97 names.put(new SeqIdName(match.getName()), match);
98 int matchlen = match.getName().length();
99 int namlen = candName.id.length();
100 while (matches.size() > 0)
102 // look through for a better one.
103 SequenceI cand = (SequenceI) matches.elementAt(0);
105 names.put(new SeqIdName(cand.getName()), cand);
106 int q, w, candlen = cand.getName().length();
107 // keep the one with an id 'closer' to the given seqnam string
108 if ((q = Math.abs(matchlen - namlen)) > (w = Math.abs(candlen
110 && candlen > matchlen)
117 if (q == w && candlen == matchlen)
123 if (best.size() == 0)
128 return (SequenceI[]) best.toArray(new SequenceI[0]);
132 * get SequenceI with closest SequenceI.getName() to seq.getName()
138 public SequenceI findIdMatch(SequenceI seq)
140 SeqIdName nam = new SeqIdName(seq.getName());
141 return findIdMatch(nam);
144 public SequenceI findIdMatch(String seqnam)
146 SeqIdName nam = new SeqIdName(seqnam);
147 return findIdMatch(nam);
151 * Find all matches for a given sequence name.
154 * string to query Matcher with.
156 public SequenceI[] findAllIdMatches(String seqnam)
159 SeqIdName nam = new SeqIdName(seqnam);
160 return findAllIdMatches(nam);
166 * Return pointers to sequences (or sequence object containers) which have
167 * same Id as a given set of different sequence objects
171 * @return SequenceI[]
173 public SequenceI[] findIdMatch(SequenceI[] seqs)
175 SequenceI[] namedseqs = null;
181 namedseqs = new SequenceI[seqs.length];
184 nam = new SeqIdName(seqs[i].getName());
186 if (names.containsKey(nam))
188 namedseqs[i] = findIdMatch(nam);
194 } while (++i < seqs.length);
201 * core findIdMatch search method
207 private SequenceI findIdMatch(
208 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
210 Vector matches = new Vector();
211 while (names.containsKey(nam))
213 matches.addElement(names.remove(nam));
215 return pickbestMatch(nam, matches);
219 * core findIdMatch search method for finding all equivalent matches
223 * @return SequenceI[]
225 private SequenceI[] findAllIdMatches(
226 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
228 Vector matches = new Vector();
229 while (names.containsKey(nam))
231 matches.addElement(names.remove(nam));
233 SequenceI[] r = pickbestMatches(nam, matches);
237 private class SeqIdName
253 public int hashCode()
255 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
259 public boolean equals(Object s)
261 if (s instanceof SeqIdName)
263 return this.equals((SeqIdName) s);
267 if (s instanceof String)
269 return this.equals((String) s);
277 * Characters that define the end of a unique sequence ID at the beginning
278 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
279 * arbritrarily extended sequence id's (like portions of an aligned set of
280 * repeats from one sequence)
282 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
286 * matches if one ID properly contains another at a whitespace boundary.
287 * TODO: (JBPNote) These are not efficient. should use char[] for speed
288 * todo: (JBPNote) Set separator characters appropriately
294 public boolean equals(SeqIdName s)
296 // TODO: JAL-732 patch for cases when name includes a list of IDs, and the
297 // match contains one ID flanked
298 if (id.length() > s.id.length())
300 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
301 .length())) > -1) : false;
305 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
306 .indexOf(s.id.charAt(id.length())) > -1)) : false;
310 public boolean equals(String s)
312 if (id.length() > s.length())
314 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
319 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
320 .indexOf(s.charAt(id.length())) > -1)) : false;