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;
23 import jalview.datamodel.DBRefEntry;
24 import jalview.datamodel.SequenceI;
26 import java.util.ArrayList;
27 import java.util.Hashtable;
28 import java.util.Vector;
31 * Routines for approximate Sequence Id resolution by name using string
32 * containment (on word boundaries) rather than equivalence. It also attempts to
33 * resolve ties where no exact match is available by picking the the id closest
36 public class SequenceIdMatcher
38 private Hashtable names;
40 public SequenceIdMatcher(SequenceI[] seqs)
42 names = new Hashtable();
43 for (int i = 0; i < seqs.length; i++)
45 // TODO: deal with ID collisions - SequenceI should be appended to list
46 // associated with this key.
47 names.put(new SeqIdName(seqs[i].getDisplayId(true)), seqs[i]);
48 SequenceI dbseq = seqs[i];
49 while (dbseq.getDatasetSequence()!=null)
51 dbseq = dbseq.getDatasetSequence();
53 // add in any interesting identifiers
54 if (dbseq.getDBRef() != null)
56 DBRefEntry dbr[] = dbseq.getDBRef();
58 for (int r = 0; r < dbr.length; r++)
60 sid = new SeqIdName(dbr[r].getAccessionId());
61 if (!names.contains(sid))
63 names.put(sid, seqs[i]);
71 * returns the closest SequenceI in matches to SeqIdName and returns all the
72 * matches to the names hash.
77 * Vector of SequenceI objects
78 * @return SequenceI closest SequenceI to SeqIdName
80 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
82 SequenceI[] st = pickbestMatches(candName, matches);
83 return st == null || st.length == 0 ? null : st[0];
87 * returns the closest SequenceI in matches to SeqIdName and returns all the
88 * matches to the names hash.
93 * Vector of SequenceI objects
94 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
97 private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
99 ArrayList best = new ArrayList();
100 SequenceI match = null;
101 if (candName == null || matches == null || matches.size() == 0)
105 match = (SequenceI) matches.elementAt(0);
106 matches.removeElementAt(0);
108 names.put(new SeqIdName(match.getName()), match);
109 int matchlen = match.getName().length();
110 int namlen = candName.id.length();
111 while (matches.size() > 0)
113 // look through for a better one.
114 SequenceI cand = (SequenceI) matches.elementAt(0);
116 names.put(new SeqIdName(cand.getName()), cand);
117 int q, w, candlen = cand.getName().length();
118 // keep the one with an id 'closer' to the given seqnam string
119 if ((q = Math.abs(matchlen - namlen)) > (w = Math.abs(candlen
121 && candlen > matchlen)
128 if (q == w && candlen == matchlen)
134 if (best.size() == 0)
139 return (SequenceI[]) best.toArray(new SequenceI[0]);
143 * get SequenceI with closest SequenceI.getName() to seq.getName()
149 public SequenceI findIdMatch(SequenceI seq)
151 SeqIdName nam = new SeqIdName(seq.getName());
152 return findIdMatch(nam);
155 public SequenceI findIdMatch(String seqnam)
157 SeqIdName nam = new SeqIdName(seqnam);
158 return findIdMatch(nam);
162 * Find all matches for a given sequence name.
165 * string to query Matcher with.
167 public SequenceI[] findAllIdMatches(String seqnam)
170 SeqIdName nam = new SeqIdName(seqnam);
171 return findAllIdMatches(nam);
177 * Return pointers to sequences (or sequence object containers) which have
178 * same Id as a given set of different sequence objects
182 * @return SequenceI[]
184 public SequenceI[] findIdMatch(SequenceI[] seqs)
186 SequenceI[] namedseqs = null;
192 namedseqs = new SequenceI[seqs.length];
195 nam = new SeqIdName(seqs[i].getName());
197 if (names.containsKey(nam))
199 namedseqs[i] = findIdMatch(nam);
205 } while (++i < seqs.length);
212 * core findIdMatch search method
218 private SequenceI findIdMatch(
219 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
221 Vector matches = new Vector();
222 while (names.containsKey(nam))
224 matches.addElement(names.remove(nam));
226 return pickbestMatch(nam, matches);
230 * core findIdMatch search method for finding all equivalent matches
234 * @return SequenceI[]
236 private SequenceI[] findAllIdMatches(
237 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
239 Vector matches = new Vector();
240 while (names.containsKey(nam))
242 matches.addElement(names.remove(nam));
244 SequenceI[] r = pickbestMatches(nam, matches);
248 private class SeqIdName
265 public int hashCode()
267 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
272 public boolean equals(Object s)
278 if (s instanceof SeqIdName)
280 return this.equals((SeqIdName) s);
284 if (s instanceof String)
286 return this.equals((String) s);
294 * Characters that define the end of a unique sequence ID at the beginning
295 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
296 * arbritrarily extended sequence id's (like portions of an aligned set of
297 * repeats from one sequence)
299 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
303 * matches if one ID properly contains another at a whitespace boundary.
304 * TODO: (JBPNote) These are not efficient. should use char[] for speed
305 * todo: (JBPNote) Set separator characters appropriately
311 public boolean equals(SeqIdName s)
313 // TODO: JAL-732 patch for cases when name includes a list of IDs, and the
314 // match contains one ID flanked
315 if (id.length() > s.id.length())
317 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
318 .length())) > -1) : false;
322 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
323 .indexOf(s.id.charAt(id.length())) > -1)) : false;
327 public boolean equals(String s)
329 if (id.length() > s.length())
331 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
336 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
337 .indexOf(s.charAt(id.length())) > -1)) : false;