JAL-1793 spike branch updated to latest
[jalview.git] / src / jalview / ext / ensembl / EnsemblLookup.java
index f314b0a..0d1b554 100644 (file)
  */
 package jalview.ext.ensembl;
 
+import jalview.bin.Cache;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.GeneLociI;
+import jalview.util.MapList;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.function.Function;
 
@@ -124,15 +128,20 @@ public class EnsemblLookup extends EnsemblRestClient
   }
 
   /**
-   * Calls the Ensembl lookup REST endpoint and retrieves the 'Parent' for the
-   * given identifier, or null if not found
+   * Calls the Ensembl lookup REST endpoint and returns
+   * <ul>
+   * <li>the 'id' for the identifier if its type is "Gene"</li>
+   * <li>the 'Parent' if its type is 'Transcript'</li>
+   * <ul>
+   * If the type is 'Translation', does a recursive call to this method, passing
+   * in the 'Parent' (transcript id).
    * 
    * @param identifier
    * @return
    */
   public String getGeneId(String identifier)
   {
-    return getResult(identifier, br -> parseGeneId(br));
+    return (String) getResult(identifier, br -> parseGeneId(br));
   }
 
   /**
@@ -144,16 +153,19 @@ public class EnsemblLookup extends EnsemblRestClient
    */
   public String getSpecies(String identifier)
   {
-    return getResult(identifier, br -> getAttribute(br, SPECIES));
+    return (String) getResult(identifier, br -> getAttribute(br, SPECIES));
   }
 
   /**
+   * Calls the /lookup/id rest service and delegates parsing of the JSON
+   * response to the supplied parser
+   * 
    * @param identifier
-   * @param attribute
+   * @param parser
    * @return
    */
-  protected String getResult(String identifier,
-          Function<BufferedReader, String> parser)
+  protected Object getResult(String identifier,
+          Function<BufferedReader, Object> parser)
   {
     List<String> ids = Arrays.asList(new String[] { identifier });
 
@@ -257,4 +269,80 @@ public class EnsemblLookup extends EnsemblRestClient
     return geneId;
   }
 
+  /**
+   * Calls the /lookup/id rest service for the given id, and if successful,
+   * parses and returns the gene's chromosomal coordinates
+   * 
+   * @param geneId
+   * @return
+   */
+  public GeneLociI getGeneLoci(String geneId)
+  {
+    return (GeneLociI) getResult(geneId, br -> parseGeneLoci(br));
+  }
+
+  /**
+   * Parses the /lookup/id response for species, asssembly_name,
+   * seq_region_name, start, end and returns an object that wraps them, or null
+   * if unsuccessful
+   * 
+   * @param br
+   * @return
+   */
+  GeneLociI parseGeneLoci(BufferedReader br)
+  {
+    JSONParser jp = new JSONParser();
+    try
+    {
+      JSONObject val = (JSONObject) jp.parse(br);
+      final String species = val.get("species").toString();
+      final String assembly = val.get("assembly_name").toString();
+      final String chromosome = val.get("seq_region_name").toString();
+      String strand = val.get("strand").toString();
+      int start = Integer.parseInt(val.get("start").toString());
+      int end = Integer.parseInt(val.get("end").toString());
+      int fromEnd = end - start + 1;
+      boolean reverseStrand = "-1".equals(strand);
+      int toStart = reverseStrand ? end : start;
+      int toEnd = reverseStrand ? start : end;
+      List<int[]> fromRange = Collections.singletonList(new int[] { 1,
+          fromEnd });
+      List<int[]> toRange = Collections.singletonList(new int[] { toStart,
+          toEnd });
+      final MapList map = new MapList(fromRange, toRange, 1, 1);
+      return new GeneLociI()
+      {
+
+        @Override
+        public String getSpeciesId()
+        {
+          return species == null ? "" : species;
+        }
+
+        @Override
+        public String getAssemblyId()
+        {
+          return assembly;
+        }
+
+        @Override
+        public String getChromosomeId()
+        {
+          return chromosome;
+        }
+
+        @Override
+        public MapList getMap()
+        {
+          return map;
+        }
+      };
+    } catch (ParseException | NullPointerException | IOException
+            | NumberFormatException | ClassCastException e)
+    {
+      Cache.log.error("Error looking up gene loci: " + e.getMessage());
+    }
+    return null;
+  }
+
 }