JAL-2523 basic sleep and retry (3 times) on 429 response code with
[jalview.git] / src / jalview / ext / ensembl / EnsemblXref.java
index 6a4f369..c002c08 100644 (file)
@@ -1,3 +1,23 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.ext.ensembl;
 
 import jalview.datamodel.AlignmentI;
@@ -17,13 +37,37 @@ 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
 {
 
+  private static final String GO_GENE_ONTOLOGY = "GO";
+
+  private String dbName = "ENSEMBL (xref)";
+
+  /**
+   * Constructor given the target domain to fetch data from
+   * 
+   * @param d
+   */
+  public EnsemblXref(String d, String dbSource, String version)
+  {
+    super(d);
+    dbName = dbSource;
+    xrefVersion = dbSource + ":" + version;
+
+  }
+
   @Override
   public String getDbName()
   {
-    return "ENSEMBL (xref)";
+    return dbName;
   }
 
   @Override
@@ -35,8 +79,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 +103,15 @@ 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. Currently takes all
+   * identifiers apart from GO terms and synonyms.
    * 
    * @param identifier
-   * @param database
+   *          an Ensembl stable identifier
    * @return
    */
-  public List<DBRefEntry> getCrossReferences(String identifier,
-          String... database)
+  public List<DBRefEntry> getCrossReferences(String identifier)
   {
     List<DBRefEntry> result = new ArrayList<DBRefEntry>();
     List<String> ids = new ArrayList<String>();
@@ -77,21 +120,14 @@ public class EnsemblXref extends EnsemblRestClient
     BufferedReader br = null;
     try
     {
-      for (String db : database)
+      URL url = getUrl(identifier);
+      if (url != null)
       {
-        URL url = getUrl(identifier, db);
-        if (url != null)
+        br = getHttpResponse(url, ids);
+        if (br != null)
         {
-          br = getHttpResponse(url, ids);
+          result = parseResponse(br);
         }
-        for (DBRefEntry xref : parseResponse(br))
-        {
-          if (!result.contains(xref))
-          {
-            result.add(xref);
-          }
-        }
-        br.close();
       }
     } catch (IOException e)
     {
@@ -114,8 +150,10 @@ 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. Returns a list of the DBRefEntry created. Note we
+   * don't parse "synonyms" as they appear to be either redirected or obsolete
+   * in Uniprot.
    * 
    * @param br
    * @return
@@ -133,12 +171,16 @@ public class EnsemblXref extends EnsemblRestClient
       while (rvals.hasNext())
       {
         JSONObject val = (JSONObject) rvals.next();
-        String dbName = val.get("dbname").toString();
+        String dbname = val.get("dbname").toString();
+        if (GO_GENE_ONTOLOGY.equals(dbname))
+        {
+          continue;
+        }
         String id = val.get("primary_id").toString();
-        if (dbName != null && id != null)
+        if (dbname != null && id != null)
         {
-          dbName = DBRefUtils.getCanonicalName(dbName);
-          DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
+          dbname = DBRefUtils.getCanonicalName(dbname);
+          DBRefEntry dbref = new DBRefEntry(dbname, getXRefVersion(), id);
           result.add(dbref);
         }
       }
@@ -149,10 +191,30 @@ public class EnsemblXref extends EnsemblRestClient
     return result;
   }
 
-  protected URL getUrl(String identifier, String db)
+  private String xrefVersion = "ENSEMBL:0";
+
+  /**
+   * version string for Xrefs - for 2.10, hardwired for ENSEMBL:0
+   * 
+   * @return
+   */
+  public String getXRefVersion()
+  {
+    return xrefVersion;
+  }
+
+  /**
+   * 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);