package jalview.ext.ensembl; import jalview.io.FileParse; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.Arrays; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; public class SeqFetcher { private static String ensemblRest = "rest.ensembl.org"; private static boolean ensemblRestavailable = false; private static long lastCheck = -1; public boolean isEnsemblAvailable() { if (isTesting || !ensemblRestavailable || System.currentTimeMillis() - lastCheck > 10000) { checkEnsembl(); lastCheck = System.currentTimeMillis(); } return ensemblRestavailable; } private boolean isTesting, testEnsemblStatus; /** * @return the isTesting */ public boolean isTesting() { return isTesting; } /** * @param isTesting * the isTesting to set */ public void setTesting(boolean isTesting) { this.isTesting = isTesting; } /** * @return the testEnsemblStatus */ public boolean isTestEnsemblStatus() { return testEnsemblStatus; } /** * @param testEnsemblStatus * the testEnsemblStatus to set */ public void setTestEnsemblStatus(boolean testEnsemblStatus) { this.testEnsemblStatus = testEnsemblStatus; } private void checkEnsembl() { if (isTesting) { ensemblRestavailable = testEnsemblStatus; return; } try { URL ping = new URL("http://" + ensemblRest + "/info/ping"); HttpURLConnection conn = (HttpURLConnection) (ping.openConnection()); if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) { ensemblRestavailable = true; return; } } catch (Error err) { err.printStackTrace(); } catch (Exception exx) { exx.printStackTrace(); } ensemblRestavailable = false; } public SeqFetcher() { // TODO Auto-generated constructor stub } public enum EnsemblSeqType { GENOMIC, CDS, TRANSCRIPT, PROTEIN, CDNA; } /** * reolve request type as an argument for sequence and features queries * * @param type */ public List getObjectTypeArg(EnsemblSeqType type) { String arg; switch (type) { case CDS: arg = "cds"; break; case TRANSCRIPT: arg = "cds"; break; case CDNA: arg = "cdna"; break; case PROTEIN: arg = "protein"; break; case GENOMIC: default: arg = "genomic"; } return Arrays.asList(new NameValuePair[] { new BasicNameValuePair("type", arg) }); } /** * return a reader to a Fasta response from the Ensembl sequence endpoint * * @param returnType * @param ids * @return * @throws IOException */ public FileParse getSequenceReader(EnsemblSeqType returnType, List ids) throws IOException { // adapted From the rest.ensembl.org documentation for sequence_id String urls = "http://" + ensemblRest + "/sequence/id"; List vals = getObjectTypeArg(returnType); boolean f = true; for (NameValuePair nvp : vals) { if (f) { f = false; urls += "?"; } else { urls += "&"; } urls += nvp.getName() + "=" + nvp.getValue(); } URL url = new URL(urls); URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setRequestMethod("POST"); httpConnection.setRequestProperty("Content-Type", "application/json"); httpConnection.setRequestProperty("Accept", "text/x-fasta"); byte[] thepostbody; { StringBuilder postBody = new StringBuilder(); postBody.append("{\"ids\":["); boolean first = true; for (String id : ids) { if (first) { first = false; } else { postBody.append(","); } postBody.append("\""); postBody.append(id.trim()); postBody.append("\""); } postBody.append("]}"); thepostbody = postBody.toString().getBytes(); } httpConnection.setRequestProperty("Content-Length", Integer.toString(thepostbody.length)); httpConnection.setUseCaches(false); httpConnection.setDoInput(true); httpConnection.setDoOutput(true); DataOutputStream wr = new DataOutputStream( httpConnection.getOutputStream()); wr.write(thepostbody); wr.flush(); wr.close(); InputStream response = connection.getInputStream(); int responseCode = httpConnection.getResponseCode(); if (responseCode != 200) { throw new RuntimeException( "Response code was not 200. Detected response was " + responseCode); } BufferedReader reader = null; reader = new BufferedReader(new InputStreamReader(response, "UTF-8")); FileParse fp = new FileParse(reader, url.toString(), "HTTP_POST"); return fp; } }