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;
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, List<SequenceI>> names;
42 private Map<SeqIdName, List<SequenceI>> excludes;
44 public SequenceIdMatcher(List<SequenceI> seqs)
46 names = new HashMap<>();
47 excludes = new HashMap<>();
52 * Adds sequences to this matcher
56 public void addAll(List<SequenceI> seqs)
58 for (SequenceI seq : seqs)
65 * Adds one sequence to this matcher
69 public void add(SequenceI seq)
71 SeqIdName key = new SeqIdName(seq.getDisplayId(true));
72 addMatchCandidate(key, seq);
73 SequenceI dbseq = seq;
74 while (dbseq.getDatasetSequence() != null)
76 dbseq = dbseq.getDatasetSequence();
78 // add in any interesting identifiers
79 if (dbseq.getDBRefs() != null)
81 DBRefEntry dbr[] = dbseq.getDBRefs();
82 for (int r = 0; r < dbr.length; r++)
84 DBRefEntry dbref = dbr[r];
85 SeqIdName sid = new SeqIdName(dbref.getAccessionId());
86 if (dbref.getMap() != null
87 && dbref.getMap().getMap().isTripletMap())
90 * dbref with 3:1 or 1:3 mapping (e.g. CDS/protein);
91 * mark as not a valid match for this id
93 List<SequenceI> excluded = excludes.get(sid);
96 excludes.put(sid, excluded = new ArrayList<>());
99 System.out.println("Excluding " + sid + "->" + seq);
102 addMatchCandidate(sid, seq);
107 void addMatchCandidate(SeqIdName key, SequenceI seq)
109 List<SequenceI> namesList = names.get(key);
110 if (namesList == null)
112 names.put(key, namesList = new ArrayList<>());
114 if (!namesList.contains(seq))
117 System.out.println("Adding " + key + "->" + seq);
122 * convenience method to make a matcher from concrete array
126 public SequenceIdMatcher(SequenceI[] sequences)
128 this(Arrays.asList(sequences));
132 * returns the closest SequenceI in matches to SeqIdName and returns all the
133 * matches to the names hash.
138 * List of SequenceI objects
139 * @return SequenceI closest SequenceI to SeqIdName
141 private SequenceI pickbestMatch(SeqIdName candName,
142 List<SequenceI> matches)
144 List<SequenceI> st = pickbestMatches(candName, matches);
145 return st == null || st.size() == 0 ? null : st.get(0);
149 * returns the closest SequenceI in matches to SeqIdName and returns all the
150 * matches to the names hash.
155 * Vector of SequenceI objects
156 * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
159 private List<SequenceI> pickbestMatches(SeqIdName candName,
160 List<SequenceI> matches)
162 List<SequenceI> best = new ArrayList<>();
163 if (candName == null || matches == null || matches.size() == 0)
167 SequenceI match = matches.remove(0);
169 addMatchCandidate(new SeqIdName(match.getName()), match);
170 int matchlen = match.getName().length();
171 int namlen = candName.id.length();
172 while (matches.size() > 0)
174 // look through for a better one.
175 SequenceI cand = matches.remove(0);
176 addMatchCandidate(new SeqIdName(cand.getName()), cand);
177 int q, w, candlen = cand.getName().length();
178 // keep the one with an id 'closer' to the given seqnam string
179 if ((q = Math.abs(matchlen - namlen)) > (w = Math
180 .abs(candlen - namlen)) && candlen > matchlen)
187 if (q == w && candlen == matchlen)
193 if (best.size() == 0)
202 * get SequenceI with closest SequenceI.getName() to seq.getName()
208 public SequenceI findIdMatch(SequenceI seq)
210 SeqIdName nam = new SeqIdName(seq.getName());
211 return findIdMatch(nam);
214 public SequenceI findIdMatch(String seqnam)
216 SeqIdName nam = new SeqIdName(seqnam);
217 return findIdMatch(nam);
221 * Find all matches for a given sequence name.
224 * string to query Matcher with.
225 * @return a new array or (possibly) null
227 public SequenceI[] findAllIdMatches(String seqnam)
230 SeqIdName nam = new SeqIdName(seqnam);
231 List<SequenceI> m = findAllIdMatches(nam);
234 return m.toArray(new SequenceI[m.size()]);
242 * Return pointers to sequences (or sequence object containers) which have
243 * same Id as a given set of different sequence objects
247 * @return SequenceI[]
249 public SequenceI[] findIdMatch(SequenceI[] seqs)
251 SequenceI[] namedseqs = null;
257 namedseqs = new SequenceI[seqs.length];
260 nam = new SeqIdName(seqs[i].getName());
262 if (names.containsKey(nam))
264 namedseqs[i] = findIdMatch(nam);
270 } while (++i < seqs.length);
277 * core findIdMatch search method
283 private SequenceI findIdMatch(SeqIdName nam)
285 List<SequenceI> matches = new ArrayList<>();
286 while (names.containsKey(nam))
288 List<SequenceI> candidates = names.remove(nam);
289 List<SequenceI> except = excludes.get(nam);
290 int j = candidates.size();
291 for (int i = 0; i < j; i++)
293 SequenceI candidate = candidates.get(i);
294 if (!except.contains(candidate))
296 matches.add(candidate);
300 return pickbestMatch(nam, matches);
304 * core findIdMatch search method for finding all equivalent matches
308 * @return SequenceI[]
310 private List<SequenceI> findAllIdMatches(
311 jalview.analysis.SequenceIdMatcher.SeqIdName nam)
313 List<SequenceI> matches = new ArrayList<>();
314 while (names.containsKey(nam))
316 matches.addAll(names.remove(nam));
318 List<SequenceI> r = pickbestMatches(nam, matches);
330 id = s.toLowerCase();
339 public int hashCode()
341 return ((id.length() >= 4) ? id.substring(0, 4).hashCode()
346 public boolean equals(Object s)
352 if (s instanceof SeqIdName)
354 return this.stringequals(((SeqIdName) s).id);
358 if (s instanceof String)
360 return this.stringequals(((String) s).toLowerCase());
368 * Characters that define the end of a unique sequence ID at the beginning
369 * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
370 * arbritrarily extended sequence id's (like portions of an aligned set of
371 * repeats from one sequence)
373 private String WORD_SEP = "~. |#\\/<>!\"" + ((char) 0x00A4)
377 * matches if one ID properly contains another at a whitespace boundary.
378 * TODO: (JBPNote) These are not efficient. should use char[] for speed
379 * todo: (JBPNote) Set separator characters appropriately
384 private boolean stringequals(String s)
386 if (id.length() > s.length())
388 return id.startsWith(s)
389 ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
394 return s.startsWith(id)
395 ? (s.equals(id) ? true
396 : (WORD_SEP.indexOf(s.charAt(id.length())) > -1))
402 * toString method returns the wrapped sequence id. For debugging purposes
403 * only, behaviour not guaranteed not to change.
406 public String toString()