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.
87 public List<DBRefEntry> getCrossReferences(String identifier,
88 List<String> databases)
90 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
91 List<String> ids = new ArrayList<String>();
94 BufferedReader br = null;
97 URL url = getUrl(identifier);
100 br = getHttpResponse(url, ids);
102 return (parseResponse(br, databases));
103 } catch (IOException e)
113 } catch (IOException e)
124 * Parses "primary_id" and "dbname" values from the JSON response and
125 * constructs a DBRefEntry if the dbname is in the list supplied. Returns a
126 * list of DBRefEntry created.
131 * @throws IOException
133 protected List<DBRefEntry> parseResponse(BufferedReader br,
134 List<String> databases)
137 JSONParser jp = new JSONParser();
138 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
141 JSONArray responses = (JSONArray) jp.parse(br);
142 Iterator rvals = responses.iterator();
143 while (rvals.hasNext())
145 JSONObject val = (JSONObject) rvals.next();
146 String dbName = val.get("dbname").toString();
147 if (databases != null && !databases.isEmpty()
148 && !databases.contains(dbName))
152 String id = val.get("primary_id").toString();
153 if (dbName != null && id != null)
155 dbName = DBRefUtils.getCanonicalName(dbName);
156 DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
160 } catch (ParseException e)
168 * Returns the URL for the REST endpoint to fetch all cross-references for an
169 * identifier. Note this may return protein cross-references for nucleotide.
170 * Filter the returned list as required.
175 protected URL getUrl(String identifier)
177 String url = getDomain() + "/xrefs/id/" + identifier
178 + "?content-type=application/json&all_levels=1";
182 } catch (MalformedURLException e)