c5945aea124abf085fa84a0e23b66d84004c3892
[jalview.git] / src / jalview / ext / ensembl / EnsemblLookup.java
1 package jalview.ext.ensembl;
2
3 import jalview.datamodel.AlignmentI;
4
5 import java.io.BufferedReader;
6 import java.io.IOException;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.util.Arrays;
10 import java.util.List;
11
12 import org.json.simple.JSONObject;
13 import org.json.simple.parser.JSONParser;
14 import org.json.simple.parser.ParseException;
15
16 /**
17  * A client for the Ensembl lookup REST endpoint; used to find the Parent gene
18  * identifier given a transcript identifier.
19  * 
20  * @author gmcarstairs
21  *
22  */
23 public class EnsemblLookup extends EnsemblRestClient
24 {
25
26   /**
27    * Default constructor (to use rest.ensembl.org)
28    */
29   public EnsemblLookup()
30   {
31     super();
32   }
33
34   /**
35    * Constructor given the target domain to fetch data from
36    * 
37    * @param
38    */
39   public EnsemblLookup(String d)
40   {
41     super(d);
42   }
43
44   @Override
45   public String getDbName()
46   {
47     return "ENSEMBL";
48   }
49
50   @Override
51   public AlignmentI getSequenceRecords(String queries) throws Exception
52   {
53     return null;
54   }
55
56   @Override
57   protected URL getUrl(List<String> ids) throws MalformedURLException
58   {
59     String identifier = ids.get(0);
60     return getUrl(identifier);
61   }
62
63   /**
64    * @param identifier
65    * @return
66    */
67   protected URL getUrl(String identifier)
68   {
69     String url = getDomain() + "/lookup/id/" + identifier
70             + "?content-type=application/json";
71     try
72     {
73       return new URL(url);
74     } catch (MalformedURLException e)
75     {
76       return null;
77     }
78   }
79
80   @Override
81   protected boolean useGetRequest()
82   {
83     return true;
84   }
85
86   @Override
87   protected String getRequestMimeType(boolean multipleIds)
88   {
89     return "application/json";
90   }
91
92   @Override
93   protected String getResponseMimeType()
94   {
95     return "application/json";
96   }
97
98   /**
99    * Calls the Ensembl lookup REST endpoint and retrieves the 'Parent' for the
100    * given identifier, or null if not found
101    * 
102    * @param identifier
103    * @return
104    */
105   public String getParent(String identifier)
106   {
107     List<String> ids = Arrays.asList(new String[] { identifier });
108   
109     BufferedReader br = null;
110     try
111     {
112       URL url = getUrl(identifier);
113       if (url != null)
114       {
115         br = getHttpResponse(url, ids);
116       }
117       return (parseResponse(br));
118     } catch (IOException e)
119     {
120       // ignore
121       return null;
122     } finally
123     {
124       if (br != null)
125       {
126         try
127         {
128           br.close();
129         } catch (IOException e)
130         {
131           // ignore
132         }
133       }
134     }
135   }
136
137   /**
138    * Parses "Parent" from the JSON response and returns the value, or null if
139    * not found
140    * 
141    * @param br
142    * @return
143    * @throws IOException
144    */
145   protected String parseResponse(BufferedReader br) throws IOException
146   {
147     String parent = null;
148     JSONParser jp = new JSONParser();
149     try
150     {
151       JSONObject val = (JSONObject) jp.parse(br);
152       parent = val.get("Parent").toString();
153     } catch (ParseException e)
154     {
155       // ignore
156     }
157     return parent;
158   }
159
160 }