2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
23 import jalview.datamodel.*;
33 * Routine which does approximate Sequence Id resolution by name using string
34 * containment (on word boundaries) rather than equivalence. It also attempts to
35 * resolve ties where no exact match is available by picking the the id closest
38 * Copyright: Copyright (c) 2004
42 * Company: Dundee University
45 * @author not attributable
48 public class SequenceIdMatcher
50 private Hashtable names;
52 public SequenceIdMatcher(SequenceI[] seqs)
54 names = new Hashtable();
55 for (int i = 0; i < seqs.length; i++)
57 names.put(new SeqIdName(seqs[i].getName()), 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 match = null;
88 if (candName == null || matches == null || matches.size() == 0)
92 match = (SequenceI) matches.elementAt(0);
93 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);
101 names.put(new SeqIdName(cand.getName()), cand);
102 int candlen = cand.getName().length();
103 // keep the one with an id 'closer' to the given seqnam string
104 if (Math.abs(matchlen - namlen) > Math.abs(candlen - namlen)
105 && candlen > matchlen)
115 * get SequenceI with closest SequenceI.getName() to seq.getName()
121 SequenceI findIdMatch(SequenceI seq)
123 SeqIdName nam = new SeqIdName(seq.getName());
124 return findIdMatch(nam);
127 SequenceI findIdMatch(String seqnam)
129 SeqIdName nam = new SeqIdName(seqnam);
130 return findIdMatch(nam);
136 * Return pointers to sequences (or sequence object containers) which have
137 * same Id as a given set of different sequence objects
141 * @return SequenceI[]
143 SequenceI[] findIdMatch(SequenceI[] seqs)
145 SequenceI[] namedseqs = null;
151 namedseqs = new SequenceI[seqs.length];
154 nam = new SeqIdName(seqs[i].getName());
156 if (names.containsKey(nam))
158 namedseqs[i] = findIdMatch(nam);
164 } while (++i < seqs.length);
171 * core findIdMatch search method
177 private SequenceI findIdMatch(
178 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
180 Vector matches = new Vector();
181 while (names.containsKey(nam))
183 matches.addElement(names.remove(nam));
185 return pickbestMatch(nam, matches);
188 private class SeqIdName
204 public int hashCode()
206 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
210 public boolean equals(Object s)
212 if (s instanceof SeqIdName)
214 return this.equals((SeqIdName) s);
218 if (s instanceof String)
220 return this.equals((String) s);
228 * Characters that define the end of a unique sequence ID at the beginning
229 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
230 * arbritrarily extended sequence id's (like portions of an aligned set of
231 * repeats from one sequence)
233 private String WORD_SEP = "~. |#\\/<>!\"£$%^*)}[@',?_";
236 * matches if one ID properly contains another at a whitespace boundary.
237 * TODO: (JBPNote) These are not efficient. should use char[] for speed
238 * todo: (JBPNote) Set separator characters appropriately
244 public boolean equals(SeqIdName s)
246 if (id.length() > s.id.length())
248 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
249 .length())) > -1) : false;
253 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
254 .indexOf(s.id.charAt(id.length())) > -1)) : false;
258 public boolean equals(String s)
260 if (id.length() > s.length())
262 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
267 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
268 .indexOf(s.charAt(id.length())) > -1)) : false;