JAL-1064 - refactor to iterate over many sources associated with a database authority
[jalview.git] / src / jalview / ws / seqfetcher / ASequenceFetcher.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
4  * \r
5  * This file is part of Jalview.\r
6  * \r
7  * Jalview is free software: you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License \r
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
10  * \r
11  * Jalview is distributed in the hope that it will be useful, but \r
12  * WITHOUT ANY WARRANTY; without even the implied warranty \r
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
14  * PURPOSE.  See the GNU General Public License for more details.\r
15  * \r
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
17  */\r
18 package jalview.ws.seqfetcher;\r
19 \r
20 import jalview.datamodel.AlignmentI;\r
21 import jalview.datamodel.DBRefEntry;\r
22 import jalview.datamodel.SequenceI;\r
23 import jalview.util.DBRefUtils;\r
24 \r
25 import java.util.ArrayList;\r
26 import java.util.Enumeration;\r
27 import java.util.HashSet;\r
28 import java.util.Hashtable;\r
29 import java.util.Iterator;\r
30 import java.util.List;\r
31 import java.util.Stack;\r
32 import java.util.Vector;\r
33 \r
34 public class ASequenceFetcher\r
35 {\r
36 \r
37   /**\r
38    * set of databases we can retrieve entries from\r
39    */\r
40   protected Hashtable<String, List<DbSourceProxy>> FETCHABLEDBS;\r
41 \r
42   public ASequenceFetcher()\r
43   {\r
44     super();\r
45   }\r
46 \r
47   /**\r
48    * get list of supported Databases\r
49    * \r
50    * @return database source string for each database - only the latest version\r
51    *         of a source db is bound to each source.\r
52    */\r
53   public String[] getSupportedDb()\r
54   {\r
55     if (FETCHABLEDBS == null)\r
56       return null;\r
57     String[] sf = new String[FETCHABLEDBS.size()];\r
58     Enumeration e = FETCHABLEDBS.keys();\r
59     int i = 0;\r
60     while (e.hasMoreElements())\r
61     {\r
62       sf[i++] = (String) e.nextElement();\r
63     }\r
64     ;\r
65     return sf;\r
66   }\r
67 \r
68   public boolean isFetchable(String source)\r
69   {\r
70     Enumeration e = FETCHABLEDBS.keys();\r
71     while (e.hasMoreElements())\r
72     {\r
73       String db = (String) e.nextElement();\r
74       if (source.compareToIgnoreCase(db) == 0)\r
75         return true;\r
76     }\r
77     jalview.bin.Cache.log.warn("isFetchable doesn't know about '" + source\r
78             + "'");\r
79     return false;\r
80   }\r
81 \r
82   public SequenceI[] getSequences(jalview.datamodel.DBRefEntry[] refs)\r
83   {\r
84     SequenceI[] ret = null;\r
85     Vector<SequenceI> rseqs = new Vector();\r
86     Hashtable<String, List<String>> queries = new Hashtable();\r
87     for (int r = 0; r < refs.length; r++)\r
88     {\r
89       if (!queries.containsKey(refs[r].getSource()))\r
90       {\r
91         queries.put(refs[r].getSource(), new ArrayList<String>());\r
92       }\r
93       List<String> qset = queries.get(refs[r].getSource());\r
94       if (!qset.contains(refs[r].getAccessionId()))\r
95       {\r
96         qset.add(refs[r].getAccessionId());\r
97       }\r
98     }\r
99     Enumeration<String> e = queries.keys();\r
100     while (e.hasMoreElements())\r
101     {\r
102       List<String> query = null;\r
103       String db = null;\r
104       db = e.nextElement();\r
105       query = queries.get(db);\r
106       if (!isFetchable(db))\r
107       {\r
108         reportStdError(db, query, new Exception(\r
109                 "Don't know how to fetch from this database :" + db));\r
110         continue;\r
111       }\r
112       Iterator<DbSourceProxy> fetchers = getSourceProxy(db).iterator();\r
113       Stack<String> queriesLeft = new Stack<String>();\r
114 //      List<String> queriesFailed = new ArrayList<String>();\r
115       queriesLeft.addAll(query);\r
116       while (fetchers.hasNext())\r
117       {\r
118         List<String> queriesMade = new ArrayList<String>();\r
119         HashSet queriesFound = new HashSet<String>();\r
120         try\r
121         {\r
122           DbSourceProxy fetcher = fetchers.next();\r
123           boolean doMultiple = fetcher.getAccessionSeparator() != null; // No\r
124           // separator\r
125           // - no\r
126           // Multiple\r
127           // Queries\r
128           while (!queriesLeft.isEmpty())\r
129           {\r
130             StringBuffer qsb = new StringBuffer();\r
131             do\r
132             {\r
133               if (qsb.length() > 0)\r
134               {\r
135                 qsb.append(fetcher.getAccessionSeparator());\r
136               }\r
137               String q = queriesLeft.pop();\r
138               queriesMade.add(q);\r
139               qsb.append(q);\r
140             } while (doMultiple && !queriesLeft.isEmpty());\r
141 \r
142             AlignmentI seqset = null;\r
143             try\r
144             {\r
145               // create a fetcher and go to it\r
146               seqset = fetcher.getSequenceRecords(qsb.toString()); // ,\r
147                       // queriesFailed);\r
148             } catch (Exception ex)\r
149             {\r
150               System.err.println("Failed to retrieve the following from "\r
151                       + db);\r
152               System.err.println(qsb);\r
153               ex.printStackTrace(System.err);\r
154             }\r
155             // TODO: Merge alignment together - perhaps\r
156             if (seqset != null)\r
157             {\r
158               SequenceI seqs[] = seqset.getSequencesArray();\r
159               if (seqs != null)\r
160               {\r
161                 for (int is = 0; is < seqs.length; is++)\r
162                 {\r
163                   rseqs.addElement(seqs[is]);\r
164                   DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]\r
165                           .getDBRef(), new DBRefEntry(db, null, null));\r
166                   if (frefs != null)\r
167                   {\r
168                     for (DBRefEntry dbr : frefs)\r
169                     {\r
170                       queriesFound.add(dbr.getAccessionId());\r
171                       queriesMade.remove(dbr.getAccessionId());\r
172                     }\r
173                   }\r
174                   seqs[is] = null;\r
175                 }\r
176               }\r
177               else\r
178               {\r
179                 if (fetcher.getRawRecords() != null)\r
180                 {\r
181                   System.out.println("# Retrieved from " + db + ":"\r
182                           + qsb.toString());\r
183                   StringBuffer rrb = fetcher.getRawRecords();\r
184                   /*\r
185                    * for (int rr = 0; rr<rrb.length; rr++) {\r
186                    */\r
187                   String hdr;\r
188                   // if (rr<qs.length)\r
189                   // {\r
190                   hdr = "# " + db + ":" + qsb.toString();\r
191                   /*\r
192                    * } else { hdr = "# part "+rr; }\r
193                    */\r
194                   System.out.println(hdr);\r
195                   if (rrb != null)\r
196                     System.out.println(rrb);\r
197                   System.out.println("# end of " + hdr);\r
198                 }\r
199 \r
200               }\r
201             }\r
202 \r
203           }\r
204         } catch (Exception ex)\r
205         {\r
206           reportStdError(db, queriesMade, ex);\r
207         }\r
208         if (queriesMade.size() > 0)\r
209         {\r
210           System.out.println("# Adding " + queriesMade.size()\r
211                   + " ids back to queries list for searching again (" + db\r
212                   + ".");\r
213           queriesLeft.addAll(queriesMade);\r
214         }\r
215       }\r
216     }\r
217     if (rseqs.size() > 0)\r
218     {\r
219       ret = new SequenceI[rseqs.size()];\r
220       Enumeration sqs = rseqs.elements();\r
221       int si = 0;\r
222       while (sqs.hasMoreElements())\r
223       {\r
224         SequenceI s = (SequenceI) sqs.nextElement();\r
225         ret[si++] = s;\r
226         s.updatePDBIds();\r
227       }\r
228     }\r
229     return ret;\r
230   }\r
231 \r
232   public void reportStdError(String db, List<String> queriesMade,\r
233           Exception ex)\r
234   {\r
235 \r
236     System.err.println("Failed to retrieve the following references from "\r
237             + db);\r
238     int n = 0;\r
239     for (String qv : queriesMade)\r
240     {\r
241       System.err.print(" " + qv + ";");\r
242       if (n++ > 10)\r
243       {\r
244         System.err.println();\r
245         n = 0;\r
246       }\r
247     }\r
248     System.err.println();\r
249     ex.printStackTrace();\r
250   }\r
251 \r
252   /**\r
253    * Retrieve an instance of the proxy for the given source\r
254    * \r
255    * @param db\r
256    *          database source string TODO: add version string/wildcard for\r
257    *          retrieval of specific DB source/version combinations.\r
258    * @return an instance of DbSourceProxy for that db.\r
259    */\r
260   public List<DbSourceProxy> getSourceProxy(String db)\r
261   {\r
262     List<DbSourceProxy> dbs = FETCHABLEDBS.get(db);\r
263     return dbs;\r
264   }\r
265 \r
266   /**\r
267    * constructs and instance of the proxy and registers it as a valid\r
268    * dbrefsource\r
269    * \r
270    * @param dbSourceProxy\r
271    *          reference for class implementing\r
272    *          jalview.ws.seqfetcher.DbSourceProxy\r
273    * @throws java.lang.IllegalArgumentException\r
274    *           if class does not implement jalview.ws.seqfetcher.DbSourceProxy\r
275    */\r
276   protected void addDBRefSourceImpl(Class dbSourceProxy)\r
277           throws java.lang.IllegalArgumentException\r
278   {\r
279     DbSourceProxy proxy = null;\r
280     try\r
281     {\r
282       Object proxyObj = dbSourceProxy.getConstructor(null)\r
283               .newInstance(null);\r
284       if (!DbSourceProxy.class.isInstance(proxyObj))\r
285       {\r
286         throw new IllegalArgumentException(\r
287                 dbSourceProxy.toString()\r
288                         + " does not implement the jalview.ws.seqfetcher.DbSourceProxy");\r
289       }\r
290       proxy = (DbSourceProxy) proxyObj;\r
291     } catch (IllegalArgumentException e)\r
292     {\r
293       throw e;\r
294     } catch (Exception e)\r
295     {\r
296       // Serious problems if this happens.\r
297       throw new Error("DBRefSource Implementation Exception", e);\r
298     }\r
299     addDbRefSourceImpl(proxy);\r
300   }\r
301 \r
302   /**\r
303    * add the properly initialised DbSourceProxy object 'proxy' to the list of\r
304    * sequence fetchers\r
305    * \r
306    * @param proxy\r
307    */\r
308   protected void addDbRefSourceImpl(DbSourceProxy proxy)\r
309   {\r
310     if (proxy != null)\r
311     {\r
312       if (FETCHABLEDBS == null)\r
313       {\r
314         FETCHABLEDBS = new Hashtable<String, List<DbSourceProxy>>();\r
315       }\r
316       List<DbSourceProxy> slist = FETCHABLEDBS.get(proxy.getDbSource());\r
317       if (slist == null)\r
318       {\r
319         FETCHABLEDBS.put(proxy.getDbSource(),\r
320                 slist = new ArrayList<DbSourceProxy>());\r
321       }\r
322       slist.add(proxy);\r
323     }\r
324   }\r
325 \r
326   /**\r
327    * test if the database handler for dbName contains the given dbProperty\r
328    * when a dbName resolves to a set of proxies - this method will return the result of the test for the first instance.\r
329    * TODO implement additional method to query all sources for a db to find one with a particular property\r
330    * @param dbName\r
331    * @param dbProperty\r
332    * @return true if proxy has the given property\r
333    */\r
334   public boolean hasDbSourceProperty(String dbName, String dbProperty)\r
335   {\r
336     // TODO: decide if invalidDbName exception is thrown here.\r
337 \r
338     List<DbSourceProxy> proxies = getSourceProxy(dbName);\r
339     if (proxies != null)\r
340     {\r
341       for (DbSourceProxy proxy : proxies)\r
342       {\r
343         if (proxy.getDbSourceProperties() != null)\r
344         {\r
345           return proxy.getDbSourceProperties().containsKey(dbProperty);\r
346         }\r
347       }\r
348     }\r
349     return false;\r
350   }\r
351 \r
352   /**\r
353    * select sources which are implemented by instances of the given class\r
354    * \r
355    * @param class that implements DbSourceProxy\r
356    * @return null or vector of source names for fetchers\r
357    */\r
358   public String[] getDbInstances(Class class1)\r
359   {\r
360     if (!jalview.ws.seqfetcher.DbSourceProxy.class.isAssignableFrom(class1))\r
361     {\r
362       throw new Error(\r
363               "Implmentation Error - getDbInstances must be given a class that implements jalview.ws.seqfetcher.DbSourceProxy (was given '"\r
364                       + class1 + "')");\r
365     }\r
366     if (FETCHABLEDBS == null)\r
367     {\r
368       return null;\r
369     }\r
370     String[] sources = null;\r
371     Vector src = new Vector();\r
372     Enumeration dbs = FETCHABLEDBS.keys();\r
373     while (dbs.hasMoreElements())\r
374     {\r
375       String dbn = (String) dbs.nextElement();\r
376       for (DbSourceProxy dbp : FETCHABLEDBS.get(dbn))\r
377       {\r
378         if (class1.isAssignableFrom(dbp.getClass()))\r
379         {\r
380           src.addElement(dbn);\r
381         }\r
382       }\r
383     }\r
384     if (src.size() > 0)\r
385     {\r
386       src.copyInto(sources = new String[src.size()]);\r
387     }\r
388     return sources;\r
389   }\r
390 }\r