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
22 * Default constructor (to use rest.ensembl.org)
30 * Constructor given the target domain to fetch data from
34 public EnsemblMap(String domain)
40 public String getDbName()
42 return DBRefSource.ENSEMBL;
46 public AlignmentI getSequenceRecords(String queries) throws Exception
48 return null; // not used
52 * Constructs a URL of the format <code>
53 * http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37?content-type=application/json
63 * @throws MalformedURLException
65 protected URL getUrl(String species, String chromosome, String fromRef,
66 String toRef, int startPos, int endPos)
67 throws MalformedURLException
70 * start-end might be reverse strand - present forwards to the service
72 boolean forward = startPos <= endPos;
73 int start = forward ? startPos : endPos;
74 int end = forward ? endPos : startPos;
75 String strand = forward ? "1" : "-1";
76 String url = String.format(
77 "%s/map/%s/%s/%s:%d..%d:%s/%s?content-type=application/json",
78 getDomain(), species, fromRef, chromosome, start, end, strand,
83 } catch (MalformedURLException e)
90 protected boolean useGetRequest()
96 protected String getRequestMimeType(boolean multipleIds)
98 return "application/json";
102 protected String getResponseMimeType()
104 return "application/json";
108 protected URL getUrl(List<String> ids) throws MalformedURLException
110 return null; // not used
113 public int[] getMapping(String species, String chromosome,
114 String fromRef, String toRef, int[] queryRange)
117 BufferedReader br = null;
121 url = getUrl(species, chromosome, fromRef, toRef, queryRange[0],
123 // System.out.println("Calling " + url);
124 br = getHttpResponse(url, null);
125 return (parseResponse(br));
126 } catch (Throwable t)
128 System.out.println("Error calling " + url + ": " + t.getMessage());
134 * Parses the JSON response from the /map REST service. The format is (with
135 * some fields omitted)
140 * "original": {"end":45109016,"start":45051610},
141 * "mapped" : {"end":43186384,"start":43128978}
148 protected int[] parseResponse(BufferedReader br)
151 JSONParser jp = new JSONParser();
155 JSONObject parsed = (JSONObject) jp.parse(br);
156 JSONArray mappings = (JSONArray) parsed.get("mappings");
158 Iterator rvals = mappings.iterator();
159 while (rvals.hasNext())
161 // todo check for "mapped"
162 JSONObject val = (JSONObject) rvals.next();
163 JSONObject mapped = (JSONObject) val.get("mapped");
164 int start = Integer.parseInt(mapped.get("start").toString());
165 int end = Integer.parseInt(mapped.get("end").toString());
166 String strand = mapped.get("strand").toString();
167 if ("1".equals(strand))
169 result = new int[] { start, end };
173 result = new int[] { end, start };
176 } catch (IOException | ParseException | NumberFormatException e)