*/
public class EBIFetchClient
{
- String format = "default";
-
- String style = "raw";
/**
* Creates a new EBIFetchClient object.
* the query formatted as db:query1;query2;query3
* @param format
* the format wanted
- * @param s
- * - unused parameter
+ * @param extension
+ * for the temporary file to hold response
* @return the file holding the response
* @throws OutOfMemoryError
*/
- public File fetchDataAsFile(String ids, String format, String s,
- String ext)
+ public File fetchDataAsFile(String ids, String format, String ext)
throws OutOfMemoryError
{
File outFile = null;
{
outFile = File.createTempFile("jalview", ext);
outFile.deleteOnExit();
- fetchData(ids, format, s, outFile);
+ fetchData(ids, format, outFile);
if (outFile.length() == 0)
{
outFile.delete();
return outFile;
}
- /**
- * Single DB multiple record retrieval
- *
- * @param ids
- * db:query1;query2;query3
- * @param format
- * raw/xml
- * @param s
- * not used - remove?
- *
- * @return Raw string array result of query set
- */
- public String[] fetchData(String ids, String format, String s)
- throws OutOfMemoryError
- {
- return fetchData(ids, format, s, null);
- }
-
- String[] fetchData(String ids, String f, String s, File outFile)
+ String[] fetchData(String ids, String f, File outFile)
throws OutOfMemoryError
{
- // Need to split
- // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
String[] rslts = new String[0];
- StringTokenizer queries = new StringTokenizer(ids, ";");
- String db = null;
- StringBuffer querystring = null;
- int nq = 0;
- while (queries.hasMoreTokens())
- {
- String query = queries.nextToken();
- int p;
- if ((p = query.indexOf(':')) > -1)
- {
- db = query.substring(0, p);
- query = query.substring(p + 1);
- }
- if (querystring == null)
- {
- querystring = new StringBuffer(query);
- nq++;
- }
- else
- {
- querystring.append("," + query);
- nq++;
- }
- }
+ StringBuilder querystring = new StringBuilder(ids.length());
+ String db = parseIds(ids, querystring);
if (db == null)
{
- System.err.println("Invalid Query string : '" + ids
- + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
+ System.err.println("Invalid Query string : '" + ids + "'");
+ System.err.println("Should be of form 'dbname:q1;q2;q3;q4'");
return null;
}
- String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
+ String[] rslt = fetchBatch(querystring.toString(), db, f, outFile);
if (rslt != null)
{
String[] nrslts = new String[rslt.length + rslts.length];
return (rslts.length == 0 ? null : rslts);
}
- public String[] fetchBatch(String ids, String dbPath, String format, String s,
- File outFile) throws OutOfMemoryError
+ /**
+ * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds
+ * queries as comma-separated items to the querystring. dbname must be
+ * specified for at least one queryId. Returns null if a mixture of different
+ * dbnames is found (ignoring case).
+ *
+ * @param ids
+ * @param queryString
+ * @return
+ */
+ static String parseIds(String ids, StringBuilder queryString)
{
- // long time = System.currentTimeMillis();
- /*
- * JAL-1855 dbfetch from ena_sequence, ena_coding
- */
- String url;
- if (dbPath.equalsIgnoreCase(DBRefSource.EMBL)
- || dbPath.equalsIgnoreCase(DBRefSource.EMBLCDS))
- {
- url = "http://www.ebi.ac.uk/ena/data/view/" + ids.toLowerCase()
- + (format != null ? "&" + format : "");
- }
- else
+ String database = null;
+ StringTokenizer queries = new StringTokenizer(ids, ";");
+ boolean appending = queryString.length() > 0;
+ while (queries.hasMoreTokens())
{
- url = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
- + dbPath.toLowerCase() + "/" + ids.toLowerCase()
- + (format != null ? "/" + format : "");
+ String query = queries.nextToken();
+ int p = query.indexOf(':');
+ if (p > -1)
+ {
+ String db = query.substring(0, p);
+ if (database != null && !db.equalsIgnoreCase(database))
+ {
+ /*
+ * different databases mixed in together - invalid
+ */
+ return null;
+ }
+ database = db;
+ query = query.substring(p + 1);
+ }
+ queryString.append(appending ? "," : "");
+ queryString.append(query);
+ appending = true;
}
+ return database;
+ }
+
+ String[] fetchBatch(String ids, String dbPath, String format,
+ File outFile) throws OutOfMemoryError
+ {
+ // long time = System.currentTimeMillis();
+ String url = buildUrl(ids, dbPath, format);
try
{
}
return null;
}
+
+ /**
+ * Constructs the URL to fetch from
+ *
+ * @param ids
+ * @param database
+ * @param format
+ * @return
+ */
+ static String buildUrl(String ids, String database, String format)
+ {
+ String url;
+ if (database.equalsIgnoreCase(DBRefSource.EMBL)
+ || database.equalsIgnoreCase(DBRefSource.EMBLCDS))
+ {
+ url = "http://www.ebi.ac.uk/ena/data/view/" + ids.toLowerCase()
+ + (format != null ? "&" + format : "");
+ }
+ else
+ {
+ url = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
+ + database.toLowerCase() + "/" + ids.toLowerCase()
+ + (format != null ? "/" + format : "");
+ }
+ return url;
+ }
}
--- /dev/null
+package jalview.ws.ebi;
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertNull;
+
+import org.testng.annotations.Test;
+
+public class EBIFetchClientTest
+{
+ /**
+ * Test method that constructs URL to fetch from
+ */
+ @Test(groups = "Functional")
+ public void testBuildUrl()
+ {
+ /*
+ * EMBL
+ */
+ assertEquals("http://www.ebi.ac.uk/ena/data/view/x53838&display=xml",
+ EBIFetchClient.buildUrl("X53838", "EMBL", "display=xml"));
+
+ /*
+ * EMBLCDS
+ */
+ assertEquals("http://www.ebi.ac.uk/ena/data/view/caa37824&display=xml",
+ EBIFetchClient.buildUrl("CAA37824", "EMBL", "display=xml"));
+
+ /*
+ * Uniprot
+ */
+ assertEquals(
+ "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/uniprot/p00340/uniprotxml",
+ EBIFetchClient.buildUrl("P00340", "UNIPROT", "uniprotxml"));
+
+ /*
+ * PDB / pdb
+ */
+ assertEquals("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/pdb/3a6s/pdb",
+ EBIFetchClient.buildUrl("3A6S", "PDB", "pdb"));
+
+ /*
+ * PDB / mmCIF
+ */
+ assertEquals(
+ "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/pdb/3a6s/mmCIF",
+ EBIFetchClient.buildUrl("3A6S", "PDB", "mmCIF"));
+ }
+
+ /**
+ * Test method that parses db:id;id;id
+ */
+ @Test(groups = "Functional")
+ public void testParseIds()
+ {
+ /*
+ * pdb, two accessions
+ */
+ StringBuilder queries = new StringBuilder();
+ String db = EBIFetchClient.parseIds("pdb:3a6s;1A70", queries);
+ assertEquals("pdb", db);
+ assertEquals("3a6s,1A70", queries.toString());
+
+ /*
+ * pdb specified on second accession
+ */
+ queries.setLength(0);
+ queries = new StringBuilder();
+ db = EBIFetchClient.parseIds("3a6s;pdb:1A70", queries);
+ assertEquals("pdb", db);
+ assertEquals("3a6s,1A70", queries.toString());
+
+ /*
+ * uniprot, one accession
+ */
+ queries.setLength(0);
+ db = EBIFetchClient.parseIds("uniprot:P00340", queries);
+ assertEquals("uniprot", db);
+ assertEquals("P00340", queries.toString());
+
+ /*
+ * uniprot, one accession, appending to existing queries
+ */
+ queries.setLength(0);
+ queries.append("P30419");
+ db = EBIFetchClient.parseIds("uniprot:P00340", queries);
+ assertEquals("uniprot", db);
+ assertEquals("P30419,P00340", queries.toString());
+
+ /*
+ * pdb and uniprot mixed - rejected
+ */
+ queries.setLength(0);
+ db = EBIFetchClient.parseIds("pdb:3a6s;1a70;uniprot:P00340", queries);
+ assertNull(db);
+ assertEquals("3a6s,1a70", queries.toString());
+
+ /*
+ * pdb and PDB mixed - ok
+ */
+ queries.setLength(0);
+ db = EBIFetchClient.parseIds("pdb:3a6s;pdb:1a70;PDB:1QIP", queries);
+ assertEquals("PDB", db);
+ assertEquals("3a6s,1a70,1QIP", queries.toString());
+
+ /*
+ * no database (improper format)
+ */
+ queries.setLength(0);
+ db = EBIFetchClient.parseIds("P00340", queries);
+ assertNull(db);
+ assertEquals("P00340", queries.toString());
+ }
+}