1 package jalview.ext.paradise;
3 import jalview.ws.HttpClientUtils;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
8 import java.io.StringReader;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.List;
15 import org.apache.http.NameValuePair;
16 import org.apache.http.message.BasicNameValuePair;
17 import org.json.simple.JSONArray;
18 import org.json.simple.JSONObject;
19 import org.json.simple.JSONStreamAware;
20 import org.json.simple.parser.ContentHandler;
21 import org.json.simple.parser.ParseException;
24 * simple methods for calling the various paradise RNA tools
28 * History: v1.0 revised from original due to refactoring of
29 * paradise-ubmc.u-strasbg.fr/webservices/annotate3d to
30 * http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview
32 public class Annotate3D
34 private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d";
35 private static ContentHandler createContentHandler()
37 ContentHandler ch = new ContentHandler() {
40 public void startJSON() throws ParseException, IOException
42 // TODO Auto-generated method stub
47 public void endJSON() throws ParseException, IOException
49 // TODO Auto-generated method stub
54 public boolean startObject() throws ParseException, IOException
56 // TODO Auto-generated method stub
61 public boolean endObject() throws ParseException, IOException
63 // TODO Auto-generated method stub
68 public boolean startObjectEntry(String key) throws ParseException,
71 // TODO Auto-generated method stub
76 public boolean endObjectEntry() throws ParseException, IOException
78 // TODO Auto-generated method stub
83 public boolean startArray() throws ParseException, IOException
85 // TODO Auto-generated method stub
90 public boolean endArray() throws ParseException, IOException
92 // TODO Auto-generated method stub
97 public boolean primitive(Object value) throws ParseException,
100 // TODO Auto-generated method stub
107 public static Iterator<Reader> getRNAMLForPDBFileAsString(String pdbfile)
110 List<NameValuePair> vals = new ArrayList<NameValuePair>();
111 vals.add(new BasicNameValuePair("tool", "rnaview"));
112 vals.add(new BasicNameValuePair("data", pdbfile));
113 vals.add(new BasicNameValuePair("output", "rnaml"));
114 return processJsonResponseFor(HttpClientUtils.doHttpUrlPost(twoDtoolsURL, vals));
116 public static Iterator<Reader> processJsonResponseFor(Reader respons) throws Exception
118 org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
120 final JSONArray responses = (JSONArray) jp.parse(respons);
121 final Iterator rvals = responses.iterator();
122 return new Iterator<Reader>()
125 public boolean hasNext()
127 return rvals.hasNext();
132 JSONObject val=(JSONObject) rvals.next();
136 sval = val.get("2D");
137 } catch (Exception x) {x.printStackTrace();};
140 System.err.println("DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"+val.toString());
144 return new StringReader((sval instanceof JSONObject) ? ((JSONObject)sval).toString():sval.toString());
149 throw new Error("Remove: Not implemented");
152 protected Object clone() throws CloneNotSupportedException
154 throw new CloneNotSupportedException("Clone: Not implemented");
156 public boolean equals(Object obj)
158 return super.equals(obj);
160 protected void finalize() throws Throwable
162 while (rvals.hasNext())
169 } catch (Exception foo)
171 throw new Exception("Couldn't parse response from Annotate3d server.",foo);
177 public static Iterator<Reader> getRNAMLForPDBId(String pdbid) throws Exception
179 List<NameValuePair> vals = new ArrayList<NameValuePair>();
180 vals.add(new BasicNameValuePair("tool", "rnaview"));
181 vals.add(new BasicNameValuePair("pdbid", pdbid));
182 vals.add(new BasicNameValuePair("format", "rnaml"));
183 java.net.URL geturl = new URL(twoDtoolsURL+"?tool=rnaview&output=rnaml&pdbid="+pdbid);
184 return processJsonResponseFor(new InputStreamReader(geturl.openStream()));