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.Collections;
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 boolean ensemblRestavailable = false;
25 private static long lastCheck = -1;
27 public boolean isEnsemblAvailable()
29 if (isTesting || !ensemblRestavailable
30 || System.currentTimeMillis() - lastCheck > 10000)
33 lastCheck = System.currentTimeMillis();
35 return ensemblRestavailable;
38 private boolean isTesting, testEnsemblStatus;
41 * @return the isTesting
43 public boolean isTesting()
50 * the isTesting to set
52 public void setTesting(boolean isTesting)
54 this.isTesting = isTesting;
58 * @return the testEnsemblStatus
60 public boolean isTestEnsemblStatus()
62 return testEnsemblStatus;
66 * @param testEnsemblStatus
67 * the testEnsemblStatus to set
69 public void setTestEnsemblStatus(boolean testEnsemblStatus)
71 this.testEnsemblStatus = testEnsemblStatus;
74 private void checkEnsembl()
78 ensemblRestavailable = testEnsemblStatus;
83 URL ping = new URL("http://" + ENSEMBL_REST + "/info/ping");
84 HttpURLConnection conn = (HttpURLConnection) (ping.openConnection());
85 if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300)
87 ensemblRestavailable = true;
92 err.printStackTrace();
93 } catch (Exception exx)
95 exx.printStackTrace();
97 ensemblRestavailable = false;
104 public enum EnsemblSeqType
106 GENOMIC("genomic"), CDS("cds"), TRANSCRIPT("cds"), PROTEIN("protein"), CDNA(
111 EnsemblSeqType(String t)
116 public String getType()
123 * reolve request type as an argument for sequence and features queries
127 public List<NameValuePair> getObjectTypeArg(EnsemblSeqType type)
129 NameValuePair nameValue = new BasicNameValuePair("type", type.getType());
130 return Collections.singletonList(nameValue);
134 * return a reader to a Fasta response from the Ensembl sequence endpoint
139 * @throws IOException
141 public FileParse getSequenceReader(EnsemblSeqType returnType,
142 List<String> ids) throws IOException
145 // adapted From the rest.ensembl.org documentation for sequence_id
147 String urls = "http://" + ENSEMBL_REST + "/sequence/id";
148 List<NameValuePair> vals = getObjectTypeArg(returnType);
150 for (NameValuePair nvp : vals)
161 urls += nvp.getName() + "=" + nvp.getValue();
164 URL url = new URL(urls);
166 URLConnection connection = url.openConnection();
167 HttpURLConnection httpConnection = (HttpURLConnection) connection;
169 httpConnection.setRequestMethod("POST");
170 httpConnection.setRequestProperty("Content-Type", "application/json");
171 httpConnection.setRequestProperty("Accept", "text/x-fasta");
174 StringBuilder postBody = new StringBuilder();
175 postBody.append("{\"ids\":[");
176 boolean first = true;
177 for (String id : ids)
185 postBody.append(",");
187 postBody.append("\"");
188 postBody.append(id.trim());
189 postBody.append("\"");
191 postBody.append("]}");
192 thepostbody = postBody.toString().getBytes();
194 httpConnection.setRequestProperty("Content-Length",
195 Integer.toString(thepostbody.length));
196 httpConnection.setUseCaches(false);
197 httpConnection.setDoInput(true);
198 httpConnection.setDoOutput(true);
200 DataOutputStream wr = new DataOutputStream(
201 httpConnection.getOutputStream());
202 wr.write(thepostbody);
206 InputStream response = connection.getInputStream();
207 int responseCode = httpConnection.getResponseCode();
209 if (responseCode != 200)
211 throw new RuntimeException(
212 "Response code was not 200. Detected response was "
216 BufferedReader reader = null;
217 reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
218 FileParse fp = new FileParse(reader, url.toString(), "HTTP_POST");