3253-omnibus save
[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.JSONUtils;
40 import jalview.util.MessageManager;
41 import jalview.ws.HttpClientUtils;
42
43 /**
44  * simple methods for calling the various paradise RNA tools
45  * 
46  * @author jimp
47  * 
48  * @version v1.0 revised from original due to refactoring of
49  *          paradise-ubmc.u-strasbg.fr/webservices/annotate3d to
50  *          http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview <br/>
51  *          See also testing URL from fjossinet:<br/>
52  *          http://charn2-ibmc.u-strasbg.fr:8080/api/compute/2d <br/>
53  *          If in doubt, check against the REST client at:
54  *          https://github.com/fjossinet/RNA-Science
55  *          -Toolbox/blob/master/pyrna/restclient.py
56  */
57 public class Annotate3D
58 {
59   // also test with
60   // "http://charn2-ibmc.u-strasbg.fr:8080/api/compute/2d";
61   private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview";
62
63   private static ContentHandler createContentHandler()
64   {
65     ContentHandler ch = new ContentHandler()
66     {
67
68       @Override
69       public void startJSON() throws ParseException, IOException
70       {
71         // TODO Auto-generated method stub
72
73       }
74
75       @Override
76       public void endJSON() throws ParseException, IOException
77       {
78         // TODO Auto-generated method stub
79
80       }
81
82       @Override
83       public boolean startObject() throws ParseException, IOException
84       {
85         // TODO Auto-generated method stub
86         return false;
87       }
88
89       @Override
90       public boolean endObject() throws ParseException, IOException
91       {
92         // TODO Auto-generated method stub
93         return false;
94       }
95
96       @Override
97       public boolean startObjectEntry(String key)
98               throws ParseException, IOException
99       {
100         // TODO Auto-generated method stub
101         return false;
102       }
103
104       @Override
105       public boolean endObjectEntry() throws ParseException, IOException
106       {
107         // TODO Auto-generated method stub
108         return false;
109       }
110
111       @Override
112       public boolean startArray() throws ParseException, IOException
113       {
114         // TODO Auto-generated method stub
115         return false;
116       }
117
118       @Override
119       public boolean endArray() throws ParseException, IOException
120       {
121         // TODO Auto-generated method stub
122         return false;
123       }
124
125       @Override
126       public boolean primitive(Object value)
127               throws ParseException, IOException
128       {
129         // TODO Auto-generated method stub
130         return false;
131       }
132
133     };
134     return ch;
135   }
136
137   public static Iterator<Reader> getRNAMLForPDBFileAsString(String pdbfile)
138           throws Exception
139   {
140     List<NameValuePair> vals = new ArrayList<>();
141     vals.add(new BasicNameValuePair("tool", "rnaview"));
142     vals.add(new BasicNameValuePair("data", pdbfile));
143     vals.add(new BasicNameValuePair("output", "rnaml"));
144     // return processJsonResponseFor(HttpClientUtils.doHttpUrlPost(twoDtoolsURL,
145     // vals));
146     ArrayList<Reader> readers = new ArrayList<>();
147     final BufferedReader postResponse = HttpClientUtils
148             .doHttpUrlPost(twoDtoolsURL, vals, 0, 0);
149     readers.add(postResponse);
150     return readers.iterator();
151
152   }
153
154   public static Iterator<Reader> processJsonResponseFor(Reader respons)
155           throws Exception
156   {
157     org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
158     try
159     {
160       final RvalsIterator rvals = new RvalsIterator(respons);
161       return rvals;
162     } catch (Exception foo)
163     {
164       throw new Exception(MessageManager.getString(
165               "exception.couldnt_parse_responde_from_annotated3d_server"),
166               foo);
167     }
168
169   }
170
171   public static Iterator<Reader> getRNAMLForPDBId(String pdbid)
172           throws Exception
173   {
174     List<NameValuePair> vals = new ArrayList<>();
175     vals.add(new BasicNameValuePair("tool", "rnaview"));
176     vals.add(new BasicNameValuePair("pdbid", pdbid));
177     vals.add(new BasicNameValuePair("output", "rnaml"));
178     java.net.URL geturl = new URL(twoDtoolsURL + "?tool=rnaview&pdbid="
179             + pdbid + "&output=rnaml");
180     // return processJsonResponseFor(new
181     // InputStreamReader(geturl.openStream()));
182     ArrayList<Reader> readers = new ArrayList<>();
183     readers.add(new InputStreamReader(geturl.openStream()));
184     return readers.iterator();
185   }
186
187 }
188
189 class RvalsIterator implements Iterator, AutoCloseable
190 {
191   private Iterator<Object> rvals;
192
193   @SuppressWarnings("unchecked")
194   protected RvalsIterator(Reader respons) throws IOException, ParseException
195   {
196     /*
197      * as in 2.11.1 (pre-JalviewJS)
198      */
199     // final JSONArray responses = (JSONArray) jp.parse(respons);
200     // this.rvals = responses.iterator();
201
202     /*
203      * as in JalviewJS (with comment that this code is never called)
204      */
205     this.rvals = ((List<Object>) JSONUtils.parse(respons)).iterator();
206   }
207
208   @Override
209   public boolean hasNext()
210   {
211     return rvals.hasNext();
212   }
213
214   @Override
215   public Reader next()
216   {
217     // JSONObject val = (JSONObject) rvals.next(); // 2.11.1
218     @SuppressWarnings("unchecked")
219     Map<String, Object> val = (Map<String, Object>) rvals.next();
220
221     Object sval = null;
222     try
223     {
224       sval = val.get("2D");
225     } catch (Exception x)
226     {
227       x.printStackTrace();
228     }
229
230     if (sval == null)
231     {
232       System.err.println(
233               "DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"
234                       + val.toString());
235
236       sval = "";
237     }
238     // 2.11.1:
239     // return new StringReader(
240     // (sval instanceof JSONObject) ? ((JSONObject) sval).toString()
241     // : sval.toString());
242     return new StringReader(sval.toString());
243
244   }
245
246   @Override
247   public void remove()
248   {
249     throw new Error(
250             MessageManager.getString("error.not_implemented_remove"));
251
252   }
253
254   @Override
255   protected Object clone() throws CloneNotSupportedException
256   {
257     throw new CloneNotSupportedException(
258             MessageManager.getString("error.not_implemented_clone"));
259   }
260
261   @Override
262   public boolean equals(Object obj)
263   {
264     return super.equals(obj);
265   }
266
267   @Override
268   public void close()
269   {
270     while (rvals.hasNext())
271     {
272       rvals.next();
273     }
274   }
275 }