JAL-1705 code tidy/comment only
[jalview.git] / src / jalview / ext / ensembl / EnsemblXref.java
index 6a4f369..f19b4d4 100644 (file)
@@ -17,9 +17,26 @@ import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
 
-public class EnsemblXref extends EnsemblRestClient
+/**
+ * A class to fetch cross-references from Ensembl by calling the /xrefs REST
+ * service
+ * 
+ * @author gmcarstairs
+ * @see http://rest.ensembl.org/documentation/info/xref_id
+ */
+class EnsemblXref extends EnsemblRestClient
 {
 
+  /**
+   * Constructor given the target domain to fetch data from
+   * 
+   * @param d
+   */
+  public EnsemblXref(String d)
+  {
+    super(d);
+  }
+
   @Override
   public String getDbName()
   {
@@ -35,8 +52,7 @@ public class EnsemblXref extends EnsemblRestClient
   @Override
   protected URL getUrl(List<String> ids) throws MalformedURLException
   {
-    // TODO Auto-generated method stub
-    return null;
+    return getUrl(ids.get(0));
   }
 
   @Override
@@ -60,15 +76,16 @@ public class EnsemblXref extends EnsemblRestClient
   /**
    * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
    * ("primary_id") for the given identifier (Ensembl accession id) and database
-   * name. The "dbname" returned by Ensembl is canonicalised to Jalview's
-   * standard version, and a DBRefEntry constructed.
+   * names. The "dbname" returned by Ensembl is canonicalised to Jalview's
+   * standard version, and a DBRefEntry constructed. If no databases are
+   * specified, all available cross-references are retrieved.
    * 
    * @param identifier
-   * @param database
+   * @param databases
    * @return
    */
   public List<DBRefEntry> getCrossReferences(String identifier,
-          String... database)
+          List<String> databases)
   {
     List<DBRefEntry> result = new ArrayList<DBRefEntry>();
     List<String> ids = new ArrayList<String>();
@@ -77,22 +94,12 @@ public class EnsemblXref extends EnsemblRestClient
     BufferedReader br = null;
     try
     {
-      for (String db : database)
-      {
-        URL url = getUrl(identifier, db);
+      URL url = getUrl(identifier);
         if (url != null)
         {
           br = getHttpResponse(url, ids);
         }
-        for (DBRefEntry xref : parseResponse(br))
-        {
-          if (!result.contains(xref))
-          {
-            result.add(xref);
-          }
-        }
-        br.close();
-      }
+      return (parseResponse(br, databases));
     } catch (IOException e)
     {
       // ignore
@@ -114,14 +121,17 @@ public class EnsemblXref extends EnsemblRestClient
   }
 
   /**
-   * Parses "primary_id" and "dbname" values from the JSON response and returns
-   * a list of DBRefEntry constructed.
+   * Parses "primary_id" and "dbname" values from the JSON response and
+   * constructs a DBRefEntry if the dbname is in the list supplied. Returns a
+   * list of DBRefEntry created.
    * 
    * @param br
+   * @param databases
    * @return
    * @throws IOException
    */
-  protected List<DBRefEntry> parseResponse(BufferedReader br)
+  protected List<DBRefEntry> parseResponse(BufferedReader br,
+          List<String> databases)
           throws IOException
   {
     JSONParser jp = new JSONParser();
@@ -134,6 +144,11 @@ public class EnsemblXref extends EnsemblRestClient
       {
         JSONObject val = (JSONObject) rvals.next();
         String dbName = val.get("dbname").toString();
+        if (databases != null && !databases.isEmpty()
+                && !databases.contains(dbName))
+        {
+          continue;
+        }
         String id = val.get("primary_id").toString();
         if (dbName != null && id != null)
         {
@@ -149,10 +164,18 @@ public class EnsemblXref extends EnsemblRestClient
     return result;
   }
 
-  protected URL getUrl(String identifier, String db)
+  /**
+   * Returns the URL for the REST endpoint to fetch all cross-references for an
+   * identifier. Note this may return protein cross-references for nucleotide.
+   * Filter the returned list as required.
+   * 
+   * @param identifier
+   * @return
+   */
+  protected URL getUrl(String identifier)
   {
-    String url = ENSEMBL_REST + "/xrefs/id/" + identifier
-            + "?content-type=application/json&external_db=" + db;
+    String url = getDomain() + "/xrefs/id/" + identifier
+            + "?content-type=application/json&all_levels=1";
     try
     {
       return new URL(url);