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.Stack;
40 import java.util.Vector;
42 public class ASequenceFetcher
46 * set of databases we can retrieve entries from
48 protected Hashtable<String, Map<String, DbSourceProxy>> fetchableDbs;
51 * comparator to sort by tier (0/1/2) and name
53 private Comparator<DbSourceProxy> proxyComparator;
58 public ASequenceFetcher()
63 * comparator to sort proxies by tier and name
65 proxyComparator = new Comparator<DbSourceProxy>()
68 public int compare(DbSourceProxy o1, DbSourceProxy o2)
71 * Tier 0 precedes 1 precedes 2
73 int compared = Integer.compare(o1.getTier(), o2.getTier());
76 // defend against NullPointer - should never happen
77 String o1Name = o1.getDbName();
78 String o2Name = o2.getDbName();
79 if (o1Name != null && o2Name != null)
81 compared = o1Name.compareToIgnoreCase(o2Name);
90 * get array of supported Databases
92 * @return database source string for each database - only the latest version
93 * of a source db is bound to each source.
95 public String[] getSupportedDb()
97 if (fetchableDbs == null)
101 String[] sf = fetchableDbs.keySet().toArray(
102 new String[fetchableDbs.size()]);
106 public boolean isFetchable(String source)
108 for (String db : fetchableDbs.keySet())
110 if (source.equalsIgnoreCase(db))
115 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(DBRefEntry[] refs, boolean dna)
130 Vector<SequenceI> rseqs = new Vector<SequenceI>();
131 Hashtable<String, List<String>> queries = new Hashtable<String, List<String>>();
132 for (int r = 0; r < refs.length; r++)
134 if (!queries.containsKey(refs[r].getSource()))
136 queries.put(refs[r].getSource(), new ArrayList<String>());
138 List<String> qset = queries.get(refs[r].getSource());
139 if (!qset.contains(refs[r].getAccessionId()))
141 qset.add(refs[r].getAccessionId());
144 Enumeration<String> e = queries.keys();
145 while (e.hasMoreElements())
147 List<String> query = null;
149 db = e.nextElement();
150 query = queries.get(db);
151 if (!isFetchable(db))
153 reportStdError(db, query, new Exception(
154 "Don't know how to fetch from this database :" + db));
158 Stack<String> queriesLeft = new Stack<String>();
159 queriesLeft.addAll(query);
161 List<DbSourceProxy> proxies = getSourceProxy(db);
162 for (DbSourceProxy fetcher : proxies)
164 List<String> queriesMade = new ArrayList<String>();
165 HashSet<String> queriesFound = new HashSet<String>();
168 if (fetcher.isDnaCoding() != dna)
170 continue; // wrong sort of data
172 boolean doMultiple = fetcher.getMaximumQueryCount() > 1;
173 while (!queriesLeft.isEmpty())
175 StringBuffer qsb = new StringBuffer();
178 if (qsb.length() > 0)
180 qsb.append(fetcher.getAccessionSeparator());
182 String q = queriesLeft.pop();
185 } while (doMultiple && !queriesLeft.isEmpty());
187 AlignmentI seqset = null;
190 // create a fetcher and go to it
191 seqset = fetcher.getSequenceRecords(qsb.toString());
192 } catch (Exception ex)
194 System.err.println("Failed to retrieve the following from "
196 System.err.println(qsb);
197 ex.printStackTrace(System.err);
199 // TODO: Merge alignment together - perhaps
202 SequenceI seqs[] = seqset.getSequencesArray();
205 for (int is = 0; is < seqs.length; is++)
207 rseqs.addElement(seqs[is]);
208 DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]
209 .getDBRefs(), new DBRefEntry(db, null, null));
212 for (DBRefEntry dbr : frefs)
214 queriesFound.add(dbr.getAccessionId());
215 queriesMade.remove(dbr.getAccessionId());
223 if (fetcher.getRawRecords() != null)
225 System.out.println("# Retrieved from " + db + ":"
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,
282 System.err.println("Failed to retrieve the following references from "
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, DbSourceProxy> dblist = fetchableDbs.get(db);
312 return new ArrayList<DbSourceProxy>();
316 * sort so that primary sources precede secondary
318 List<DbSourceProxy> dbs = new ArrayList<DbSourceProxy>(dblist.values());
319 Collections.sort(dbs, proxyComparator);
324 * constructs an instance of the proxy and registers it as a valid dbrefsource
326 * @param dbSourceProxy
327 * reference for class implementing
328 * jalview.ws.seqfetcher.DbSourceProxy
330 protected void addDBRefSourceImpl(
331 Class<? extends DbSourceProxy> dbSourceProxy)
332 throws IllegalArgumentException
334 DbSourceProxy proxy = null;
337 DbSourceProxy proxyObj = dbSourceProxy.getConstructor().newInstance();
339 } catch (IllegalArgumentException e)
342 } catch (Exception e)
344 // Serious problems if this happens.
347 .getString("error.dbrefsource_implementation_exception"),
350 addDbRefSourceImpl(proxy);
354 * add the properly initialised DbSourceProxy object 'proxy' to the list of
359 protected void addDbRefSourceImpl(DbSourceProxy proxy)
363 if (fetchableDbs == null)
365 fetchableDbs = new Hashtable<String, Map<String, DbSourceProxy>>();
367 Map<String, DbSourceProxy> slist = fetchableDbs.get(proxy
371 fetchableDbs.put(proxy.getDbSource(),
372 slist = new Hashtable<String, DbSourceProxy>());
374 slist.put(proxy.getDbName(), proxy);
379 * select sources which are implemented by instances of the given class
381 * @param class that implements DbSourceProxy
382 * @return null or vector of source names for fetchers
384 public String[] getDbInstances(Class class1)
386 if (!DbSourceProxy.class.isAssignableFrom(class1))
391 "error.implementation_error_dbinstance_must_implement_interface",
392 new String[] { class1.toString() }));
394 if (fetchableDbs == null)
398 String[] sources = null;
399 Vector<String> src = new Vector<String>();
400 Enumeration<String> dbs = fetchableDbs.keys();
401 while (dbs.hasMoreElements())
403 String dbn = dbs.nextElement();
404 for (DbSourceProxy dbp : fetchableDbs.get(dbn).values())
406 if (class1.isAssignableFrom(dbp.getClass()))
414 src.copyInto(sources = new String[src.size()]);
419 public DbSourceProxy[] getDbSourceProxyInstances(Class class1)
421 List<DbSourceProxy> prlist = new ArrayList<DbSourceProxy>();
422 for (String fetchable : getSupportedDb())
424 for (DbSourceProxy pr : getSourceProxy(fetchable))
426 if (class1.isInstance(pr))
432 if (prlist.size() == 0)
436 return prlist.toArray(new DbSourceProxy[0]);
440 * Returns a preferred feature colouring scheme for the given source, or null
441 * if none is defined.
446 public FeatureSettingsModelI getFeatureColourScheme(String source)
449 * return the first non-null colour scheme for any proxy for
450 * this database source
452 for (DbSourceProxy proxy : getSourceProxy(source))
454 FeatureSettingsModelI preferredColours = proxy
455 .getFeatureColourScheme();
456 if (preferredColours != null)
458 return preferredColours;