JSON refactoring
[jalview.git] / src / jalview / ext / ensembl / EnsemblXref.java
index d4c5b18..eedfe97 100644 (file)
@@ -1,8 +1,29 @@
+/*
+ * 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;
 import jalview.datamodel.DBRefEntry;
 import jalview.util.DBRefUtils;
+import jalview.util.JSONUtils;
 
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -11,10 +32,8 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
-import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
 
 /**
@@ -22,15 +41,32 @@ import org.json.simple.parser.ParseException;
  * 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
@@ -42,8 +78,7 @@ 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
@@ -52,107 +87,74 @@ class EnsemblXref extends EnsemblRestClient
     return true;
   }
 
-  @Override
-  protected String getRequestMimeType(boolean multipleIds)
-  {
-    return "application/json";
-  }
-
-  @Override
-  protected String getResponseMimeType()
-  {
-    return "application/json";
-  }
-
   /**
    * 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)
+  @SuppressWarnings("unchecked")
+  public List<DBRefEntry> getCrossReferences(String identifier)
   {
-    List<DBRefEntry> result = new ArrayList<DBRefEntry>();
-    List<String> ids = new ArrayList<String>();
+    List<DBRefEntry> result = new ArrayList<>();
+    List<String> ids = new ArrayList<>();
     ids.add(identifier);
 
-    BufferedReader br = null;
     try
     {
-      URL url = getUrl(identifier);
-        if (url != null)
-        {
-          br = getHttpResponse(url, ids);
-        }
-      return (parseResponse(br, databases));
-    } catch (IOException e)
-    {
-      // ignore
-    } finally
-    {
-      if (br != null)
+      Iterator<Object> rvals = (Iterator<Object>) getJSON(getUrl(identifier), ids, -1, MODE_ITERATOR, null);
+      while (rvals.hasNext())
       {
-        try
-        {
-          br.close();
-        } catch (IOException e)
+        Map<String, Object> val = (Map<String, Object>) rvals.next();
+        String db = val.get("dbname").toString();
+        String id = val.get("primary_id").toString();
+        if (db != null && id != null
+                && !GO_GENE_ONTOLOGY.equals(db))
         {
-          // ignore
+          db = DBRefUtils.getCanonicalName(db);
+          DBRefEntry dbref = new DBRefEntry(db, getXRefVersion(), id);
+          result.add(dbref);
         }
       }
+    } catch (ParseException | IOException e)
+    {
+      // ignore
     }
-
     return result;
   }
 
+//  /**
+//   * 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
+//   * @throws IOException
+//   */
+//  @SuppressWarnings("unchecked")
+//protected List<DBRefEntry> parseResponse(BufferedReader br)
+//          throws IOException
+//  {
+//    return result;
+//  }
+//
+  private String xrefVersion = "ENSEMBL:0";
+
   /**
-   * 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.
+   * version string for Xrefs - for 2.10, hardwired for ENSEMBL:0
    * 
-   * @param br
-   * @param databases
    * @return
-   * @throws IOException
    */
-  protected List<DBRefEntry> parseResponse(BufferedReader br,
-          List<String> databases)
-          throws IOException
+  public String getXRefVersion()
   {
-    JSONParser jp = new JSONParser();
-    List<DBRefEntry> result = new ArrayList<DBRefEntry>();
-    try
-    {
-      JSONArray responses = (JSONArray) jp.parse(br);
-      Iterator rvals = responses.iterator();
-      while (rvals.hasNext())
-      {
-        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)
-        {
-          dbName = DBRefUtils.getCanonicalName(dbName);
-          DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
-          result.add(dbref);
-        }
-      }
-    } catch (ParseException e)
-    {
-      // ignore
-    }
-    return result;
+    return xrefVersion;
   }
 
   /**
@@ -165,8 +167,8 @@ class EnsemblXref extends EnsemblRestClient
    */
   protected URL getUrl(String identifier)
   {
-    String url = ENSEMBL_REST + "/xrefs/id/" + identifier
-            + "?content-type=application/json&all_levels=1";
+    String url = getDomain() + "/xrefs/id/" + identifier
+            + CONTENT_TYPE_JSON + "&all_levels=1";
     try
     {
       return new URL(url);