2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 // add in any interesting identifiers
44 if (seqs[i].getDBRef() != null)
46 DBRefEntry dbr[] = seqs[i].getDBRef();
48 for (int r = 0; r < dbr.length; r++)
50 sid = new SeqIdName(dbr[r].getAccessionId());
51 if (!names.contains(sid))
53 names.put(sid, seqs[i]);
61 * returns the closest SequenceI in matches to SeqIdName and returns all the
62 * matches to the names hash.
67 * Vector of SequenceI objects
68 * @return SequenceI closest SequenceI to SeqIdName
70 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
72 SequenceI[] st = pickbestMatches(candName, matches);
73 return st == null || st.length == 0 ? null : st[0];
77 * returns the closest SequenceI in matches to SeqIdName and returns all the
78 * matches to the names hash.
83 * Vector of SequenceI objects
84 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
87 private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
89 ArrayList best = new ArrayList();
90 SequenceI match = null;
91 if (candName == null || matches == null || matches.size() == 0)
95 match = (SequenceI) matches.elementAt(0);
96 matches.removeElementAt(0);
98 names.put(new SeqIdName(match.getName()), match);
99 int matchlen = match.getName().length();
100 int namlen = candName.id.length();
101 while (matches.size() > 0)
103 // look through for a better one.
104 SequenceI cand = (SequenceI) matches.elementAt(0);
106 names.put(new SeqIdName(cand.getName()), cand);
107 int q, w, candlen = cand.getName().length();
108 // keep the one with an id 'closer' to the given seqnam string
109 if ((q = Math.abs(matchlen - namlen)) > (w = Math.abs(candlen
111 && candlen > matchlen)
118 if (q == w && candlen == matchlen)
124 if (best.size() == 0)
129 return (SequenceI[]) best.toArray(new SequenceI[0]);
133 * get SequenceI with closest SequenceI.getName() to seq.getName()
139 public SequenceI findIdMatch(SequenceI seq)
141 SeqIdName nam = new SeqIdName(seq.getName());
142 return findIdMatch(nam);
145 public SequenceI findIdMatch(String seqnam)
147 SeqIdName nam = new SeqIdName(seqnam);
148 return findIdMatch(nam);
152 * Find all matches for a given sequence name.
155 * string to query Matcher with.
157 public SequenceI[] findAllIdMatches(String seqnam)
160 SeqIdName nam = new SeqIdName(seqnam);
161 return findAllIdMatches(nam);
167 * Return pointers to sequences (or sequence object containers) which have
168 * same Id as a given set of different sequence objects
172 * @return SequenceI[]
174 public SequenceI[] findIdMatch(SequenceI[] seqs)
176 SequenceI[] namedseqs = null;
182 namedseqs = new SequenceI[seqs.length];
185 nam = new SeqIdName(seqs[i].getName());
187 if (names.containsKey(nam))
189 namedseqs[i] = findIdMatch(nam);
195 } while (++i < seqs.length);
202 * core findIdMatch search method
208 private SequenceI findIdMatch(
209 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
211 Vector matches = new Vector();
212 while (names.containsKey(nam))
214 matches.addElement(names.remove(nam));
216 return pickbestMatch(nam, matches);
220 * core findIdMatch search method for finding all equivalent matches
224 * @return SequenceI[]
226 private SequenceI[] findAllIdMatches(
227 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
229 Vector matches = new Vector();
230 while (names.containsKey(nam))
232 matches.addElement(names.remove(nam));
234 SequenceI[] r = pickbestMatches(nam, matches);
238 private class SeqIdName
254 public int hashCode()
256 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
260 public boolean equals(Object s)
262 if (s instanceof SeqIdName)
264 return this.equals((SeqIdName) s);
268 if (s instanceof String)
270 return this.equals((String) s);
278 * Characters that define the end of a unique sequence ID at the beginning
279 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
280 * arbritrarily extended sequence id's (like portions of an aligned set of
281 * repeats from one sequence)
283 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
287 * matches if one ID properly contains another at a whitespace boundary.
288 * TODO: (JBPNote) These are not efficient. should use char[] for speed
289 * todo: (JBPNote) Set separator characters appropriately
295 public boolean equals(SeqIdName s)
297 // TODO: JAL-732 patch for cases when name includes a list of IDs, and the
298 // match contains one ID flanked
299 if (id.length() > s.id.length())
301 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
302 .length())) > -1) : false;
306 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
307 .indexOf(s.id.charAt(id.length())) > -1)) : false;
311 public boolean equals(String s)
313 if (id.length() > s.length())
315 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
320 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
321 .indexOf(s.charAt(id.length())) > -1)) : false;