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.api.FeatureSettingsModelI;
24 import jalview.bin.Cache;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.DBRefUtils;
29 import jalview.util.MessageManager;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.Enumeration;
35 import java.util.HashSet;
36 import java.util.Hashtable;
37 import java.util.List;
39 import java.util.Map.Entry;
40 import java.util.Stack;
41 import java.util.Vector;
43 public class ASequenceFetcher
47 * set of databases we can retrieve entries from
49 protected Hashtable<String, Map<String, DbSourceProxyRoot>> fetchableDbs;
52 * comparator to sort by tier (0/1/2) and name
54 private Comparator<DbSourceProxy> proxyComparator;
59 protected ASequenceFetcher()
64 * comparator to sort proxies by tier and name
66 proxyComparator = new Comparator<DbSourceProxy>()
69 public int compare(DbSourceProxy o1, DbSourceProxy o2)
72 * Tier 0 precedes 1 precedes 2
74 int compared = Integer.compare(o1.getTier(), o2.getTier());
77 // defend against NullPointer - should never happen
78 String o1Name = o1.getDbName();
79 String o2Name = o2.getDbName();
80 if (o1Name != null && o2Name != null)
82 compared = o1Name.compareToIgnoreCase(o2Name);
91 * get array of supported Databases
93 * @return database source string for each database - only the latest version
94 * of a source db is bound to each source.
96 public String[] getSupportedDb()
98 if (fetchableDbs == null)
102 String[] sf = fetchableDbs.keySet()
103 .toArray(new String[fetchableDbs.size()]);
107 public boolean isFetchable(String source)
109 for (String db : fetchableDbs.keySet())
111 if (source.equalsIgnoreCase(db))
116 Cache.log.warn("isFetchable doesn't know about '" + source + "'");
121 * Fetch sequences for the given cross-references
125 * if true, only fetch from nucleotide data sources, else peptide
128 public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
130 Vector<SequenceI> rseqs = new Vector<>();
131 Hashtable<String, List<String>> queries = new Hashtable<>();
132 for (DBRefEntry ref : refs)
134 String canonical = DBRefUtils.getCanonicalName(ref.getSource());
135 if (!queries.containsKey(canonical))
137 queries.put(canonical, new ArrayList<String>());
139 List<String> qset = queries.get(canonical);
140 if (!qset.contains(ref.getAccessionId()))
142 qset.add(ref.getAccessionId());
145 Enumeration<String> e = queries.keys();
146 while (e.hasMoreElements())
148 List<String> query = null;
150 db = e.nextElement();
151 query = queries.get(db);
152 if (!isFetchable(db))
154 reportStdError(db, query, new Exception(
155 "Don't know how to fetch from this database :" + db));
159 Stack<String> queriesLeft = new Stack<>();
160 queriesLeft.addAll(query);
162 List<DbSourceProxy> proxies = getSourceProxy(db);
163 for (DbSourceProxy fetcher : proxies)
165 List<String> queriesMade = new ArrayList<>();
166 HashSet<String> queriesFound = new HashSet<>();
169 if (fetcher.isDnaCoding() != dna)
171 continue; // wrong sort of data
173 boolean doMultiple = fetcher.getMaximumQueryCount() > 1;
174 while (!queriesLeft.isEmpty())
176 StringBuffer qsb = new StringBuffer();
179 if (qsb.length() > 0)
181 qsb.append(fetcher.getAccessionSeparator());
183 String q = queriesLeft.pop();
186 } while (doMultiple && !queriesLeft.isEmpty());
188 AlignmentI seqset = null;
191 // create a fetcher and go to it
192 seqset = fetcher.getSequenceRecords(qsb.toString());
193 } catch (Exception ex)
196 "Failed to retrieve the following from " + db);
197 System.err.println(qsb);
198 ex.printStackTrace(System.err);
200 // TODO: Merge alignment together - perhaps
203 SequenceI seqs[] = seqset.getSequencesArray();
206 for (int is = 0; is < seqs.length; is++)
208 rseqs.addElement(seqs[is]);
209 // BH 2015.01.25 check about version/accessid being null here
210 List<DBRefEntry> frefs = DBRefUtils.searchRefs(
211 seqs[is].getDBRefs(),
212 new DBRefEntry(db, null, null), DBRefUtils.SEARCH_MODE_FULL);
213 for (DBRefEntry dbr : frefs)
215 queriesFound.add(dbr.getAccessionId());
216 queriesMade.remove(dbr.getAccessionId());
223 if (fetcher.getRawRecords() != null)
226 "# Retrieved from " + db + ":" + qsb.toString());
227 StringBuffer rrb = fetcher.getRawRecords();
229 * for (int rr = 0; rr<rrb.length; rr++) {
234 hdr = "# " + db + ":" + qsb.toString();
236 * } else { hdr = "# part "+rr; }
238 System.out.println(hdr);
241 System.out.println(rrb);
243 System.out.println("# end of " + hdr);
250 } catch (Exception ex)
252 reportStdError(db, queriesMade, ex);
254 if (queriesMade.size() > 0)
256 System.out.println("# Adding " + queriesMade.size()
257 + " ids back to queries list for searching again (" + db
259 queriesLeft.addAll(queriesMade);
264 SequenceI[] result = null;
265 if (rseqs.size() > 0)
267 result = new SequenceI[rseqs.size()];
269 for (SequenceI s : rseqs)
278 public void reportStdError(String db, List<String> queriesMade,
283 "Failed to retrieve the following references from " + db);
285 for (String qv : queriesMade)
287 System.err.print(" " + qv + ";");
290 System.err.println();
294 System.err.println();
295 ex.printStackTrace();
299 * Returns a list of proxies for the given source
302 * database source string TODO: add version string/wildcard for
303 * retrieval of specific DB source/version combinations.
304 * @return a list of DbSourceProxy for the db
306 public List<DbSourceProxy> getSourceProxy(String db)
308 db = DBRefUtils.getCanonicalName(db);
309 Map<String, DbSourceProxyRoot> dblist = fetchableDbs.get(db);
312 return new ArrayList<>();
316 * sort so that primary sources precede secondary
318 List<DbSourceProxy> dbs = new ArrayList<>();
319 for (Entry<String, DbSourceProxyRoot> entry : dblist.entrySet())
321 DbSourceProxyRoot proxy = entry.getValue();
322 if (proxy instanceof DbRoot)
324 proxy = setProxy((DbRoot) proxy, dblist);
326 dbs.add((DbSourceProxy) proxy);
328 Collections.sort(dbs, proxyComparator);
332 class DbRoot implements DbSourceProxyRoot
335 private String sourceName;
337 private String className;
339 DbRoot(String sourceName, String className)
341 this.sourceName = sourceName;
342 this.className = className;
346 public String getDbSource()
352 * lazy class creation
354 * @return the actual proxy object
356 public DbSourceProxy getProxy()
360 System.err.println("ASeqFetch " + className);
361 return (DbSourceProxy) Class.forName(className).newInstance();
362 } catch (Exception e)
364 // Serious problems if this happens.
365 throw new Error(MessageManager.getString(
366 "error.dbrefsource_implementation_exception"), e);
373 * constructs an instance of the proxy and registers it as a valid dbrefsource
375 * @param dbSourceProxyClass
376 * reference for class implementing
377 * jalview.ws.seqfetcher.DbSourceProxy
379 protected void addDBRefSourceImpl(
380 Class<? extends DbSourceProxy> dbSourceProxyClass)
381 throws IllegalArgumentException
383 DbSourceProxy proxy = null;
386 proxy = dbSourceProxyClass.getConstructor().newInstance();
387 } catch (IllegalArgumentException e)
390 } catch (Exception e)
392 // Serious problems if this happens.
393 throw new Error(MessageManager
394 .getString("error.dbrefsource_implementation_exception"), e);
396 addDbRefSourceImpl(proxy);
399 public void addDBRefSourceImpl(String sourceName, String className)
401 addDbRefSourceImpl(new DbRoot(sourceName, className));
405 * add the properly initialised DbSourceProxy object 'proxy' to the list of
410 void addDbRefSourceImpl(DbSourceProxyRoot proxy)
414 if (fetchableDbs == null)
416 fetchableDbs = new Hashtable<>();
418 String key = proxy.getDbSource();
419 Map<String, DbSourceProxyRoot> slist = fetchableDbs.get(key);
422 fetchableDbs.put(key, slist = new Hashtable<>());
424 if (proxy instanceof DbRoot)
426 slist.put("", proxy);
430 slist.put(((DbSourceProxy) proxy).getDbName(), proxy);
436 * select sources which are implemented by instances of the given class
439 * that implements DbSourceProxy
440 * @return null or vector of source names for fetchers
442 public String[] getDbInstances(Class<?> class1)
444 if (!DbSourceProxy.class.isAssignableFrom(class1))
446 throw new Error(MessageManager.formatMessage(
447 "error.implementation_error_dbinstance_must_implement_interface",
449 { class1.toString() }));
451 if (fetchableDbs == null)
455 Vector<String> src = new Vector<>();
456 for (String dbSource : fetchableDbs.keySet())
458 Map<String, DbSourceProxyRoot> dblist = fetchableDbs.get(dbSource);
459 for (Entry<String, DbSourceProxyRoot> entry : dblist.entrySet())
461 DbSourceProxyRoot proxy = entry.getValue();
462 if (proxy instanceof DbRoot)
464 proxy = setProxy((DbRoot) proxy, dblist);
466 Class<?> c = proxy.getClass();
467 if (class1 == c || class1.isAssignableFrom(c))
469 src.addElement(dbSource);
473 String[] sources = null;
476 src.copyInto(sources = new String[src.size()]);
481 private DbSourceProxyRoot setProxy(DbRoot root,
482 Map<String, DbSourceProxyRoot> dblist)
484 DbSourceProxy proxy = root.getProxy();
485 // Time to create the actual proxy
487 dblist.put(proxy.getDbName(), proxy);
491 public DbSourceProxy[] getDbSourceProxyInstances(Class<?> class1)
493 if (fetchableDbs == null)
497 List<DbSourceProxy> prlist = new ArrayList<>();
498 for (String fetchable : fetchableDbs.keySet())
500 for (DbSourceProxy pr : getSourceProxy(fetchable))
502 if (class1.isInstance(pr))
508 if (prlist.size() == 0)
512 return prlist.toArray(new DbSourceProxy[0]);
516 * Returns a preferred feature colouring scheme for the given source, or null
517 * if none is defined.
522 public FeatureSettingsModelI getFeatureColourScheme(String source)
525 * return the first non-null colour scheme for any proxy for
526 * this database source
528 for (DbSourceProxy proxy : getSourceProxy(source))
530 FeatureSettingsModelI preferredColours = proxy
531 .getFeatureColourScheme();
532 if (preferredColours != null)
534 return preferredColours;