JAL-1705 fetch Uniprot and PDB xrefs for Ensembl protein products
[jalview.git] / src / jalview / ext / ensembl / EnsemblXref.java
1 package jalview.ext.ensembl;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.DBRefEntry;
5 import jalview.util.DBRefUtils;
6
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14
15 import org.json.simple.JSONArray;
16 import org.json.simple.JSONObject;
17 import org.json.simple.parser.JSONParser;
18 import org.json.simple.parser.ParseException;
19
20 public class EnsemblXref extends EnsemblRestClient
21 {
22
23   @Override
24   public String getDbName()
25   {
26     return "ENSEMBL (xref)";
27   }
28
29   @Override
30   public AlignmentI getSequenceRecords(String queries) throws Exception
31   {
32     return null;
33   }
34
35   @Override
36   protected URL getUrl(List<String> ids) throws MalformedURLException
37   {
38     // TODO Auto-generated method stub
39     return null;
40   }
41
42   @Override
43   protected boolean useGetRequest()
44   {
45     return true;
46   }
47
48   @Override
49   protected String getRequestMimeType(boolean multipleIds)
50   {
51     return "application/json";
52   }
53
54   @Override
55   protected String getResponseMimeType()
56   {
57     return "application/json";
58   }
59
60   /**
61    * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
62    * ("primary_id") for the given identifier (Ensembl accession id) and database
63    * name. The "dbname" returned by Ensembl is canonicalised to Jalview's
64    * standard version, and a DBRefEntry constructed.
65    * 
66    * @param identifier
67    * @param database
68    * @return
69    */
70   public List<DBRefEntry> getCrossReferences(String identifier,
71           String... database)
72   {
73     List<DBRefEntry> result = new ArrayList<DBRefEntry>();
74     List<String> ids = new ArrayList<String>();
75     ids.add(identifier);
76
77     BufferedReader br = null;
78     try
79     {
80       for (String db : database)
81       {
82         URL url = getUrl(identifier, db);
83         if (url != null)
84         {
85           br = getHttpResponse(url, ids);
86         }
87         for (DBRefEntry xref : parseResponse(br))
88         {
89           if (!result.contains(xref))
90           {
91             result.add(xref);
92           }
93         }
94         br.close();
95       }
96     } catch (IOException e)
97     {
98       // ignore
99     } finally
100     {
101       if (br != null)
102       {
103         try
104         {
105           br.close();
106         } catch (IOException e)
107         {
108           // ignore
109         }
110       }
111     }
112
113     return result;
114   }
115
116   /**
117    * Parses "primary_id" and "dbname" values from the JSON response and returns
118    * a list of DBRefEntry constructed.
119    * 
120    * @param br
121    * @return
122    * @throws IOException
123    */
124   protected List<DBRefEntry> parseResponse(BufferedReader br)
125           throws IOException
126   {
127     JSONParser jp = new JSONParser();
128     List<DBRefEntry> result = new ArrayList<DBRefEntry>();
129     try
130     {
131       JSONArray responses = (JSONArray) jp.parse(br);
132       Iterator rvals = responses.iterator();
133       while (rvals.hasNext())
134       {
135         JSONObject val = (JSONObject) rvals.next();
136         String dbName = val.get("dbname").toString();
137         String id = val.get("primary_id").toString();
138         if (dbName != null && id != null)
139         {
140           dbName = DBRefUtils.getCanonicalName(dbName);
141           DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
142           result.add(dbref);
143         }
144       }
145     } catch (ParseException e)
146     {
147       // ignore
148     }
149     return result;
150   }
151
152   protected URL getUrl(String identifier, String db)
153   {
154     String url = ENSEMBL_REST + "/xrefs/id/" + identifier
155             + "?content-type=application/json&external_db=" + db;
156     try
157     {
158       return new URL(url);
159     } catch (MalformedURLException e)
160     {
161       return null;
162     }
163   }
164
165 }