JAL-2189 apply license
[jalview.git] / src / jalview / ext / ensembl / EnsemblLookup.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.ensembl;
22
23 import jalview.datamodel.AlignmentI;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.net.MalformedURLException;
28 import java.net.URL;
29 import java.util.Arrays;
30 import java.util.List;
31
32 import org.json.simple.JSONObject;
33 import org.json.simple.parser.JSONParser;
34 import org.json.simple.parser.ParseException;
35
36 /**
37  * A client for the Ensembl lookup REST endpoint; used to find the Parent gene
38  * identifier given a transcript identifier.
39  * 
40  * @author gmcarstairs
41  *
42  */
43 public class EnsemblLookup extends EnsemblRestClient
44 {
45
46   /**
47    * Default constructor (to use rest.ensembl.org)
48    */
49   public EnsemblLookup()
50   {
51     super();
52   }
53
54   /**
55    * Constructor given the target domain to fetch data from
56    * 
57    * @param
58    */
59   public EnsemblLookup(String d)
60   {
61     super(d);
62   }
63
64   @Override
65   public String getDbName()
66   {
67     return "ENSEMBL";
68   }
69
70   @Override
71   public AlignmentI getSequenceRecords(String queries) throws Exception
72   {
73     return null;
74   }
75
76   @Override
77   protected URL getUrl(List<String> ids) throws MalformedURLException
78   {
79     String identifier = ids.get(0);
80     return getUrl(identifier);
81   }
82
83   /**
84    * @param identifier
85    * @return
86    */
87   protected URL getUrl(String identifier)
88   {
89     String url = getDomain() + "/lookup/id/" + identifier
90             + "?content-type=application/json";
91     try
92     {
93       return new URL(url);
94     } catch (MalformedURLException e)
95     {
96       return null;
97     }
98   }
99
100   @Override
101   protected boolean useGetRequest()
102   {
103     return true;
104   }
105
106   @Override
107   protected String getRequestMimeType(boolean multipleIds)
108   {
109     return "application/json";
110   }
111
112   @Override
113   protected String getResponseMimeType()
114   {
115     return "application/json";
116   }
117
118   /**
119    * Calls the Ensembl lookup REST endpoint and retrieves the 'Parent' for the
120    * given identifier, or null if not found
121    * 
122    * @param identifier
123    * @return
124    */
125   public String getParent(String identifier)
126   {
127     List<String> ids = Arrays.asList(new String[] { identifier });
128
129     BufferedReader br = null;
130     try
131     {
132       URL url = getUrl(identifier);
133       if (url != null)
134       {
135         br = getHttpResponse(url, ids);
136       }
137       return (parseResponse(br));
138     } catch (IOException e)
139     {
140       // ignore
141       return null;
142     } finally
143     {
144       if (br != null)
145       {
146         try
147         {
148           br.close();
149         } catch (IOException e)
150         {
151           // ignore
152         }
153       }
154     }
155   }
156
157   /**
158    * Parses "Parent" from the JSON response and returns the value, or null if
159    * not found
160    * 
161    * @param br
162    * @return
163    * @throws IOException
164    */
165   protected String parseResponse(BufferedReader br) throws IOException
166   {
167     String parent = null;
168     JSONParser jp = new JSONParser();
169     try
170     {
171       JSONObject val = (JSONObject) jp.parse(br);
172       parent = val.get("Parent").toString();
173     } catch (ParseException e)
174     {
175       // ignore
176     }
177     return parent;
178   }
179
180 }