update author list in license for (JAL-826)
[jalview.git] / src / jalview / analysis / SequenceIdMatcher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.analysis;
19
20 import java.util.*;
21
22 import jalview.datamodel.*;
23
24 /**
25  * Routines for approximate Sequence Id resolution by name using string
26  * containment (on word boundaries) rather than equivalence. It also attempts to
27  * resolve ties where no exact match is available by picking the the id closest
28  * to the query.
29  */
30 public class SequenceIdMatcher
31 {
32   private Hashtable names;
33
34   public SequenceIdMatcher(SequenceI[] seqs)
35   {
36     names = new Hashtable();
37     for (int i = 0; i < seqs.length; i++)
38     {
39       // TODO: deal with ID collisions - SequenceI should be appended to list associated with this key.
40       names.put(new SeqIdName(seqs[i].getDisplayId(true)), seqs[i]);
41       // add in any interesting identifiers
42       if (seqs[i].getDBRef() != null)
43       {
44         DBRefEntry dbr[] = seqs[i].getDBRef();
45         SeqIdName sid = null;
46         for (int r = 0; r < dbr.length; r++)
47         {
48           sid = new SeqIdName(dbr[r].getAccessionId());
49           if (!names.contains(sid))
50           {
51             names.put(sid, seqs[i]);
52           }
53         }
54       }
55     }
56   }
57
58   /**
59    * returns the closest SequenceI in matches to SeqIdName and returns all the
60    * matches to the names hash.
61    * 
62    * @param candName
63    *          SeqIdName
64    * @param matches
65    *          Vector of SequenceI objects
66    * @return SequenceI closest SequenceI to SeqIdName
67    */
68   private SequenceI pickbestMatch(SeqIdName candName, Vector matches)
69   {
70     SequenceI[] st= pickbestMatches(candName, matches);
71     return st==null || st.length==0 ? null : st[0];
72   }
73   /**
74    * returns the closest SequenceI in matches to SeqIdName and returns all the
75    * matches to the names hash.
76    * 
77    * @param candName
78    *          SeqIdName
79    * @param matches
80    *          Vector of SequenceI objects
81    * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[] ties }
82    */
83   private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
84   {
85     ArrayList best=new ArrayList();
86     SequenceI match = null;
87     if (candName == null || matches == null || matches.size() == 0)
88     {
89       return null;
90     }
91     match = (SequenceI) matches.elementAt(0);
92     matches.removeElementAt(0);
93     best.add(match);
94     names.put(new SeqIdName(match.getName()), match);
95     int matchlen = match.getName().length();
96     int namlen = candName.id.length();
97     while (matches.size() > 0)
98     {
99       // look through for a better one.
100       SequenceI cand = (SequenceI) matches.elementAt(0);
101       matches.remove(0);
102       names.put(new SeqIdName(cand.getName()), cand);
103       int q,w,candlen = cand.getName().length();
104       // keep the one with an id 'closer' to the given seqnam string
105       if ((q=Math.abs(matchlen - namlen)) > (w=Math.abs(candlen - namlen))
106               && candlen > matchlen)
107       {
108         best.clear();
109         match = cand;
110         matchlen = candlen;
111         best.add(match);
112       }
113       if (q==w && candlen==matchlen)
114       {
115         // record any ties
116         best.add(cand);
117       }
118     }
119     if (best.size()==0) { return null; };
120     return (SequenceI[]) best.toArray(new SequenceI[0]);
121   }
122
123   /**
124    * get SequenceI with closest SequenceI.getName() to seq.getName()
125    * 
126    * @param seq
127    *          SequenceI
128    * @return SequenceI
129    */
130   public SequenceI findIdMatch(SequenceI seq)
131   {
132     SeqIdName nam = new SeqIdName(seq.getName());
133     return findIdMatch(nam);
134   }
135
136   public SequenceI findIdMatch(String seqnam)
137   {
138     SeqIdName nam = new SeqIdName(seqnam);
139     return findIdMatch(nam);
140   }
141
142   /**
143    *  Find all matches for a given sequence name.
144    *  @param seqnam string to query Matcher with.
145    */
146   public SequenceI[] findAllIdMatches(String seqnam)
147   {
148
149     SeqIdName nam = new SeqIdName(seqnam);
150     return findAllIdMatches(nam);
151   }
152
153   /**
154    * findIdMatch
155    * 
156    * Return pointers to sequences (or sequence object containers) which have
157    * same Id as a given set of different sequence objects
158    * 
159    * @param seqs
160    *          SequenceI[]
161    * @return SequenceI[]
162    */
163   public SequenceI[] findIdMatch(SequenceI[] seqs)
164   {
165     SequenceI[] namedseqs = null;
166     int i = 0;
167     SeqIdName nam;
168
169     if (seqs.length > 0)
170     {
171       namedseqs = new SequenceI[seqs.length];
172       do
173       {
174         nam = new SeqIdName(seqs[i].getName());
175
176         if (names.containsKey(nam))
177         {
178           namedseqs[i] = findIdMatch(nam);
179         }
180         else
181         {
182           namedseqs[i] = null;
183         }
184       } while (++i < seqs.length);
185     }
186
187     return namedseqs;
188   }
189
190   /**
191    * core findIdMatch search method
192    * 
193    * @param nam
194    *          SeqIdName
195    * @return SequenceI
196    */
197   private SequenceI findIdMatch(
198           jalview.analysis.SequenceIdMatcher.SeqIdName nam)
199   {
200     Vector matches = new Vector();
201     while (names.containsKey(nam))
202     {
203       matches.addElement(names.remove(nam));
204     }
205     return pickbestMatch(nam, matches);
206   }
207   /**
208    * core findIdMatch search method for finding all equivalent matches
209    * 
210    * @param nam
211    *          SeqIdName
212    * @return SequenceI[]
213    */
214   private SequenceI[] findAllIdMatches(
215           jalview.analysis.SequenceIdMatcher.SeqIdName nam)
216   {
217     Vector matches = new Vector();
218     while (names.containsKey(nam))
219     {
220       matches.addElement(names.remove(nam));
221     }
222     SequenceI[] r=pickbestMatches(nam, matches);
223     return r;
224   }
225
226
227   private class SeqIdName
228   {
229     String id;
230
231     SeqIdName(String s)
232     {
233       if (s != null)
234       {
235         id = new String(s);
236       }
237       else
238       {
239         id = "";
240       }
241     }
242
243     public int hashCode()
244     {
245       return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id
246               .hashCode());
247     }
248
249     public boolean equals(Object s)
250     {
251       if (s instanceof SeqIdName)
252       {
253         return this.equals((SeqIdName) s);
254       }
255       else
256       {
257         if (s instanceof String)
258         {
259           return this.equals((String) s);
260         }
261       }
262
263       return false;
264     }
265
266     /**
267      * Characters that define the end of a unique sequence ID at the beginning
268      * of an arbitrary ID string JBPNote: This is a heuristic that will fail for
269      * arbritrarily extended sequence id's (like portions of an aligned set of
270      * repeats from one sequence)
271      */
272     private String WORD_SEP = "~. |#\\/<>!\""+((char)0x00A4)+"$%^*)}[@',?_";
273
274     /**
275      * matches if one ID properly contains another at a whitespace boundary.
276      * TODO: (JBPNote) These are not efficient. should use char[] for speed
277      * todo: (JBPNote) Set separator characters appropriately
278      * 
279      * @param s
280      *          SeqIdName
281      * @return boolean
282      */
283     public boolean equals(SeqIdName s)
284     {
285       // TODO: JAL-732 patch for cases when name includes a list of IDs, and the match contains one ID flanked
286       if (id.length() > s.id.length())
287       {
288         return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id
289                 .length())) > -1) : false;
290       }
291       else
292       {
293         return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP
294                 .indexOf(s.id.charAt(id.length())) > -1)) : false;
295       }
296     }
297
298     public boolean equals(String s)
299     {
300       if (id.length() > s.length())
301       {
302         return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1)
303                 : false;
304       }
305       else
306       {
307         return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP
308                 .indexOf(s.charAt(id.length())) > -1)) : false;
309       }
310     }
311   }
312 }