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.Arrays;
14 import java.util.List;
16 import org.apache.http.NameValuePair;
17 import org.apache.http.message.BasicNameValuePair;
19 public class SeqFetcher
21 private static String ensemblRest = "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://" + ensemblRest + "/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;
103 // TODO Auto-generated constructor stub
106 public enum EnsemblSeqType
108 GENOMIC, CDS, TRANSCRIPT, PROTEIN, CDNA;
112 * reolve request type as an argument for sequence and features queries
116 public List<NameValuePair> getObjectTypeArg(EnsemblSeqType type)
137 return Arrays.asList(new NameValuePair[]
138 { new BasicNameValuePair("type", arg) });
142 * return a reader to a Fasta response from the Ensembl sequence endpoint
147 * @throws IOException
149 public FileParse getSequenceReader(EnsemblSeqType returnType,
150 List<String> ids) throws IOException
153 // adapted From the rest.ensembl.org documentation for sequence_id
155 String urls = "http://" + ensemblRest + "/sequence/id";
156 List<NameValuePair> vals = getObjectTypeArg(returnType);
158 for (NameValuePair nvp : vals)
169 urls += nvp.getName() + "=" + nvp.getValue();
172 URL url = new URL(urls);
174 URLConnection connection = url.openConnection();
175 HttpURLConnection httpConnection = (HttpURLConnection) connection;
177 httpConnection.setRequestMethod("POST");
178 httpConnection.setRequestProperty("Content-Type", "application/json");
179 httpConnection.setRequestProperty("Accept", "text/x-fasta");
182 StringBuilder postBody = new StringBuilder();
183 postBody.append("{\"ids\":[");
184 boolean first = true;
185 for (String id : ids)
193 postBody.append(",");
195 postBody.append("\"");
196 postBody.append(id.trim());
197 postBody.append("\"");
199 postBody.append("]}");
200 thepostbody = postBody.toString().getBytes();
202 httpConnection.setRequestProperty("Content-Length",
203 Integer.toString(thepostbody.length));
204 httpConnection.setUseCaches(false);
205 httpConnection.setDoInput(true);
206 httpConnection.setDoOutput(true);
208 DataOutputStream wr = new DataOutputStream(
209 httpConnection.getOutputStream());
210 wr.write(thepostbody);
214 InputStream response = connection.getInputStream();
215 int responseCode = httpConnection.getResponseCode();
217 if (responseCode != 200)
219 throw new RuntimeException(
220 "Response code was not 200. Detected response was "
224 BufferedReader reader = null;
225 reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
226 FileParse fp = new FileParse(reader, url.toString(), "HTTP_POST");