X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fext%2Fensembl%2FEnsemblLookup.java;h=f314b0a8ffdcb8e8528e2223368782ee39b28b92;hb=8d3aefef72e993e55c0f6b5c26e3979ae7269e0f;hp=31da9c005bbcc0b5cf151ba30b39d142098a49b5;hpb=3eb959d7f16b64260b8bec08d49b28ffc9670517;p=jalview.git diff --git a/src/jalview/ext/ensembl/EnsemblLookup.java b/src/jalview/ext/ensembl/EnsemblLookup.java index 31da9c0..f314b0a 100644 --- a/src/jalview/ext/ensembl/EnsemblLookup.java +++ b/src/jalview/ext/ensembl/EnsemblLookup.java @@ -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 parser) + { List 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 }