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