2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.analysis;
23 import jalview.datamodel.*;
26 * Routines for approximate Sequence Id resolution by name using string
27 * containment (on word boundaries) rather than equivalence. It also attempts to
28 * resolve ties where no exact match is available by picking the the id closest
31 public class SequenceIdMatcher
33 private Hashtable names;
35 public SequenceIdMatcher(SequenceI[] seqs)
37 names = new Hashtable();
38 for (int i = 0; i < seqs.length; i++)
40 // TODO: deal with ID collisions - SequenceI should be appended to list
41 // associated with this key.
42 names.put(new SeqIdName(seqs[i].getDisplayId(true)), seqs[i]);
43 SequenceI dbseq = seqs[i];
44 while (dbseq.getDatasetSequence()!=null)
46 dbseq = dbseq.getDatasetSequence();
48 // add in any interesting identifiers
49 if (dbseq.getDBRef() != null)
51 DBRefEntry dbr[] = dbseq.getDBRef();
53 for (int r = 0; r < dbr.length; r++)
55 sid = new SeqIdName(dbr[r].getAccessionId());
56 if (!names.contains(sid))
58 names.put(sid, seqs[i]);
66 * returns the closest SequenceI in matches to SeqIdName and returns all the
67 * matches to the names hash.
72 * Vector of SequenceI objects
73 * @return SequenceI closest SequenceI to SeqIdName
75 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
77 SequenceI[] st = pickbestMatches(candName, matches);
78 return st == null || st.length == 0 ? null : st[0];
82 * returns the closest SequenceI in matches to SeqIdName and returns all the
83 * matches to the names hash.
88 * Vector of SequenceI objects
89 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
92 private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
94 ArrayList best = new ArrayList();
95 SequenceI match = null;
96 if (candName == null || matches == null || matches.size() == 0)
100 match = (SequenceI) matches.elementAt(0);
101 matches.removeElementAt(0);
103 names.put(new SeqIdName(match.getName()), match);
104 int matchlen = match.getName().length();
105 int namlen = candName.id.length();
106 while (matches.size() > 0)
108 // look through for a better one.
109 SequenceI cand = (SequenceI) matches.elementAt(0);
111 names.put(new SeqIdName(cand.getName()), cand);
112 int q, w, candlen = cand.getName().length();
113 // keep the one with an id 'closer' to the given seqnam string
114 if ((q = Math.abs(matchlen - namlen)) > (w = Math.abs(candlen
116 && candlen > matchlen)
123 if (q == w && candlen == matchlen)
129 if (best.size() == 0)
134 return (SequenceI[]) best.toArray(new SequenceI[0]);
138 * get SequenceI with closest SequenceI.getName() to seq.getName()
144 public SequenceI findIdMatch(SequenceI seq)
146 SeqIdName nam = new SeqIdName(seq.getName());
147 return findIdMatch(nam);
150 public SequenceI findIdMatch(String seqnam)
152 SeqIdName nam = new SeqIdName(seqnam);
153 return findIdMatch(nam);
157 * Find all matches for a given sequence name.
160 * string to query Matcher with.
162 public SequenceI[] findAllIdMatches(String seqnam)
165 SeqIdName nam = new SeqIdName(seqnam);
166 return findAllIdMatches(nam);
172 * Return pointers to sequences (or sequence object containers) which have
173 * same Id as a given set of different sequence objects
177 * @return SequenceI[]
179 public SequenceI[] findIdMatch(SequenceI[] seqs)
181 SequenceI[] namedseqs = null;
187 namedseqs = new SequenceI[seqs.length];
190 nam = new SeqIdName(seqs[i].getName());
192 if (names.containsKey(nam))
194 namedseqs[i] = findIdMatch(nam);
200 } while (++i < seqs.length);
207 * core findIdMatch search method
213 private SequenceI findIdMatch(
214 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
216 Vector matches = new Vector();
217 while (names.containsKey(nam))
219 matches.addElement(names.remove(nam));
221 return pickbestMatch(nam, matches);
225 * core findIdMatch search method for finding all equivalent matches
229 * @return SequenceI[]
231 private SequenceI[] findAllIdMatches(
232 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
234 Vector matches = new Vector();
235 while (names.containsKey(nam))
237 matches.addElement(names.remove(nam));
239 SequenceI[] r = pickbestMatches(nam, matches);
243 private class SeqIdName
259 public int hashCode()
261 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
265 public boolean equals(Object s)
267 if (s instanceof SeqIdName)
269 return this.equals((SeqIdName) s);
273 if (s instanceof String)
275 return this.equals((String) s);
283 * Characters that define the end of a unique sequence ID at the beginning
284 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
285 * arbritrarily extended sequence id's (like portions of an aligned set of
286 * repeats from one sequence)
288 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
292 * matches if one ID properly contains another at a whitespace boundary.
293 * TODO: (JBPNote) These are not efficient. should use char[] for speed
294 * todo: (JBPNote) Set separator characters appropriately
300 public boolean equals(SeqIdName s)
302 // TODO: JAL-732 patch for cases when name includes a list of IDs, and the
303 // match contains one ID flanked
304 if (id.length() > s.id.length())
306 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
307 .length())) > -1) : false;
311 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
312 .indexOf(s.id.charAt(id.length())) > -1)) : false;
316 public boolean equals(String s)
318 if (id.length() > s.length())
320 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
325 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
326 .indexOf(s.charAt(id.length())) > -1)) : false;