JAL-1941 added test URL and notes about PyRNA restclient
[jalview.git] / src / jalview / ext / ensembl / EnsemblXref.java
index 36bd7c5..fa86865 100644 (file)
@@ -17,9 +17,28 @@ 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";
+
+  /**
+   * Constructor given the target domain to fetch data from
+   * 
+   * @param d
+   */
+  public EnsemblXref(String d)
+  {
+    super(d);
+  }
+
   @Override
   public String getDbName()
   {
@@ -35,8 +54,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
@@ -61,15 +79,14 @@ 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
    * 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.
+   * standard version, and a DBRefEntry constructed. Currently takes all
+   * identifiers apart from GO terms and synonyms.
    * 
    * @param identifier
-   * @param databases
+   *          an Ensembl stable identifier
    * @return
    */
-  public List<DBRefEntry> getCrossReferences(String identifier,
-          List<String> databases)
+  public List<DBRefEntry> getCrossReferences(String identifier)
   {
     List<DBRefEntry> result = new ArrayList<DBRefEntry>();
     List<String> ids = new ArrayList<String>();
@@ -79,11 +96,11 @@ public class EnsemblXref extends EnsemblRestClient
     try
     {
       URL url = getUrl(identifier);
-        if (url != null)
-        {
-          br = getHttpResponse(url, ids);
-        }
-      return (parseResponse(br, databases));
+      if (url != null)
+      {
+        br = getHttpResponse(url, ids);
+      }
+      return (parseResponse(br));
     } catch (IOException e)
     {
       // ignore
@@ -106,16 +123,15 @@ public class EnsemblXref extends EnsemblRestClient
 
   /**
    * 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.
+   * 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
-   * @param databases
    * @return
    * @throws IOException
    */
-  protected List<DBRefEntry> parseResponse(BufferedReader br,
-          List<String> databases)
+  protected List<DBRefEntry> parseResponse(BufferedReader br)
           throws IOException
   {
     JSONParser jp = new JSONParser();
@@ -128,8 +144,7 @@ public class EnsemblXref extends EnsemblRestClient
       {
         JSONObject val = (JSONObject) rvals.next();
         String dbName = val.get("dbname").toString();
-        if (databases != null && !databases.isEmpty()
-                && !databases.contains(dbName))
+        if (dbName.equals(GO_GENE_ONTOLOGY))
         {
           continue;
         }
@@ -158,7 +173,7 @@ public class EnsemblXref extends EnsemblRestClient
    */
   protected URL getUrl(String identifier)
   {
-    String url = ENSEMBL_REST + "/xrefs/id/" + identifier
+    String url = getDomain() + "/xrefs/id/" + identifier
             + "?content-type=application/json&all_levels=1";
     try
     {