1 package jalview.ext.ensembl;
3 import jalview.io.FileParse;
5 import java.io.BufferedReader;
6 import java.io.DataOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.net.HttpURLConnection;
12 import java.net.URLConnection;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.apache.http.NameValuePair;
17 import org.apache.http.message.BasicNameValuePair;
19 public class SeqFetcher
21 private final static String ENSEMBL_REST = "rest.ensembl.org";
23 private static final String SEQUENCE_ID_URL = "http://" + ENSEMBL_REST + "/sequence/id";
25 private static final String PING_URL = "http://" + ENSEMBL_REST + "/info/ping";
27 private final static long RETEST_INTERVAL = 10000L; // 10 seconds
29 private static boolean ensemblRestAvailable = false;
31 private static long lastCheck = -1;
34 * Rechecks if Ensembl is responding, unless the last check was successful and
35 * the retest interval has not yet elapsed. Returns true if Ensembl is up,
40 public boolean isEnsemblAvailable()
42 long now = System.currentTimeMillis();
43 boolean retest = now - lastCheck > RETEST_INTERVAL;
44 if (ensemblRestAvailable && !retest)
48 ensemblRestAvailable = checkEnsembl();
50 return ensemblRestAvailable;
54 * Tries to connect to Ensembl's REST 'ping' endpoint, and returns true if
55 * successful, else false
59 private boolean checkEnsembl()
63 URL ping = new URL(PING_URL);
64 HttpURLConnection conn = (HttpURLConnection) ping.openConnection();
65 int rc = conn.getResponseCode();
67 if (rc >= 200 && rc < 300)
73 System.err.println("Error connecting to " + PING_URL + ": "
83 public enum EnsemblSeqType
85 GENOMIC("genomic"), CDS("cds"), TRANSCRIPT("cds"), PROTEIN("protein"), CDNA(
90 EnsemblSeqType(String t)
95 public String getType()
102 * Returns a list of additional URL query parameters to specify the desired
103 * sequence type (genomic/cds/protein etc), and data format Fasta
107 public List<NameValuePair> getAdditionalParameters(EnsemblSeqType type)
109 List<NameValuePair> params = new ArrayList<NameValuePair>();
110 params.add(new BasicNameValuePair("type", type.getType()));
111 params.add(new BasicNameValuePair("content-type", "text/x-fasta"));
116 * return a reader to a Fasta response from the Ensembl sequence endpoint
121 * @throws IOException
123 public FileParse getSequenceReader(EnsemblSeqType returnType,
124 List<String> ids) throws IOException
126 // see http://rest.ensembl.org/documentation/info/sequence_id
128 String urlstring = SEQUENCE_ID_URL;
129 List<NameValuePair> vals = getAdditionalParameters(returnType);
130 boolean first = true;
131 for (NameValuePair nvp : vals)
133 urlstring += first ? "?" : "&";
135 urlstring += nvp.getName() + "=" + nvp.getValue();
138 URL url = new URL(urlstring);
140 URLConnection connection = url.openConnection();
141 HttpURLConnection httpConnection = (HttpURLConnection) connection;
143 httpConnection.setRequestMethod("POST");
144 httpConnection.setRequestProperty("Content-Type", "application/json");
145 httpConnection.setRequestProperty("Accept", "text/x-fasta");
148 StringBuilder postBody = new StringBuilder();
149 postBody.append("{\"ids\":[");
151 for (String id : ids)
155 postBody.append(",");
158 postBody.append("\"");
159 postBody.append(id.trim());
160 postBody.append("\"");
162 postBody.append("]}");
163 thepostbody = postBody.toString().getBytes();
165 httpConnection.setRequestProperty("Content-Length",
166 Integer.toString(thepostbody.length));
167 httpConnection.setUseCaches(false);
168 httpConnection.setDoInput(true);
169 httpConnection.setDoOutput(true);
171 DataOutputStream wr = new DataOutputStream(
172 httpConnection.getOutputStream());
173 wr.write(thepostbody);
177 InputStream response = connection.getInputStream();
178 int responseCode = httpConnection.getResponseCode();
180 if (responseCode != 200)
182 throw new RuntimeException(
183 "Response code was not 200. Detected response was "
187 BufferedReader reader = null;
188 reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
189 FileParse fp = new FileParse(reader, url.toString(), "HTTP_POST");