1 package jalview.ext.ensembl;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
7 import java.util.ArrayList;
8 import java.util.Iterator;
11 import org.json.simple.JSONArray;
12 import org.json.simple.JSONObject;
13 import org.json.simple.parser.JSONParser;
14 import org.json.simple.parser.ParseException;
17 * A client for the Ensembl xrefs/symbol REST service;
19 * @see http://rest.ensembl.org/documentation/info/xref_external
23 public class EnsemblSymbol extends EnsemblXref
26 * Constructor given the target domain to fetch data from
30 public EnsemblSymbol(String d)
36 * Returns the first "id" value in gene identifier format from the JSON
37 * response, or null if none found
43 protected String parseResponse(BufferedReader br)
46 JSONParser jp = new JSONParser();
50 JSONArray responses = (JSONArray) jp.parse(br);
51 Iterator rvals = responses.iterator();
52 while (rvals.hasNext())
54 JSONObject val = (JSONObject) rvals.next();
55 String id = val.get("id").toString();
56 if (id != null && isGeneIdentifier(id))
62 } catch (ParseException e)
69 protected URL getUrl(String id, Species species)
71 String url = getDomain() + "/xrefs/symbol/" + species.toString() + "/"
73 + "?content-type=application/json";
77 } catch (MalformedURLException e)
84 * Calls the Ensembl xrefs REST 'symbol' endpoint and retrieves any gene ids
85 * for the given identifier, for any known model organisms
90 public List<String> getIds(String identifier)
92 List<String> result = new ArrayList<String>();
93 List<String> ids = new ArrayList<String>();
96 String[] queries = identifier.split(getAccessionSeparator());
97 BufferedReader br = null;
100 for (String query : queries)
102 for (Species taxon : Species.values())
104 if (taxon.isModelOrganism())
106 URL url = getUrl(query, taxon);
109 br = getHttpResponse(url, ids);
111 String geneId = parseResponse(br);
119 } catch (IOException e)
129 } catch (IOException e)