2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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 // add in any interesting identifiers
46 if (seqs[i].getDBRef() != null)
48 DBRefEntry dbr[] = seqs[i].getDBRef();
50 for (int r = 0; r < dbr.length; r++)
52 sid = new SeqIdName(dbr[r].getAccessionId());
53 if (!names.contains(sid))
55 names.put(sid, seqs[i]);
63 * returns the closest SequenceI in matches to SeqIdName and returns all the
64 * matches to the names hash.
69 * Vector of SequenceI objects
70 * @return SequenceI closest SequenceI to SeqIdName
72 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
74 SequenceI[] st = pickbestMatches(candName, matches);
75 return st == null || st.length == 0 ? null : st[0];
79 * returns the closest SequenceI in matches to SeqIdName and returns all the
80 * matches to the names hash.
85 * Vector of SequenceI objects
86 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
89 private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
91 ArrayList best = new ArrayList();
92 SequenceI match = null;
93 if (candName == null || matches == null || matches.size() == 0)
97 match = (SequenceI) matches.elementAt(0);
98 matches.removeElementAt(0);
100 names.put(new SeqIdName(match.getName()), match);
101 int matchlen = match.getName().length();
102 int namlen = candName.id.length();
103 while (matches.size() > 0)
105 // look through for a better one.
106 SequenceI cand = (SequenceI) matches.elementAt(0);
108 names.put(new SeqIdName(cand.getName()), cand);
109 int q, w, candlen = cand.getName().length();
110 // keep the one with an id 'closer' to the given seqnam string
111 if ((q = Math.abs(matchlen - namlen)) > (w = Math.abs(candlen
113 && candlen > matchlen)
120 if (q == w && candlen == matchlen)
126 if (best.size() == 0)
131 return (SequenceI[]) best.toArray(new SequenceI[0]);
135 * get SequenceI with closest SequenceI.getName() to seq.getName()
141 public SequenceI findIdMatch(SequenceI seq)
143 SeqIdName nam = new SeqIdName(seq.getName());
144 return findIdMatch(nam);
147 public SequenceI findIdMatch(String seqnam)
149 SeqIdName nam = new SeqIdName(seqnam);
150 return findIdMatch(nam);
154 * Find all matches for a given sequence name.
157 * string to query Matcher with.
159 public SequenceI[] findAllIdMatches(String seqnam)
162 SeqIdName nam = new SeqIdName(seqnam);
163 return findAllIdMatches(nam);
169 * Return pointers to sequences (or sequence object containers) which have
170 * same Id as a given set of different sequence objects
174 * @return SequenceI[]
176 public SequenceI[] findIdMatch(SequenceI[] seqs)
178 SequenceI[] namedseqs = null;
184 namedseqs = new SequenceI[seqs.length];
187 nam = new SeqIdName(seqs[i].getName());
189 if (names.containsKey(nam))
191 namedseqs[i] = findIdMatch(nam);
197 } while (++i < seqs.length);
204 * core findIdMatch search method
210 private SequenceI findIdMatch(
211 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
213 Vector matches = new Vector();
214 while (names.containsKey(nam))
216 matches.addElement(names.remove(nam));
218 return pickbestMatch(nam, matches);
222 * core findIdMatch search method for finding all equivalent matches
226 * @return SequenceI[]
228 private SequenceI[] findAllIdMatches(
229 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
231 Vector matches = new Vector();
232 while (names.containsKey(nam))
234 matches.addElement(names.remove(nam));
236 SequenceI[] r = pickbestMatches(nam, matches);
240 private class SeqIdName
256 public int hashCode()
258 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
262 public boolean equals(Object s)
264 if (s instanceof SeqIdName)
266 return this.equals((SeqIdName) s);
270 if (s instanceof String)
272 return this.equals((String) s);
280 * Characters that define the end of a unique sequence ID at the beginning
281 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
282 * arbritrarily extended sequence id's (like portions of an aligned set of
283 * repeats from one sequence)
285 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
289 * matches if one ID properly contains another at a whitespace boundary.
290 * TODO: (JBPNote) These are not efficient. should use char[] for speed
291 * todo: (JBPNote) Set separator characters appropriately
297 public boolean equals(SeqIdName s)
299 // TODO: JAL-732 patch for cases when name includes a list of IDs, and the
300 // match contains one ID flanked
301 if (id.length() > s.id.length())
303 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
304 .length())) > -1) : false;
308 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
309 .indexOf(s.id.charAt(id.length())) > -1)) : false;
313 public boolean equals(String s)
315 if (id.length() > s.length())
317 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
322 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
323 .indexOf(s.charAt(id.length())) > -1)) : false;