2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.datamodel.DBRefEntry;
24 import jalview.datamodel.SequenceI;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Vector;
33 * Routines for 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 public class SequenceIdMatcher
40 private HashMap<SeqIdName, SequenceI> names;
42 public SequenceIdMatcher(List<SequenceI> seqs)
44 names = new HashMap<SeqIdName, SequenceI>();
49 * add more sequences to this matcher - also used by the constructor
53 public void addAll(List<SequenceI> seqs)
55 for (SequenceI seq : seqs)
57 // TODO: deal with ID collisions - SequenceI should be appended to list
58 // associated with this key.
59 names.put(new SeqIdName(seq.getDisplayId(true)), seq);
60 SequenceI dbseq = seq;
61 while (dbseq.getDatasetSequence()!=null)
63 dbseq = dbseq.getDatasetSequence();
65 // add in any interesting identifiers
66 if (dbseq.getDBRef() != null)
68 DBRefEntry dbr[] = dbseq.getDBRef();
70 for (int r = 0; r < dbr.length; r++)
72 sid = new SeqIdName(dbr[r].getAccessionId());
73 if (!names.containsKey(sid))
83 * convenience method to make a matcher from concrete array
87 public SequenceIdMatcher(SequenceI[] sequences)
89 this(Arrays.asList(sequences));
93 * returns the closest SequenceI in matches to SeqIdName and returns all the
94 * matches to the names hash.
99 * List of SequenceI objects
100 * @return SequenceI closest SequenceI to SeqIdName
102 private SequenceI pickbestMatch(SeqIdName candName,
103 List<SequenceI> matches)
105 List<SequenceI> st = pickbestMatches(candName, matches);
106 return st == null || st.size() == 0 ? null : st.get(0);
110 * returns the closest SequenceI in matches to SeqIdName and returns all the
111 * matches to the names hash.
116 * Vector of SequenceI objects
117 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
120 private List<SequenceI> pickbestMatches(SeqIdName candName,
121 List<SequenceI> matches)
123 ArrayList<SequenceI> best = new ArrayList<SequenceI>();
124 if (candName == null || matches == null || matches.size() == 0)
128 SequenceI match = matches.remove(0);
130 names.put(new SeqIdName(match.getName()), match);
131 int matchlen = match.getName().length();
132 int namlen = candName.id.length();
133 while (matches.size() > 0)
135 // look through for a better one.
136 SequenceI cand = matches.remove(0);
137 names.put(new SeqIdName(cand.getName()), cand);
138 int q, w, candlen = cand.getName().length();
139 // keep the one with an id 'closer' to the given seqnam string
140 if ((q = Math.abs(matchlen - namlen)) > (w = Math.abs(candlen
142 && candlen > matchlen)
149 if (q == w && candlen == matchlen)
155 if (best.size() == 0)
164 * get SequenceI with closest SequenceI.getName() to seq.getName()
170 public SequenceI findIdMatch(SequenceI seq)
172 SeqIdName nam = new SeqIdName(seq.getName());
173 return findIdMatch(nam);
176 public SequenceI findIdMatch(String seqnam)
178 SeqIdName nam = new SeqIdName(seqnam);
179 return findIdMatch(nam);
183 * Find all matches for a given sequence name.
186 * string to query Matcher with.
187 * @return a new array or (possibly) null
189 public SequenceI[] findAllIdMatches(String seqnam)
192 SeqIdName nam = new SeqIdName(seqnam);
193 List<SequenceI> m = findAllIdMatches(nam);
196 return m.toArray(new SequenceI[m.size()]);
204 * Return pointers to sequences (or sequence object containers) which have
205 * same Id as a given set of different sequence objects
209 * @return SequenceI[]
211 public SequenceI[] findIdMatch(SequenceI[] seqs)
213 SequenceI[] namedseqs = null;
219 namedseqs = new SequenceI[seqs.length];
222 nam = new SeqIdName(seqs[i].getName());
224 if (names.containsKey(nam))
226 namedseqs[i] = findIdMatch(nam);
232 } while (++i < seqs.length);
239 * core findIdMatch search method
245 private SequenceI findIdMatch(
246 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
248 Vector matches = new Vector();
249 while (names.containsKey(nam))
251 matches.addElement(names.remove(nam));
253 return pickbestMatch(nam, matches);
257 * core findIdMatch search method for finding all equivalent matches
261 * @return SequenceI[]
263 private List<SequenceI> findAllIdMatches(
264 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
266 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
267 while (names.containsKey(nam))
269 matches.add(names.remove(nam));
271 List<SequenceI> r = pickbestMatches(nam, matches);
275 private class SeqIdName
292 public int hashCode()
294 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
299 public boolean equals(Object s)
305 if (s instanceof SeqIdName)
307 return this.equals((SeqIdName) s);
311 if (s instanceof String)
313 return this.equals((String) s);
321 * Characters that define the end of a unique sequence ID at the beginning
322 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
323 * arbritrarily extended sequence id's (like portions of an aligned set of
324 * repeats from one sequence)
326 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
330 * matches if one ID properly contains another at a whitespace boundary.
331 * TODO: (JBPNote) These are not efficient. should use char[] for speed
332 * todo: (JBPNote) Set separator characters appropriately
338 public boolean equals(SeqIdName s)
340 // TODO: JAL-732 patch for cases when name includes a list of IDs, and the
341 // match contains one ID flanked
342 if (id.length() > s.id.length())
344 return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
345 .length())) > -1) : false;
349 return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
350 .indexOf(s.id.charAt(id.length())) > -1)) : false;
354 public boolean equals(String s)
356 if (id.length() > s.length())
358 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
363 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
364 .indexOf(s.charAt(id.length())) > -1)) : false;