2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.*;
25 * Routines for approximate Sequence Id resolution by name using string
26 * containment (on word boundaries) rather than equivalence. It also attempts to
27 * resolve ties where no exact match is available by picking the the id closest
30 public class SequenceIdMatcher
32 private Hashtable names;
34 public SequenceIdMatcher(SequenceI[] seqs)
36 names = new Hashtable();
37 for (int i = 0; i < seqs.length; i++)
39 // TODO: deal with ID collisions - SequenceI should be appended to list associated with this key.
40 names.put(new SeqIdName(seqs[i].getDisplayId(true)), seqs[i]);
41 // add in any interesting identifiers
42 if (seqs[i].getDBRef() != null)
44 DBRefEntry dbr[] = seqs[i].getDBRef();
46 for (int r = 0; r < dbr.length; r++)
48 sid = new SeqIdName(dbr[r].getAccessionId());
49 if (!names.contains(sid))
51 names.put(sid, seqs[i]);
59 * returns the closest SequenceI in matches to SeqIdName and returns all the
60 * matches to the names hash.
65 * Vector of SequenceI objects
66 * @return SequenceI closest SequenceI to SeqIdName
68 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
70 SequenceI[] st= pickbestMatches(candName, matches);
71 return st==null || st.length==0 ? null : st[0];
74 * returns the closest SequenceI in matches to SeqIdName and returns all the
75 * matches to the names hash.
80 * Vector of SequenceI objects
81 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[] ties }
83 private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
85 ArrayList best=new ArrayList();
86 SequenceI match = null;
87 if (candName == null || matches == null || matches.size() == 0)
91 match = (SequenceI) matches.elementAt(0);
92 matches.removeElementAt(0);
94 names.put(new SeqIdName(match.getName()), match);
95 int matchlen = match.getName().length();
96 int namlen = candName.id.length();
97 while (matches.size() > 0)
99 // look through for a better one.
100 SequenceI cand = (SequenceI) matches.elementAt(0);
102 names.put(new SeqIdName(cand.getName()), cand);
103 int q,w,candlen = cand.getName().length();
104 // keep the one with an id 'closer' to the given seqnam string
105 if ((q=Math.abs(matchlen - namlen)) > (w=Math.abs(candlen - namlen))
106 && candlen > matchlen)
113 if (q==w && candlen==matchlen)
119 if (best.size()==0) { return null; };
120 return (SequenceI[]) best.toArray(new SequenceI[0]);
124 * get SequenceI with closest SequenceI.getName() to seq.getName()
130 public SequenceI findIdMatch(SequenceI seq)
132 SeqIdName nam = new SeqIdName(seq.getName());
133 return findIdMatch(nam);
136 public SequenceI findIdMatch(String seqnam)
138 SeqIdName nam = new SeqIdName(seqnam);
139 return findIdMatch(nam);
143 * Find all matches for a given sequence name.
144 * @param seqnam string to query Matcher with.
146 public SequenceI[] findAllIdMatches(String seqnam)
149 SeqIdName nam = new SeqIdName(seqnam);
150 return findAllIdMatches(nam);
156 * Return pointers to sequences (or sequence object containers) which have
157 * same Id as a given set of different sequence objects
161 * @return SequenceI[]
163 public SequenceI[] findIdMatch(SequenceI[] seqs)
165 SequenceI[] namedseqs = null;
171 namedseqs = new SequenceI[seqs.length];
174 nam = new SeqIdName(seqs[i].getName());
176 if (names.containsKey(nam))
178 namedseqs[i] = findIdMatch(nam);
184 } while (++i < seqs.length);
191 * core findIdMatch search method
197 private SequenceI findIdMatch(
198 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
200 Vector matches = new Vector();
201 while (names.containsKey(nam))
203 matches.addElement(names.remove(nam));
205 return pickbestMatch(nam, matches);
208 * core findIdMatch search method for finding all equivalent matches
212 * @return SequenceI[]
214 private SequenceI[] findAllIdMatches(
215 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
217 Vector matches = new Vector();
218 while (names.containsKey(nam))
220 matches.addElement(names.remove(nam));
222 SequenceI[] r=pickbestMatches(nam, matches);
227 private class SeqIdName
243 public int hashCode()
245 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
249 public boolean equals(Object s)
251 if (s instanceof SeqIdName)
253 return this.equals((SeqIdName) s);
257 if (s instanceof String)
259 return this.equals((String) s);
267 * Characters that define the end of a unique sequence ID at the beginning
268 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
269 * arbritrarily extended sequence id's (like portions of an aligned set of
270 * repeats from one sequence)
272 private String WORD_SEP = "~. |#\\/<>!\""+((char)0x00A4)+"$%^*)}[@',?_";
275 * matches if one ID properly contains another at a whitespace boundary.
276 * TODO: (JBPNote) These are not efficient. should use char[] for speed
277 * todo: (JBPNote) Set separator characters appropriately
283 public boolean equals(SeqIdName s)
285 // TODO: JAL-732 patch for cases when name includes a list of IDs, and the match contains one ID flanked
286 if (id.length() > s.id.length())
288 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
289 .length())) > -1) : false;
293 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
294 .indexOf(s.id.charAt(id.length())) > -1)) : false;
298 public boolean equals(String s)
300 if (id.length() > s.length())
302 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
307 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
308 .indexOf(s.charAt(id.length())) > -1)) : false;