Merge branch 'develop' into features/JAL-1793VCF; lambda Function for
[jalview.git] / src / jalview / ext / ensembl / EnsemblLookup.java
index 31da9c0..f314b0a 100644 (file)
@@ -28,23 +28,24 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Arrays;
 import java.util.List;
+import java.util.function.Function;
 
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
 
 /**
- * A client for the Ensembl lookup REST endpoint; used to find the Parent gene
- * identifier given a transcript identifier.
+ * A client for the Ensembl lookup REST endpoint
  * 
  * @author gmcarstairs
- *
  */
 public class EnsemblLookup extends EnsemblRestClient
 {
+  private static final String SPECIES = "species";
 
-  private static final String OBJECT_TYPE_TRANSLATION = "Translation";
   private static final String PARENT = "Parent";
+
+  private static final String OBJECT_TYPE_TRANSLATION = "Translation";
   private static final String OBJECT_TYPE_TRANSCRIPT = "Transcript";
   private static final String ID = "id";
   private static final String OBJECT_TYPE_GENE = "Gene";
@@ -131,6 +132,29 @@ public class EnsemblLookup extends EnsemblRestClient
    */
   public String getGeneId(String identifier)
   {
+    return getResult(identifier, br -> parseGeneId(br));
+  }
+
+  /**
+   * Calls the Ensembl lookup REST endpoint and retrieves the 'species' for the
+   * given identifier, or null if not found
+   * 
+   * @param identifier
+   * @return
+   */
+  public String getSpecies(String identifier)
+  {
+    return getResult(identifier, br -> getAttribute(br, SPECIES));
+  }
+
+  /**
+   * @param identifier
+   * @param attribute
+   * @return
+   */
+  protected String getResult(String identifier,
+          Function<BufferedReader, String> parser)
+  {
     List<String> ids = Arrays.asList(new String[] { identifier });
 
     BufferedReader br = null;
@@ -141,7 +165,7 @@ public class EnsemblLookup extends EnsemblRestClient
       {
         br = getHttpResponse(url, ids);
       }
-      return br == null ? null : parseResponse(br);
+      return br == null ? null : parser.apply(br);
     } catch (IOException e)
     {
       // ignore
@@ -162,6 +186,29 @@ public class EnsemblLookup extends EnsemblRestClient
   }
 
   /**
+   * Answers the value of 'attribute' from the JSON response, or null if not
+   * found
+   * 
+   * @param br
+   * @param attribute
+   * @return
+   */
+  protected String getAttribute(BufferedReader br, String attribute)
+  {
+    String value = null;
+    JSONParser jp = new JSONParser();
+    try
+    {
+      JSONObject val = (JSONObject) jp.parse(br);
+      value = val.get(attribute).toString();
+    } catch (ParseException | NullPointerException | IOException e)
+    {
+      // ignore
+    }
+    return value;
+  }
+
+  /**
    * Parses the JSON response and returns the gene identifier, or null if not
    * found. If the returned object_type is Gene, returns the id, if Transcript
    * returns the Parent. If it is Translation (peptide identifier), then the
@@ -169,9 +216,8 @@ public class EnsemblLookup extends EnsemblRestClient
    * 
    * @param br
    * @return
-   * @throws IOException
    */
-  protected String parseResponse(BufferedReader br) throws IOException
+  protected String parseGeneId(BufferedReader br)
   {
     String geneId = null;
     JSONParser jp = new JSONParser();
@@ -204,7 +250,7 @@ public class EnsemblLookup extends EnsemblRestClient
                           + " looping on Parent!");
         }
       }
-    } catch (ParseException e)
+    } catch (ParseException | IOException e)
     {
       // ignore
     }