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.HashSet;
30 import java.util.List;
34 * Routines for approximate Sequence Id resolution by name using string
35 * containment (on word boundaries) rather than equivalence. It also attempts to
36 * resolve ties where no exact match is available by picking the the id closest
39 public class SequenceIdMatcher
42 * weak hash for each sequence
44 private HashMap<SeqIdName, Set<SequenceI>> names;
47 // * cache of values removed for each query string.
49 // private HashMap<String, List<SequenceI>> resolved;
52 * do we index sequences on all 'words' in ID string ?
54 private boolean wordBased = false;
57 * Characters that define the end of a unique sequence ID at the beginning of
58 * an arbitrary ID string JBPNote: This is a heuristic that will fail for
59 * arbritrarily extended sequence id's (like portions of an aligned set of
60 * repeats from one sequence)
62 static String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
66 * @return true if matcher is word-based (ie string key matches one of the
67 * words within the body of one or more sequence IDs)
69 public boolean isWordBased()
75 * Construct a standard (non-word based) matcher. To configure word based
76 * matching, use the fully qualified constructor
80 public SequenceIdMatcher(List<SequenceI> seqs)
86 * construct a new matcher for a set of sequences, configured as required.
87 * Note: enabling word based matching
89 * @param wordBasedMatch
90 * - when true, "myseq" matches "X|myseq" and "myseq"
93 public SequenceIdMatcher(boolean wordBasedMatch, List<SequenceI> seqs)
95 wordBased = wordBasedMatch;
96 names = new HashMap<SeqIdName, Set<SequenceI>>();
101 * add more sequences to this matcher - also used by the constructor
105 public void addAll(List<SequenceI> seqs)
107 for (SequenceI seq : seqs)
113 private void addSeqIdName(SeqIdName idname, SequenceI seq)
115 Set<SequenceI> seqset = names.get(idname);
118 seqset = new HashSet<SequenceI>();
119 names.put(idname, seqset);
124 public void addSeq(SequenceI seq)
126 // TODO: deal with ID collisions - SequenceI should be appended to list
127 // associated with this key.
128 addSeqIdName(new SeqIdName(seq.getDisplayId(true)), seq);
131 for (SeqIdName key : getWordsFor(seq))
133 addSeqIdName(key, seq);
136 SequenceI dbseq = seq;
137 // TODO add test for database xref resolution
138 while (dbseq.getDatasetSequence() != null)
140 dbseq = dbseq.getDatasetSequence();
142 // add in any interesting identifiers
143 if (dbseq.getDBRefs() != null)
145 DBRefEntry dbr[] = dbseq.getDBRefs();
146 SeqIdName sid = null;
147 for (int r = 0; r < dbr.length; r++)
149 sid = new SeqIdName(dbr[r].getAccessionId());
150 if (!names.containsKey(sid))
152 addSeqIdName(sid, seq);
159 * generate word based keys for the given sequence
162 * @return list of split keys
164 public static List<SeqIdName> getWordsFor(SequenceI seq)
166 ArrayList<SeqIdName> keys = new ArrayList<SeqIdName>();
167 String name = seq.getName(), limits = "/" + seq.getStart() + "-"
169 int namel = name.length();
170 char[] sep = new char[WORD_SEP.length()];
171 // find only the separators present in the ID.
172 for (int i = 0; i < sep.length; i++)
174 sep[i] = WORD_SEP.charAt(i);
175 if (seq.getName().indexOf("" + sep[i]) == -1)
182 for (int i = 0; i < sep.length; i++)
187 while ((m = name.indexOf(sep[i], p)) > p)
190 if (m > 0 && m - p > 5)
192 // split to end of word m with this delimiter
193 keys.add(new SeqIdName(name.substring(p, m) + limits));
197 // index word after this delimiter m
198 keys.add(new SeqIdName(name.substring(m + 1) + limits));
204 // index word after this delimiter m
205 keys.add(new SeqIdName(name.substring(p) + limits));
213 * convenience method to make a matcher from concrete array Note: in order to
214 * support word based matching, use the fully qualified constructor
218 public SequenceIdMatcher(SequenceI[] sequences)
220 this(Arrays.asList(sequences));
224 * returns the closest SequenceI in matches to SeqIdName and returns all the
225 * matches to the names hash.
230 * List of SequenceI objects
231 * @return SequenceI closest SequenceI to SeqIdName
233 private SequenceI pickbestMatch(SeqIdName candName,
234 List<SequenceI> matches)
236 List<SequenceI> st = pickbestMatches(candName, matches);
237 return st == null || st.size() == 0 ? null : st.get(0);
241 * returns the SequenceI's with exact word matches to candName
246 * List of SequenceI objects - some of which may be duplicates
247 * @return { word matches to candName }
249 private List<SequenceI> pickwordMatches(SeqIdName candName,
250 List<SequenceI> matches)
252 List<SequenceI> best = new ArrayList<SequenceI>();
253 for (SequenceI match : matches)
255 if (!best.contains(match))
257 if (candName.equalsCase(match.getDisplayId(true)))
259 // put the exact match at the beginning
273 * returns the closest SequenceI in matches to SeqIdName and returns all the
274 * matches to the names hash.
279 * Vector of SequenceI objects
280 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
283 private List<SequenceI> pickbestMatches(SeqIdName candName,
284 List<SequenceI> matches)
286 ArrayList<SequenceI> best = new ArrayList<SequenceI>();
287 if (candName == null || matches == null || matches.size() == 0)
291 SequenceI match = matches.remove(0);
294 int matchlen = match.getName().length();
295 int namlen = candName.id.length();
296 while (matches.size() > 0)
298 // look through for a better one.
299 SequenceI cand = matches.remove(0);
301 int q, w, candlen = cand.getName().length();
302 // keep the one with an id 'closer' to the given seqnam string
303 boolean is_closer = ((q = Math.abs(matchlen - namlen)) > (w = Math
304 .abs(candlen - namlen)) && candlen > matchlen);
305 // if not closer, then check if current best is actually identical in case
309 || (candName.equalsCase(cand.getName()) && !candName
310 .equalsCase(best.get(0).getName())))
319 if (q == w && candlen == matchlen)
321 // equivalently good, and matches with case as well. so
327 if (best.size() == 0)
336 * get SequenceI with closest SequenceI.getName() to seq.getName()
342 public SequenceI findIdMatch(SequenceI seq)
344 SeqIdName nam = new SeqIdName(seq.getName());
345 return findIdMatch(nam);
348 public SequenceI findIdMatch(String seqnam)
350 SeqIdName nam = new SeqIdName(seqnam);
351 return findIdMatch(nam);
355 * Find all matches for a given sequence name.
358 * string to query Matcher with.
359 * @return a new array or null of no match exists
361 public SequenceI[] findAllIdMatches(String seqnam)
364 SeqIdName nam = new SeqIdName(seqnam);
365 List<SequenceI> m = findAllIdMatches(nam);
366 if (m != null && m.size() > 0)
368 return m.toArray(new SequenceI[m.size()]);
376 * Return pointers to sequences (or sequence object containers) which have
377 * same Id as a given set of different sequence objects
381 * @return SequenceI[]
383 public SequenceI[] findIdMatch(SequenceI[] seqs)
385 SequenceI[] namedseqs = null;
391 namedseqs = new SequenceI[seqs.length];
394 nam = new SeqIdName(seqs[i].getName());
396 if (names.containsKey(nam))
398 namedseqs[i] = findIdMatch(nam);
404 } while (++i < seqs.length);
411 * core findIdMatch search method
417 private SequenceI findIdMatch(
418 jalview.analysis.SeqIdName nam)
420 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
421 while (names.containsKey(nam))
423 matches.addAll(names.remove(nam));
425 return pickbestMatch(nam, matches);
429 * core findIdMatch search method for finding all equivalent matches
433 * @return SequenceI[]
435 private List<SequenceI> findAllIdMatches(
436 jalview.analysis.SeqIdName nam)
438 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
439 while (names.containsKey(nam))
441 matches.addAll(names.remove(nam));
443 List<SequenceI> r = (wordBased) ? pickwordMatches(nam, matches)
444 : pickbestMatches(nam, matches);