JAL-1269 RNAML returned directly from Annotate3d rather than embedded in a JSON array
[jalview.git] / src / jalview / ext / paradise / Annotate3D.java
1 package jalview.ext.paradise;
2
3 import jalview.ws.HttpClientUtils;
4
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.Reader;
8 import java.io.StringReader;
9 import java.net.URL;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.List;
14
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;
22
23 /**
24  * simple methods for calling the various paradise RNA tools
25  * 
26  * @author jimp
27  * 
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
31  */
32 public class Annotate3D
33 {
34   private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d";
35   private static ContentHandler createContentHandler()
36   {
37     ContentHandler ch = new ContentHandler() {
38
39       @Override
40       public void startJSON() throws ParseException, IOException
41       {
42         // TODO Auto-generated method stub
43         
44       }
45
46       @Override
47       public void endJSON() throws ParseException, IOException
48       {
49         // TODO Auto-generated method stub
50         
51       }
52
53       @Override
54       public boolean startObject() throws ParseException, IOException
55       {
56         // TODO Auto-generated method stub
57         return false;
58       }
59
60       @Override
61       public boolean endObject() throws ParseException, IOException
62       {
63         // TODO Auto-generated method stub
64         return false;
65       }
66
67       @Override
68       public boolean startObjectEntry(String key) throws ParseException,
69               IOException
70       {
71         // TODO Auto-generated method stub
72         return false;
73       }
74
75       @Override
76       public boolean endObjectEntry() throws ParseException, IOException
77       {
78         // TODO Auto-generated method stub
79         return false;
80       }
81
82       @Override
83       public boolean startArray() throws ParseException, IOException
84       {
85         // TODO Auto-generated method stub
86         return false;
87       }
88
89       @Override
90       public boolean endArray() throws ParseException, IOException
91       {
92         // TODO Auto-generated method stub
93         return false;
94       }
95
96       @Override
97       public boolean primitive(Object value) throws ParseException,
98               IOException
99       {
100         // TODO Auto-generated method stub
101         return false;
102       }
103       
104     };
105     return ch;
106   }
107   public static Iterator<Reader> getRNAMLForPDBFileAsString(String pdbfile)
108           throws Exception
109   {
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));
115     ArrayList<Reader> readers = new ArrayList<Reader>();
116     readers.add(HttpClientUtils.doHttpUrlPost(twoDtoolsURL, vals));
117     return readers.iterator();
118
119   }
120   public static Iterator<Reader> processJsonResponseFor(Reader respons) throws Exception
121   {
122     org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
123     try {
124       final JSONArray responses = (JSONArray) jp.parse(respons);
125       final Iterator rvals = responses.iterator();
126       return new Iterator<Reader>() 
127         {
128           @Override
129           public boolean hasNext()
130           {
131             return rvals.hasNext();
132           }
133           @Override
134           public Reader next()
135           {
136             JSONObject val=(JSONObject) rvals.next();
137             
138             Object sval = null;
139             try {
140               sval = val.get("2D");
141             } catch (Exception x) {x.printStackTrace();};
142             if (sval==null)
143             {
144               System.err.println("DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"+val.toString());
145               
146               sval = "";
147             }
148             return new StringReader((sval instanceof JSONObject) ? ((JSONObject)sval).toString():sval.toString());
149             
150           }@Override
151           public void remove()
152           {
153             throw new Error("Remove: Not implemented");
154             
155           }@Override
156           protected Object clone() throws CloneNotSupportedException
157           {
158             throw new CloneNotSupportedException("Clone: Not implemented");
159           }@Override
160           public boolean equals(Object obj)
161           {
162             return super.equals(obj);
163           }@Override
164           protected void finalize() throws Throwable
165           {
166             while (rvals.hasNext())
167             {
168               rvals.next();
169             }
170             super.finalize();
171           }
172         };
173     } catch (Exception foo)
174     {
175       throw new Exception("Couldn't parse response from Annotate3d server.",foo);
176     }
177     
178     
179   }
180
181   public static Iterator<Reader> getRNAMLForPDBId(String pdbid) throws Exception
182   {
183     List<NameValuePair> vals = new ArrayList<NameValuePair>();
184     vals.add(new BasicNameValuePair("tool", "rnaview"));
185     vals.add(new BasicNameValuePair("pdbid", pdbid));
186     vals.add(new BasicNameValuePair("output", "rnaml"));
187     java.net.URL geturl = new URL(twoDtoolsURL+"?tool=rnaview&pdbid="+pdbid+"&output=rnaml");
188     //return processJsonResponseFor(new InputStreamReader(geturl.openStream()));
189     ArrayList<Reader> readers = new ArrayList<Reader>();
190     readers.add(new InputStreamReader(geturl.openStream()));
191     return readers.iterator();
192   }
193
194 }