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 * Answers the domain (http://rest.ensembl.org or
66 * http://rest.ensemblgenomes.org) for the given division, or null if not
67 * recognised by Ensembl.
72 public String getDomain(String division)
74 if (divisions == null)
78 return divisions.get(division.toUpperCase());
82 * On first request only, populate the lookup map by fetching the list of
83 * divisions known to EnsemblGenomes.
87 divisions = new HashMap<>();
90 * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
92 divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain);
94 BufferedReader br = null;
97 URL url = getDivisionsUrl(ensemblGenomesDomain);
100 br = getHttpResponse(url, null);
102 parseResponse(br, ensemblGenomesDomain);
103 } catch (IOException e)
113 } catch (IOException e)
122 * Parses the JSON response to /info/divisions, and add each to the lookup map
127 void parseResponse(BufferedReader br, String domain)
129 JSONParser jp = new JSONParser();
133 JSONArray parsed = (JSONArray) jp.parse(br);
135 Iterator rvals = parsed.iterator();
136 while (rvals.hasNext())
138 String division = rvals.next().toString();
139 divisions.put(division.toUpperCase(), domain);
141 } catch (IOException | ParseException | NumberFormatException e)
148 * Constructs the URL for the EnsemblGenomes /info/divisions REST service
152 * @throws MalformedURLException
154 URL getDivisionsUrl(String domain) throws MalformedURLException
156 return new URL(domain
157 + "/info/divisions?content-type=application/json");
161 * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes
165 public Set<String> getDivisions() {
166 if (divisions == null)
171 return divisions.keySet();