JAL-4409 Refactoring to allow easier reuse of classes with getdown
[jalview.git] / src / jalview / ext / paradise / Annotate3D.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ext.paradise;
22
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;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33
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;
38
39 import jalview.util.HttpUtils;
40 import jalview.util.JSONUtils;
41 import jalview.util.MessageManager;
42 import jalview.ws.HttpClientUtils;
43
44 /**
45  * simple methods for calling the various paradise RNA tools
46  * 
47  * @author jimp
48  * 
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
57  */
58 public class Annotate3D
59 {
60   // also test with
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";
63
64   private static ContentHandler createContentHandler()
65   {
66     ContentHandler ch = new ContentHandler()
67     {
68
69       @Override
70       public void startJSON() throws ParseException, IOException
71       {
72         // TODO Auto-generated method stub
73
74       }
75
76       @Override
77       public void endJSON() throws ParseException, IOException
78       {
79         // TODO Auto-generated method stub
80
81       }
82
83       @Override
84       public boolean startObject() throws ParseException, IOException
85       {
86         // TODO Auto-generated method stub
87         return false;
88       }
89
90       @Override
91       public boolean endObject() throws ParseException, IOException
92       {
93         // TODO Auto-generated method stub
94         return false;
95       }
96
97       @Override
98       public boolean startObjectEntry(String key)
99               throws ParseException, IOException
100       {
101         // TODO Auto-generated method stub
102         return false;
103       }
104
105       @Override
106       public boolean endObjectEntry() throws ParseException, IOException
107       {
108         // TODO Auto-generated method stub
109         return false;
110       }
111
112       @Override
113       public boolean startArray() throws ParseException, IOException
114       {
115         // TODO Auto-generated method stub
116         return false;
117       }
118
119       @Override
120       public boolean endArray() throws ParseException, IOException
121       {
122         // TODO Auto-generated method stub
123         return false;
124       }
125
126       @Override
127       public boolean primitive(Object value)
128               throws ParseException, IOException
129       {
130         // TODO Auto-generated method stub
131         return false;
132       }
133
134     };
135     return ch;
136   }
137
138   public static Iterator<Reader> getRNAMLForPDBFileAsString(String pdbfile)
139           throws Exception
140   {
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,
146     // vals));
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();
152
153   }
154
155   public static Iterator<Reader> processJsonResponseFor(Reader respons)
156           throws Exception
157   {
158     org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
159     try
160     {
161       final RvalsIterator rvals = new RvalsIterator(respons);
162       return rvals;
163     } catch (Exception foo)
164     {
165       throw new Exception(MessageManager.getString(
166               "exception.couldnt_parse_responde_from_annotated3d_server"),
167               foo);
168     }
169
170   }
171
172   public static Iterator<Reader> getRNAMLForPDBId(String pdbid)
173           throws Exception
174   {
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();
186   }
187
188 }
189
190 class RvalsIterator implements Iterator, AutoCloseable
191 {
192   private Iterator<Object> rvals;
193
194   @SuppressWarnings("unchecked")
195   protected RvalsIterator(Reader respons) throws IOException, ParseException
196   {
197     /*
198      * as in 2.11.1 (pre-JalviewJS)
199      */
200     // final JSONArray responses = (JSONArray) jp.parse(respons);
201     // this.rvals = responses.iterator();
202
203     /*
204      * as in JalviewJS (with comment that this code is never called)
205      */
206     this.rvals = ((List<Object>) JSONUtils.parse(respons)).iterator();
207   }
208
209   @Override
210   public boolean hasNext()
211   {
212     return rvals.hasNext();
213   }
214
215   @Override
216   public Reader next()
217   {
218     // JSONObject val = (JSONObject) rvals.next(); // 2.11.1
219     @SuppressWarnings("unchecked")
220     Map<String, Object> val = (Map<String, Object>) rvals.next();
221
222     Object sval = null;
223     try
224     {
225       sval = val.get("2D");
226     } catch (Exception x)
227     {
228       x.printStackTrace();
229     }
230
231     if (sval == null)
232     {
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 :"
235                       + val.toString());
236
237       sval = "";
238     }
239     // 2.11.1:
240     // return new StringReader(
241     // (sval instanceof JSONObject) ? ((JSONObject) sval).toString()
242     // : sval.toString());
243     return new StringReader(sval.toString());
244
245   }
246
247   @Override
248   public void remove()
249   {
250     throw new Error(
251             MessageManager.getString("error.not_implemented_remove"));
252
253   }
254
255   @Override
256   protected Object clone() throws CloneNotSupportedException
257   {
258     throw new CloneNotSupportedException(
259             MessageManager.getString("error.not_implemented_clone"));
260   }
261
262   @Override
263   public boolean equals(Object obj)
264   {
265     return super.equals(obj);
266   }
267
268   @Override
269   public void close()
270   {
271     while (rvals.hasNext())
272     {
273       rvals.next();
274     }
275   }
276 }