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 * Adds sequences to this matcher
53 public void addAll(List<SequenceI> seqs)
55 for (SequenceI seq : seqs)
62 * Adds one sequence to this matcher
66 public void add(SequenceI seq)
68 // TODO: deal with ID collisions - SequenceI should be appended to list
69 // associated with this key.
70 names.put(new SeqIdName(seq.getDisplayId(true)), seq);
71 SequenceI dbseq = seq;
72 while (dbseq.getDatasetSequence() != null)
74 dbseq = dbseq.getDatasetSequence();
76 // add in any interesting identifiers
77 if (dbseq.getDBRefs() != null)
79 DBRefEntry dbr[] = dbseq.getDBRefs();
81 for (int r = 0; r < dbr.length; r++)
83 sid = new SeqIdName(dbr[r].getAccessionId());
84 if (!names.containsKey(sid))
93 * convenience method to make a matcher from concrete array
97 public SequenceIdMatcher(SequenceI[] sequences)
99 this(Arrays.asList(sequences));
103 * returns the closest SequenceI in matches to SeqIdName and returns all the
104 * matches to the names hash.
109 * List of SequenceI objects
110 * @return SequenceI closest SequenceI to SeqIdName
112 private SequenceI pickbestMatch(SeqIdName candName,
113 List<SequenceI> matches)
115 List<SequenceI> st = pickbestMatches(candName, matches);
116 return st == null || st.size() == 0 ? null : st.get(0);
120 * returns the closest SequenceI in matches to SeqIdName and returns all the
121 * matches to the names hash.
126 * Vector of SequenceI objects
127 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
130 private List<SequenceI> pickbestMatches(SeqIdName candName,
131 List<SequenceI> matches)
133 ArrayList<SequenceI> best = new ArrayList<SequenceI>();
134 if (candName == null || matches == null || matches.size() == 0)
138 SequenceI match = matches.remove(0);
140 names.put(new SeqIdName(match.getName()), match);
141 int matchlen = match.getName().length();
142 int namlen = candName.id.length();
143 while (matches.size() > 0)
145 // look through for a better one.
146 SequenceI cand = matches.remove(0);
147 names.put(new SeqIdName(cand.getName()), cand);
148 int q, w, candlen = cand.getName().length();
149 // keep the one with an id 'closer' to the given seqnam string
150 if ((q = Math.abs(matchlen - namlen)) > (w = Math.abs(candlen
152 && candlen > matchlen)
159 if (q == w && candlen == matchlen)
165 if (best.size() == 0)
174 * get SequenceI with closest SequenceI.getName() to seq.getName()
180 public SequenceI findIdMatch(SequenceI seq)
182 SeqIdName nam = new SeqIdName(seq.getName());
183 return findIdMatch(nam);
186 public SequenceI findIdMatch(String seqnam)
188 SeqIdName nam = new SeqIdName(seqnam);
189 return findIdMatch(nam);
193 * Find all matches for a given sequence name.
196 * string to query Matcher with.
197 * @return a new array or (possibly) null
199 public SequenceI[] findAllIdMatches(String seqnam)
202 SeqIdName nam = new SeqIdName(seqnam);
203 List<SequenceI> m = findAllIdMatches(nam);
206 return m.toArray(new SequenceI[m.size()]);
214 * Return pointers to sequences (or sequence object containers) which have
215 * same Id as a given set of different sequence objects
219 * @return SequenceI[]
221 public SequenceI[] findIdMatch(SequenceI[] seqs)
223 SequenceI[] namedseqs = null;
229 namedseqs = new SequenceI[seqs.length];
232 nam = new SeqIdName(seqs[i].getName());
234 if (names.containsKey(nam))
236 namedseqs[i] = findIdMatch(nam);
242 } while (++i < seqs.length);
249 * core findIdMatch search method
255 private SequenceI findIdMatch(
256 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
258 Vector matches = new Vector();
259 while (names.containsKey(nam))
261 matches.addElement(names.remove(nam));
263 return pickbestMatch(nam, matches);
267 * core findIdMatch search method for finding all equivalent matches
271 * @return SequenceI[]
273 private List<SequenceI> findAllIdMatches(
274 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
276 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
277 while (names.containsKey(nam))
279 matches.add(names.remove(nam));
281 List<SequenceI> r = pickbestMatches(nam, matches);
293 id = s.toLowerCase();
302 public int hashCode()
304 return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
309 public boolean equals(Object s)
315 if (s instanceof SeqIdName)
317 return this.stringequals(((SeqIdName) s).id);
321 if (s instanceof String)
323 return this.stringequals(((String) s).toLowerCase());
331 * Characters that define the end of a unique sequence ID at the beginning
332 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
333 * arbritrarily extended sequence id's (like portions of an aligned set of
334 * repeats from one sequence)
336 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
340 * matches if one ID properly contains another at a whitespace boundary.
341 * TODO: (JBPNote) These are not efficient. should use char[] for speed
342 * todo: (JBPNote) Set separator characters appropriately
347 private boolean stringequals(String s)
349 if (id.length() > s.length())
351 return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
356 return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
357 .indexOf(s.charAt(id.length())) > -1)) : false;
362 * toString method returns the wrapped sequence id. For debugging purposes
363 * only, behaviour not guaranteed not to change.
366 public String toString()