2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.ws.seqfetcher;
21 import jalview.datamodel.AlignmentI;
22 import jalview.datamodel.DBRefEntry;
23 import jalview.datamodel.SequenceI;
24 import jalview.util.DBRefUtils;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.HashSet;
29 import java.util.Hashtable;
30 import java.util.Iterator;
31 import java.util.List;
33 import java.util.Stack;
34 import java.util.Vector;
36 public class ASequenceFetcher
40 * set of databases we can retrieve entries from
42 protected Hashtable<String, Map<String, DbSourceProxy>> FETCHABLEDBS;
44 public ASequenceFetcher()
50 * get list of supported Databases
52 * @return database source string for each database - only the latest version
53 * of a source db is bound to each source.
55 public String[] getSupportedDb()
57 if (FETCHABLEDBS == null)
59 String[] sf = new String[FETCHABLEDBS.size()];
60 Enumeration e = FETCHABLEDBS.keys();
62 while (e.hasMoreElements())
64 sf[i++] = (String) e.nextElement();
70 public boolean isFetchable(String source)
72 Enumeration e = FETCHABLEDBS.keys();
73 while (e.hasMoreElements())
75 String db = (String) e.nextElement();
76 if (source.compareToIgnoreCase(db) == 0)
79 jalview.bin.Cache.log.warn("isFetchable doesn't know about '" + source
84 public SequenceI[] getSequences(jalview.datamodel.DBRefEntry[] refs)
86 SequenceI[] ret = null;
87 Vector<SequenceI> rseqs = new Vector();
88 Hashtable<String, List<String>> queries = new Hashtable();
89 for (int r = 0; r < refs.length; r++)
91 if (!queries.containsKey(refs[r].getSource()))
93 queries.put(refs[r].getSource(), new ArrayList<String>());
95 List<String> qset = queries.get(refs[r].getSource());
96 if (!qset.contains(refs[r].getAccessionId()))
98 qset.add(refs[r].getAccessionId());
101 Enumeration<String> e = queries.keys();
102 while (e.hasMoreElements())
104 List<String> query = null;
106 db = e.nextElement();
107 query = queries.get(db);
108 if (!isFetchable(db))
110 reportStdError(db, query, new Exception(
111 "Don't know how to fetch from this database :" + db));
114 Iterator<DbSourceProxy> fetchers = getSourceProxy(db).iterator();
115 Stack<String> queriesLeft = new Stack<String>();
116 // List<String> queriesFailed = new ArrayList<String>();
117 queriesLeft.addAll(query);
118 while (fetchers.hasNext())
120 List<String> queriesMade = new ArrayList<String>();
121 HashSet queriesFound = new HashSet<String>();
124 DbSourceProxy fetcher = fetchers.next();
125 boolean doMultiple = fetcher.getAccessionSeparator() != null; // No
130 while (!queriesLeft.isEmpty())
132 StringBuffer qsb = new StringBuffer();
135 if (qsb.length() > 0)
137 qsb.append(fetcher.getAccessionSeparator());
139 String q = queriesLeft.pop();
142 } while (doMultiple && !queriesLeft.isEmpty());
144 AlignmentI seqset = null;
147 // create a fetcher and go to it
148 seqset = fetcher.getSequenceRecords(qsb.toString()); // ,
150 } catch (Exception ex)
152 System.err.println("Failed to retrieve the following from "
154 System.err.println(qsb);
155 ex.printStackTrace(System.err);
157 // TODO: Merge alignment together - perhaps
160 SequenceI seqs[] = seqset.getSequencesArray();
163 for (int is = 0; is < seqs.length; is++)
165 rseqs.addElement(seqs[is]);
166 DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]
167 .getDBRef(), new DBRefEntry(db, null, null));
170 for (DBRefEntry dbr : frefs)
172 queriesFound.add(dbr.getAccessionId());
173 queriesMade.remove(dbr.getAccessionId());
181 if (fetcher.getRawRecords() != null)
183 System.out.println("# Retrieved from " + db + ":"
185 StringBuffer rrb = fetcher.getRawRecords();
187 * for (int rr = 0; rr<rrb.length; rr++) {
192 hdr = "# " + db + ":" + qsb.toString();
194 * } else { hdr = "# part "+rr; }
196 System.out.println(hdr);
198 System.out.println(rrb);
199 System.out.println("# end of " + hdr);
206 } catch (Exception ex)
208 reportStdError(db, queriesMade, ex);
210 if (queriesMade.size() > 0)
212 System.out.println("# Adding " + queriesMade.size()
213 + " ids back to queries list for searching again (" + db
215 queriesLeft.addAll(queriesMade);
219 if (rseqs.size() > 0)
221 ret = new SequenceI[rseqs.size()];
222 Enumeration sqs = rseqs.elements();
224 while (sqs.hasMoreElements())
226 SequenceI s = (SequenceI) sqs.nextElement();
234 public void reportStdError(String db, List<String> queriesMade,
238 System.err.println("Failed to retrieve the following references from "
241 for (String qv : queriesMade)
243 System.err.print(" " + qv + ";");
246 System.err.println();
250 System.err.println();
251 ex.printStackTrace();
255 * Retrieve an instance of the proxy for the given source
258 * database source string TODO: add version string/wildcard for
259 * retrieval of specific DB source/version combinations.
260 * @return an instance of DbSourceProxy for that db.
262 public List<DbSourceProxy> getSourceProxy(String db)
264 List<DbSourceProxy> dbs;
265 Map<String, DbSourceProxy> dblist = FETCHABLEDBS.get(db);
268 return new ArrayList<DbSourceProxy>();
271 if (dblist.size() > 1)
273 DbSourceProxy[] l = dblist.values().toArray(new DbSourceProxy[0]);
275 String[] nm = new String[l.length];
276 // make sure standard dbs appear first, followed by reference das sources, followed by anything else.
277 for (DbSourceProxy s : l)
279 nm[i++] = ""+s.getTier()+s.getDbName().toLowerCase();
281 jalview.util.QuickSort.sort(nm, l);
282 dbs = new ArrayList<DbSourceProxy>();
283 for (i = l.length - 1; i >= 0; i--)
290 dbs = new ArrayList<DbSourceProxy>(dblist.values());
296 * constructs and instance of the proxy and registers it as a valid
299 * @param dbSourceProxy
300 * reference for class implementing
301 * jalview.ws.seqfetcher.DbSourceProxy
302 * @throws java.lang.IllegalArgumentException
303 * if class does not implement jalview.ws.seqfetcher.DbSourceProxy
305 protected void addDBRefSourceImpl(Class dbSourceProxy)
306 throws java.lang.IllegalArgumentException
308 DbSourceProxy proxy = null;
311 Object proxyObj = dbSourceProxy.getConstructor(null)
313 if (!DbSourceProxy.class.isInstance(proxyObj))
315 throw new IllegalArgumentException(
316 dbSourceProxy.toString()
317 + " does not implement the jalview.ws.seqfetcher.DbSourceProxy");
319 proxy = (DbSourceProxy) proxyObj;
320 } catch (IllegalArgumentException e)
323 } catch (Exception e)
325 // Serious problems if this happens.
326 throw new Error("DBRefSource Implementation Exception", e);
328 addDbRefSourceImpl(proxy);
332 * add the properly initialised DbSourceProxy object 'proxy' to the list of
337 protected void addDbRefSourceImpl(DbSourceProxy proxy)
341 if (FETCHABLEDBS == null)
343 FETCHABLEDBS = new Hashtable<String, Map<String, DbSourceProxy>>();
345 Map<String, DbSourceProxy> slist = FETCHABLEDBS.get(proxy
349 FETCHABLEDBS.put(proxy.getDbSource(),
350 slist = new Hashtable<String, DbSourceProxy>());
352 slist.put(proxy.getDbName(), proxy);
357 * test if the database handler for dbName contains the given dbProperty when
358 * a dbName resolves to a set of proxies - this method will return the result
359 * of the test for the first instance. TODO implement additional method to
360 * query all sources for a db to find one with a particular property
364 * @return true if proxy has the given property
366 public boolean hasDbSourceProperty(String dbName, String dbProperty)
368 // TODO: decide if invalidDbName exception is thrown here.
370 List<DbSourceProxy> proxies = getSourceProxy(dbName);
373 for (DbSourceProxy proxy : proxies)
375 if (proxy.getDbSourceProperties() != null)
377 return proxy.getDbSourceProperties().containsKey(dbProperty);
385 * select sources which are implemented by instances of the given class
387 * @param class that implements DbSourceProxy
388 * @return null or vector of source names for fetchers
390 public String[] getDbInstances(Class class1)
392 if (!jalview.ws.seqfetcher.DbSourceProxy.class.isAssignableFrom(class1))
395 "Implmentation Error - getDbInstances must be given a class that implements jalview.ws.seqfetcher.DbSourceProxy (was given '"
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())
427 for (DbSourceProxy pr : getSourceProxy(fetchable))
429 if (class1.isInstance(pr))
434 if (prlist.size() == 0)
438 return prlist.toArray(new DbSourceProxy[0]);