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 names.put(new SeqIdName(seqs[i].getName()), seqs[i]);
57 // add in any interesting identifiers
58 if (seqs[i].getDBRef() != null)
60 DBRefEntry dbr[] = seqs[i].getDBRef();
62 for (int r = 0; r < dbr.length; r++)
64 sid = new SeqIdName(dbr[r].getAccessionId());
65 if (!names.contains(sid))
67 names.put(sid, seqs[i]);
75 * returns the closest SequenceI in matches to SeqIdName and returns all the
76 * matches to the names hash.
81 * Vector of SequenceI objects
82 * @return SequenceI closest SequenceI to SeqIdName
84 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
86 SequenceI match = null;
87 if (candName == null || matches == null || matches.size() == 0)
91 match = (SequenceI) matches.elementAt(0);
92 matches.removeElementAt(0);
93 names.put(new SeqIdName(match.getName()), match);
94 int matchlen = match.getName().length();
95 int namlen = candName.id.length();
96 while (matches.size() > 0)
98 // look through for a better one.
99 SequenceI cand = (SequenceI) matches.elementAt(0);
100 names.put(new SeqIdName(cand.getName()), cand);
101 int candlen = cand.getName().length();
102 // keep the one with an id 'closer' to the given seqnam string
103 if (Math.abs(matchlen - namlen) > Math.abs(candlen - namlen)
104 && candlen > matchlen)
114 * get SequenceI with closest SequenceI.getName() to seq.getName()
120 SequenceI findIdMatch(SequenceI seq)
122 SeqIdName nam = new SeqIdName(seq.getName());
123 return findIdMatch(nam);
126 SequenceI findIdMatch(String seqnam)
128 SeqIdName nam = new SeqIdName(seqnam);
129 return findIdMatch(nam);
135 * Return pointers to sequences (or sequence object containers) which have
136 * same Id as a given set of different sequence objects
140 * @return SequenceI[]
142 SequenceI[] findIdMatch(SequenceI[] seqs)
144 SequenceI[] namedseqs = null;
150 namedseqs = new SequenceI[seqs.length];
153 nam = new SeqIdName(seqs[i].getName());
155 if (names.containsKey(nam))
157 namedseqs[i] = findIdMatch(nam);
163 } while (++i < seqs.length);
170 * core findIdMatch search method
176 private SequenceI findIdMatch(
177 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
179 Vector matches = new Vector();
180 while (names.containsKey(nam))
182 matches.addElement(names.remove(nam));
184 return pickbestMatch(nam, matches);
187 private class SeqIdName
203 public int hashCode()
205 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
209 public boolean equals(Object s)
211 if (s instanceof SeqIdName)
213 return this.equals((SeqIdName) s);
217 if (s instanceof String)
219 return this.equals((String) s);
227 * Characters that define the end of a unique sequence ID at the beginning
228 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
229 * arbritrarily extended sequence id's (like portions of an aligned set of
230 * repeats from one sequence)
232 private String WORD_SEP = "~. |#\\/<>!\"£$%^*)}[@',?_";
235 * matches if one ID properly contains another at a whitespace boundary.
236 * TODO: (JBPNote) These are not efficient. should use char[] for speed
237 * todo: (JBPNote) Set separator characters appropriately
243 public boolean equals(SeqIdName s)
245 if (id.length() > s.id.length())
247 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
248 .length())) > -1) : false;
252 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
253 .indexOf(s.id.charAt(id.length())) > -1)) : false;
257 public boolean equals(String s)
259 if (id.length() > s.length())
261 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
266 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
267 .indexOf(s.charAt(id.length())) > -1)) : false;