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;
31 import java.util.function.Function;
33 import org.json.simple.JSONObject;
34 import org.json.simple.parser.JSONParser;
35 import org.json.simple.parser.ParseException;
38 * A client for the Ensembl lookup REST endpoint
42 public class EnsemblLookup extends EnsemblRestClient
44 private static final String SPECIES = "species";
46 private static final String PARENT = "Parent";
48 private static final String OBJECT_TYPE_TRANSLATION = "Translation";
49 private static final String OBJECT_TYPE_TRANSCRIPT = "Transcript";
50 private static final String ID = "id";
51 private static final String OBJECT_TYPE_GENE = "Gene";
52 private static final String OBJECT_TYPE = "object_type";
55 * Default constructor (to use rest.ensembl.org)
57 public EnsemblLookup()
63 * Constructor given the target domain to fetch data from
67 public EnsemblLookup(String d)
73 public String getDbName()
79 public AlignmentI getSequenceRecords(String queries) throws Exception
85 protected URL getUrl(List<String> ids) throws MalformedURLException
87 String identifier = ids.get(0);
88 return getUrl(identifier);
95 protected URL getUrl(String identifier)
97 String url = getDomain() + "/lookup/id/" + identifier
102 } catch (MalformedURLException e)
109 protected boolean useGetRequest()
115 protected String getRequestMimeType(boolean multipleIds)
117 return "application/json";
121 protected String getResponseMimeType()
123 return "application/json";
127 * Calls the Ensembl lookup REST endpoint and retrieves the 'Parent' for the
128 * given identifier, or null if not found
133 public String getGeneId(String identifier)
135 return getResult(identifier, br -> parseGeneId(br));
139 * Calls the Ensembl lookup REST endpoint and retrieves the 'species' for the
140 * given identifier, or null if not found
145 public String getSpecies(String identifier)
147 return getResult(identifier, br -> getAttribute(br, SPECIES));
155 protected String getResult(String identifier,
156 Function<BufferedReader, String> parser)
158 List<String> ids = Arrays.asList(new String[] { identifier });
160 BufferedReader br = null;
163 URL url = getUrl(identifier);
166 br = getHttpResponse(url, ids);
168 return br == null ? null : parser.apply(br);
169 } catch (IOException e)
180 } catch (IOException e)
189 * Answers the value of 'attribute' from the JSON response, or null if not
196 protected String getAttribute(BufferedReader br, String attribute)
199 JSONParser jp = new JSONParser();
202 JSONObject val = (JSONObject) jp.parse(br);
203 value = val.get(attribute).toString();
204 } catch (ParseException | NullPointerException | IOException e)
212 * Parses the JSON response and returns the gene identifier, or null if not
213 * found. If the returned object_type is Gene, returns the id, if Transcript
214 * returns the Parent. If it is Translation (peptide identifier), then the
215 * Parent is the transcript identifier, so we redo the search with this value.
220 protected String parseGeneId(BufferedReader br)
222 String geneId = null;
223 JSONParser jp = new JSONParser();
226 JSONObject val = (JSONObject) jp.parse(br);
227 String type = val.get(OBJECT_TYPE).toString();
228 if (OBJECT_TYPE_GENE.equalsIgnoreCase(type))
230 geneId = val.get(ID).toString();
232 else if (OBJECT_TYPE_TRANSCRIPT.equalsIgnoreCase(type))
234 geneId = val.get(PARENT).toString();
236 else if (OBJECT_TYPE_TRANSLATION.equalsIgnoreCase(type))
238 String transcriptId = val.get(PARENT).toString();
241 geneId = getGeneId(transcriptId);
242 } catch (StackOverflowError e)
245 * unlikely data condition error!
248 .println("** Ensembl lookup "
249 + getUrl(transcriptId).toString()
250 + " looping on Parent!");
253 } catch (ParseException | IOException e)