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
5 * This file is part of Jalview.
\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
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
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
18 package jalview.ws.seqfetcher;
\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
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
34 public class ASequenceFetcher
\r
38 * set of databases we can retrieve entries from
\r
40 protected Hashtable<String, List<DbSourceProxy>> FETCHABLEDBS;
\r
42 public ASequenceFetcher()
\r
48 * get list of supported Databases
\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
53 public String[] getSupportedDb()
\r
55 if (FETCHABLEDBS == null)
\r
57 String[] sf = new String[FETCHABLEDBS.size()];
\r
58 Enumeration e = FETCHABLEDBS.keys();
\r
60 while (e.hasMoreElements())
\r
62 sf[i++] = (String) e.nextElement();
\r
68 public boolean isFetchable(String source)
\r
70 Enumeration e = FETCHABLEDBS.keys();
\r
71 while (e.hasMoreElements())
\r
73 String db = (String) e.nextElement();
\r
74 if (source.compareToIgnoreCase(db) == 0)
\r
77 jalview.bin.Cache.log.warn("isFetchable doesn't know about '" + source
\r
82 public SequenceI[] getSequences(jalview.datamodel.DBRefEntry[] refs)
\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
89 if (!queries.containsKey(refs[r].getSource()))
\r
91 queries.put(refs[r].getSource(), new ArrayList<String>());
\r
93 List<String> qset = queries.get(refs[r].getSource());
\r
94 if (!qset.contains(refs[r].getAccessionId()))
\r
96 qset.add(refs[r].getAccessionId());
\r
99 Enumeration<String> e = queries.keys();
\r
100 while (e.hasMoreElements())
\r
102 List<String> query = null;
\r
104 db = e.nextElement();
\r
105 query = queries.get(db);
\r
106 if (!isFetchable(db))
\r
108 reportStdError(db, query, new Exception(
\r
109 "Don't know how to fetch from this database :" + db));
\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
118 List<String> queriesMade = new ArrayList<String>();
\r
119 HashSet queriesFound = new HashSet<String>();
\r
122 DbSourceProxy fetcher = fetchers.next();
\r
123 boolean doMultiple = fetcher.getAccessionSeparator() != null; // No
\r
128 while (!queriesLeft.isEmpty())
\r
130 StringBuffer qsb = new StringBuffer();
\r
133 if (qsb.length() > 0)
\r
135 qsb.append(fetcher.getAccessionSeparator());
\r
137 String q = queriesLeft.pop();
\r
138 queriesMade.add(q);
\r
140 } while (doMultiple && !queriesLeft.isEmpty());
\r
142 AlignmentI seqset = null;
\r
145 // create a fetcher and go to it
\r
146 seqset = fetcher.getSequenceRecords(qsb.toString()); // ,
\r
148 } catch (Exception ex)
\r
150 System.err.println("Failed to retrieve the following from "
\r
152 System.err.println(qsb);
\r
153 ex.printStackTrace(System.err);
\r
155 // TODO: Merge alignment together - perhaps
\r
156 if (seqset != null)
\r
158 SequenceI seqs[] = seqset.getSequencesArray();
\r
161 for (int is = 0; is < seqs.length; is++)
\r
163 rseqs.addElement(seqs[is]);
\r
164 DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]
\r
165 .getDBRef(), new DBRefEntry(db, null, null));
\r
168 for (DBRefEntry dbr : frefs)
\r
170 queriesFound.add(dbr.getAccessionId());
\r
171 queriesMade.remove(dbr.getAccessionId());
\r
179 if (fetcher.getRawRecords() != null)
\r
181 System.out.println("# Retrieved from " + db + ":"
\r
183 StringBuffer rrb = fetcher.getRawRecords();
\r
185 * for (int rr = 0; rr<rrb.length; rr++) {
\r
188 // if (rr<qs.length)
\r
190 hdr = "# " + db + ":" + qsb.toString();
\r
192 * } else { hdr = "# part "+rr; }
\r
194 System.out.println(hdr);
\r
196 System.out.println(rrb);
\r
197 System.out.println("# end of " + hdr);
\r
204 } catch (Exception ex)
\r
206 reportStdError(db, queriesMade, ex);
\r
208 if (queriesMade.size() > 0)
\r
210 System.out.println("# Adding " + queriesMade.size()
\r
211 + " ids back to queries list for searching again (" + db
\r
213 queriesLeft.addAll(queriesMade);
\r
217 if (rseqs.size() > 0)
\r
219 ret = new SequenceI[rseqs.size()];
\r
220 Enumeration sqs = rseqs.elements();
\r
222 while (sqs.hasMoreElements())
\r
224 SequenceI s = (SequenceI) sqs.nextElement();
\r
232 public void reportStdError(String db, List<String> queriesMade,
\r
236 System.err.println("Failed to retrieve the following references from "
\r
239 for (String qv : queriesMade)
\r
241 System.err.print(" " + qv + ";");
\r
244 System.err.println();
\r
248 System.err.println();
\r
249 ex.printStackTrace();
\r
253 * Retrieve an instance of the proxy for the given source
\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
260 public List<DbSourceProxy> getSourceProxy(String db)
\r
262 List<DbSourceProxy> dbs = FETCHABLEDBS.get(db);
\r
267 * constructs and instance of the proxy and registers it as a valid
\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
276 protected void addDBRefSourceImpl(Class dbSourceProxy)
\r
277 throws java.lang.IllegalArgumentException
\r
279 DbSourceProxy proxy = null;
\r
282 Object proxyObj = dbSourceProxy.getConstructor(null)
\r
283 .newInstance(null);
\r
284 if (!DbSourceProxy.class.isInstance(proxyObj))
\r
286 throw new IllegalArgumentException(
\r
287 dbSourceProxy.toString()
\r
288 + " does not implement the jalview.ws.seqfetcher.DbSourceProxy");
\r
290 proxy = (DbSourceProxy) proxyObj;
\r
291 } catch (IllegalArgumentException e)
\r
294 } catch (Exception e)
\r
296 // Serious problems if this happens.
\r
297 throw new Error("DBRefSource Implementation Exception", e);
\r
299 addDbRefSourceImpl(proxy);
\r
303 * add the properly initialised DbSourceProxy object 'proxy' to the list of
\r
304 * sequence fetchers
\r
308 protected void addDbRefSourceImpl(DbSourceProxy proxy)
\r
312 if (FETCHABLEDBS == null)
\r
314 FETCHABLEDBS = new Hashtable<String, List<DbSourceProxy>>();
\r
316 List<DbSourceProxy> slist = FETCHABLEDBS.get(proxy.getDbSource());
\r
319 FETCHABLEDBS.put(proxy.getDbSource(),
\r
320 slist = new ArrayList<DbSourceProxy>());
\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
331 * @param dbProperty
\r
332 * @return true if proxy has the given property
\r
334 public boolean hasDbSourceProperty(String dbName, String dbProperty)
\r
336 // TODO: decide if invalidDbName exception is thrown here.
\r
338 List<DbSourceProxy> proxies = getSourceProxy(dbName);
\r
339 if (proxies != null)
\r
341 for (DbSourceProxy proxy : proxies)
\r
343 if (proxy.getDbSourceProperties() != null)
\r
345 return proxy.getDbSourceProperties().containsKey(dbProperty);
\r
353 * select sources which are implemented by instances of the given class
\r
355 * @param class that implements DbSourceProxy
\r
356 * @return null or vector of source names for fetchers
\r
358 public String[] getDbInstances(Class class1)
\r
360 if (!jalview.ws.seqfetcher.DbSourceProxy.class.isAssignableFrom(class1))
\r
363 "Implmentation Error - getDbInstances must be given a class that implements jalview.ws.seqfetcher.DbSourceProxy (was given '"
\r
366 if (FETCHABLEDBS == null)
\r
370 String[] sources = null;
\r
371 Vector src = new Vector();
\r
372 Enumeration dbs = FETCHABLEDBS.keys();
\r
373 while (dbs.hasMoreElements())
\r
375 String dbn = (String) dbs.nextElement();
\r
376 for (DbSourceProxy dbp : FETCHABLEDBS.get(dbn))
\r
378 if (class1.isAssignableFrom(dbp.getClass()))
\r
380 src.addElement(dbn);
\r
384 if (src.size() > 0)
\r
386 src.copyInto(sources = new String[src.size()]);
\r