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;
24 import jalview.datamodel.DBRefEntry;
25 import jalview.util.DBRefUtils;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.net.MalformedURLException;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
35 import org.json.simple.JSONArray;
36 import org.json.simple.JSONObject;
37 import org.json.simple.parser.JSONParser;
38 import org.json.simple.parser.ParseException;
41 * A class to fetch cross-references from Ensembl by calling the /xrefs REST
45 * @see http://rest.ensembl.org/documentation/info/xref_id
47 class EnsemblXref extends EnsemblRestClient
50 private static final String GO_GENE_ONTOLOGY = "GO";
52 private String dbName = "ENSEMBL (xref)";
55 * Constructor given the target domain to fetch data from
59 public EnsemblXref(String d, String dbSource, String version)
63 xrefVersion = dbSource + ":" + version;
68 public String getDbName()
74 public AlignmentI getSequenceRecords(String queries) throws Exception
80 protected URL getUrl(List<String> ids) throws MalformedURLException
82 return getUrl(ids.get(0));
86 protected boolean useGetRequest()
92 protected String getRequestMimeType(boolean multipleIds)
94 return "application/json";
98 protected String getResponseMimeType()
100 return "application/json";
104 * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
105 * ("primary_id") for the given identifier (Ensembl accession id) and database
106 * names. The "dbname" returned by Ensembl is canonicalised to Jalview's
107 * standard version, and a DBRefEntry constructed. Currently takes all
108 * identifiers apart from GO terms and synonyms.
111 * an Ensembl stable identifier
114 public List<DBRefEntry> getCrossReferences(String identifier)
116 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
117 List<String> ids = new ArrayList<String>();
120 BufferedReader br = null;
123 URL url = getUrl(identifier);
126 br = getHttpResponse(url, ids);
128 return (parseResponse(br));
129 } catch (IOException e)
139 } catch (IOException e)
150 * Parses "primary_id" and "dbname" values from the JSON response and
151 * constructs a DBRefEntry. Returns a list of the DBRefEntry created. Note we
152 * don't parse "synonyms" as they appear to be either redirected or obsolete
157 * @throws IOException
159 protected List<DBRefEntry> parseResponse(BufferedReader br)
162 JSONParser jp = new JSONParser();
163 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
166 JSONArray responses = (JSONArray) jp.parse(br);
167 Iterator rvals = responses.iterator();
168 while (rvals.hasNext())
170 JSONObject val = (JSONObject) rvals.next();
171 String dbName = val.get("dbname").toString();
172 if (dbName.equals(GO_GENE_ONTOLOGY))
176 String id = val.get("primary_id").toString();
177 if (dbName != null && id != null)
179 dbName = DBRefUtils.getCanonicalName(dbName);
180 DBRefEntry dbref = new DBRefEntry(dbName, getXRefVersion(), id);
184 } catch (ParseException e)
191 private String xrefVersion = "ENSEMBL:0";
194 * version string for Xrefs - for 2.10, hardwired for ENSEMBL:0
198 public String getXRefVersion()
204 * Returns the URL for the REST endpoint to fetch all cross-references for an
205 * identifier. Note this may return protein cross-references for nucleotide.
206 * Filter the returned list as required.
211 protected URL getUrl(String identifier)
213 String url = getDomain() + "/xrefs/id/" + identifier
214 + "?content-type=application/json&all_levels=1";
218 } catch (MalformedURLException e)