JAL-2113 EBIFetchClient refactor / unit tests / remove unused fields and
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 23 May 2016 10:07:16 +0000 (11:07 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 23 May 2016 10:07:16 +0000 (11:07 +0100)
parameters

src/MCview/PDBViewer.java
src/jalview/ws/dbsources/EmblXmlSource.java
src/jalview/ws/dbsources/Pdb.java
src/jalview/ws/dbsources/Uniprot.java
src/jalview/ws/ebi/EBIFetchClient.java
test/jalview/ws/ebi/EBIFetchClientTest.java [new file with mode: 0644]

index e032c7a..d5f0d0b 100755 (executable)
@@ -151,7 +151,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
     {
       EBIFetchClient ebi = new EBIFetchClient();
       String query = "pdb:" + pdbentry.getId();
-      pdbentry.setFile(ebi.fetchDataAsFile(query, "default", "raw", ".xml")
+      pdbentry.setFile(ebi.fetchDataAsFile(query, "default", ".xml")
               .getAbsolutePath());
 
       if (pdbentry.getFile() != null)
index b71f032..2049766 100644 (file)
@@ -64,7 +64,6 @@ public abstract class EmblXmlSource extends EbiFileRetrievedProxy
     {
       reply = dbFetch.fetchDataAsFile(
               emprefx.toLowerCase() + ":" + query.trim(), "display=xml",
-              null,
               ".xml");
     } catch (Exception e)
     {
index 4a089f7..0d590b0 100644 (file)
@@ -136,7 +136,7 @@ public class Pdb extends EbiFileRetrievedProxy
             : ".xml";
     EBIFetchClient ebi = new EBIFetchClient();
     file = ebi.fetchDataAsFile("pdb:" + id,
-            getCurrentDefaultFomart().toLowerCase(), "raw", ext)
+            getCurrentDefaultFomart().toLowerCase(), ext)
             .getAbsolutePath();
     stopQuery();
     if (file == null)
index 17f1842..8cc0ce4 100644 (file)
@@ -165,7 +165,7 @@ public class Uniprot extends DbSourceProxyImpl
       // uniprotxml parameter required since december 2007
       // uniprotkb dbname changed introduced december 2008
       File file = ebi.fetchDataAsFile("uniprotkb:" + queries, "uniprotxml",
-              null, ".xml");
+              ".xml");
       Vector<UniprotEntry> entries = getUniprotEntries(new FileReader(file));
 
       if (entries != null)
index f55aad4..dd3cc16 100644 (file)
@@ -42,9 +42,6 @@ import java.util.StringTokenizer;
  */
 public class EBIFetchClient
 {
-  String format = "default";
-
-  String style = "raw";
 
   /**
    * Creates a new EBIFetchClient object.
@@ -93,14 +90,13 @@ public class EBIFetchClient
    *          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;
@@ -108,7 +104,7 @@ public class EBIFetchClient
     {
       outFile = File.createTempFile("jalview", ext);
       outFile.deleteOnExit();
-      fetchData(ids, format, s, outFile);
+      fetchData(ids, format, outFile);
       if (outFile.length() == 0)
       {
         outFile.delete();
@@ -120,61 +116,19 @@ public class EBIFetchClient
     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];
@@ -186,26 +140,50 @@ public class EBIFetchClient
     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
     {
@@ -260,4 +238,30 @@ public class EBIFetchClient
     }
     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;
+  }
 }
diff --git a/test/jalview/ws/ebi/EBIFetchClientTest.java b/test/jalview/ws/ebi/EBIFetchClientTest.java
new file mode 100644 (file)
index 0000000..4eaa5b1
--- /dev/null
@@ -0,0 +1,113 @@
+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());
+  }
+}