ids) throws MalformedURLException
{
return null; // not used
}
public int[] getMapping(String species, String chromosome,
String fromRef, String toRef, int[] queryRange)
{
URL url = null;
BufferedReader br = null;
try
{
url = getUrl(species, chromosome, fromRef, toRef, queryRange[0],
queryRange[1]);
// System.out.println("Calling " + url);
br = getHttpResponse(url, null);
return (parseResponse(br));
} catch (Throwable t)
{
System.out.println("Error calling " + url + ": " + t.getMessage());
return null;
}
}
/**
* Parses the JSON response from the /map REST service. The format is (with
* some fields omitted)
*
*
* {"mappings":
* [{
* "original": {"end":45109016,"start":45051610},
* "mapped" : {"end":43186384,"start":43128978}
* }] }
*
*
* @param br
* @return
*/
protected int[] parseResponse(BufferedReader br)
{
int[] result = null;
JSONParser jp = new JSONParser();
try
{
JSONObject parsed = (JSONObject) jp.parse(br);
JSONArray mappings = (JSONArray) parsed.get("mappings");
Iterator rvals = mappings.iterator();
while (rvals.hasNext())
{
// todo check for "mapped"
JSONObject val = (JSONObject) rvals.next();
JSONObject mapped = (JSONObject) val.get("mapped");
int start = Integer.parseInt(mapped.get("start").toString());
int end = Integer.parseInt(mapped.get("end").toString());
String strand = mapped.get("strand").toString();
if ("1".equals(strand))
{
result = new int[] { start, end };
}
else
{
result = new int[] { end, start };
}
}
} catch (IOException | ParseException | NumberFormatException e)
{
// ignore
}
return result;
}
}