Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.ext.ensembl

File EnsemblLookup.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

28
73
20
1
338
206
38
0.52
3.65
20
1.9

Classes

Class Line # Actions
EnsemblLookup 47 73 38 121
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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.bin.Cache;
24    import jalview.datamodel.AlignmentI;
25    import jalview.datamodel.GeneLociI;
26    import jalview.util.MapList;
27   
28    import java.io.BufferedReader;
29    import java.io.IOException;
30    import java.net.MalformedURLException;
31    import java.net.URL;
32    import java.util.Arrays;
33    import java.util.Collections;
34    import java.util.List;
35   
36    import org.json.simple.JSONObject;
37    import org.json.simple.parser.JSONParser;
38    import org.json.simple.parser.ParseException;
39   
40    /**
41    * A client for the Ensembl /lookup REST endpoint, used to find the gene
42    * identifier given a gene, transcript or protein identifier, or to extract the
43    * species or chromosomal coordinates from the same service response
44    *
45    * @author gmcarstairs
46    */
 
47    public class EnsemblLookup extends EnsemblRestClient
48    {
49    private static final String SPECIES = "species";
50   
51    /**
52    * Default constructor (to use rest.ensembl.org)
53    */
 
54  0 toggle public EnsemblLookup()
55    {
56  0 super();
57    }
58   
59    /**
60    * Constructor given the target domain to fetch data from
61    *
62    * @param
63    */
 
64  0 toggle public EnsemblLookup(String d)
65    {
66  0 super(d);
67    }
68   
 
69  0 toggle @Override
70    public String getDbName()
71    {
72  0 return "ENSEMBL";
73    }
74   
 
75  0 toggle @Override
76    public AlignmentI getSequenceRecords(String queries) throws Exception
77    {
78  0 return null;
79    }
80   
 
81  0 toggle @Override
82    protected URL getUrl(List<String> ids) throws MalformedURLException
83    {
84  0 String identifier = ids.get(0);
85  0 return getUrl(identifier, null);
86    }
87   
88    /**
89    * Gets the url for lookup of the given identifier, optionally with objectType
90    * also specified in the request
91    *
92    * @param identifier
93    * @param objectType
94    * @return
95    */
 
96  0 toggle protected URL getUrl(String identifier, String objectType)
97    {
98  0 String url = getDomain() + "/lookup/id/" + identifier
99    + CONTENT_TYPE_JSON;
100  0 if (objectType != null)
101    {
102  0 url += "&" + OBJECT_TYPE + "=" + objectType;
103    }
104   
105  0 try
106    {
107  0 return new URL(url);
108    } catch (MalformedURLException e)
109    {
110  0 return null;
111    }
112    }
113   
 
114  0 toggle @Override
115    protected boolean useGetRequest()
116    {
117  0 return true;
118    }
119   
 
120  0 toggle @Override
121    protected String getRequestMimeType(boolean multipleIds)
122    {
123  0 return "application/json";
124    }
125   
 
126  0 toggle @Override
127    protected String getResponseMimeType()
128    {
129  0 return "application/json";
130    }
131   
132    /**
133    * Returns the gene id related to the given identifier (which may be for a
134    * gene, transcript or protein), or null if none is found
135    *
136    * @param identifier
137    * @return
138    */
 
139  0 toggle public String getGeneId(String identifier)
140    {
141  0 return getGeneId(identifier, null);
142    }
143   
144    /**
145    * Returns the gene id related to the given identifier (which may be for a
146    * gene, transcript or protein), or null if none is found
147    *
148    * @param identifier
149    * @param objectType
150    * @return
151    */
 
152  0 toggle public String getGeneId(String identifier, String objectType)
153    {
154  0 return parseGeneId(getResult(identifier, objectType));
155    }
156   
157    /**
158    * Parses the JSON response and returns the gene identifier, or null if not
159    * found. If the returned object_type is Gene, returns the id, if Transcript
160    * returns the Parent. If it is Translation (peptide identifier), then the
161    * Parent is the transcript identifier, so we redo the search with this value.
162    *
163    * @param br
164    * @return
165    */
 
166  0 toggle protected String parseGeneId(JSONObject val)
167    {
168  0 if (val == null)
169    {
170  0 return null;
171    }
172  0 String geneId = null;
173  0 String type = val.get(OBJECT_TYPE).toString();
174  0 if (OBJECT_TYPE_GENE.equalsIgnoreCase(type))
175    {
176    // got the gene - just returns its id
177  0 geneId = val.get(JSON_ID).toString();
178    }
179  0 else if (OBJECT_TYPE_TRANSCRIPT.equalsIgnoreCase(type))
180    {
181    // got the transcript - return its (Gene) Parent
182  0 geneId = val.get(PARENT).toString();
183    }
184  0 else if (OBJECT_TYPE_TRANSLATION.equalsIgnoreCase(type))
185    {
186    // got the protein - get its Parent, restricted to type Transcript
187  0 String transcriptId = val.get(PARENT).toString();
188  0 geneId = getGeneId(transcriptId, OBJECT_TYPE_TRANSCRIPT);
189    }
190   
191  0 return geneId;
192    }
193   
194    /**
195    * Calls the Ensembl lookup REST endpoint and retrieves the 'species' for the
196    * given identifier, or null if not found
197    *
198    * @param identifier
199    * @return
200    */
 
201  0 toggle public String getSpecies(String identifier)
202    {
203  0 String species = null;
204  0 JSONObject json = getResult(identifier, null);
205  0 if (json != null)
206    {
207  0 Object o = json.get(SPECIES);
208  0 if (o != null)
209    {
210  0 species = o.toString();
211    }
212    }
213  0 return species;
214    }
215   
216    /**
217    * Calls the /lookup/id rest service and returns the response as a JSONObject,
218    * or null if any error
219    *
220    * @param identifier
221    * @param objectType
222    * (optional)
223    * @return
224    */
 
225  0 toggle protected JSONObject getResult(String identifier, String objectType)
226    {
227  0 List<String> ids = Arrays.asList(new String[] { identifier });
228   
229  0 BufferedReader br = null;
230  0 try
231    {
232  0 URL url = getUrl(identifier, objectType);
233   
234  0 if (url != null)
235    {
236  0 br = getHttpResponse(url, ids);
237    }
238  0 return br == null ? null : (JSONObject) (new JSONParser().parse(br));
239    } catch (IOException | ParseException e)
240    {
241  0 System.err.println("Error parsing " + identifier + " lookup response "
242    + e.getMessage());
243  0 return null;
244    } finally
245    {
246  0 if (br != null)
247    {
248  0 try
249    {
250  0 br.close();
251    } catch (IOException e)
252    {
253    // ignore
254    }
255    }
256    }
257    }
258   
259    /**
260    * Calls the /lookup/id rest service for the given id, and if successful,
261    * parses and returns the gene's chromosomal coordinates
262    *
263    * @param geneId
264    * @return
265    */
 
266  0 toggle public GeneLociI getGeneLoci(String geneId)
267    {
268  0 return parseGeneLoci(getResult(geneId, OBJECT_TYPE_GENE));
269    }
270   
271    /**
272    * Parses the /lookup/id response for species, asssembly_name,
273    * seq_region_name, start, end and returns an object that wraps them, or null
274    * if unsuccessful
275    *
276    * @param json
277    * @return
278    */
 
279  0 toggle GeneLociI parseGeneLoci(JSONObject json)
280    {
281  0 if (json == null)
282    {
283  0 return null;
284    }
285   
286  0 try
287    {
288  0 final String species = json.get("species").toString();
289  0 final String assembly = json.get("assembly_name").toString();
290  0 final String chromosome = json.get("seq_region_name").toString();
291  0 String strand = json.get("strand").toString();
292  0 int start = Integer.parseInt(json.get("start").toString());
293  0 int end = Integer.parseInt(json.get("end").toString());
294  0 int fromEnd = end - start + 1;
295  0 boolean reverseStrand = "-1".equals(strand);
296  0 int toStart = reverseStrand ? end : start;
297  0 int toEnd = reverseStrand ? start : end;
298  0 List<int[]> fromRange = Collections.singletonList(new int[] { 1,
299    fromEnd });
300  0 List<int[]> toRange = Collections.singletonList(new int[] { toStart,
301    toEnd });
302  0 final MapList map = new MapList(fromRange, toRange, 1, 1);
303  0 return new GeneLociI()
304    {
305   
 
306  0 toggle @Override
307    public String getSpeciesId()
308    {
309  0 return species == null ? "" : species;
310    }
311   
 
312  0 toggle @Override
313    public String getAssemblyId()
314    {
315  0 return assembly;
316    }
317   
 
318  0 toggle @Override
319    public String getChromosomeId()
320    {
321  0 return chromosome;
322    }
323   
 
324  0 toggle @Override
325    public MapList getMap()
326    {
327  0 return map;
328    }
329    };
330    } catch (NullPointerException | NumberFormatException e)
331    {
332  0 Cache.log.error("Error looking up gene loci: " + e.getMessage());
333  0 e.printStackTrace();
334    }
335  0 return null;
336    }
337   
338    }