2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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]);
62 * returns the closest SequenceI in matches to SeqIdName and returns all the
63 * matches to the names hash.
68 * Vector of SequenceI objects
69 * @return SequenceI closest SequenceI to SeqIdName
71 private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
73 SequenceI match = null;
74 if (candName == null || matches == null || matches.size() == 0)
78 match = (SequenceI) matches.elementAt(0);
79 matches.removeElementAt(0);
80 names.put(new SeqIdName(match.getName()), match);
81 int matchlen = match.getName().length();
82 int namlen = candName.id.length();
83 while (matches.size() > 0)
85 // look through for a better one.
86 SequenceI cand = (SequenceI) matches.elementAt(0);
87 names.put(new SeqIdName(cand.getName()), cand);
88 int candlen = cand.getName().length();
89 // keep the one with an id 'closer' to the given seqnam string
90 if (Math.abs(matchlen - namlen) > Math.abs(candlen - namlen)
91 && candlen > matchlen)
101 * get SequenceI with closest SequenceI.getName() to seq.getName()
107 SequenceI findIdMatch(SequenceI seq)
109 SeqIdName nam = new SeqIdName(seq.getName());
110 return findIdMatch(nam);
113 SequenceI findIdMatch(String seqnam)
115 SeqIdName nam = new SeqIdName(seqnam);
116 return findIdMatch(nam);
122 * Return pointers to sequences (or sequence object containers) which have
123 * same Id as a given set of different sequence objects
127 * @return SequenceI[]
129 SequenceI[] findIdMatch(SequenceI[] seqs)
131 SequenceI[] namedseqs = null;
137 namedseqs = new SequenceI[seqs.length];
140 nam = new SeqIdName(seqs[i].getName());
142 if (names.containsKey(nam))
144 namedseqs[i] = findIdMatch(nam);
150 } while (++i < seqs.length);
157 * core findIdMatch search method
163 private SequenceI findIdMatch(
164 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
166 Vector matches = new Vector();
167 while (names.containsKey(nam))
169 matches.addElement(names.remove(nam));
171 return pickbestMatch(nam, matches);
174 private class SeqIdName
190 public int hashCode()
192 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
196 public boolean equals(Object s)
198 if (s instanceof SeqIdName)
200 return this.equals((SeqIdName) s);
204 if (s instanceof String)
206 return this.equals((String) s);
214 * Characters that define the end of a unique sequence ID at the beginning
215 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
216 * arbritrarily extended sequence id's (like portions of an aligned set of
217 * repeats from one sequence)
219 private String WORD_SEP = "~. |#\\/<>!\"£$%^*)}[@',?_";
222 * matches if one ID properly contains another at a whitespace boundary.
223 * TODO: (JBPNote) These are not efficient. should use char[] for speed
224 * todo: (JBPNote) Set separator characters appropriately
230 public boolean equals(SeqIdName s)
232 if (id.length() > s.id.length())
234 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
235 .length())) > -1) : false;
239 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
240 .indexOf(s.id.charAt(id.length())) > -1)) : false;
244 public boolean equals(String s)
246 if (id.length() > s.length())
248 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
253 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
254 .indexOf(s.charAt(id.length())) > -1)) : false;