JAL-2738 correct handling of reverse strand genes
[jalview.git] / src / jalview / ext / ensembl / EnsemblMap.java
index d522ea8..05cc897 100644 (file)
@@ -48,13 +48,35 @@ public class EnsemblMap extends EnsemblRestClient
     return null; // not used
   }
 
+  /**
+   * Constructs a URL of the format <code>
+   * http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37?content-type=application/json
+   * </code>
+   * 
+   * @param species
+   * @param chromosome
+   * @param fromRef
+   * @param toRef
+   * @param startPos
+   * @param endPos
+   * @return
+   * @throws MalformedURLException
+   */
   protected URL getUrl(String species, String chromosome, String fromRef,
           String toRef, int startPos, int endPos)
           throws MalformedURLException
   {
-    String url = getDomain() + "/map/" + species + "/" + fromRef + "/"
-            + chromosome + ":" + startPos + ".." + endPos + ":1/" + toRef
-            + "?content-type=application/json";
+    /*
+     * start-end might be reverse strand - present forwards to the service
+     */
+    boolean forward = startPos <= endPos;
+    int start = forward ? startPos : endPos;
+    int end = forward ? endPos : startPos;
+    String strand = forward ? "1" : "-1";
+    String url = String.format(
+            "%s/map/%s/%s/%s:%d..%d:%s/%s?content-type=application/json",
+            getDomain(), species, fromRef, chromosome, start, end, strand,
+            toRef);
     try
     {
       return new URL(url);
@@ -98,6 +120,7 @@ public class EnsemblMap extends EnsemblRestClient
     {
       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)
@@ -138,9 +161,17 @@ public class EnsemblMap extends EnsemblRestClient
         // todo check for "mapped"
         JSONObject val = (JSONObject) rvals.next();
         JSONObject mapped = (JSONObject) val.get("mapped");
-        String start = mapped.get("start").toString();
-        String end = mapped.get("end").toString();
-        result = new int[] { Integer.parseInt(start), Integer.parseInt(end) };
+        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)
     {