Push 1793 latest to spike branch
[jalview.git] / src / jalview / ext / ensembl / EnsemblLookup.java
index eb8f90e..0968663 100644 (file)
@@ -42,6 +42,9 @@ import org.json.simple.parser.ParseException;
  */
 public class EnsemblLookup extends EnsemblRestClient
 {
+  private static final String SPECIES = "species";
+
+  private static final String PARENT = "Parent";
 
   /**
    * Default constructor (to use rest.ensembl.org)
@@ -124,6 +127,28 @@ public class EnsemblLookup extends EnsemblRestClient
    */
   public String getParent(String identifier)
   {
+    return getAttribute(identifier, PARENT);
+  }
+
+  /**
+   * 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 getAttribute(identifier, SPECIES);
+  }
+
+  /**
+   * @param identifier
+   * @param attribute
+   * @return
+   */
+  protected String getAttribute(String identifier, String attribute)
+  {
     List<String> ids = Arrays.asList(new String[] { identifier });
 
     BufferedReader br = null;
@@ -134,7 +159,7 @@ public class EnsemblLookup extends EnsemblRestClient
       {
         br = getHttpResponse(url, ids);
       }
-      return (parseResponse(br));
+      return (parseResponse(br, attribute));
     } catch (IOException e)
     {
       // ignore
@@ -155,22 +180,23 @@ public class EnsemblLookup extends EnsemblRestClient
   }
 
   /**
-   * Parses "Parent" from the JSON response and returns the value, or null if
-   * not found
+   * Parses the value of 'attribute' from the JSON response and returns the
+   * value, or null if not found
    * 
    * @param br
+   * @param attribute
    * @return
    * @throws IOException
    */
-  protected String parseResponse(BufferedReader br) throws IOException
+  protected String parseResponse(BufferedReader br, String attribute) throws IOException
   {
     String parent = null;
     JSONParser jp = new JSONParser();
     try
     {
       JSONObject val = (JSONObject) jp.parse(br);
-      parent = val.get("Parent").toString();
-    } catch (ParseException e)
+      parent = val.get(attribute).toString();
+    } catch (ParseException | NullPointerException e)
     {
       // ignore
     }