1 package jalview.ext.ensembl;
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.DBRefSource;
5 import jalview.datamodel.GeneLociI;
6 import jalview.util.JSONUtils;
7 import jalview.util.MapList;
9 import java.io.BufferedReader;
10 import java.io.IOException;
11 import java.net.MalformedURLException;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Iterator;
16 import java.util.List;
19 import org.json.simple.parser.ParseException;
21 public class EnsemblMap extends EnsemblRestClient
23 private static final String MAPPED = "mapped";
25 private static final String MAPPINGS = "mappings";
27 private static final String CDS = "cds";
29 private static final String CDNA = "cdna";
32 * Default constructor (to use rest.ensembl.org)
40 * Constructor given the target domain to fetch data from
44 public EnsemblMap(String domain)
50 public String getDbName()
52 return DBRefSource.ENSEMBL;
56 public AlignmentI getSequenceRecords(String queries) throws Exception
58 return null; // not used
62 * Constructs a URL of the format <code>
63 * http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37?content-type=application/json
73 * @throws MalformedURLException
75 protected URL getAssemblyMapUrl(String species, String chromosome, String fromRef,
76 String toRef, int startPos, int endPos)
77 throws MalformedURLException
80 * start-end might be reverse strand - present forwards to the service
82 boolean forward = startPos <= endPos;
83 int start = forward ? startPos : endPos;
84 int end = forward ? endPos : startPos;
85 String strand = forward ? "1" : "-1";
86 String url = String.format(
87 "%s/map/%s/%s/%s:%d..%d:%s/%s?content-type=application/json",
88 getDomain(), species, fromRef, chromosome, start, end, strand,
94 protected boolean useGetRequest()
100 protected URL getUrl(List<String> ids) throws MalformedURLException
102 return null; // not used
106 * Calls the REST /map service to get the chromosomal coordinates (start/end)
107 * in 'toRef' that corresponding to the (start/end) queryRange in 'fromRef'
115 * @see http://rest.ensemblgenomes.org/documentation/info/assembly_map
117 public int[] getAssemblyMapping(String species, String chromosome,
118 String fromRef, String toRef, int[] queryRange)
123 url = getAssemblyMapUrl(species, chromosome, fromRef, toRef, queryRange[0],
125 return (parseAssemblyMappingResponse(url));
126 } catch (Throwable t)
128 System.out.println("Error calling " + url + ": " + t.getMessage());
134 * Parses the JSON response from the /map/<species>/ REST service. The
135 * format is (with some fields omitted)
140 * "original": {"end":45109016,"start":45051610},
141 * "mapped" : {"end":43186384,"start":43128978}
148 @SuppressWarnings("unchecked")
149 protected int[] parseAssemblyMappingResponse(URL url)
155 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 = new 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);
290 String assembly = null;
291 String chromosome = null;
293 List<int[]> regions = new ArrayList<>();
295 while (rvals.hasNext())
297 Map<String, Object> val = (Map<String, Object>) rvals.next();
298 Map<String, Object> original = (Map<String, Object>) val.get("original");
299 fromEnd = Integer.parseInt(original.get("end").toString());
301 Map<String, Object> mapped = (Map<String, Object>) val.get(MAPPED);
302 int start = Integer.parseInt(mapped.get("start").toString());
303 int end = Integer.parseInt(mapped.get("end").toString());
304 String ass = mapped.get("assembly_name").toString();
305 if (assembly != null && !assembly.equals(ass))
308 .println("EnsemblMap found multiple assemblies - can't resolve");
312 String chr = mapped.get("seq_region_name").toString();
313 if (chromosome != null && !chromosome.equals(chr))
316 .println("EnsemblMap found multiple chromosomes - can't resolve");
320 String strand = mapped.get("strand").toString();
321 if ("-1".equals(strand))
323 regions.add(new int[] { end, start });
327 regions.add(new int[] { start, end });
332 * processed all mapped regions on chromosome, assemble the result,
333 * having first fetched the species id for the accession
335 final String species = new EnsemblLookup(domain)
336 .getSpecies(accession);
337 final String as = assembly;
338 final String chr = chromosome;
339 List<int[]> fromRange = Collections.singletonList(new int[] { 1,
341 final MapList map = new MapList(fromRange, regions, 1, 1);
342 return new GeneLociI()
346 public String getSpeciesId()
348 return species == null ? "" : species;
352 public String getAssemblyId()
358 public String getChromosomeId()
364 public MapList getMap()
369 } catch (IOException | ParseException | NumberFormatException e)