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)
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.
330 .getString("error.dbrefsource_implementation_exception"),
333 addDbRefSourceImpl(proxy);
337 * add the properly initialised DbSourceProxy object 'proxy' to the list of
342 protected void addDbRefSourceImpl(DbSourceProxy proxy)
346 if (FETCHABLEDBS == null)
348 FETCHABLEDBS = new Hashtable<String, Map<String, DbSourceProxy>>();
350 Map<String, DbSourceProxy> slist = FETCHABLEDBS.get(proxy
354 FETCHABLEDBS.put(proxy.getDbSource(),
355 slist = new Hashtable<String, DbSourceProxy>());
357 slist.put(proxy.getDbName(), proxy);
362 * test if the database handler for dbName contains the given dbProperty when
363 * a dbName resolves to a set of proxies - this method will return the result
364 * of the test for the first instance. TODO implement additional method to
365 * query all sources for a db to find one with a particular property
369 * @return true if proxy has the given property
371 public boolean hasDbSourceProperty(String dbName, String dbProperty)
373 // TODO: decide if invalidDbName exception is thrown here.
375 List<DbSourceProxy> proxies = getSourceProxy(dbName);
378 for (DbSourceProxy proxy : proxies)
380 if (proxy.getDbSourceProperties() != null)
382 return proxy.getDbSourceProperties().containsKey(dbProperty);
390 * select sources which are implemented by instances of the given class
392 * @param class that implements DbSourceProxy
393 * @return null or vector of source names for fetchers
395 public String[] getDbInstances(Class class1)
397 if (!jalview.ws.seqfetcher.DbSourceProxy.class.isAssignableFrom(class1))
402 "error.implementation_error_dbinstance_must_implement_interface",
403 new String[] { class1.toString() }));
405 if (FETCHABLEDBS == null)
409 String[] sources = null;
410 Vector src = new Vector();
411 Enumeration dbs = FETCHABLEDBS.keys();
412 while (dbs.hasMoreElements())
414 String dbn = (String) dbs.nextElement();
415 for (DbSourceProxy dbp : FETCHABLEDBS.get(dbn).values())
417 if (class1.isAssignableFrom(dbp.getClass()))
425 src.copyInto(sources = new String[src.size()]);
430 public DbSourceProxy[] getDbSourceProxyInstances(Class class1)
432 ArrayList<DbSourceProxy> prlist = new ArrayList<DbSourceProxy>();
433 for (String fetchable : getSupportedDb())
435 for (DbSourceProxy pr : getSourceProxy(fetchable))
437 if (class1.isInstance(pr))
443 if (prlist.size() == 0)
447 return prlist.toArray(new DbSourceProxy[0]);