X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=inline;f=src%2Fjalview%2Fext%2Fensembl%2FEnsemblLookup.java;h=0968663535632d16938a7a6834a682ddc34b888e;hb=7339a917af1f439b734947e2b14165887d188aa3;hp=eb8f90ee738dcb1391137e6d0e755284df00bdf4;hpb=9587a72768299145f9137c5842d4287307dc5d38;p=jalview.git diff --git a/src/jalview/ext/ensembl/EnsemblLookup.java b/src/jalview/ext/ensembl/EnsemblLookup.java index eb8f90e..0968663 100644 --- a/src/jalview/ext/ensembl/EnsemblLookup.java +++ b/src/jalview/ext/ensembl/EnsemblLookup.java @@ -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 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 }