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.IOException;
9 import java.net.MalformedURLException;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Iterator;
14 import java.util.List;
17 import org.json.simple.parser.ParseException;
19 public class EnsemblMap extends EnsemblRestClient
21 private static final String MAPPED = "mapped";
23 private static final String MAPPINGS = "mappings";
25 private static final String CDS = "cds";
27 private static final String CDNA = "cdna";
30 * Default constructor (to use rest.ensembl.org)
38 * Constructor given the target domain to fetch data from
42 public EnsemblMap(String domain)
48 public String getDbName()
50 return DBRefSource.ENSEMBL;
54 public AlignmentI getSequenceRecords(String queries) throws Exception
56 return null; // not used
60 * Constructs a URL of the format <code>
61 * http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37?content-type=application/json
71 * @throws MalformedURLException
73 protected URL getAssemblyMapUrl(String species, String chromosome, String fromRef,
74 String toRef, int startPos, int endPos)
75 throws MalformedURLException
78 * start-end might be reverse strand - present forwards to the service
80 boolean forward = startPos <= endPos;
81 int start = forward ? startPos : endPos;
82 int end = forward ? endPos : startPos;
83 String strand = forward ? "1" : "-1";
84 String url = String.format(
85 "%s/map/%s/%s/%s:%d..%d:%s/%s?content-type=application/json",
86 getDomain(), species, fromRef, chromosome, start, end, strand,
92 protected boolean useGetRequest()
98 protected URL getUrl(List<String> ids) throws MalformedURLException
100 return null; // not used
104 * Calls the REST /map service to get the chromosomal coordinates (start/end)
105 * in 'toRef' that corresponding to the (start/end) queryRange in 'fromRef'
113 * @see http://rest.ensemblgenomes.org/documentation/info/assembly_map
115 public int[] getAssemblyMapping(String species, String chromosome,
116 String fromRef, String toRef, int[] queryRange)
121 url = getAssemblyMapUrl(species, chromosome, fromRef, toRef, queryRange[0],
123 return (parseAssemblyMappingResponse(url));
124 } catch (Throwable t)
126 System.out.println("Error calling " + url + ": " + t.getMessage());
132 * Parses the JSON response from the /map/<species>/ REST service. The
133 * format is (with some fields omitted)
138 * "original": {"end":45109016,"start":45051610},
139 * "mapped" : {"end":43186384,"start":43128978}
146 @SuppressWarnings("unchecked")
147 protected int[] parseAssemblyMappingResponse(URL url)
153 Iterator<Object> rvals = (Iterator<Object>) getJSON(url, null, -1, MODE_ITERATOR, MAPPINGS);
158 while (rvals.hasNext())
160 // todo check for "mapped"
161 Map<String, Object> val = (Map<String, Object>) rvals.next();
162 Map<String, Object> mapped = (Map<String, Object>) val.get(MAPPED);
163 int start = Integer.parseInt(mapped.get("start").toString());
164 int end = Integer.parseInt(mapped.get("end").toString());
165 String strand = mapped.get("strand").toString();
166 if ("1".equals(strand))
168 result = new int[] { start, end };
172 result = new int[] { end, start };
175 } catch (IOException | ParseException | NumberFormatException e)
183 * Calls the REST /map/cds/id service, and returns a DBRefEntry holding the
184 * returned chromosomal coordinates, or returns null if the call fails
187 * e.g. Ensembl, EnsemblMetazoa
189 * e.g. ENST00000592782, Y55B1AR.1.1
194 public GeneLociI getCdsMapping(String division, String accession,
197 return getIdMapping(division, accession, start, end, CDS);
201 * Calls the REST /map/cdna/id service, and returns a DBRefEntry holding the
202 * returned chromosomal coordinates, or returns null if the call fails
205 * e.g. Ensembl, EnsemblMetazoa
207 * e.g. ENST00000592782, Y55B1AR.1.1
212 public GeneLociI getCdnaMapping(String division, String accession,
215 return getIdMapping(division, accession, start, end, CDNA);
218 GeneLociI getIdMapping(String division, String accession, int start,
219 int end, String cdsOrCdna)
224 String domain = EnsemblInfo.getDomain(division);
227 url = getIdMapUrl(domain, accession, start, end, cdsOrCdna);
228 return (parseIdMappingResponse(url, accession, domain));
231 } catch (Throwable t)
233 System.out.println("Error calling " + url + ": " + t.getMessage());
239 * Constructs a URL to the /map/cds/<id> or /map/cdna/<id> REST service. The
240 * REST call is to either ensembl or ensemblgenomes, as determined from the
241 * division, e.g. Ensembl or EnsemblProtists.
249 * @throws MalformedURLException
251 URL getIdMapUrl(String domain, String accession, int start, int end,
252 String cdsOrCdna) throws MalformedURLException
255 .format("%s/map/%s/%s/%d..%d?include_original_region=1&content-type=application/json",
256 domain, cdsOrCdna, accession, start, end);
261 * Parses the JSON response from the /map/cds/ or /map/cdna REST service. The
267 * {"assembly_name":"TAIR10","end":2501311,"seq_region_name":"1","gap":0,
268 * "strand":-1,"coord_system":"chromosome","rank":0,"start":2501114},
269 * {"assembly_name":"TAIR10","end":2500815,"seq_region_name":"1","gap":0,
270 * "strand":-1,"coord_system":"chromosome","rank":0,"start":2500714}
280 @SuppressWarnings("unchecked")
281 GeneLociI parseIdMappingResponse(URL url, String accession,
287 Iterator<Object> rvals = (Iterator<Object>) getJSON(url, null, -1, MODE_ITERATOR, MAPPINGS);
292 String assembly = null;
293 String chromosome = null;
295 List<int[]> regions = new ArrayList<>();
297 while (rvals.hasNext())
299 Map<String, Object> val = (Map<String, Object>) rvals.next();
300 Map<String, Object> original = (Map<String, Object>) val.get("original");
301 fromEnd = Integer.parseInt(original.get("end").toString());
303 Map<String, Object> mapped = (Map<String, Object>) val.get(MAPPED);
304 int start = Integer.parseInt(mapped.get("start").toString());
305 int end = Integer.parseInt(mapped.get("end").toString());
306 String ass = mapped.get("assembly_name").toString();
307 if (assembly != null && !assembly.equals(ass))
310 .println("EnsemblMap found multiple assemblies - can't resolve");
314 String chr = mapped.get("seq_region_name").toString();
315 if (chromosome != null && !chromosome.equals(chr))
318 .println("EnsemblMap found multiple chromosomes - can't resolve");
322 String strand = mapped.get("strand").toString();
323 if ("-1".equals(strand))
325 regions.add(new int[] { end, start });
329 regions.add(new int[] { start, end });
334 * processed all mapped regions on chromosome, assemble the result,
335 * having first fetched the species id for the accession
337 final String species = new EnsemblLookup(domain)
338 .getSpecies(accession);
339 final String as = assembly;
340 final String chr = chromosome;
341 List<int[]> fromRange = Collections.singletonList(new int[] { 1,
343 final MapList map = new MapList(fromRange, regions, 1, 1);
344 return new GeneLociI()
348 public String getSpeciesId()
350 return species == null ? "" : species;
354 public String getAssemblyId()
360 public String getChromosomeId()
366 public MapList getMap()
371 } catch (IOException | ParseException | NumberFormatException e)