From 7339a917af1f439b734947e2b14165887d188aa3 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 19 Oct 2017 15:00:34 +0100 Subject: [PATCH] JAL-2755 add species lookup --- src/jalview/ext/ensembl/EnsemblLookup.java | 38 +++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 6 deletions(-) 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 } -- 1.7.10.2