2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.seqfetcher;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.DBRefEntry;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.DBRefUtils;
27 import jalview.util.MessageManager;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31 import java.util.HashSet;
32 import java.util.Hashtable;
33 import java.util.Iterator;
34 import java.util.List;
36 import java.util.Stack;
37 import java.util.Vector;
39 public class ASequenceFetcher
43 * set of databases we can retrieve entries from
45 protected Hashtable<String, Map<String, DbSourceProxy>> FETCHABLEDBS;
47 public ASequenceFetcher()
53 * get list of supported Databases
55 * @return database source string for each database - only the latest version
56 * of a source db is bound to each source.
58 public String[] getSupportedDb()
60 if (FETCHABLEDBS == null)
62 String[] sf = new String[FETCHABLEDBS.size()];
63 Enumeration e = FETCHABLEDBS.keys();
65 while (e.hasMoreElements())
67 sf[i++] = (String) e.nextElement();
73 public boolean isFetchable(String source)
75 Enumeration e = FETCHABLEDBS.keys();
76 while (e.hasMoreElements())
78 String db = (String) e.nextElement();
79 if (source.compareToIgnoreCase(db) == 0)
82 jalview.bin.Cache.log.warn("isFetchable doesn't know about '" + source
87 public SequenceI[] getSequences(jalview.datamodel.DBRefEntry[] refs)
89 SequenceI[] ret = null;
90 Vector<SequenceI> rseqs = new Vector();
91 Hashtable<String, List<String>> queries = new Hashtable();
92 for (int r = 0; r < refs.length; r++)
94 if (!queries.containsKey(refs[r].getSource()))
96 queries.put(refs[r].getSource(), new ArrayList<String>());
98 List<String> qset = queries.get(refs[r].getSource());
99 if (!qset.contains(refs[r].getAccessionId()))
101 qset.add(refs[r].getAccessionId());
104 Enumeration<String> e = queries.keys();
105 while (e.hasMoreElements())
107 List<String> query = null;
109 db = e.nextElement();
110 query = queries.get(db);
111 if (!isFetchable(db))
113 reportStdError(db, query, new Exception(
114 "Don't know how to fetch from this database :" + db));
117 Iterator<DbSourceProxy> fetchers = getSourceProxy(db).iterator();
118 Stack<String> queriesLeft = new Stack<String>();
119 // List<String> queriesFailed = new ArrayList<String>();
120 queriesLeft.addAll(query);
121 while (fetchers.hasNext())
123 List<String> queriesMade = new ArrayList<String>();
124 HashSet queriesFound = new HashSet<String>();
127 DbSourceProxy fetcher = fetchers.next();
128 boolean doMultiple = fetcher.getAccessionSeparator() != null; // No
133 while (!queriesLeft.isEmpty())
135 StringBuffer qsb = new StringBuffer();
138 if (qsb.length() > 0)
140 qsb.append(fetcher.getAccessionSeparator());
142 String q = queriesLeft.pop();
145 } while (doMultiple && !queriesLeft.isEmpty());
147 AlignmentI seqset = null;
150 // create a fetcher and go to it
151 seqset = fetcher.getSequenceRecords(qsb.toString()); // ,
153 } catch (Exception ex)
155 System.err.println("Failed to retrieve the following from "
157 System.err.println(qsb);
158 ex.printStackTrace(System.err);
160 // TODO: Merge alignment together - perhaps
163 SequenceI seqs[] = seqset.getSequencesArray();
166 for (int is = 0; is < seqs.length; is++)
168 rseqs.addElement(seqs[is]);
169 DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]
170 .getDBRef(), new DBRefEntry(db, null, null));
173 for (DBRefEntry dbr : frefs)
175 queriesFound.add(dbr.getAccessionId());
176 queriesMade.remove(dbr.getAccessionId());
184 if (fetcher.getRawRecords() != null)
186 System.out.println("# Retrieved from " + db + ":"
188 StringBuffer rrb = fetcher.getRawRecords();
190 * for (int rr = 0; rr<rrb.length; rr++) {
195 hdr = "# " + db + ":" + qsb.toString();
197 * } else { hdr = "# part "+rr; }
199 System.out.println(hdr);
201 System.out.println(rrb);
202 System.out.println("# end of " + hdr);
209 } catch (Exception ex)
211 reportStdError(db, queriesMade, ex);
213 if (queriesMade.size() > 0)
215 System.out.println("# Adding " + queriesMade.size()
216 + " ids back to queries list for searching again (" + db
218 queriesLeft.addAll(queriesMade);
222 if (rseqs.size() > 0)
224 ret = new SequenceI[rseqs.size()];
225 Enumeration sqs = rseqs.elements();
227 while (sqs.hasMoreElements())
229 SequenceI s = (SequenceI) sqs.nextElement();
237 public void reportStdError(String db, List<String> queriesMade,
241 System.err.println("Failed to retrieve the following references from "
244 for (String qv : queriesMade)
246 System.err.print(" " + qv + ";");
249 System.err.println();
253 System.err.println();
254 ex.printStackTrace();
258 * Retrieve an instance of the proxy for the given source
261 * database source string TODO: add version string/wildcard for
262 * retrieval of specific DB source/version combinations.
263 * @return an instance of DbSourceProxy for that db.
265 public List<DbSourceProxy> getSourceProxy(String db)
267 List<DbSourceProxy> dbs;
268 Map<String, DbSourceProxy> dblist = FETCHABLEDBS.get(db);
271 return new ArrayList<DbSourceProxy>();
274 if (dblist.size() > 1)
276 DbSourceProxy[] l = dblist.values().toArray(new DbSourceProxy[0]);
278 String[] nm = new String[l.length];
279 // make sure standard dbs appear first, followed by reference das sources,
280 // followed by anything else.
281 for (DbSourceProxy s : l)
283 nm[i++] = "" + s.getTier() + s.getDbName().toLowerCase();
285 jalview.util.QuickSort.sort(nm, l);
286 dbs = new ArrayList<DbSourceProxy>();
287 for (i = l.length - 1; i >= 0; i--)
294 dbs = new ArrayList<DbSourceProxy>(dblist.values());
300 * constructs and instance of the proxy and registers it as a valid
303 * @param dbSourceProxy
304 * reference for class implementing
305 * jalview.ws.seqfetcher.DbSourceProxy
306 * @throws java.lang.IllegalArgumentException
307 * if class does not implement jalview.ws.seqfetcher.DbSourceProxy
309 protected void addDBRefSourceImpl(Class dbSourceProxy)
310 throws java.lang.IllegalArgumentException
312 DbSourceProxy proxy = null;
315 Object proxyObj = dbSourceProxy.getConstructor(null)
317 if (!DbSourceProxy.class.isInstance(proxyObj))
319 throw new IllegalArgumentException(
320 dbSourceProxy.toString()
321 + " does not implement the jalview.ws.seqfetcher.DbSourceProxy");
323 proxy = (DbSourceProxy) proxyObj;
324 } catch (IllegalArgumentException e)
327 } catch (Exception e)
329 // Serious problems if this happens.
330 throw new Error(MessageManager.getString("error.dbrefsource_implementation_exception"), e);
332 addDbRefSourceImpl(proxy);
336 * add the properly initialised DbSourceProxy object 'proxy' to the list of
341 protected void addDbRefSourceImpl(DbSourceProxy proxy)
345 if (FETCHABLEDBS == null)
347 FETCHABLEDBS = new Hashtable<String, Map<String, DbSourceProxy>>();
349 Map<String, DbSourceProxy> slist = FETCHABLEDBS.get(proxy
353 FETCHABLEDBS.put(proxy.getDbSource(),
354 slist = new Hashtable<String, DbSourceProxy>());
356 slist.put(proxy.getDbName(), proxy);
361 * test if the database handler for dbName contains the given dbProperty when
362 * a dbName resolves to a set of proxies - this method will return the result
363 * of the test for the first instance. TODO implement additional method to
364 * query all sources for a db to find one with a particular property
368 * @return true if proxy has the given property
370 public boolean hasDbSourceProperty(String dbName, String dbProperty)
372 // TODO: decide if invalidDbName exception is thrown here.
374 List<DbSourceProxy> proxies = getSourceProxy(dbName);
377 for (DbSourceProxy proxy : proxies)
379 if (proxy.getDbSourceProperties() != null)
381 return proxy.getDbSourceProperties().containsKey(dbProperty);
389 * select sources which are implemented by instances of the given class
391 * @param class that implements DbSourceProxy
392 * @return null or vector of source names for fetchers
394 public String[] getDbInstances(Class class1)
396 if (!jalview.ws.seqfetcher.DbSourceProxy.class.isAssignableFrom(class1))
398 throw new Error(MessageManager.formatMessage("error.implementation_error_dbinstance_must_implement_interface", new String[]{class1.toString()}));
400 if (FETCHABLEDBS == null)
404 String[] sources = null;
405 Vector src = new Vector();
406 Enumeration dbs = FETCHABLEDBS.keys();
407 while (dbs.hasMoreElements())
409 String dbn = (String) dbs.nextElement();
410 for (DbSourceProxy dbp : FETCHABLEDBS.get(dbn).values())
412 if (class1.isAssignableFrom(dbp.getClass()))
420 src.copyInto(sources = new String[src.size()]);
425 public DbSourceProxy[] getDbSourceProxyInstances(Class class1)
427 ArrayList<DbSourceProxy> prlist = new ArrayList<DbSourceProxy>();
428 for (String fetchable : getSupportedDb())
429 for (DbSourceProxy pr : getSourceProxy(fetchable))
431 if (class1.isInstance(pr))
436 if (prlist.size() == 0)
440 return prlist.toArray(new DbSourceProxy[0]);