1 package jalview.ext.ensembl;
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.DBRefSource;
5 import jalview.datamodel.GeneLociI;
6 import jalview.util.MapList;
8 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.net.MalformedURLException;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.Iterator;
15 import java.util.List;
17 import org.json.simple.JSONArray;
18 import org.json.simple.JSONObject;
19 import org.json.simple.parser.JSONParser;
20 import org.json.simple.parser.ParseException;
22 public class EnsemblMap extends EnsemblRestClient
24 private static final String MAPPED = "mapped";
26 private static final String MAPPINGS = "mappings";
28 private static final String CDS = "cds";
30 private static final String CDNA = "cdna";
33 * Default constructor (to use rest.ensembl.org)
41 public String getDbName()
43 return DBRefSource.ENSEMBL;
47 public AlignmentI getSequenceRecords(String queries) throws Exception
49 return null; // not used
53 * Constructs a URL of the format <code>
54 * http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37?content-type=application/json
64 * @throws MalformedURLException
66 protected URL getAssemblyMapUrl(String species, String chromosome, String fromRef,
67 String toRef, int startPos, int endPos)
68 throws MalformedURLException
71 * start-end might be reverse strand - present forwards to the service
73 boolean forward = startPos <= endPos;
74 int start = forward ? startPos : endPos;
75 int end = forward ? endPos : startPos;
76 String strand = forward ? "1" : "-1";
77 String url = String.format(
78 "%s/map/%s/%s/%s:%d..%d:%s/%s?content-type=application/json",
79 getDomain(), species, fromRef, chromosome, start, end, strand,
85 protected boolean useGetRequest()
91 protected URL getUrl(List<String> ids) throws MalformedURLException
93 return null; // not used
97 * Calls the REST /map service to get the chromosomal coordinates (start/end)
98 * in 'toRef' that corresponding to the (start/end) queryRange in 'fromRef'
106 * @see http://rest.ensembl.org/documentation/info/assembly_map
108 public int[] getAssemblyMapping(String species, String chromosome,
109 String fromRef, String toRef, int[] queryRange)
112 BufferedReader br = null;
116 url = getAssemblyMapUrl(species, chromosome, fromRef, toRef, queryRange[0],
118 br = getHttpResponse(url, null);
119 return (parseAssemblyMappingResponse(br));
120 } catch (Throwable t)
122 System.out.println("Error calling " + url + ": " + t.getMessage());
131 } catch (IOException e)
140 * Parses the JSON response from the /map/<species>/ REST service. The
141 * format is (with some fields omitted)
146 * "original": {"end":45109016,"start":45051610},
147 * "mapped" : {"end":43186384,"start":43128978}
154 protected int[] parseAssemblyMappingResponse(BufferedReader br)
157 JSONParser jp = new JSONParser();
161 JSONObject parsed = (JSONObject) jp.parse(br);
162 JSONArray mappings = (JSONArray) parsed.get(MAPPINGS);
164 Iterator rvals = mappings.iterator();
165 while (rvals.hasNext())
167 // todo check for "mapped"
168 JSONObject val = (JSONObject) rvals.next();
169 JSONObject mapped = (JSONObject) val.get(MAPPED);
170 int start = Integer.parseInt(mapped.get("start").toString());
171 int end = Integer.parseInt(mapped.get("end").toString());
172 String strand = mapped.get("strand").toString();
173 if ("1".equals(strand))
175 result = new int[] { start, end };
179 result = new int[] { end, start };
182 } catch (IOException | ParseException | NumberFormatException e)
190 * Calls the REST /map/cds/id service, and returns a DBRefEntry holding the
191 * returned chromosomal coordinates, or returns null if the call fails
194 * e.g. Ensembl, EnsemblMetazoa
196 * e.g. ENST00000592782, Y55B1AR.1.1
201 public GeneLociI getCdsMapping(String division, String accession,
204 return getIdMapping(division, accession, start, end, CDS);
208 * Calls the REST /map/cdna/id service, and returns a DBRefEntry holding the
209 * returned chromosomal coordinates, or returns null if the call fails
212 * e.g. Ensembl, EnsemblMetazoa
214 * e.g. ENST00000592782, Y55B1AR.1.1
219 public GeneLociI getCdnaMapping(String division, String accession,
222 return getIdMapping(division, accession, start, end, CDNA);
225 GeneLociI getIdMapping(String division, String accession, int start,
226 int end, String cdsOrCdna)
229 BufferedReader br = null;
233 String domain = new EnsemblInfo().getDomain(division);
236 url = getIdMapUrl(domain, accession, start, end, cdsOrCdna);
237 br = getHttpResponse(url, null);
240 return (parseIdMappingResponse(br, accession));
244 } catch (Throwable t)
246 System.out.println("Error calling " + url + ": " + t.getMessage());
255 } catch (IOException e)
264 * Constructs a URL to the /map/cds/<id> or /map/cdna/<id> REST service
272 * @throws MalformedURLException
274 URL getIdMapUrl(String domain, String accession, int start, int end,
275 String cdsOrCdna) throws MalformedURLException
278 .format("%s/map/%s/%s/%d..%d?include_original_region=1&content-type=application/json",
279 domain, cdsOrCdna, accession, start, end);
284 * Parses the JSON response from the /map/cds/ or /map/cdna REST service. The
290 * {"assembly_name":"TAIR10","end":2501311,"seq_region_name":"1","gap":0,
291 * "strand":-1,"coord_system":"chromosome","rank":0,"start":2501114},
292 * {"assembly_name":"TAIR10","end":2500815,"seq_region_name":"1","gap":0,
293 * "strand":-1,"coord_system":"chromosome","rank":0,"start":2500714}
302 GeneLociI parseIdMappingResponse(BufferedReader br, String accession)
304 JSONParser jp = new JSONParser();
308 JSONObject parsed = (JSONObject) jp.parse(br);
309 JSONArray mappings = (JSONArray) parsed.get(MAPPINGS);
311 Iterator rvals = mappings.iterator();
312 String assembly = null;
313 String chromosome = null;
315 List<int[]> regions = new ArrayList<>();
317 while (rvals.hasNext())
319 JSONObject val = (JSONObject) rvals.next();
320 JSONObject original = (JSONObject) val.get("original");
321 fromEnd = Integer.parseInt(original.get("end").toString());
323 JSONObject mapped = (JSONObject) val.get(MAPPED);
324 int start = Integer.parseInt(mapped.get("start").toString());
325 int end = Integer.parseInt(mapped.get("end").toString());
326 String ass = mapped.get("assembly_name").toString();
327 if (assembly != null && !assembly.equals(ass))
330 .println("EnsemblMap found multiple assemblies - can't resolve");
334 String chr = mapped.get("seq_region_name").toString();
335 if (chromosome != null && !chromosome.equals(chr))
338 .println("EnsemblMap found multiple chromosomes - can't resolve");
342 String strand = mapped.get("strand").toString();
343 if ("-1".equals(strand))
345 regions.add(new int[] { end, start });
349 regions.add(new int[] { start, end });
354 * processed all mapped regions on chromosome, assemble the result,
355 * having first fetched the species id for the accession
357 final String species = new EnsemblLookup().getSpecies(accession);
358 final String as = assembly;
359 final String chr = chromosome;
360 List<int[]> fromRange = Collections.singletonList(new int[] { 1,
362 final MapList map = new MapList(fromRange, regions, 1, 1);
363 return new GeneLociI()
367 public String getSpeciesId()
369 return species == null ? "" : species;
373 public String getAssemblyId()
379 public String getChromosomeId()
385 public MapList getMap()
390 } catch (IOException | ParseException | NumberFormatException e)