*/
package jalview.ws.seqfetcher;
+import jalview.api.FeatureSettingsModelI;
import jalview.bin.Cache;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.DBRefEntry;
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;
/*
* set of databases we can retrieve entries from
*/
- protected Hashtable<String, Map<String, DbSourceProxy>> fetchableDbs;
+ protected Hashtable<String, Map<String, DbSourceProxyRoot>> fetchableDbs;
/*
* comparator to sort by tier (0/1/2) and name
/**
* Constructor
*/
- public ASequenceFetcher()
+ protected ASequenceFetcher()
{
- super();
-
/*
* comparator to sort proxies by tier and name
*/
{
return null;
}
- String[] sf = fetchableDbs.keySet().toArray(
- new String[fetchableDbs.size()]);
+ String[] sf = fetchableDbs.keySet()
+ .toArray(new String[fetchableDbs.size()]);
return sf;
}
return true;
}
}
- Cache.log.warn("isFetchable doesn't know about '" + source
- + "'");
+ Cache.log.warn("isFetchable doesn't know about '" + source + "'");
return false;
}
* if true, only fetch from nucleotide data sources, else peptide
* @return
*/
- public SequenceI[] getSequences(DBRefEntry[] refs, boolean dna)
+ public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
{
- Vector<SequenceI> rseqs = new Vector<SequenceI>();
- Hashtable<String, List<String>> queries = new Hashtable<String, List<String>>();
- for (int r = 0; r < refs.length; r++)
+ Vector<SequenceI> rseqs = new Vector<>();
+ Hashtable<String, List<String>> queries = new Hashtable<>();
+ for (DBRefEntry ref : refs)
{
- if (!queries.containsKey(refs[r].getSource()))
+ String canonical = DBRefUtils.getCanonicalName(ref.getSource());
+ if (!queries.containsKey(canonical))
{
- queries.put(refs[r].getSource(), new ArrayList<String>());
+ queries.put(canonical, new ArrayList<String>());
}
- List<String> qset = queries.get(refs[r].getSource());
- if (!qset.contains(refs[r].getAccessionId()))
+ List<String> qset = queries.get(canonical);
+ if (!qset.contains(ref.getAccessionId()))
{
- qset.add(refs[r].getAccessionId());
+ qset.add(ref.getAccessionId());
}
}
Enumeration<String> e = queries.keys();
continue;
}
- Stack<String> queriesLeft = new Stack<String>();
+ Stack<String> queriesLeft = new Stack<>();
queriesLeft.addAll(query);
- for (DbSourceProxy fetcher : getSourceProxy(db))
+ List<DbSourceProxy> proxies = getSourceProxy(db);
+ for (DbSourceProxy fetcher : proxies)
{
- List<String> queriesMade = new ArrayList<String>();
- HashSet<String> queriesFound = new HashSet<String>();
+ List<String> queriesMade = new ArrayList<>();
+ HashSet<String> queriesFound = new HashSet<>();
try
{
if (fetcher.isDnaCoding() != dna)
{
continue; // wrong sort of data
}
- boolean doMultiple = fetcher.getAccessionSeparator() != null;
- // No separator - no Multiple Queries
+ boolean doMultiple = fetcher.getMaximumQueryCount() > 1;
while (!queriesLeft.isEmpty())
{
StringBuffer qsb = new StringBuffer();
try
{
// create a fetcher and go to it
- seqset = fetcher.getSequenceRecords(qsb.toString()); // ,
- // queriesFailed);
+ seqset = fetcher.getSequenceRecords(qsb.toString());
} catch (Exception ex)
{
- System.err.println("Failed to retrieve the following from "
- + db);
+ System.err.println(
+ "Failed to retrieve the following from " + db);
System.err.println(qsb);
ex.printStackTrace(System.err);
}
for (int is = 0; is < seqs.length; is++)
{
rseqs.addElement(seqs[is]);
- DBRefEntry[] frefs = DBRefUtils.searchRefs(seqs[is]
- .getDBRefs(), new DBRefEntry(db, null, null));
- if (frefs != null)
+ // BH 2015.01.25 check about version/accessid being null here
+ List<DBRefEntry> frefs = DBRefUtils.searchRefs(
+ seqs[is].getDBRefs(),
+ new DBRefEntry(db, null, null), DBRefUtils.SEARCH_MODE_FULL);
+ for (DBRefEntry dbr : frefs)
{
- for (DBRefEntry dbr : frefs)
- {
- queriesFound.add(dbr.getAccessionId());
- queriesMade.remove(dbr.getAccessionId());
- }
+ queriesFound.add(dbr.getAccessionId());
+ queriesMade.remove(dbr.getAccessionId());
}
seqs[is] = null;
}
{
if (fetcher.getRawRecords() != null)
{
- System.out.println("# Retrieved from " + db + ":"
- + qsb.toString());
+ System.out.println(
+ "# Retrieved from " + db + ":" + qsb.toString());
StringBuffer rrb = fetcher.getRawRecords();
/*
* for (int rr = 0; rr<rrb.length; rr++) {
{
System.out.println("# Adding " + queriesMade.size()
+ " ids back to queries list for searching again (" + db
- + ".");
+ + ")");
queriesLeft.addAll(queriesMade);
}
}
Exception ex)
{
- System.err.println("Failed to retrieve the following references from "
- + db);
+ System.err.println(
+ "Failed to retrieve the following references from " + db);
int n = 0;
for (String qv : queriesMade)
{
public List<DbSourceProxy> getSourceProxy(String db)
{
db = DBRefUtils.getCanonicalName(db);
- Map<String, DbSourceProxy> dblist = fetchableDbs.get(db);
+ Map<String, DbSourceProxyRoot> dblist = fetchableDbs.get(db);
if (dblist == null)
{
- return new ArrayList<DbSourceProxy>();
+ return new ArrayList<>();
}
/*
* sort so that primary sources precede secondary
*/
- List<DbSourceProxy> dbs = new ArrayList<DbSourceProxy>(dblist.values());
+ List<DbSourceProxy> dbs = new ArrayList<>();
+ for (Entry<String, DbSourceProxyRoot> 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<? extends DbSourceProxy> dbSourceProxy)
+ Class<? extends DbSourceProxy> dbSourceProxyClass)
throws IllegalArgumentException
{
DbSourceProxy proxy = null;
try
{
- DbSourceProxy proxyObj = dbSourceProxy.getConstructor().newInstance();
- proxy = proxyObj;
+ proxy = dbSourceProxyClass.getConstructor().newInstance();
} catch (IllegalArgumentException e)
{
throw e;
} catch (Exception e)
{
// Serious problems if this happens.
- throw new Error(
- MessageManager
- .getString("error.dbrefsource_implementation_exception"),
- e);
+ throw new Error(MessageManager
+ .getString("error.dbrefsource_implementation_exception"), e);
}
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<String, Map<String, DbSourceProxy>>();
+ fetchableDbs = new Hashtable<>();
}
- Map<String, DbSourceProxy> slist = fetchableDbs.get(proxy
- .getDbSource());
+ String key = proxy.getDbSource();
+ Map<String, DbSourceProxyRoot> slist = fetchableDbs.get(key);
if (slist == null)
{
- fetchableDbs.put(proxy.getDbSource(),
- slist = new Hashtable<String, DbSourceProxy>());
+ 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 that implements DbSourceProxy
+ * @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))
{
- throw new Error(
- MessageManager
- .formatMessage(
- "error.implementation_error_dbinstance_must_implement_interface",
- new String[] { class1.toString() }));
+ throw new Error(MessageManager.formatMessage(
+ "error.implementation_error_dbinstance_must_implement_interface",
+ new String[]
+ { class1.toString() }));
}
if (fetchableDbs == null)
{
return null;
}
- String[] sources = null;
- Vector<String> src = new Vector<String>();
- Enumeration<String> dbs = fetchableDbs.keys();
- while (dbs.hasMoreElements())
+ Vector<String> src = new Vector<>();
+ for (String dbSource : fetchableDbs.keySet())
{
- String dbn = dbs.nextElement();
- for (DbSourceProxy dbp : fetchableDbs.get(dbn).values())
+ Map<String, DbSourceProxyRoot> dblist = fetchableDbs.get(dbSource);
+ for (Entry<String, DbSourceProxyRoot> entry : dblist.entrySet())
{
- if (class1.isAssignableFrom(dbp.getClass()))
+ DbSourceProxyRoot proxy = entry.getValue();
+ if (proxy instanceof DbRoot)
+ {
+ proxy = setProxy((DbRoot) proxy, dblist);
+ }
+ Class<?> c = proxy.getClass();
+ if (class1 == c || class1.isAssignableFrom(c))
{
- src.addElement(dbn);
+ src.addElement(dbSource);
}
}
}
+ String[] sources = null;
if (src.size() > 0)
{
src.copyInto(sources = new String[src.size()]);
return sources;
}
- public DbSourceProxy[] getDbSourceProxyInstances(Class class1)
+ private DbSourceProxyRoot setProxy(DbRoot root,
+ Map<String, DbSourceProxyRoot> 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<DbSourceProxy> prlist = new ArrayList<DbSourceProxy>();
- for (String fetchable : getSupportedDb())
+ if (fetchableDbs == null)
+ {
+ return null;
+ }
+ List<DbSourceProxy> prlist = new ArrayList<>();
+ for (String fetchable : fetchableDbs.keySet())
{
for (DbSourceProxy pr : getSourceProxy(fetchable))
{
return prlist.toArray(new DbSourceProxy[0]);
}
+ /**
+ * Returns a preferred feature colouring scheme for the given source, or null
+ * if none is defined.
+ *
+ * @param source
+ * @return
+ */
+ public FeatureSettingsModelI getFeatureColourScheme(String source)
+ {
+ /*
+ * return the first non-null colour scheme for any proxy for
+ * this database source
+ */
+ for (DbSourceProxy proxy : getSourceProxy(source))
+ {
+ FeatureSettingsModelI preferredColours = proxy
+ .getFeatureColourScheme();
+ if (preferredColours != null)
+ {
+ return preferredColours;
+ }
+ }
+ return null;
+ }
}