2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.ext.ensembl;
23 import jalview.bin.Cache;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.GeneLociI;
26 import jalview.util.MapList;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
36 import org.json.simple.JSONObject;
37 import org.json.simple.parser.JSONParser;
38 import org.json.simple.parser.ParseException;
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
47 public class EnsemblLookup extends EnsemblRestClient
49 private static final String SPECIES = "species";
52 * Default constructor (to use rest.ensembl.org)
54 public EnsemblLookup()
60 * Constructor given the target domain to fetch data from
64 public EnsemblLookup(String d)
70 public String getDbName()
76 public AlignmentI getSequenceRecords(String queries) throws Exception
82 protected URL getUrl(List<String> ids) throws MalformedURLException
84 String identifier = ids.get(0);
85 return getUrl(identifier, null);
89 * Gets the url for lookup of the given identifier, optionally with objectType
90 * also specified in the request
96 protected URL getUrl(String identifier, String objectType)
98 String url = getDomain() + "/lookup/id/" + identifier
100 if (objectType != null)
102 url += "&" + OBJECT_TYPE + "=" + objectType;
108 } catch (MalformedURLException e)
115 protected boolean useGetRequest()
121 protected String getRequestMimeType(boolean multipleIds)
123 return "application/json";
127 protected String getResponseMimeType()
129 return "application/json";
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
139 public String getGeneId(String identifier)
141 return getGeneId(identifier, null);
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
152 public String getGeneId(String identifier, String objectType)
154 return parseGeneId(getResult(identifier, objectType));
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.
166 protected String parseGeneId(JSONObject val)
172 String geneId = null;
173 String type = val.get(OBJECT_TYPE).toString();
174 if (OBJECT_TYPE_GENE.equalsIgnoreCase(type))
176 // got the gene - just returns its id
177 geneId = val.get(JSON_ID).toString();
179 else if (OBJECT_TYPE_TRANSCRIPT.equalsIgnoreCase(type))
181 // got the transcript - return its (Gene) Parent
182 geneId = val.get(PARENT).toString();
184 else if (OBJECT_TYPE_TRANSLATION.equalsIgnoreCase(type))
186 // got the protein - get its Parent, restricted to type Transcript
187 String transcriptId = val.get(PARENT).toString();
188 geneId = getGeneId(transcriptId, OBJECT_TYPE_TRANSCRIPT);
195 * Calls the Ensembl lookup REST endpoint and retrieves the 'species' for the
196 * given identifier, or null if not found
201 public String getSpecies(String identifier)
203 String species = null;
204 JSONObject json = getResult(identifier, null);
207 Object o = json.get(SPECIES);
210 species = o.toString();
217 * Calls the /lookup/id rest service and returns the response as a JSONObject,
218 * or null if any error
225 protected JSONObject getResult(String identifier, String objectType)
227 List<String> ids = Arrays.asList(new String[] { identifier });
229 BufferedReader br = null;
232 URL url = getUrl(identifier, objectType);
236 br = getHttpResponse(url, ids);
238 return br == null ? null : (JSONObject) (new JSONParser().parse(br));
239 } catch (IOException | ParseException e)
241 System.err.println("Error parsing " + identifier + " lookup response "
251 } catch (IOException e)
260 * Calls the /lookup/id rest service for the given id, and if successful,
261 * parses and returns the gene's chromosomal coordinates
266 public GeneLociI getGeneLoci(String geneId)
268 return parseGeneLoci(getResult(geneId, OBJECT_TYPE_GENE));
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
279 GeneLociI parseGeneLoci(JSONObject json)
288 final String species = json.get("species").toString();
289 final String assembly = json.get("assembly_name").toString();
290 final String chromosome = json.get("seq_region_name").toString();
291 String strand = json.get("strand").toString();
292 int start = Integer.parseInt(json.get("start").toString());
293 int end = Integer.parseInt(json.get("end").toString());
294 int fromEnd = end - start + 1;
295 boolean reverseStrand = "-1".equals(strand);
296 int toStart = reverseStrand ? end : start;
297 int toEnd = reverseStrand ? start : end;
298 List<int[]> fromRange = Collections.singletonList(new int[] { 1,
300 List<int[]> toRange = Collections.singletonList(new int[] { toStart,
302 final MapList map = new MapList(fromRange, toRange, 1, 1);
303 return new GeneLociI()
307 public String getSpeciesId()
309 return species == null ? "" : species;
313 public String getAssemblyId()
319 public String getChromosomeId()
325 public MapList getMap()
330 } catch (NullPointerException | NumberFormatException e)
332 Cache.log.error("Error looking up gene loci: " + e.getMessage());