REST service. The
* REST call is to either ensembl or ensemblgenomes, as determined from the
* division, e.g. Ensembl or EnsemblProtists.
*
* @param domain
* @param accession
* @param start
* @param end
* @param cdsOrCdna
* @return
* @throws MalformedURLException
*/
URL getIdMapUrl(String domain, String accession, int start, int end,
String cdsOrCdna) throws MalformedURLException
{
String url = String
.format("%s/map/%s/%s/%d..%d?include_original_region=1&content-type=application/json",
domain, cdsOrCdna, accession, start, end);
return new URL(url);
}
/**
* Parses the JSON response from the /map/cds/ or /map/cdna REST service. The
* format is
*
*
* {"mappings":
* [
* {"assembly_name":"TAIR10","end":2501311,"seq_region_name":"1","gap":0,
* "strand":-1,"coord_system":"chromosome","rank":0,"start":2501114},
* {"assembly_name":"TAIR10","end":2500815,"seq_region_name":"1","gap":0,
* "strand":-1,"coord_system":"chromosome","rank":0,"start":2500714}
* ]
* }
*
*
* @param br
* @param accession
* @param domain
* @return
*/
GeneLociI parseIdMappingResponse(BufferedReader br, String accession,
String domain)
{
JSONParser jp = new JSONParser();
try
{
JSONObject parsed = (JSONObject) jp.parse(br);
JSONArray mappings = (JSONArray) parsed.get(MAPPINGS);
Iterator rvals = mappings.iterator();
String assembly = null;
String chromosome = null;
int fromEnd = 0;
List regions = new ArrayList<>();
while (rvals.hasNext())
{
JSONObject val = (JSONObject) rvals.next();
JSONObject original = (JSONObject) val.get("original");
fromEnd = Integer.parseInt(original.get("end").toString());
JSONObject mapped = (JSONObject) val.get(MAPPED);
int start = Integer.parseInt(mapped.get("start").toString());
int end = Integer.parseInt(mapped.get("end").toString());
String ass = mapped.get("assembly_name").toString();
if (assembly != null && !assembly.equals(ass))
{
System.err
.println("EnsemblMap found multiple assemblies - can't resolve");
return null;
}
assembly = ass;
String chr = mapped.get("seq_region_name").toString();
if (chromosome != null && !chromosome.equals(chr))
{
System.err
.println("EnsemblMap found multiple chromosomes - can't resolve");
return null;
}
chromosome = chr;
String strand = mapped.get("strand").toString();
if ("-1".equals(strand))
{
regions.add(new int[] { end, start });
}
else
{
regions.add(new int[] { start, end });
}
}
/*
* processed all mapped regions on chromosome, assemble the result,
* having first fetched the species id for the accession
*/
final String species = new EnsemblLookup(domain)
.getSpecies(accession);
final String as = assembly;
final String chr = chromosome;
List fromRange = Collections.singletonList(new int[] { 1,
fromEnd });
final MapList map = new MapList(fromRange, regions, 1, 1);
return new GeneLociI()
{
@Override
public String getSpeciesId()
{
return species == null ? "" : species;
}
@Override
public String getAssemblyId()
{
return as;
}
@Override
public String getChromosomeId()
{
return chr;
}
@Override
public MapList getMap()
{
return map;
}
};
} catch (IOException | ParseException | NumberFormatException e)
{
// ignore
}
return null;
}
}