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 List<DBRefEntry> dbr = dbseq.getDBRefs();
81 for (int r = 0, nr = dbr.size(); r < nr; r++)
83 sid = new SeqIdName(dbr.get(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
151 .abs(candlen - namlen)) && candlen > matchlen)
158 if (q == w && candlen == matchlen)
164 if (best.size() == 0)
173 * get SequenceI with closest SequenceI.getName() to seq.getName()
179 public SequenceI findIdMatch(SequenceI seq)
181 SeqIdName nam = new SeqIdName(seq.getName());
182 return findIdMatch(nam);
185 public SequenceI findIdMatch(String seqnam)
187 SeqIdName nam = new SeqIdName(seqnam);
188 return findIdMatch(nam);
192 * Find all matches for a given sequence name.
195 * string to query Matcher with.
196 * @return a new array or (possibly) null
198 public SequenceI[] findAllIdMatches(String seqnam)
201 SeqIdName nam = new SeqIdName(seqnam);
202 List<SequenceI> m = findAllIdMatches(nam);
205 return m.toArray(new SequenceI[m.size()]);
213 * Return pointers to sequences (or sequence object containers) which have
214 * same Id as a given set of different sequence objects
218 * @return SequenceI[]
220 public SequenceI[] findIdMatch(SequenceI[] seqs)
222 SequenceI[] namedseqs = null;
228 namedseqs = new SequenceI[seqs.length];
231 nam = new SeqIdName(seqs[i].getName());
233 if (names.containsKey(nam))
235 namedseqs[i] = findIdMatch(nam);
241 } while (++i < seqs.length);
248 * core findIdMatch search method
254 private SequenceI findIdMatch(
255 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
257 Vector matches = new Vector();
258 while (names.containsKey(nam))
260 matches.addElement(names.remove(nam));
262 return pickbestMatch(nam, matches);
266 * core findIdMatch search method for finding all equivalent matches
270 * @return SequenceI[]
272 private List<SequenceI> findAllIdMatches(
273 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
275 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
276 while (names.containsKey(nam))
278 matches.add(names.remove(nam));
280 List<SequenceI> r = pickbestMatches(nam, matches);
292 id = s.toLowerCase();
301 public int hashCode()
303 return ((id.length() >= 4) ? id.substring(0, 4).hashCode()
308 public boolean equals(Object s)
314 if (s instanceof SeqIdName)
316 return this.stringequals(((SeqIdName) s).id);
320 if (s instanceof String)
322 return this.stringequals(((String) s).toLowerCase());
330 * Characters that define the end of a unique sequence ID at the beginning
331 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
332 * arbritrarily extended sequence id's (like portions of an aligned set of
333 * repeats from one sequence)
335 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
339 * matches if one ID properly contains another at a whitespace boundary.
340 * TODO: (JBPNote) These are not efficient. should use char[] for speed
341 * todo: (JBPNote) Set separator characters appropriately
346 private boolean stringequals(String s)
348 if (id.length() > s.length())
350 return id.startsWith(s)
351 ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
356 return s.startsWith(id)
357 ? (s.equals(id) ? true
358 : (WORD_SEP.indexOf(s.charAt(id.length())) > -1))
364 * toString method returns the wrapped sequence id. For debugging purposes
365 * only, behaviour not guaranteed not to change.
368 public String toString()