update author list in license for (JAL-826)
[jalview.git] / src / jalview / util / DBRefUtils.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.util;
19
20 import java.util.*;
21
22 import jalview.datamodel.*;
23
24 public class DBRefUtils
25 {
26   /**
27    * Utilities for handling DBRef objects and their collections.
28    */
29   /**
30    * 
31    * @param dbrefs
32    *          Vector of DBRef objects to search
33    * @param sources
34    *          String[] array of source DBRef IDs to retrieve
35    * @return Vector
36    */
37   public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
38           String[] sources)
39   {
40     if (dbrefs == null)
41     {
42       return null;
43     }
44     if (sources == null)
45     {
46       return dbrefs;
47     }
48     Hashtable srcs = new Hashtable();
49     Vector res = new Vector();
50
51     for (int i = 0; i < sources.length; i++)
52     {
53       srcs.put(new String(sources[i]), new Integer(i));
54     }
55     for (int i = 0, j = dbrefs.length; i < j; i++)
56     {
57       if (srcs.containsKey(dbrefs[i].getSource()))
58       {
59         res.addElement(dbrefs[i]);
60       }
61     }
62
63     if (res.size() > 0)
64     {
65       DBRefEntry[] reply = new DBRefEntry[res.size()];
66       for (int i = 0; i < res.size(); i++)
67       {
68         reply[i] = (DBRefEntry) res.elementAt(i);
69       }
70       return reply;
71     }
72     res = null;
73     // there are probable memory leaks in the hashtable!
74     return null;
75   }
76
77   /**
78    * isDasCoordinateSystem
79    * 
80    * @param string
81    *          String
82    * @param dBRefEntry
83    *          DBRefEntry
84    * @return boolean true if Source DBRefEntry is compatible with DAS
85    *         CoordinateSystem name
86    */
87   public static Hashtable DasCoordinateSystemsLookup = null;
88
89   public static boolean isDasCoordinateSystem(String string,
90           DBRefEntry dBRefEntry)
91   {
92     if (DasCoordinateSystemsLookup == null)
93     {
94       // TODO: Make a DasCoordinateSystemsLookup properties resource
95       // Initialise
96       DasCoordinateSystemsLookup = new Hashtable();
97       DasCoordinateSystemsLookup.put("pdbresnum",
98               jalview.datamodel.DBRefSource.PDB);
99       DasCoordinateSystemsLookup.put("uniprot",
100               jalview.datamodel.DBRefSource.UNIPROT);
101       DasCoordinateSystemsLookup.put("EMBL",
102               jalview.datamodel.DBRefSource.EMBL);
103       // DasCoordinateSystemsLookup.put("EMBL",
104       // jalview.datamodel.DBRefSource.EMBLCDS);
105     }
106
107     String coordsys = (String) DasCoordinateSystemsLookup.get(string
108             .toLowerCase());
109     if (coordsys != null)
110     {
111       return coordsys.equals(dBRefEntry.getSource());
112     }
113     return false;
114   }
115
116   public static Hashtable CanonicalSourceNameLookup = null;
117
118   /**
119    * look up source in an internal list of database reference sources and return
120    * the canonical jalview name for the source, or the original string if it has
121    * no canonical form.
122    * 
123    * @param source
124    * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
125    *         or original source
126    */
127   public static String getCanonicalName(String source)
128   {
129     if (CanonicalSourceNameLookup == null)
130     {
131       CanonicalSourceNameLookup = new Hashtable();
132       CanonicalSourceNameLookup.put("uniprotkb/swiss-prot",
133               jalview.datamodel.DBRefSource.UNIPROT);
134       CanonicalSourceNameLookup.put("uniprotkb/trembl",
135               jalview.datamodel.DBRefSource.UNIPROT);
136       CanonicalSourceNameLookup.put("pdb",
137               jalview.datamodel.DBRefSource.PDB);
138     }
139     String canonical = (String) CanonicalSourceNameLookup.get(source
140             .toLowerCase());
141     if (canonical == null)
142     {
143       return source;
144     }
145     return canonical;
146   }
147
148   /**
149    * find RefEntry corresponding to a particular pattern the equals method of
150    * each entry is used, from String attributes right down to Mapping
151    * attributes.
152    * 
153    * @param ref
154    *          Set of references to search
155    * @param entry
156    *          pattern to collect - null any entry for wildcard match
157    * @return
158    */
159   public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
160   {
161     return searchRefs(ref, entry,
162             matchDbAndIdAndEitherMapOrEquivalentMapList);
163   }
164
165   public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry,
166           DbRefComp comparator)
167   {
168     if (ref == null || entry == null)
169       return null;
170     Vector rfs = new Vector();
171     for (int i = 0; i < ref.length; i++)
172     {
173       if (comparator.matches(entry, ref[i]))
174       {
175         rfs.addElement(ref[i]);
176       }
177     }
178     // TODO Auto-generated method stub
179     if (rfs.size() > 0)
180     {
181       DBRefEntry[] rf = new DBRefEntry[rfs.size()];
182       rfs.copyInto(rf);
183       return rf;
184     }
185     return null;
186   }
187
188   public interface DbRefComp
189   {
190     public boolean matches(DBRefEntry refa, DBRefEntry refb);
191   }
192
193   /**
194    * match on all non-null fields in refa
195    */
196   public static DbRefComp matchNonNullonA = new DbRefComp()
197   {
198     public boolean matches(DBRefEntry refa, DBRefEntry refb)
199     {
200       if (refa.getSource() == null
201               || refb.getSource().equals(refa.getSource()))
202       {
203         if (refa.getVersion() == null
204                 || refb.getVersion().equals(refa.getVersion()))
205         {
206           if (refa.getAccessionId() == null
207                   || refb.getAccessionId().equals(refa.getAccessionId()))
208           {
209             if (refa.getMap() == null
210                     || (refb.getMap() != null && refb.getMap().equals(
211                             refa.getMap())))
212             {
213               return true;
214             }
215           }
216         }
217       }
218       return false;
219     }
220   };
221
222   /**
223    * either field is null or field matches for all of source, version, accession
224    * id and map.
225    */
226   public static DbRefComp matchEitherNonNull = new DbRefComp()
227   {
228     public boolean matches(DBRefEntry refa, DBRefEntry refb)
229     {
230       if ((refa.getSource() == null || refb.getSource() == null)
231               || refb.getSource().equals(refa.getSource()))
232       {
233         if ((refa.getVersion() == null || refb.getVersion() == null)
234                 || refb.getVersion().equals(refa.getVersion()))
235         {
236           if ((refa.getAccessionId() == null || refb.getAccessionId() == null)
237                   || refb.getAccessionId().equals(refa.getAccessionId()))
238           {
239             if ((refa.getMap() == null || refb.getMap() == null)
240                     || (refb.getMap() != null && refb.getMap().equals(
241                             refa.getMap())))
242             {
243               return true;
244             }
245           }
246         }
247       }
248       return false;
249     }
250   };
251
252   /**
253    * accession ID and DB must be identical. Version is ignored. Map is either
254    * not defined or is a match (or is compatible?)
255    */
256   public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
257   {
258     public boolean matches(DBRefEntry refa, DBRefEntry refb)
259     {
260       if (refa.getSource() != null && refb.getSource() != null
261               && refb.getSource().equals(refa.getSource()))
262       {
263         // We dont care about version
264         // if ((refa.getVersion()==null || refb.getVersion()==null)
265         // || refb.getVersion().equals(refa.getVersion()))
266         // {
267         if (refa.getAccessionId() != null && refb.getAccessionId() != null
268                 || refb.getAccessionId().equals(refa.getAccessionId()))
269         {
270           if ((refa.getMap() == null || refb.getMap() == null)
271                   || (refa.getMap() != null && refb.getMap() != null && refb
272                           .getMap().equals(refa.getMap())))
273           {
274             return true;
275           }
276         }
277       }
278       return false;
279     }
280   };
281
282   /**
283    * accession ID and DB must be identical. Version is ignored. No map on either
284    * or map but no maplist on either or maplist of map on a is the complement of
285    * maplist of map on b.
286    */
287   public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
288   {
289     public boolean matches(DBRefEntry refa, DBRefEntry refb)
290     {
291       if (refa.getSource() != null && refb.getSource() != null
292               && refb.getSource().equals(refa.getSource()))
293       {
294         // We dont care about version
295         // if ((refa.getVersion()==null || refb.getVersion()==null)
296         // || refb.getVersion().equals(refa.getVersion()))
297         // {
298         if (refa.getAccessionId() != null && refb.getAccessionId() != null
299                 || refb.getAccessionId().equals(refa.getAccessionId()))
300         {
301           if ((refa.getMap() == null && refb.getMap() == null)
302                   || (refa.getMap() != null && refb.getMap() != null))
303             if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
304                     || (refb.getMap().getMap() != null
305                             && refa.getMap().getMap() != null && refb
306                             .getMap().getMap().getInverse()
307                             .equals(refa.getMap().getMap())))
308             {
309               return true;
310             }
311         }
312       }
313       return false;
314     }
315   };
316
317   /**
318    * accession ID and DB must be identical. Version is ignored. No map on both
319    * or or map but no maplist on either or maplist of map on a is equivalent to
320    * the maplist of map on b.
321    */
322   public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
323   {
324     public boolean matches(DBRefEntry refa, DBRefEntry refb)
325     {
326       if (refa.getSource() != null && refb.getSource() != null
327               && refb.getSource().equals(refa.getSource()))
328       {
329         // We dont care about version
330         // if ((refa.getVersion()==null || refb.getVersion()==null)
331         // || refb.getVersion().equals(refa.getVersion()))
332         // {
333         if (refa.getAccessionId() != null && refb.getAccessionId() != null
334                 || refb.getAccessionId().equals(refa.getAccessionId()))
335         {
336           if (refa.getMap() == null && refb.getMap() == null)
337           {
338             return true;
339           }
340           if (refa.getMap() != null
341                   && refb.getMap() != null
342                   && ((refb.getMap().getMap() == null && refa.getMap()
343                           .getMap() == null) || (refb.getMap().getMap() != null
344                           && refa.getMap().getMap() != null && refb
345                           .getMap().getMap().equals(refa.getMap().getMap()))))
346           {
347             return true;
348           }
349         }
350       }
351       return false;
352     }
353   };
354
355   /**
356    * accession ID and DB must be identical. Version is ignored. No map on either
357    * or map but no maplist on either or maplist of map on a is equivalent to the
358    * maplist of map on b.
359    */
360   public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
361   {
362     public boolean matches(DBRefEntry refa, DBRefEntry refb)
363     {
364       // System.err.println("Comparing A: "+refa.getSrcAccString()+(refa.hasMap()?" has map.":"."));
365       // System.err.println("Comparing B: "+refb.getSrcAccString()+(refb.hasMap()?" has map.":"."));
366       if (refa.getSource() != null && refb.getSource() != null
367               && refb.getSource().equals(refa.getSource()))
368       {
369         // We dont care about version
370         // if ((refa.getVersion()==null || refb.getVersion()==null)
371         // || refb.getVersion().equals(refa.getVersion()))
372         // {
373         if (refa.getAccessionId() != null && refb.getAccessionId() != null
374                 && refb.getAccessionId().equals(refa.getAccessionId()))
375         {
376           if (refa.getMap() == null || refb.getMap() == null)
377           {
378             return true;
379           }
380           if ((refa.getMap() != null && refb.getMap() != null)
381                   && (refb.getMap().getMap() == null && refa.getMap()
382                           .getMap() == null)
383                   || (refb.getMap().getMap() != null
384                           && refa.getMap().getMap() != null && (refb
385                           .getMap().getMap().equals(refa.getMap().getMap()))))
386           { // getMap().getMap().containsEither(false,refa.getMap().getMap())
387             return true;
388           }
389         }
390       }
391       return false;
392     }
393   };
394
395   /**
396    * used by file parsers to generate DBRefs from annotation within file (eg
397    * stockholm)
398    * 
399    * @param dbname
400    * @param version
401    * @param acn
402    * @param seq
403    *          where to anotate with reference
404    * @return parsed version of entry that was added to seq (if any)
405    */
406   public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
407           String version, String acn)
408   {
409     DBRefEntry ref = null;
410     if (dbname != null)
411     {
412       String locsrc = jalview.util.DBRefUtils.getCanonicalName(dbname);
413       if (locsrc.equals(jalview.datamodel.DBRefSource.PDB))
414       {
415         // check for chaincode and mapping
416         // PFAM style stockhom PDB citation
417         com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex(
418                 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;([0-9]+)-([0-9]+)");
419         if (r.search(acn.trim()))
420         {
421           String pdbid = r.stringMatched(1);
422           String chaincode = r.stringMatched(2);
423           String mapstart = r.stringMatched(3);
424           String mapend = r.stringMatched(4);
425           if (chaincode.equals(" "))
426           {
427             chaincode = "_";
428           }
429           // construct pdb ref.
430           ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
431           PDBEntry pdbr = new PDBEntry();
432           pdbr.setId(pdbid);
433           seq.addPDBId(pdbr);
434         }
435       }
436       else
437       {
438         // default:
439         ref = new DBRefEntry(locsrc, version, acn);
440       }
441     }
442     if (ref != null)
443     {
444       seq.addDBRef(ref);
445     }
446     return ref;
447   }
448
449 }