1 package jalview.ext.ensembl;
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.DBRefEntry;
5 import jalview.util.DBRefUtils;
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
15 import org.json.simple.JSONArray;
16 import org.json.simple.JSONObject;
17 import org.json.simple.parser.JSONParser;
18 import org.json.simple.parser.ParseException;
21 * A class to fetch cross-references from Ensembl by calling the /xrefs REST
25 * @see http://rest.ensembl.org/documentation/info/xref_id
27 class EnsemblXref extends EnsemblRestClient
30 private static final String GO_GENE_ONTOLOGY = "GO";
32 private String dbName = "ENSEMBL (xref)";
35 * Constructor given the target domain to fetch data from
39 public EnsemblXref(String d, String dbSource, String version)
43 xrefVersion = dbSource + ":" + version;
48 public String getDbName()
54 public AlignmentI getSequenceRecords(String queries) throws Exception
60 protected URL getUrl(List<String> ids) throws MalformedURLException
62 return getUrl(ids.get(0));
66 protected boolean useGetRequest()
72 protected String getRequestMimeType(boolean multipleIds)
74 return "application/json";
78 protected String getResponseMimeType()
80 return "application/json";
84 * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
85 * ("primary_id") for the given identifier (Ensembl accession id) and database
86 * names. The "dbname" returned by Ensembl is canonicalised to Jalview's
87 * standard version, and a DBRefEntry constructed. Currently takes all
88 * identifiers apart from GO terms and synonyms.
91 * an Ensembl stable identifier
94 public List<DBRefEntry> getCrossReferences(String identifier)
96 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
97 List<String> ids = new ArrayList<String>();
100 BufferedReader br = null;
103 URL url = getUrl(identifier);
106 br = getHttpResponse(url, ids);
108 return (parseResponse(br));
109 } catch (IOException e)
119 } catch (IOException e)
130 * Parses "primary_id" and "dbname" values from the JSON response and
131 * constructs a DBRefEntry. Returns a list of the DBRefEntry created. Note we
132 * don't parse "synonyms" as they appear to be either redirected or obsolete
137 * @throws IOException
139 protected List<DBRefEntry> parseResponse(BufferedReader br)
142 JSONParser jp = new JSONParser();
143 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
146 JSONArray responses = (JSONArray) jp.parse(br);
147 Iterator rvals = responses.iterator();
148 while (rvals.hasNext())
150 JSONObject val = (JSONObject) rvals.next();
151 String dbName = val.get("dbname").toString();
152 if (dbName.equals(GO_GENE_ONTOLOGY))
156 String id = val.get("primary_id").toString();
157 if (dbName != null && id != null)
159 dbName = DBRefUtils.getCanonicalName(dbName);
160 DBRefEntry dbref = new DBRefEntry(dbName, getXRefVersion(), id);
164 } catch (ParseException e)
171 private String xrefVersion = "ENSEMBL:0";
174 * version string for Xrefs - for 2.10, hardwired for ENSEMBL:0
178 public String getXRefVersion()
184 * Returns the URL for the REST endpoint to fetch all cross-references for an
185 * identifier. Note this may return protein cross-references for nucleotide.
186 * Filter the returned list as required.
191 protected URL getUrl(String identifier)
193 String url = getDomain() + "/xrefs/id/" + identifier
194 + "?content-type=application/json&all_levels=1";
198 } catch (MalformedURLException e)