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 public String getDbName()
33 return "ENSEMBL (xref)";
37 public AlignmentI getSequenceRecords(String queries) throws Exception
43 protected URL getUrl(List<String> ids) throws MalformedURLException
45 return getUrl(ids.get(0));
49 protected boolean useGetRequest()
55 protected String getRequestMimeType(boolean multipleIds)
57 return "application/json";
61 protected String getResponseMimeType()
63 return "application/json";
67 * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
68 * ("primary_id") for the given identifier (Ensembl accession id) and database
69 * names. The "dbname" returned by Ensembl is canonicalised to Jalview's
70 * standard version, and a DBRefEntry constructed. If no databases are
71 * specified, all available cross-references are retrieved.
77 public List<DBRefEntry> getCrossReferences(String identifier,
78 List<String> databases)
80 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
81 List<String> ids = new ArrayList<String>();
84 BufferedReader br = null;
87 URL url = getUrl(identifier);
90 br = getHttpResponse(url, ids);
92 return (parseResponse(br, databases));
93 } catch (IOException e)
103 } catch (IOException e)
114 * Parses "primary_id" and "dbname" values from the JSON response and
115 * constructs a DBRefEntry if the dbname is in the list supplied. Returns a
116 * list of DBRefEntry created.
121 * @throws IOException
123 protected List<DBRefEntry> parseResponse(BufferedReader br,
124 List<String> databases)
127 JSONParser jp = new JSONParser();
128 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
131 JSONArray responses = (JSONArray) jp.parse(br);
132 Iterator rvals = responses.iterator();
133 while (rvals.hasNext())
135 JSONObject val = (JSONObject) rvals.next();
136 String dbName = val.get("dbname").toString();
137 if (databases != null && !databases.isEmpty()
138 && !databases.contains(dbName))
142 String id = val.get("primary_id").toString();
143 if (dbName != null && id != null)
145 dbName = DBRefUtils.getCanonicalName(dbName);
146 DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
150 } catch (ParseException e)
158 * Returns the URL for the REST endpoint to fetch all cross-references for an
159 * identifier. Note this may return protein cross-references for nucleotide.
160 * Filter the returned list as required.
165 protected URL getUrl(String identifier)
167 String url = ENSEMBL_REST + "/xrefs/id/" + identifier
168 + "?content-type=application/json&all_levels=1";
172 } catch (MalformedURLException e)