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";
33 * Constructor given the target domain to fetch data from
37 public EnsemblXref(String d)
43 public String getDbName()
45 return "ENSEMBL (xref)";
49 public AlignmentI getSequenceRecords(String queries) throws Exception
55 protected URL getUrl(List<String> ids) throws MalformedURLException
57 return getUrl(ids.get(0));
61 protected boolean useGetRequest()
67 protected String getRequestMimeType(boolean multipleIds)
69 return "application/json";
73 protected String getResponseMimeType()
75 return "application/json";
79 * Calls the Ensembl xrefs REST endpoint and retrieves any cross-references
80 * ("primary_id") for the given identifier (Ensembl accession id) and database
81 * names. The "dbname" returned by Ensembl is canonicalised to Jalview's
82 * standard version, and a DBRefEntry constructed. Currently takes all
83 * identifiers apart from GO terms and synonyms.
86 * an Ensembl stable identifier
89 public List<DBRefEntry> getCrossReferences(String identifier)
91 List<DBRefEntry> result = new ArrayList<DBRefEntry>();
92 List<String> ids = new ArrayList<String>();
95 BufferedReader br = null;
98 URL url = getUrl(identifier);
101 br = getHttpResponse(url, ids);
103 return (parseResponse(br));
104 } catch (IOException e)
114 } catch (IOException e)
125 * Parses "primary_id" and "dbname" values from the JSON response and
126 * constructs a DBRefEntry. Returns a list of the DBRefEntry created. Note we
127 * don't parse "synonyms" as they appear to be either redirected or obsolete
132 * @throws IOException
134 protected List<DBRefEntry> parseResponse(BufferedReader br)
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 (dbName.equals(GO_GENE_ONTOLOGY))
151 String id = val.get("primary_id").toString();
152 if (dbName != null && id != null)
154 dbName = DBRefUtils.getCanonicalName(dbName);
155 DBRefEntry dbref = new DBRefEntry(dbName, "0", id);
159 } catch (ParseException e)
167 * Returns the URL for the REST endpoint to fetch all cross-references for an
168 * identifier. Note this may return protein cross-references for nucleotide.
169 * Filter the returned list as required.
174 protected URL getUrl(String identifier)
176 String url = getDomain() + "/xrefs/id/" + identifier
177 + "?content-type=application/json&all_levels=1";
181 } catch (MalformedURLException e)