2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
\r
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
18 package jalview.ws.seqfetcher;
\r
20 import jalview.datamodel.AlignmentI;
\r
21 import jalview.datamodel.DBRefEntry;
\r
22 import jalview.datamodel.SequenceI;
\r
23 import jalview.util.DBRefUtils;
\r
25 import java.util.ArrayList;
\r
26 import java.util.Collection;
\r
27 import java.util.Enumeration;
\r
28 import java.util.HashSet;
\r
29 import java.util.Hashtable;
\r
30 import java.util.Iterator;
\r
31 import java.util.List;
\r
32 import java.util.Map;
\r
33 import java.util.Stack;
\r
34 import java.util.Vector;
\r
36 public class ASequenceFetcher
\r
40 * set of databases we can retrieve entries from
\r
42 protected Hashtable<String, Map<String, DbSourceProxy>> FETCHABLEDBS;
\r
44 public ASequenceFetcher()
\r
50 * get list of supported Databases
\r
52 * @return database source string for each database - only the latest version
\r
53 * of a source db is bound to each source.
\r
55 public String[] getSupportedDb()
\r
57 if (FETCHABLEDBS == null)
\r
59 String[] sf = new String[FETCHABLEDBS.size()];
\r
60 Enumeration e = FETCHABLEDBS.keys();
\r
62 while (e.hasMoreElements())
\r
64 sf[i++] = (String) e.nextElement();
\r
70 public boolean isFetchable(String source)
\r
72 Enumeration e = FETCHABLEDBS.keys();
\r
73 while (e.hasMoreElements())
\r
75 String db = (String) e.nextElement();
\r
76 if (source.compareToIgnoreCase(db) == 0)
\r
79 jalview.bin.Cache.log.warn("isFetchable doesn't know about '" + source
\r
84 public SequenceI[] getSequences(jalview.datamodel.DBRefEntry[] refs)
\r
86 SequenceI[] ret = null;
\r
87 Vector<SequenceI> rseqs = new Vector();
\r
88 Hashtable<String, List<String>> queries = new Hashtable();
\r
89 for (int r = 0; r < refs.length; r++)
\r
91 if (!queries.containsKey(refs[r].getSource()))
\r
93 queries.put(refs[r].getSource(), new ArrayList<String>());
\r
95 List<String> qset = queries.get(refs[r].getSource());
\r
96 if (!qset.contains(refs[r].getAccessionId()))
\r
98 qset.add(refs[r].getAccessionId());
\r
101 Enumeration<String> e = queries.keys();
\r
102 while (e.hasMoreElements())
\r
104 List<String> query = null;
\r
106 db = e.nextElement();
\r
107 query = queries.get(db);
\r
108 if (!isFetchable(db))
\r
110 reportStdError(db, query, new Exception(
\r
111 "Don't know how to fetch from this database :" + db));
\r
114 Iterator<DbSourceProxy> fetchers = getSourceProxy(db).iterator();
\r
115 Stack<String> queriesLeft = new Stack<String>();
\r
116 // List<String> queriesFailed = new ArrayList<String>();
\r
117 queriesLeft.addAll(query);
\r
118 while (fetchers.hasNext())
\r
120 List<String> queriesMade = new ArrayList<String>();
\r
121 HashSet queriesFound = new HashSet<String>();
\r
124 DbSourceProxy fetcher = fetchers.next();
\r
125 boolean doMultiple = fetcher.getAccessionSeparator() != null; // No
\r
130 while (!queriesLeft.isEmpty())
\r
132 StringBuffer qsb = new StringBuffer();
\r
135 if (qsb.length() > 0)
\r
137 qsb.append(fetcher.getAccessionSeparator());
\r
139 String q = queriesLeft.pop();
\r
140 queriesMade.add(q);
\r
142 } while (doMultiple && !queriesLeft.isEmpty());
\r
144 AlignmentI seqset = null;
\r
147 // create a fetcher and go to it
\r
148 seqset = fetcher.getSequenceRecords(qsb.toString()); // ,
\r
150 } catch (Exception ex)
\r
152 System.err.println("Failed to retrieve the following from "
\r
154 System.err.println(qsb);
\r
155 ex.printStackTrace(System.err);
\r
157 // TODO: Merge alignment together - perhaps
\r
158 if (seqset != null)
\r
160 SequenceI seqs[] = seqset.getSequencesArray();
\r
163 for (int is = 0; is < seqs.length; is++)
\r
165 rseqs.addElement(seqs[is]);
\r
166 DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]
\r
167 .getDBRef(), new DBRefEntry(db, null, null));
\r
170 for (DBRefEntry dbr : frefs)
\r
172 queriesFound.add(dbr.getAccessionId());
\r
173 queriesMade.remove(dbr.getAccessionId());
\r
181 if (fetcher.getRawRecords() != null)
\r
183 System.out.println("# Retrieved from " + db + ":"
\r
185 StringBuffer rrb = fetcher.getRawRecords();
\r
187 * for (int rr = 0; rr<rrb.length; rr++) {
\r
190 // if (rr<qs.length)
\r
192 hdr = "# " + db + ":" + qsb.toString();
\r
194 * } else { hdr = "# part "+rr; }
\r
196 System.out.println(hdr);
\r
198 System.out.println(rrb);
\r
199 System.out.println("# end of " + hdr);
\r
206 } catch (Exception ex)
\r
208 reportStdError(db, queriesMade, ex);
\r
210 if (queriesMade.size() > 0)
\r
212 System.out.println("# Adding " + queriesMade.size()
\r
213 + " ids back to queries list for searching again (" + db
\r
215 queriesLeft.addAll(queriesMade);
\r
219 if (rseqs.size() > 0)
\r
221 ret = new SequenceI[rseqs.size()];
\r
222 Enumeration sqs = rseqs.elements();
\r
224 while (sqs.hasMoreElements())
\r
226 SequenceI s = (SequenceI) sqs.nextElement();
\r
234 public void reportStdError(String db, List<String> queriesMade,
\r
238 System.err.println("Failed to retrieve the following references from "
\r
241 for (String qv : queriesMade)
\r
243 System.err.print(" " + qv + ";");
\r
246 System.err.println();
\r
250 System.err.println();
\r
251 ex.printStackTrace();
\r
255 * Retrieve an instance of the proxy for the given source
\r
258 * database source string TODO: add version string/wildcard for
\r
259 * retrieval of specific DB source/version combinations.
\r
260 * @return an instance of DbSourceProxy for that db.
\r
262 public List<DbSourceProxy> getSourceProxy(String db)
\r
264 List<DbSourceProxy> dbs;
\r
265 Collection<DbSourceProxy> dblist = FETCHABLEDBS.get(db).values();
\r
266 if (dblist.size()>1)
\r
268 DbSourceProxy[] l=dblist.toArray(new DbSourceProxy[0]);
\r
270 String[] nm=new String[l.length];
\r
271 for (DbSourceProxy s:l)
\r
273 nm[i++]=s.getDbName().toLowerCase();
\r
275 jalview.util.QuickSort.sort(nm,l);
\r
276 dbs = new ArrayList<DbSourceProxy>();
\r
277 for (i=l.length-1;i>=0; i--)
\r
282 dbs = new ArrayList<DbSourceProxy>(dblist);
\r
288 * constructs and instance of the proxy and registers it as a valid
\r
291 * @param dbSourceProxy
\r
292 * reference for class implementing
\r
293 * jalview.ws.seqfetcher.DbSourceProxy
\r
294 * @throws java.lang.IllegalArgumentException
\r
295 * if class does not implement jalview.ws.seqfetcher.DbSourceProxy
\r
297 protected void addDBRefSourceImpl(Class dbSourceProxy)
\r
298 throws java.lang.IllegalArgumentException
\r
300 DbSourceProxy proxy = null;
\r
303 Object proxyObj = dbSourceProxy.getConstructor(null)
\r
304 .newInstance(null);
\r
305 if (!DbSourceProxy.class.isInstance(proxyObj))
\r
307 throw new IllegalArgumentException(
\r
308 dbSourceProxy.toString()
\r
309 + " does not implement the jalview.ws.seqfetcher.DbSourceProxy");
\r
311 proxy = (DbSourceProxy) proxyObj;
\r
312 } catch (IllegalArgumentException e)
\r
315 } catch (Exception e)
\r
317 // Serious problems if this happens.
\r
318 throw new Error("DBRefSource Implementation Exception", e);
\r
320 addDbRefSourceImpl(proxy);
\r
324 * add the properly initialised DbSourceProxy object 'proxy' to the list of
\r
325 * sequence fetchers
\r
329 protected void addDbRefSourceImpl(DbSourceProxy proxy)
\r
333 if (FETCHABLEDBS == null)
\r
335 FETCHABLEDBS = new Hashtable<String, Map<String,DbSourceProxy>>();
\r
337 Map<String,DbSourceProxy> slist = FETCHABLEDBS.get(proxy.getDbSource());
\r
340 FETCHABLEDBS.put(proxy.getDbSource(),
\r
341 slist = new Hashtable<String,DbSourceProxy>());
\r
343 slist.put(proxy.getDbName(),proxy);
\r
348 * test if the database handler for dbName contains the given dbProperty
\r
349 * when a dbName resolves to a set of proxies - this method will return the result of the test for the first instance.
\r
350 * TODO implement additional method to query all sources for a db to find one with a particular property
\r
352 * @param dbProperty
\r
353 * @return true if proxy has the given property
\r
355 public boolean hasDbSourceProperty(String dbName, String dbProperty)
\r
357 // TODO: decide if invalidDbName exception is thrown here.
\r
359 List<DbSourceProxy> proxies = getSourceProxy(dbName);
\r
360 if (proxies != null)
\r
362 for (DbSourceProxy proxy : proxies)
\r
364 if (proxy.getDbSourceProperties() != null)
\r
366 return proxy.getDbSourceProperties().containsKey(dbProperty);
\r
374 * select sources which are implemented by instances of the given class
\r
376 * @param class that implements DbSourceProxy
\r
377 * @return null or vector of source names for fetchers
\r
379 public String[] getDbInstances(Class class1)
\r
381 if (!jalview.ws.seqfetcher.DbSourceProxy.class.isAssignableFrom(class1))
\r
384 "Implmentation Error - getDbInstances must be given a class that implements jalview.ws.seqfetcher.DbSourceProxy (was given '"
\r
387 if (FETCHABLEDBS == null)
\r
391 String[] sources = null;
\r
392 Vector src = new Vector();
\r
393 Enumeration dbs = FETCHABLEDBS.keys();
\r
394 while (dbs.hasMoreElements())
\r
396 String dbn = (String) dbs.nextElement();
\r
397 for (DbSourceProxy dbp : FETCHABLEDBS.get(dbn).values())
\r
399 if (class1.isAssignableFrom(dbp.getClass()))
\r
401 src.addElement(dbn);
\r
405 if (src.size() > 0)
\r
407 src.copyInto(sources = new String[src.size()]);
\r
411 public DbSourceProxy[] getDbSourceProxyInstances(
\r
414 ArrayList<DbSourceProxy> prlist=new ArrayList<DbSourceProxy>();
\r
415 for (String fetchable:getSupportedDb())
\r
416 for (DbSourceProxy pr:getSourceProxy(fetchable))
\r
418 if (class1.isInstance(pr)) {prlist.add(pr);}
\r
420 if (prlist.size()==0)
\r
424 return prlist.toArray(new DbSourceProxy[0]);
\r