2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ext.paradise;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import java.io.StringReader;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
34 import org.apache.http.NameValuePair;
35 import org.apache.http.message.BasicNameValuePair;
36 import org.json.simple.parser.ContentHandler;
37 import org.json.simple.parser.ParseException;
39 import jalview.util.HttpUtils;
40 import jalview.util.JSONUtils;
41 import jalview.util.MessageManager;
42 import jalview.ws.HttpClientUtils;
45 * simple methods for calling the various paradise RNA tools
49 * @version v1.0 revised from original due to refactoring of
50 * paradise-ubmc.u-strasbg.fr/webservices/annotate3d to
51 * http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview <br/>
52 * See also testing URL from fjossinet:<br/>
53 * http://charn2-ibmc.u-strasbg.fr:8080/api/compute/2d <br/>
54 * If in doubt, check against the REST client at:
55 * https://github.com/fjossinet/RNA-Science
56 * -Toolbox/blob/master/pyrna/restclient.py
58 public class Annotate3D
61 // "http://charn2-ibmc.u-strasbg.fr:8080/api/compute/2d";
62 private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview";
64 private static ContentHandler createContentHandler()
66 ContentHandler ch = new ContentHandler()
70 public void startJSON() throws ParseException, IOException
72 // TODO Auto-generated method stub
77 public void endJSON() throws ParseException, IOException
79 // TODO Auto-generated method stub
84 public boolean startObject() throws ParseException, IOException
86 // TODO Auto-generated method stub
91 public boolean endObject() throws ParseException, IOException
93 // TODO Auto-generated method stub
98 public boolean startObjectEntry(String key)
99 throws ParseException, IOException
101 // TODO Auto-generated method stub
106 public boolean endObjectEntry() throws ParseException, IOException
108 // TODO Auto-generated method stub
113 public boolean startArray() throws ParseException, IOException
115 // TODO Auto-generated method stub
120 public boolean endArray() throws ParseException, IOException
122 // TODO Auto-generated method stub
127 public boolean primitive(Object value)
128 throws ParseException, IOException
130 // TODO Auto-generated method stub
138 public static Iterator<Reader> getRNAMLForPDBFileAsString(String pdbfile)
141 List<NameValuePair> vals = new ArrayList<>();
142 vals.add(new BasicNameValuePair("tool", "rnaview"));
143 vals.add(new BasicNameValuePair("data", pdbfile));
144 vals.add(new BasicNameValuePair("output", "rnaml"));
145 // return processJsonResponseFor(HttpClientUtils.doHttpUrlPost(twoDtoolsURL,
147 ArrayList<Reader> readers = new ArrayList<>();
148 final BufferedReader postResponse = HttpClientUtils
149 .doHttpUrlPost(twoDtoolsURL, vals, 0, 0);
150 readers.add(postResponse);
151 return readers.iterator();
155 public static Iterator<Reader> processJsonResponseFor(Reader respons)
158 org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
161 final RvalsIterator rvals = new RvalsIterator(respons);
163 } catch (Exception foo)
165 throw new Exception(MessageManager.getString(
166 "exception.couldnt_parse_responde_from_annotated3d_server"),
172 public static Iterator<Reader> getRNAMLForPDBId(String pdbid)
175 List<NameValuePair> vals = new ArrayList<>();
176 vals.add(new BasicNameValuePair("tool", "rnaview"));
177 vals.add(new BasicNameValuePair("pdbid", pdbid));
178 vals.add(new BasicNameValuePair("output", "rnaml"));
179 java.net.URL geturl = new URL(twoDtoolsURL + "?tool=rnaview&pdbid="
180 + pdbid + "&output=rnaml");
181 // return processJsonResponseFor(new
182 // InputStreamReader(geturl.openStream()));
183 ArrayList<Reader> readers = new ArrayList<>();
184 readers.add(new InputStreamReader(HttpUtils.openStream(geturl)));
185 return readers.iterator();
190 class RvalsIterator implements Iterator, AutoCloseable
192 private Iterator<Object> rvals;
194 @SuppressWarnings("unchecked")
195 protected RvalsIterator(Reader respons) throws IOException, ParseException
198 * as in 2.11.1 (pre-JalviewJS)
200 // final JSONArray responses = (JSONArray) jp.parse(respons);
201 // this.rvals = responses.iterator();
204 * as in JalviewJS (with comment that this code is never called)
206 this.rvals = ((List<Object>) JSONUtils.parse(respons)).iterator();
210 public boolean hasNext()
212 return rvals.hasNext();
218 // JSONObject val = (JSONObject) rvals.next(); // 2.11.1
219 @SuppressWarnings("unchecked")
220 Map<String, Object> val = (Map<String, Object>) rvals.next();
225 sval = val.get("2D");
226 } catch (Exception x)
233 jalview.bin.Console.errPrintln(
234 "DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"
240 // return new StringReader(
241 // (sval instanceof JSONObject) ? ((JSONObject) sval).toString()
242 // : sval.toString());
243 return new StringReader(sval.toString());
251 MessageManager.getString("error.not_implemented_remove"));
256 protected Object clone() throws CloneNotSupportedException
258 throw new CloneNotSupportedException(
259 MessageManager.getString("error.not_implemented_clone"));
263 public boolean equals(Object obj)
265 return super.equals(obj);
271 while (rvals.hasNext())