2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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.*;
32 * Routine which does approximate Sequence Id resolution by name using string
33 * containment (on word boundaries) rather than equivalence. It also attempts to
34 * resolve ties where no exact match is available by picking the the id closest
37 * Copyright: Copyright (c) 2004
41 * Company: Dundee University
44 * @author not attributable
47 public class SequenceIdMatcher
49 private Hashtable names;
51 public SequenceIdMatcher(SequenceI[] seqs)
53 names = new Hashtable();
54 for (int i = 0; i < seqs.length; i++)
56 // TODO: deal with ID collisions - SequenceI should be appended to list associated with this key.
57 names.put(new SeqIdName(seqs[i].getDisplayId(true)), seqs[i]);
58 // add in any interesting identifiers
59 if (seqs[i].getDBRef() != null)
61 DBRefEntry dbr[] = seqs[i].getDBRef();
63 for (int r = 0; r < dbr.length; r++)
65 sid = new SeqIdName(dbr[r].getAccessionId());
66 if (!names.contains(sid))
68 names.put(sid, seqs[i]);
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 SequenceI closest SequenceI to SeqIdName
85 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
87 SequenceI[] st= pickbestMatches(candName, matches);
88 return st==null || st.length==0 ? null : st[0];
91 * returns the closest SequenceI in matches to SeqIdName and returns all the
92 * matches to the names hash.
97 * Vector of SequenceI objects
98 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[] ties }
100 private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
102 ArrayList best=new ArrayList();
103 SequenceI match = null;
104 if (candName == null || matches == null || matches.size() == 0)
108 match = (SequenceI) matches.elementAt(0);
109 matches.removeElementAt(0);
111 names.put(new SeqIdName(match.getName()), match);
112 int matchlen = match.getName().length();
113 int namlen = candName.id.length();
114 while (matches.size() > 0)
116 // look through for a better one.
117 SequenceI cand = (SequenceI) matches.elementAt(0);
119 names.put(new SeqIdName(cand.getName()), cand);
120 int q,w,candlen = cand.getName().length();
121 // keep the one with an id 'closer' to the given seqnam string
122 if ((q=Math.abs(matchlen - namlen)) > (w=Math.abs(candlen - namlen))
123 && candlen > matchlen)
130 if (q==w && candlen==matchlen)
136 if (best.size()==0) { return null; };
137 return (SequenceI[]) best.toArray(new SequenceI[0]);
141 * get SequenceI with closest SequenceI.getName() to seq.getName()
147 public SequenceI findIdMatch(SequenceI seq)
149 SeqIdName nam = new SeqIdName(seq.getName());
150 return findIdMatch(nam);
153 public SequenceI findIdMatch(String seqnam)
155 SeqIdName nam = new SeqIdName(seqnam);
156 return findIdMatch(nam);
160 * Find all matches for a given sequence name.
161 * @param seqnam string to query Matcher with.
163 public SequenceI[] findAllIdMatches(String seqnam)
166 SeqIdName nam = new SeqIdName(seqnam);
167 return findAllIdMatches(nam);
173 * Return pointers to sequences (or sequence object containers) which have
174 * same Id as a given set of different sequence objects
178 * @return SequenceI[]
180 public SequenceI[] findIdMatch(SequenceI[] seqs)
182 SequenceI[] namedseqs = null;
188 namedseqs = new SequenceI[seqs.length];
191 nam = new SeqIdName(seqs[i].getName());
193 if (names.containsKey(nam))
195 namedseqs[i] = findIdMatch(nam);
201 } while (++i < seqs.length);
208 * core findIdMatch search method
214 private SequenceI findIdMatch(
215 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
217 Vector matches = new Vector();
218 while (names.containsKey(nam))
220 matches.addElement(names.remove(nam));
222 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);
244 private class SeqIdName
260 public int hashCode()
262 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
266 public boolean equals(Object s)
268 if (s instanceof SeqIdName)
270 return this.equals((SeqIdName) s);
274 if (s instanceof String)
276 return this.equals((String) s);
284 * Characters that define the end of a unique sequence ID at the beginning
285 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
286 * arbritrarily extended sequence id's (like portions of an aligned set of
287 * repeats from one sequence)
289 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 match contains one ID flanked
303 if (id.length() > s.id.length())
305 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
306 .length())) > -1) : false;
310 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
311 .indexOf(s.id.charAt(id.length())) > -1)) : false;
315 public boolean equals(String s)
317 if (id.length() > s.length())
319 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
324 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
325 .indexOf(s.charAt(id.length())) > -1)) : false;