2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ext.ensembl;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.DBRefSource;
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.net.MalformedURLException;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
36 import org.json.simple.JSONArray;
37 import org.json.simple.parser.JSONParser;
38 import org.json.simple.parser.ParseException;
40 public class EnsemblInfo extends EnsemblRestClient
44 * cached results of REST /info/divisions service, currently
47 * { "ENSEMBLFUNGI", "http://rest.ensemblgenomes.org"},
48 * "ENSEMBLBACTERIA", "http://rest.ensemblgenomes.org"},
49 * "ENSEMBLPROTISTS", "http://rest.ensemblgenomes.org"},
50 * "ENSEMBLMETAZOA", "http://rest.ensemblgenomes.org"},
51 * "ENSEMBLPLANTS", "http://rest.ensemblgenomes.org"},
52 * "ENSEMBL", "http://rest.ensembl.org" }
55 * The values for EnsemblGenomes are retrieved by a REST call, that for
56 * Ensembl is added programmatically for convenience of lookup
58 private static Map<String, String> divisions;
61 public String getDbName()
67 public AlignmentI getSequenceRecords(String queries) throws Exception
73 protected URL getUrl(List<String> ids) throws MalformedURLException
79 protected boolean useGetRequest()
85 * Answers the domain (http://rest.ensembl.org or
86 * http://rest.ensemblgenomes.org) for the given division, or null if not
87 * recognised by Ensembl.
92 public String getDomain(String division)
94 if (divisions == null)
98 return divisions.get(division.toUpperCase());
102 * On first request only, populate the lookup map by fetching the list of
103 * divisions known to EnsemblGenomes.
105 void fetchDivisions()
107 divisions = new HashMap<>();
110 * for convenience, pre-fill ensembl.org as the domain for "ENSEMBL"
112 divisions.put(DBRefSource.ENSEMBL.toUpperCase(), ensemblDomain);
114 BufferedReader br = null;
117 URL url = getDivisionsUrl(ensemblGenomesDomain);
120 br = getHttpResponse(url, null);
122 parseResponse(br, ensemblGenomesDomain);
123 } catch (IOException e)
133 } catch (IOException e)
142 * Parses the JSON response to /info/divisions, and add each to the lookup map
147 void parseResponse(BufferedReader br, String domain)
149 JSONParser jp = new JSONParser();
153 JSONArray parsed = (JSONArray) jp.parse(br);
155 Iterator rvals = parsed.iterator();
156 while (rvals.hasNext())
158 String division = rvals.next().toString();
159 divisions.put(division.toUpperCase(), domain);
161 } catch (IOException | ParseException | NumberFormatException e)
168 * Constructs the URL for the EnsemblGenomes /info/divisions REST service
172 * @throws MalformedURLException
174 URL getDivisionsUrl(String domain) throws MalformedURLException
176 return new URL(domain
177 + "/info/divisions?content-type=application/json");
181 * Returns the set of 'divisions' recognised by Ensembl or EnsemblGenomes
185 public Set<String> getDivisions() {
186 if (divisions == null)
191 return divisions.keySet();