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.Iterator;
11 import java.util.List;
13 import org.json.simple.JSONArray;
14 import org.json.simple.JSONObject;
15 import org.json.simple.parser.JSONParser;
16 import org.json.simple.parser.ParseException;
18 public class EnsemblMap extends EnsemblRestClient
20 private static final String MAPPED = "mapped";
22 private static final String MAPPINGS = "mappings";
25 * Default constructor (to use rest.ensembl.org)
33 * Constructor given the target domain to fetch data from
37 public EnsemblMap(String domain)
43 public String getDbName()
45 return DBRefSource.ENSEMBL;
49 public AlignmentI getSequenceRecords(String queries) throws Exception
51 return null; // not used
55 * Constructs a URL of the format <code>
56 * http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37?content-type=application/json
66 * @throws MalformedURLException
68 protected URL getAssemblyMapUrl(String species, String chromosome, String fromRef,
69 String toRef, int startPos, int endPos)
70 throws MalformedURLException
73 * start-end might be reverse strand - present forwards to the service
75 boolean forward = startPos <= endPos;
76 int start = forward ? startPos : endPos;
77 int end = forward ? endPos : startPos;
78 String strand = forward ? "1" : "-1";
79 String url = String.format(
80 "%s/map/%s/%s/%s:%d..%d:%s/%s?content-type=application/json",
81 getDomain(), species, fromRef, chromosome, start, end, strand,
87 protected boolean useGetRequest()
93 protected URL getUrl(List<String> ids) throws MalformedURLException
95 return null; // not used
99 * Calls the REST /map service to get the chromosomal coordinates (start/end)
100 * in 'toRef' that corresponding to the (start/end) queryRange in 'fromRef'
108 * @see http://rest.ensemblgenomes.org/documentation/info/assembly_map
110 public int[] getAssemblyMapping(String species, String chromosome,
111 String fromRef, String toRef, int[] queryRange)
114 BufferedReader br = null;
118 url = getAssemblyMapUrl(species, chromosome, fromRef, toRef, queryRange[0],
120 br = getHttpResponse(url, null);
121 return (parseAssemblyMappingResponse(br));
122 } catch (Throwable t)
124 System.out.println("Error calling " + url + ": " + t.getMessage());
133 } catch (IOException e)
142 * Parses the JSON response from the /map/<species>/ REST service. The
143 * format is (with some fields omitted)
148 * "original": {"end":45109016,"start":45051610},
149 * "mapped" : {"end":43186384,"start":43128978}
156 protected int[] parseAssemblyMappingResponse(BufferedReader br)
159 JSONParser jp = new JSONParser();
163 JSONObject parsed = (JSONObject) jp.parse(br);
164 JSONArray mappings = (JSONArray) parsed.get(MAPPINGS);
166 Iterator rvals = mappings.iterator();
167 while (rvals.hasNext())
169 // todo check for "mapped"
170 JSONObject val = (JSONObject) rvals.next();
171 JSONObject mapped = (JSONObject) val.get(MAPPED);
172 int start = Integer.parseInt(mapped.get("start").toString());
173 int end = Integer.parseInt(mapped.get("end").toString());
174 String strand = mapped.get("strand").toString();
175 if ("1".equals(strand))
177 result = new int[] { start, end };
181 result = new int[] { end, start };
184 } catch (IOException | ParseException | NumberFormatException e)
192 * Constructs a URL to the /map/cds/<id> or /map/cdna/<id> REST service. The
193 * REST call is to either ensembl or ensemblgenomes, as determined from the
194 * division, e.g. Ensembl or EnsemblProtists.
202 * @throws MalformedURLException
204 URL getIdMapUrl(String domain, String accession, int start, int end,
205 String cdsOrCdna) throws MalformedURLException
208 .format("%s/map/%s/%s/%d..%d?include_original_region=1&content-type=application/json",
209 domain, cdsOrCdna, accession, start, end);