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
32 public EnsemblSymbol(String domain, String dbName, String dbVersion)
34 super(domain, dbName, dbVersion);
38 * Returns the first "id" value in gene identifier format from the JSON
39 * response, or null if none found
45 protected String parseSymbolResponse(BufferedReader br)
48 JSONParser jp = new JSONParser();
52 JSONArray responses = (JSONArray) jp.parse(br);
53 Iterator rvals = responses.iterator();
54 while (rvals.hasNext())
56 JSONObject val = (JSONObject) rvals.next();
57 String id = val.get("id").toString();
58 if (id != null && isGeneIdentifier(id))
64 } catch (ParseException e)
71 protected URL getUrl(String id, Species species)
73 String url = getDomain() + "/xrefs/symbol/" + species.toString() + "/"
75 + "?content-type=application/json";
79 } catch (MalformedURLException e)
86 * Calls the Ensembl xrefs REST 'symbol' endpoint and retrieves any gene ids
87 * for the given identifier, for any known model organisms
92 public List<String> getIds(String identifier)
94 List<String> result = new ArrayList<String>();
95 List<String> ids = new ArrayList<String>();
98 String[] queries = identifier.split(getAccessionSeparator());
99 BufferedReader br = null;
102 for (String query : queries)
104 for (Species taxon : Species.values())
106 if (taxon.isModelOrganism())
108 URL url = getUrl(query, taxon);
111 br = getHttpResponse(url, ids);
113 String geneId = parseSymbolResponse(br);
121 } catch (IOException e)
131 } catch (IOException e)