X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Fseqfetcher%2FASequenceFetcher.java;h=97babfd0769a321695ad08fd1db2d72cbf1cb79f;hb=fcb944cdd9de849c89f0a8744ae6e56e22de9c1e;hp=977f9dad1fe8b8989c050806b9d64e2c90a672bf;hpb=3d0101179759ef157b088ea135423cd909512d9f;p=jalview.git diff --git a/src/jalview/ws/seqfetcher/ASequenceFetcher.java b/src/jalview/ws/seqfetcher/ASequenceFetcher.java index 977f9da..97babfd 100644 --- a/src/jalview/ws/seqfetcher/ASequenceFetcher.java +++ b/src/jalview/ws/seqfetcher/ASequenceFetcher.java @@ -36,6 +36,7 @@ import java.util.HashSet; import java.util.Hashtable; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Stack; import java.util.Vector; @@ -45,7 +46,7 @@ public class ASequenceFetcher /* * set of databases we can retrieve entries from */ - protected Hashtable> fetchableDbs; + protected Hashtable> fetchableDbs; /* * comparator to sort by tier (0/1/2) and name @@ -57,8 +58,6 @@ public class ASequenceFetcher */ protected ASequenceFetcher() { - super(); - /* * comparator to sort proxies by tier and name */ @@ -126,15 +125,16 @@ public class ASequenceFetcher */ public SequenceI[] getSequences(List refs, boolean dna) { - Vector rseqs = new Vector(); - Hashtable> queries = new Hashtable>(); + Vector rseqs = new Vector<>(); + Hashtable> queries = new Hashtable<>(); for (DBRefEntry ref : refs) { - if (!queries.containsKey(ref.getSource())) + String canonical = DBRefUtils.getCanonicalName(ref.getSource()); + if (!queries.containsKey(canonical)) { - queries.put(ref.getSource(), new ArrayList()); + queries.put(canonical, new ArrayList()); } - List qset = queries.get(ref.getSource()); + List qset = queries.get(canonical); if (!qset.contains(ref.getAccessionId())) { qset.add(ref.getAccessionId()); @@ -154,14 +154,14 @@ public class ASequenceFetcher continue; } - Stack queriesLeft = new Stack(); + Stack queriesLeft = new Stack<>(); queriesLeft.addAll(query); List proxies = getSourceProxy(db); for (DbSourceProxy fetcher : proxies) { - List queriesMade = new ArrayList(); - HashSet queriesFound = new HashSet(); + List queriesMade = new ArrayList<>(); + HashSet queriesFound = new HashSet<>(); try { if (fetcher.isDnaCoding() != dna) @@ -204,9 +204,10 @@ public class ASequenceFetcher for (int is = 0; is < seqs.length; is++) { rseqs.addElement(seqs[is]); + // BH 2015.01.25 check about version/accessid being null here List frefs = DBRefUtils.searchRefs( seqs[is].getDBRefs(), - new DBRefEntry(db, null, null)); + new DBRefEntry(db, null, null), DBRefUtils.SEARCH_MODE_FULL); for (DBRefEntry dbr : frefs) { queriesFound.add(dbr.getAccessionId()); @@ -303,36 +304,83 @@ public class ASequenceFetcher public List getSourceProxy(String db) { db = DBRefUtils.getCanonicalName(db); - Map dblist = fetchableDbs.get(db); + Map dblist = fetchableDbs.get(db); if (dblist == null) { - return new ArrayList(); + return new ArrayList<>(); } /* * sort so that primary sources precede secondary */ - List dbs = new ArrayList(dblist.values()); + List dbs = new ArrayList<>(); + for (Entry entry : dblist.entrySet()) + { + DbSourceProxyRoot proxy = entry.getValue(); + if (proxy instanceof DbRoot) + { + proxy = setProxy((DbRoot) proxy, dblist); + } + dbs.add((DbSourceProxy) proxy); + } Collections.sort(dbs, proxyComparator); return dbs; } + class DbRoot implements DbSourceProxyRoot + { + + private String sourceName; + + private String className; + + DbRoot(String sourceName, String className) + { + this.sourceName = sourceName; + this.className = className; + } + + @Override + public String getDbSource() + { + return sourceName; + } + + /** + * lazy class creation + * + * @return the actual proxy object + */ + public DbSourceProxy getProxy() + { + try + { + return (DbSourceProxy) Class.forName(className).newInstance(); + } catch (Exception e) + { + // Serious problems if this happens. + throw new Error(MessageManager.getString( + "error.dbrefsource_implementation_exception"), e); + } + } + + } + /** * constructs an instance of the proxy and registers it as a valid dbrefsource * - * @param dbSourceProxy + * @param dbSourceProxyClass * reference for class implementing * jalview.ws.seqfetcher.DbSourceProxy */ protected void addDBRefSourceImpl( - Class dbSourceProxy) + Class dbSourceProxyClass) throws IllegalArgumentException { DbSourceProxy proxy = null; try { - DbSourceProxy proxyObj = dbSourceProxy.getConstructor().newInstance(); - proxy = proxyObj; + proxy = dbSourceProxyClass.getConstructor().newInstance(); } catch (IllegalArgumentException e) { throw e; @@ -345,39 +393,50 @@ public class ASequenceFetcher addDbRefSourceImpl(proxy); } + public void addDBRefSourceImpl(String sourceName, String className) + { + addDbRefSourceImpl(new DbRoot(sourceName, className)); + } + /** * add the properly initialised DbSourceProxy object 'proxy' to the list of * sequence fetchers * * @param proxy */ - protected void addDbRefSourceImpl(DbSourceProxy proxy) + void addDbRefSourceImpl(DbSourceProxyRoot proxy) { if (proxy != null) { if (fetchableDbs == null) { - fetchableDbs = new Hashtable>(); + fetchableDbs = new Hashtable<>(); } - Map slist = fetchableDbs - .get(proxy.getDbSource()); + String key = proxy.getDbSource(); + Map slist = fetchableDbs.get(key); if (slist == null) { - fetchableDbs.put(proxy.getDbSource(), - slist = new Hashtable()); + fetchableDbs.put(key, slist = new Hashtable<>()); + } + if (proxy instanceof DbRoot) + { + slist.put("", proxy); + } + else + { + slist.put(((DbSourceProxy) proxy).getDbName(), proxy); } - slist.put(proxy.getDbName(), proxy); } } /** * select sources which are implemented by instances of the given class * - * @param class + * @param class1 * that implements DbSourceProxy * @return null or vector of source names for fetchers */ - public String[] getDbInstances(Class class1) + public String[] getDbInstances(Class class1) { if (!DbSourceProxy.class.isAssignableFrom(class1)) { @@ -390,20 +449,25 @@ public class ASequenceFetcher { return null; } - String[] sources = null; - Vector src = new Vector(); - Enumeration dbs = fetchableDbs.keys(); - while (dbs.hasMoreElements()) + Vector src = new Vector<>(); + for (String dbSource : fetchableDbs.keySet()) { - String dbn = dbs.nextElement(); - for (DbSourceProxy dbp : fetchableDbs.get(dbn).values()) + Map dblist = fetchableDbs.get(dbSource); + for (Entry entry : dblist.entrySet()) { - if (class1.isAssignableFrom(dbp.getClass())) + DbSourceProxyRoot proxy = entry.getValue(); + if (proxy instanceof DbRoot) { - src.addElement(dbn); + proxy = setProxy((DbRoot) proxy, dblist); + } + Class c = proxy.getClass(); + if (class1 == c || class1.isAssignableFrom(c)) + { + src.addElement(dbSource); } } } + String[] sources = null; if (src.size() > 0) { src.copyInto(sources = new String[src.size()]); @@ -411,10 +475,24 @@ public class ASequenceFetcher return sources; } - public DbSourceProxy[] getDbSourceProxyInstances(Class class1) + private DbSourceProxyRoot setProxy(DbRoot root, + Map dblist) + { + DbSourceProxy proxy = root.getProxy(); + // Time to create the actual proxy + dblist.remove(""); + dblist.put(proxy.getDbName(), proxy); + return proxy; + } + + public DbSourceProxy[] getDbSourceProxyInstances(Class class1) { - List prlist = new ArrayList(); - for (String fetchable : getSupportedDb()) + if (fetchableDbs == null) + { + return null; + } + List prlist = new ArrayList<>(); + for (String fetchable : fetchableDbs.keySet()) { for (DbSourceProxy pr : getSourceProxy(fetchable)) {