--- /dev/null
+package jalview.ext.ensembl;
+
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.DBRefEntry;
+import jalview.util.DBRefUtils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+
+public class EnsemblXref extends EnsemblRestClient
+{
+
+ @Override
+ public String getDbName()
+ {
+ return "ENSEMBL (xref)";
+ }
+
+ @Override
+ public AlignmentI getSequenceRecords(String queries) throws Exception
+ {
+ return null;
+ }
+
+ @Override
+ protected URL getUrl(List<String> ids) throws MalformedURLException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ protected boolean useGetRequest()
+ {
+ 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
+ * name. The "dbname" returned by Ensembl is canonicalised to Jalview's
+ * standard version, and a DBRefEntry constructed.
+ *
+ * @param identifier
+ * @param database
+ * @return
+ */
+ public List<DBRefEntry> getCrossReferences(String identifier,
+ String... database)
+ {
+ List<DBRefEntry> result = new ArrayList<DBRefEntry>();
+ List<String> ids = new ArrayList<String>();
+ ids.add(identifier);
+
+ BufferedReader br = null;
+ try
+ {
+ for (String db : database)
+ {
+ URL url = getUrl(identifier, db);
+ if (url != null)
+ {
+ br = getHttpResponse(url, ids);
+ }
+ for (DBRefEntry xref : parseResponse(br))
+ {
+ if (!result.contains(xref))
+ {
+ result.add(xref);
+ }
+ }
+ br.close();
+ }
+ } catch (IOException e)
+ {
+ // ignore
+ } finally
+ {
+ if (br != null)
+ {
+ try
+ {
+ br.close();
+ } catch (IOException e)
+ {
+ // ignore
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Parses "primary_id" and "dbname" values from the JSON response and returns
+ * a list of DBRefEntry constructed.
+ *
+ * @param br
+ * @return
+ * @throws IOException
+ */
+ protected List<DBRefEntry> parseResponse(BufferedReader br)
+ throws IOException
+ {
+ 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();
+ 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;
+ }
+
+ protected URL getUrl(String identifier, String db)
+ {
+ String url = ENSEMBL_REST + "/xrefs/id/" + identifier
+ + "?content-type=application/json&external_db=" + db;
+ try
+ {
+ return new URL(url);
+ } catch (MalformedURLException e)
+ {
+ return null;
+ }
+ }
+
+}