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 java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.HashSet;
26 import java.util.Hashtable;
27 import java.util.Iterator;
28 import java.util.List;
30 import java.util.Stack;
31 import java.util.Vector;
33 import jalview.datamodel.AlignmentI;
34 import jalview.datamodel.DBRefEntry;
35 import jalview.datamodel.SequenceI;
36 import jalview.util.DBRefUtils;
37 import jalview.util.MessageManager;
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)
64 String[] sf = new String[FETCHABLEDBS.size()];
65 Enumeration e = FETCHABLEDBS.keys();
67 while (e.hasMoreElements())
69 sf[i++] = (String) e.nextElement();
75 public boolean isFetchable(String source)
77 Enumeration e = FETCHABLEDBS.keys();
78 while (e.hasMoreElements())
80 String db = (String) e.nextElement();
81 if (source.compareToIgnoreCase(db) == 0)
86 jalview.bin.Cache.log.warn("isFetchable doesn't know about '" + source
91 public SequenceI[] getSequences(jalview.datamodel.DBRefEntry[] refs)
93 SequenceI[] ret = null;
94 Vector<SequenceI> rseqs = new Vector();
95 Hashtable<String, List<String>> queries = new Hashtable();
96 for (int r = 0; r < refs.length; r++)
98 if (!queries.containsKey(refs[r].getSource()))
100 queries.put(refs[r].getSource(), new ArrayList<String>());
102 List<String> qset = queries.get(refs[r].getSource());
103 if (!qset.contains(refs[r].getAccessionId()))
105 qset.add(refs[r].getAccessionId());
108 Enumeration<String> e = queries.keys();
109 while (e.hasMoreElements())
111 List<String> query = null;
113 db = e.nextElement();
114 query = queries.get(db);
115 if (!isFetchable(db))
117 reportStdError(db, query, new Exception(
118 "Don't know how to fetch from this database :" + db));
121 Iterator<DbSourceProxy> fetchers = getSourceProxy(db).iterator();
122 Stack<String> queriesLeft = new Stack<String>();
123 // List<String> queriesFailed = new ArrayList<String>();
124 queriesLeft.addAll(query);
125 while (fetchers.hasNext())
127 List<String> queriesMade = new ArrayList<String>();
128 HashSet queriesFound = new HashSet<String>();
131 DbSourceProxy fetcher = fetchers.next();
132 boolean doMultiple = fetcher.getAccessionSeparator() != null; // No
137 while (!queriesLeft.isEmpty())
139 StringBuffer qsb = new StringBuffer();
142 if (qsb.length() > 0)
144 qsb.append(fetcher.getAccessionSeparator());
146 String q = queriesLeft.pop();
149 } while (doMultiple && !queriesLeft.isEmpty());
151 AlignmentI seqset = null;
154 // create a fetcher and go to it
155 seqset = fetcher.getSequenceRecords(qsb.toString()); // ,
157 } catch (Exception ex)
159 System.err.println("Failed to retrieve the following from "
161 System.err.println(qsb);
162 ex.printStackTrace(System.err);
164 // TODO: Merge alignment together - perhaps
167 SequenceI seqs[] = seqset.getSequencesArray();
170 for (int is = 0; is < seqs.length; is++)
172 rseqs.addElement(seqs[is]);
173 DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]
174 .getDBRef(), new DBRefEntry(db, null, null));
177 for (DBRefEntry dbr : frefs)
179 queriesFound.add(dbr.getAccessionId());
180 queriesMade.remove(dbr.getAccessionId());
188 if (fetcher.getRawRecords() != null)
190 System.out.println("# Retrieved from " + db + ":"
192 StringBuffer rrb = fetcher.getRawRecords();
194 * for (int rr = 0; rr<rrb.length; rr++) {
199 hdr = "# " + db + ":" + qsb.toString();
201 * } else { hdr = "# part "+rr; }
203 System.out.println(hdr);
206 System.out.println(rrb);
208 System.out.println("# end of " + hdr);
215 } catch (Exception ex)
217 reportStdError(db, queriesMade, ex);
219 if (queriesMade.size() > 0)
221 System.out.println("# Adding " + queriesMade.size()
222 + " ids back to queries list for searching again (" + db
224 queriesLeft.addAll(queriesMade);
228 if (rseqs.size() > 0)
230 ret = new SequenceI[rseqs.size()];
231 Enumeration sqs = rseqs.elements();
233 while (sqs.hasMoreElements())
235 SequenceI s = (SequenceI) sqs.nextElement();
243 public void reportStdError(String db, List<String> queriesMade,
247 System.err.println("Failed to retrieve the following references from "
250 for (String qv : queriesMade)
252 System.err.print(" " + qv + ";");
255 System.err.println();
259 System.err.println();
260 ex.printStackTrace();
264 * Retrieve an instance of the proxy for the given source
267 * database source string TODO: add version string/wildcard for
268 * retrieval of specific DB source/version combinations.
269 * @return an instance of DbSourceProxy for that db.
271 public List<DbSourceProxy> getSourceProxy(String db)
273 List<DbSourceProxy> dbs;
274 Map<String, DbSourceProxy> dblist = FETCHABLEDBS.get(db);
277 return new ArrayList<DbSourceProxy>();
280 if (dblist.size() > 1)
282 DbSourceProxy[] l = dblist.values().toArray(new DbSourceProxy[0]);
284 String[] nm = new String[l.length];
285 // make sure standard dbs appear first, followed by reference das sources,
286 // followed by anything else.
287 for (DbSourceProxy s : l)
289 nm[i++] = "" + s.getTier() + s.getDbName().toLowerCase();
291 jalview.util.QuickSort.sort(nm, l);
292 dbs = new ArrayList<DbSourceProxy>();
293 for (i = l.length - 1; i >= 0; i--)
300 dbs = new ArrayList<DbSourceProxy>(dblist.values());
306 * constructs and instance of the proxy and registers it as a valid
309 * @param dbSourceProxy
310 * reference for class implementing
311 * jalview.ws.seqfetcher.DbSourceProxy
313 protected void addDBRefSourceImpl(
314 Class<? extends DbSourceProxy> dbSourceProxy)
315 throws java.lang.IllegalArgumentException
317 DbSourceProxy proxy = null;
320 DbSourceProxy proxyObj = dbSourceProxy.getConstructor().newInstance();
322 } catch (IllegalArgumentException e)
325 } catch (Exception e)
327 // Serious problems if this happens.
328 throw new Error(MessageManager.getString("error.dbrefsource_implementation_exception"), e);
330 addDbRefSourceImpl(proxy);
334 * add the properly initialised DbSourceProxy object 'proxy' to the list of
339 protected void addDbRefSourceImpl(DbSourceProxy proxy)
343 if (FETCHABLEDBS == null)
345 FETCHABLEDBS = new Hashtable<String, Map<String, DbSourceProxy>>();
347 Map<String, DbSourceProxy> slist = FETCHABLEDBS.get(proxy
351 FETCHABLEDBS.put(proxy.getDbSource(),
352 slist = new Hashtable<String, DbSourceProxy>());
354 slist.put(proxy.getDbName(), proxy);
359 * test if the database handler for dbName contains the given dbProperty when
360 * a dbName resolves to a set of proxies - this method will return the result
361 * of the test for the first instance. TODO implement additional method to
362 * query all sources for a db to find one with a particular property
366 * @return true if proxy has the given property
368 public boolean hasDbSourceProperty(String dbName, String dbProperty)
370 // TODO: decide if invalidDbName exception is thrown here.
372 List<DbSourceProxy> proxies = getSourceProxy(dbName);
375 for (DbSourceProxy proxy : proxies)
377 if (proxy.getDbSourceProperties() != null)
379 return proxy.getDbSourceProperties().containsKey(dbProperty);
387 * select sources which are implemented by instances of the given class
389 * @param class that implements DbSourceProxy
390 * @return null or vector of source names for fetchers
392 public String[] getDbInstances(Class class1)
394 if (!jalview.ws.seqfetcher.DbSourceProxy.class.isAssignableFrom(class1))
396 throw new Error(MessageManager.formatMessage("error.implementation_error_dbinstance_must_implement_interface", new String[]{class1.toString()}));
398 if (FETCHABLEDBS == null)
402 String[] sources = null;
403 Vector src = new Vector();
404 Enumeration dbs = FETCHABLEDBS.keys();
405 while (dbs.hasMoreElements())
407 String dbn = (String) dbs.nextElement();
408 for (DbSourceProxy dbp : FETCHABLEDBS.get(dbn).values())
410 if (class1.isAssignableFrom(dbp.getClass()))
418 src.copyInto(sources = new String[src.size()]);
423 public DbSourceProxy[] getDbSourceProxyInstances(Class class1)
425 ArrayList<DbSourceProxy> prlist = new ArrayList<DbSourceProxy>();
426 for (String fetchable : getSupportedDb())
428 for (DbSourceProxy pr : getSourceProxy(fetchable))
430 if (class1.isInstance(pr))
436 if (prlist.size() == 0)
440 return prlist.toArray(new DbSourceProxy[0]);