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.bin.Cache;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.DBRefUtils;
28 import jalview.util.MessageManager;
29 import jalview.util.QuickSort;
31 import java.util.ArrayList;
32 import java.util.Enumeration;
33 import java.util.HashSet;
34 import java.util.Hashtable;
35 import java.util.Iterator;
36 import java.util.List;
38 import java.util.Stack;
39 import java.util.Vector;
41 public class ASequenceFetcher
45 * set of databases we can retrieve entries from
47 protected Hashtable<String, Map<String, DbSourceProxy>> FETCHABLEDBS;
49 public ASequenceFetcher()
55 * get list of supported Databases
57 * @return database source string for each database - only the latest version
58 * of a source db is bound to each source.
60 public String[] getSupportedDb()
62 if (FETCHABLEDBS == null)
66 String[] sf = new String[FETCHABLEDBS.size()];
67 Enumeration e = FETCHABLEDBS.keys();
69 while (e.hasMoreElements())
71 sf[i++] = (String) e.nextElement();
77 public boolean isFetchable(String source)
79 Enumeration e = FETCHABLEDBS.keys();
80 while (e.hasMoreElements())
82 String db = (String) e.nextElement();
83 if (source.compareToIgnoreCase(db) == 0)
88 Cache.log.warn("isFetchable doesn't know about '" + source
93 public SequenceI[] getSequences(DBRefEntry[] refs)
95 SequenceI[] ret = null;
96 Vector<SequenceI> rseqs = new Vector();
97 Hashtable<String, List<String>> queries = new Hashtable();
98 for (int r = 0; r < refs.length; r++)
100 if (!queries.containsKey(refs[r].getSource()))
102 queries.put(refs[r].getSource(), new ArrayList<String>());
104 List<String> qset = queries.get(refs[r].getSource());
105 if (!qset.contains(refs[r].getAccessionId()))
107 qset.add(refs[r].getAccessionId());
110 Enumeration<String> e = queries.keys();
111 while (e.hasMoreElements())
113 List<String> query = null;
115 db = e.nextElement();
116 query = queries.get(db);
117 if (!isFetchable(db))
119 reportStdError(db, query, new Exception(
120 "Don't know how to fetch from this database :" + db));
123 Iterator<DbSourceProxy> fetchers = getSourceProxy(db).iterator();
124 Stack<String> queriesLeft = new Stack<String>();
125 // List<String> queriesFailed = new ArrayList<String>();
126 queriesLeft.addAll(query);
127 while (fetchers.hasNext())
129 List<String> queriesMade = new ArrayList<String>();
130 HashSet queriesFound = new HashSet<String>();
133 DbSourceProxy fetcher = fetchers.next();
134 boolean doMultiple = fetcher.getAccessionSeparator() != null; // No
139 while (!queriesLeft.isEmpty())
141 StringBuffer qsb = new StringBuffer();
144 if (qsb.length() > 0)
146 qsb.append(fetcher.getAccessionSeparator());
148 String q = queriesLeft.pop();
151 } while (doMultiple && !queriesLeft.isEmpty());
153 AlignmentI seqset = null;
156 // create a fetcher and go to it
157 seqset = fetcher.getSequenceRecords(qsb.toString()); // ,
159 } catch (Exception ex)
161 System.err.println("Failed to retrieve the following from "
163 System.err.println(qsb);
164 ex.printStackTrace(System.err);
166 // TODO: Merge alignment together - perhaps
169 SequenceI seqs[] = seqset.getSequencesArray();
172 for (int is = 0; is < seqs.length; is++)
174 rseqs.addElement(seqs[is]);
175 DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]
176 .getDBRef(), new DBRefEntry(db, null, null));
179 for (DBRefEntry dbr : frefs)
181 queriesFound.add(dbr.getAccessionId());
182 queriesMade.remove(dbr.getAccessionId());
190 if (fetcher.getRawRecords() != null)
192 System.out.println("# Retrieved from " + db + ":"
194 StringBuffer rrb = fetcher.getRawRecords();
196 * for (int rr = 0; rr<rrb.length; rr++) {
201 hdr = "# " + db + ":" + qsb.toString();
203 * } else { hdr = "# part "+rr; }
205 System.out.println(hdr);
208 System.out.println(rrb);
210 System.out.println("# end of " + hdr);
217 } catch (Exception ex)
219 reportStdError(db, queriesMade, ex);
221 if (queriesMade.size() > 0)
223 System.out.println("# Adding " + queriesMade.size()
224 + " ids back to queries list for searching again (" + db
226 queriesLeft.addAll(queriesMade);
230 if (rseqs.size() > 0)
232 ret = new SequenceI[rseqs.size()];
233 Enumeration sqs = rseqs.elements();
235 while (sqs.hasMoreElements())
237 SequenceI s = (SequenceI) sqs.nextElement();
245 public void reportStdError(String db, List<String> queriesMade,
249 System.err.println("Failed to retrieve the following references from "
252 for (String qv : queriesMade)
254 System.err.print(" " + qv + ";");
257 System.err.println();
261 System.err.println();
262 ex.printStackTrace();
266 * Retrieve an instance of the proxy for the given source
269 * database source string TODO: add version string/wildcard for
270 * retrieval of specific DB source/version combinations.
271 * @return an instance of DbSourceProxy for that db.
273 public List<DbSourceProxy> getSourceProxy(String db)
275 List<DbSourceProxy> dbs;
276 Map<String, DbSourceProxy> dblist = FETCHABLEDBS.get(db);
279 return new ArrayList<DbSourceProxy>();
282 if (dblist.size() > 1)
284 DbSourceProxy[] l = dblist.values().toArray(new DbSourceProxy[0]);
286 String[] nm = new String[l.length];
287 // make sure standard dbs appear first, followed by reference das sources,
288 // followed by anything else.
289 for (DbSourceProxy s : l)
291 nm[i++] = "" + s.getTier() + s.getDbName().toLowerCase();
293 QuickSort.sort(nm, l);
294 dbs = new ArrayList<DbSourceProxy>();
295 for (i = l.length - 1; i >= 0; i--)
302 dbs = new ArrayList<DbSourceProxy>(dblist.values());
308 * constructs and instance of the proxy and registers it as a valid
311 * @param dbSourceProxy
312 * reference for class implementing
313 * jalview.ws.seqfetcher.DbSourceProxy
315 protected void addDBRefSourceImpl(
316 Class<? extends DbSourceProxy> dbSourceProxy)
317 throws java.lang.IllegalArgumentException
319 DbSourceProxy proxy = null;
322 DbSourceProxy proxyObj = dbSourceProxy.getConstructor().newInstance();
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 (!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())
430 for (DbSourceProxy pr : getSourceProxy(fetchable))
432 if (class1.isInstance(pr))
438 if (prlist.size() == 0)
442 return prlist.toArray(new DbSourceProxy[0]);