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.datamodel.AlignmentI;
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.net.MalformedURLException;
29 import java.util.Arrays;
30 import java.util.List;
32 import org.json.simple.JSONObject;
33 import org.json.simple.parser.JSONParser;
34 import org.json.simple.parser.ParseException;
37 * A client for the Ensembl lookup REST endpoint, used to find the gene
38 * identifier given a gene, transcript or protein identifier.
42 public class EnsemblLookup extends EnsemblRestClient
45 private static final String OBJECT_TYPE_TRANSLATION = "Translation";
46 private static final String PARENT = "Parent";
47 private static final String OBJECT_TYPE_TRANSCRIPT = "Transcript";
48 private static final String ID = "id";
49 private static final String OBJECT_TYPE_GENE = "Gene";
50 private static final String OBJECT_TYPE = "object_type";
53 * keep track of last identifier retrieved to break loops
55 private String lastId;
58 * Default constructor (to use rest.ensembl.org)
60 public EnsemblLookup()
66 * Constructor given the target domain to fetch data from
70 public EnsemblLookup(String d)
76 public String getDbName()
82 public AlignmentI getSequenceRecords(String queries) throws Exception
88 protected URL getUrl(List<String> ids) throws MalformedURLException
90 String identifier = ids.get(0);
91 return getUrl(identifier, null);
95 * Gets the url for lookup of the given identifier, optionally with objectType
96 * also specified in the request
102 protected URL getUrl(String identifier, String objectType)
104 String url = getDomain() + "/lookup/id/" + identifier
106 if (objectType != null)
108 url += "&" + OBJECT_TYPE + "=" + objectType;
114 } catch (MalformedURLException e)
121 protected boolean useGetRequest()
127 protected String getRequestMimeType(boolean multipleIds)
129 return "application/json";
133 protected String getResponseMimeType()
135 return "application/json";
139 * Returns the gene id related to the given identifier, which may be for a
140 * gene, transcript or protein
145 public String getGeneId(String identifier)
147 return getGeneId(identifier, null);
151 * Calls the Ensembl lookup REST endpoint and retrieves the 'Parent' for the
152 * given identifier, or null if not found
159 public String getGeneId(String identifier, String objectType)
161 List<String> ids = Arrays.asList(new String[] { identifier });
163 BufferedReader br = null;
167 URL url = getUrl(identifier, objectType);
169 if (identifier.equals(lastId))
171 System.err.println("** Ensembl lookup " + url.toString()
172 + " looping on Parent!");
180 br = getHttpResponse(url, ids);
182 return br == null ? null : parseResponse(br);
183 } catch (IOException e)
194 } catch (IOException e)
203 * Parses the JSON response and returns the gene identifier, or null if not
204 * found. If the returned object_type is Gene, returns the id, if Transcript
205 * returns the Parent. If it is Translation (peptide identifier), then the
206 * Parent is the transcript identifier, so we redo the search with this value.
210 * @throws IOException
212 protected String parseResponse(BufferedReader br) throws IOException
214 String geneId = null;
215 JSONParser jp = new JSONParser();
218 JSONObject val = (JSONObject) jp.parse(br);
219 String type = val.get(OBJECT_TYPE).toString();
220 if (OBJECT_TYPE_GENE.equalsIgnoreCase(type))
222 // got the gene - just returns its id
223 geneId = val.get(ID).toString();
225 else if (OBJECT_TYPE_TRANSCRIPT.equalsIgnoreCase(type))
227 // got the transcript - return its (Gene) Parent
228 geneId = val.get(PARENT).toString();
230 else if (OBJECT_TYPE_TRANSLATION.equalsIgnoreCase(type))
232 // got the protein - get its Parent, restricted to type Transcript
233 String transcriptId = val.get(PARENT).toString();
234 geneId = getGeneId(transcriptId, OBJECT_TYPE_TRANSCRIPT);
236 } catch (ParseException e)