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() + "/"
74 + id + "?content-type=application/json";
78 } catch (MalformedURLException e)
85 * Calls the Ensembl xrefs REST 'symbol' endpoint and retrieves any gene ids
86 * for the given identifier, for any known model organisms
91 public List<String> getIds(String identifier)
93 List<String> result = new ArrayList<String>();
94 List<String> ids = new ArrayList<String>();
97 String[] queries = identifier.split(getAccessionSeparator());
98 BufferedReader br = null;
101 for (String query : queries)
103 for (Species taxon : Species.values())
105 if (taxon.isModelOrganism())
107 URL url = getUrl(query, taxon);
110 br = getHttpResponse(url, ids);
112 String geneId = parseSymbolResponse(br);
120 } catch (IOException e)
130 } catch (IOException e)