2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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;
28 import java.util.ArrayList;
29 import java.util.Enumeration;
30 import java.util.HashSet;
31 import java.util.Hashtable;
32 import java.util.Iterator;
33 import java.util.List;
35 import java.util.Stack;
36 import java.util.Vector;
38 public class ASequenceFetcher
42 * set of databases we can retrieve entries from
44 protected Hashtable<String, Map<String, DbSourceProxy>> FETCHABLEDBS;
46 public ASequenceFetcher()
52 * get list of supported Databases
54 * @return database source string for each database - only the latest version
55 * of a source db is bound to each source.
57 public String[] getSupportedDb()
59 if (FETCHABLEDBS == null)
61 String[] sf = new String[FETCHABLEDBS.size()];
62 Enumeration e = FETCHABLEDBS.keys();
64 while (e.hasMoreElements())
66 sf[i++] = (String) e.nextElement();
72 public boolean isFetchable(String source)
74 Enumeration e = FETCHABLEDBS.keys();
75 while (e.hasMoreElements())
77 String db = (String) e.nextElement();
78 if (source.compareToIgnoreCase(db) == 0)
81 jalview.bin.Cache.log.warn("isFetchable doesn't know about '" + source
86 public SequenceI[] getSequences(jalview.datamodel.DBRefEntry[] refs)
88 SequenceI[] ret = null;
89 Vector<SequenceI> rseqs = new Vector();
90 Hashtable<String, List<String>> queries = new Hashtable();
91 for (int r = 0; r < refs.length; r++)
93 if (!queries.containsKey(refs[r].getSource()))
95 queries.put(refs[r].getSource(), new ArrayList<String>());
97 List<String> qset = queries.get(refs[r].getSource());
98 if (!qset.contains(refs[r].getAccessionId()))
100 qset.add(refs[r].getAccessionId());
103 Enumeration<String> e = queries.keys();
104 while (e.hasMoreElements())
106 List<String> query = null;
108 db = e.nextElement();
109 query = queries.get(db);
110 if (!isFetchable(db))
112 reportStdError(db, query, new Exception(
113 "Don't know how to fetch from this database :" + db));
116 Iterator<DbSourceProxy> fetchers = getSourceProxy(db).iterator();
117 Stack<String> queriesLeft = new Stack<String>();
118 // List<String> queriesFailed = new ArrayList<String>();
119 queriesLeft.addAll(query);
120 while (fetchers.hasNext())
122 List<String> queriesMade = new ArrayList<String>();
123 HashSet queriesFound = new HashSet<String>();
126 DbSourceProxy fetcher = fetchers.next();
127 boolean doMultiple = fetcher.getAccessionSeparator() != null; // No
132 while (!queriesLeft.isEmpty())
134 StringBuffer qsb = new StringBuffer();
137 if (qsb.length() > 0)
139 qsb.append(fetcher.getAccessionSeparator());
141 String q = queriesLeft.pop();
144 } while (doMultiple && !queriesLeft.isEmpty());
146 AlignmentI seqset = null;
149 // create a fetcher and go to it
150 seqset = fetcher.getSequenceRecords(qsb.toString()); // ,
152 } catch (Exception ex)
154 System.err.println("Failed to retrieve the following from "
156 System.err.println(qsb);
157 ex.printStackTrace(System.err);
159 // TODO: Merge alignment together - perhaps
162 SequenceI seqs[] = seqset.getSequencesArray();
165 for (int is = 0; is < seqs.length; is++)
167 rseqs.addElement(seqs[is]);
168 DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]
169 .getDBRef(), new DBRefEntry(db, null, null));
172 for (DBRefEntry dbr : frefs)
174 queriesFound.add(dbr.getAccessionId());
175 queriesMade.remove(dbr.getAccessionId());
183 if (fetcher.getRawRecords() != null)
185 System.out.println("# Retrieved from " + db + ":"
187 StringBuffer rrb = fetcher.getRawRecords();
189 * for (int rr = 0; rr<rrb.length; rr++) {
194 hdr = "# " + db + ":" + qsb.toString();
196 * } else { hdr = "# part "+rr; }
198 System.out.println(hdr);
200 System.out.println(rrb);
201 System.out.println("# end of " + hdr);
208 } catch (Exception ex)
210 reportStdError(db, queriesMade, ex);
212 if (queriesMade.size() > 0)
214 System.out.println("# Adding " + queriesMade.size()
215 + " ids back to queries list for searching again (" + db
217 queriesLeft.addAll(queriesMade);
221 if (rseqs.size() > 0)
223 ret = new SequenceI[rseqs.size()];
224 Enumeration sqs = rseqs.elements();
226 while (sqs.hasMoreElements())
228 SequenceI s = (SequenceI) sqs.nextElement();
236 public void reportStdError(String db, List<String> queriesMade,
240 System.err.println("Failed to retrieve the following references from "
243 for (String qv : queriesMade)
245 System.err.print(" " + qv + ";");
248 System.err.println();
252 System.err.println();
253 ex.printStackTrace();
257 * Retrieve an instance of the proxy for the given source
260 * database source string TODO: add version string/wildcard for
261 * retrieval of specific DB source/version combinations.
262 * @return an instance of DbSourceProxy for that db.
264 public List<DbSourceProxy> getSourceProxy(String db)
266 List<DbSourceProxy> dbs;
267 Map<String, DbSourceProxy> dblist = FETCHABLEDBS.get(db);
270 return new ArrayList<DbSourceProxy>();
273 if (dblist.size() > 1)
275 DbSourceProxy[] l = dblist.values().toArray(new DbSourceProxy[0]);
277 String[] nm = new String[l.length];
278 // make sure standard dbs appear first, followed by reference das sources, followed by anything else.
279 for (DbSourceProxy s : l)
281 nm[i++] = ""+s.getTier()+s.getDbName().toLowerCase();
283 jalview.util.QuickSort.sort(nm, l);
284 dbs = new ArrayList<DbSourceProxy>();
285 for (i = l.length - 1; i >= 0; i--)
292 dbs = new ArrayList<DbSourceProxy>(dblist.values());
298 * constructs and instance of the proxy and registers it as a valid
301 * @param dbSourceProxy
302 * reference for class implementing
303 * jalview.ws.seqfetcher.DbSourceProxy
304 * @throws java.lang.IllegalArgumentException
305 * if class does not implement jalview.ws.seqfetcher.DbSourceProxy
307 protected void addDBRefSourceImpl(Class dbSourceProxy)
308 throws java.lang.IllegalArgumentException
310 DbSourceProxy proxy = null;
313 Object proxyObj = dbSourceProxy.getConstructor(null)
315 if (!DbSourceProxy.class.isInstance(proxyObj))
317 throw new IllegalArgumentException(
318 dbSourceProxy.toString()
319 + " does not implement the jalview.ws.seqfetcher.DbSourceProxy");
321 proxy = (DbSourceProxy) proxyObj;
322 } catch (IllegalArgumentException e)
325 } catch (Exception e)
327 // Serious problems if this happens.
328 throw new 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))
397 "Implmentation Error - getDbInstances must be given a class that implements jalview.ws.seqfetcher.DbSourceProxy (was given '"
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]);