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
31 * Constructor given the target domain to fetch data from
35 public EnsemblXref(String d)
41 public String getDbName()
43 return "ENSEMBL (xref)";
47 public AlignmentI getSequenceRecords(String queries) throws Exception
53 protected URL getUrl(List<String> ids) throws MalformedURLException
55 return getUrl(ids.get(0));
59 protected boolean useGetRequest()
65 protected String getRequestMimeType(boolean multipleIds)
67 return "application/json";
71 protected String getResponseMimeType()
73 return "application/json";
77 * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
78 * ("primary_id") for the given identifier (Ensembl accession id) and database
79 * names. The "dbname" returned by Ensembl is canonicalised to Jalview's
80 * standard version, and a DBRefEntry constructed. If no databases are
81 * specified, all available cross-references are retrieved.
84 * an Ensembl stable identifier
86 * optional list of Ensembl cross-referenced databases of interest
89 public List<DBRefEntry> getCrossReferences(String identifier,
90 List<String> databases)
92 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
93 List<String> ids = new ArrayList<String>();
96 BufferedReader br = null;
99 URL url = getUrl(identifier);
102 br = getHttpResponse(url, ids);
104 return (parseResponse(br, databases));
105 } catch (IOException e)
115 } catch (IOException e)
126 * Parses "primary_id" and "dbname" values from the JSON response and
127 * constructs a DBRefEntry if the dbname is in the list supplied. Returns a
128 * list of the DBRefEntry created. Note we don't parse "synonyms" as they
129 * appear to be either redirected or obsolete in Uniprot.
134 * @throws IOException
136 protected List<DBRefEntry> parseResponse(BufferedReader br,
137 List<String> databases)
140 JSONParser jp = new JSONParser();
141 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
144 JSONArray responses = (JSONArray) jp.parse(br);
145 Iterator rvals = responses.iterator();
146 while (rvals.hasNext())
148 JSONObject val = (JSONObject) rvals.next();
149 String dbName = val.get("dbname").toString();
150 if (databases != null && !databases.isEmpty()
151 && !databases.contains(dbName))
155 String id = val.get("primary_id").toString();
156 if (dbName != null && id != null)
158 dbName = DBRefUtils.getCanonicalName(dbName);
159 DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
163 } catch (ParseException e)
171 * Returns the URL for the REST endpoint to fetch all cross-references for an
172 * identifier. Note this may return protein cross-references for nucleotide.
173 * Filter the returned list as required.
178 protected URL getUrl(String identifier)
180 String url = getDomain() + "/xrefs/id/" + identifier
181 + "?content-type=application/json&all_levels=1";
185 } catch (MalformedURLException e)