Push 1793 latest to spike branch
[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   private static final String SPECIES = "species";
46
47   private static final String PARENT = "Parent";
48
49   /**
50    * Default constructor (to use rest.ensembl.org)
51    */
52   public EnsemblLookup()
53   {
54     super();
55   }
56
57   /**
58    * Constructor given the target domain to fetch data from
59    * 
60    * @param
61    */
62   public EnsemblLookup(String d)
63   {
64     super(d);
65   }
66
67   @Override
68   public String getDbName()
69   {
70     return "ENSEMBL";
71   }
72
73   @Override
74   public AlignmentI getSequenceRecords(String queries) throws Exception
75   {
76     return null;
77   }
78
79   @Override
80   protected URL getUrl(List<String> ids) throws MalformedURLException
81   {
82     String identifier = ids.get(0);
83     return getUrl(identifier);
84   }
85
86   /**
87    * @param identifier
88    * @return
89    */
90   protected URL getUrl(String identifier)
91   {
92     String url = getDomain() + "/lookup/id/" + identifier
93             + "?content-type=application/json";
94     try
95     {
96       return new URL(url);
97     } catch (MalformedURLException e)
98     {
99       return null;
100     }
101   }
102
103   @Override
104   protected boolean useGetRequest()
105   {
106     return true;
107   }
108
109   @Override
110   protected String getRequestMimeType(boolean multipleIds)
111   {
112     return "application/json";
113   }
114
115   @Override
116   protected String getResponseMimeType()
117   {
118     return "application/json";
119   }
120
121   /**
122    * Calls the Ensembl lookup REST endpoint and retrieves the 'Parent' for the
123    * given identifier, or null if not found
124    * 
125    * @param identifier
126    * @return
127    */
128   public String getParent(String identifier)
129   {
130     return getAttribute(identifier, PARENT);
131   }
132
133   /**
134    * Calls the Ensembl lookup REST endpoint and retrieves the 'species' for the
135    * given identifier, or null if not found
136    * 
137    * @param identifier
138    * @return
139    */
140   public String getSpecies(String identifier)
141   {
142     return getAttribute(identifier, SPECIES);
143   }
144
145   /**
146    * @param identifier
147    * @param attribute
148    * @return
149    */
150   protected String getAttribute(String identifier, String attribute)
151   {
152     List<String> ids = Arrays.asList(new String[] { identifier });
153
154     BufferedReader br = null;
155     try
156     {
157       URL url = getUrl(identifier);
158       if (url != null)
159       {
160         br = getHttpResponse(url, ids);
161       }
162       return (parseResponse(br, attribute));
163     } catch (IOException e)
164     {
165       // ignore
166       return null;
167     } finally
168     {
169       if (br != null)
170       {
171         try
172         {
173           br.close();
174         } catch (IOException e)
175         {
176           // ignore
177         }
178       }
179     }
180   }
181
182   /**
183    * Parses the value of 'attribute' from the JSON response and returns the
184    * value, or null if not found
185    * 
186    * @param br
187    * @param attribute
188    * @return
189    * @throws IOException
190    */
191   protected String parseResponse(BufferedReader br, String attribute) throws IOException
192   {
193     String parent = null;
194     JSONParser jp = new JSONParser();
195     try
196     {
197       JSONObject val = (JSONObject) jp.parse(br);
198       parent = val.get(attribute).toString();
199     } catch (ParseException | NullPointerException e)
200     {
201       // ignore
202     }
203     return parent;
204   }
205
206 }