*/
public class EnsemblLookup extends EnsemblRestClient
{
+ private static final String SPECIES = "species";
+
+ private static final String PARENT = "Parent";
/**
* Default constructor (to use rest.ensembl.org)
*/
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;
{
br = getHttpResponse(url, ids);
}
- return (parseResponse(br));
+ return (parseResponse(br, attribute));
} catch (IOException e)
{
// ignore
}
/**
- * 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
}