1 package jalview.ext.ensembl;
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.DBRefSource;
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.net.MalformedURLException;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.List;
16 import org.json.simple.JSONArray;
17 import org.json.simple.parser.JSONParser;
18 import org.json.simple.parser.ParseException;
20 public class EnsemblInfo extends EnsemblRestClient
24 * cached results of REST /info/divisions service, currently
27 * { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
28 * "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
29 * "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
30 * "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
31 * "ENSEMBLPLANTS", "http://rest.ensemblgenomes.org"},
32 * "ENSEMBL", "http://rest.ensembl.org" }
35 * The values for EnsemblGenomes are retrieved by a REST call, that for
36 * Ensembl is added programmatically for convenience of lookup
38 private static Map<String, String> divisions;
41 public String getDbName()
47 public AlignmentI getSequenceRecords(String queries) throws Exception
53 protected URL getUrl(List<String> ids) throws MalformedURLException
59 protected boolean useGetRequest()
65 protected String getRequestMimeType(boolean multipleIds)
67 return "application/json";
71 protected String getResponseMimeType()
73 return "application/json";
77 * Answers the domain (http://rest.ensembl.org or
78 * http://rest.ensemblgenomes.org) for the given division, or null if not
79 * recognised by Ensembl.
84 public String getDomain(String division)
86 if (divisions == null)
90 return divisions.get(division.toUpperCase());
94 * On first request only, populate the lookup map by fetching the list of
95 * divisions known to EnsemblGenomes.
99 divisions = new HashMap<>();
102 * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
104 divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain);
106 BufferedReader br = null;
109 URL url = getDivisionsUrl(ensemblGenomesDomain);
112 br = getHttpResponse(url, null);
114 parseResponse(br, ensemblGenomesDomain);
115 } catch (IOException e)
125 } catch (IOException e)
134 * Parses the JSON response to /info/divisions, and add each to the lookup map
139 void parseResponse(BufferedReader br, String domain)
141 JSONParser jp = new JSONParser();
145 JSONArray parsed = (JSONArray) jp.parse(br);
147 Iterator rvals = parsed.iterator();
148 while (rvals.hasNext())
150 String division = rvals.next().toString();
151 divisions.put(division.toUpperCase(), domain);
153 } catch (IOException | ParseException | NumberFormatException e)
160 * Constructs the URL for the EnsemblGenomes /info/divisions REST service
164 * @throws MalformedURLException
166 URL getDivisionsUrl(String domain) throws MalformedURLException
168 return new URL(domain
169 + "/info/divisions?content-type=application/json");
173 * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes
177 public Set<String> getDivisions() {
178 if (divisions == null)
183 return divisions.keySet();