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";
*/
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<BufferedReader, String> parser)
+ {
List<String> ids = Arrays.asList(new String[] { identifier });
BufferedReader br = null;
{
br = getHttpResponse(url, ids);
}
- return br == null ? null : parseResponse(br);
+ return br == null ? null : parser.apply(br);
} catch (IOException e)
{
// ignore
}
/**
+ * 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
*
* @param br
* @return
- * @throws IOException
*/
- protected String parseResponse(BufferedReader br) throws IOException
+ protected String parseGeneId(BufferedReader br)
{
String geneId = null;
JSONParser jp = new JSONParser();
+ " looping on Parent!");
}
}
- } catch (ParseException e)
+ } catch (ParseException | IOException e)
{
// ignore
}