JAL-2089 patch broken merge to master for Release 2.10.0b1
[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 jalview.util.MessageManager;
24 import jalview.ws.HttpClientUtils;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.io.Reader;
30 import java.io.StringReader;
31 import java.net.URL;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35
36 import org.apache.http.NameValuePair;
37 import org.apache.http.message.BasicNameValuePair;
38 import org.json.simple.JSONArray;
39 import org.json.simple.JSONObject;
40 import org.json.simple.parser.ContentHandler;
41 import org.json.simple.parser.ParseException;
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) throws ParseException,
98               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) throws ParseException,
127               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<NameValuePair>();
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<Reader>();
147     final BufferedReader postResponse = HttpClientUtils.doHttpUrlPost(
148             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 JSONArray responses = (JSONArray) jp.parse(respons);
161       final Iterator rvals = responses.iterator();
162       return new Iterator<Reader>()
163       {
164         @Override
165         public boolean hasNext()
166         {
167           return rvals.hasNext();
168         }
169
170         @Override
171         public Reader next()
172         {
173           JSONObject val = (JSONObject) rvals.next();
174
175           Object sval = null;
176           try
177           {
178             sval = val.get("2D");
179           } catch (Exception x)
180           {
181             x.printStackTrace();
182           }
183           ;
184           if (sval == null)
185           {
186             System.err
187                     .println("DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"
188                             + val.toString());
189
190             sval = "";
191           }
192           return new StringReader(
193                   (sval instanceof JSONObject) ? ((JSONObject) sval)
194                           .toString() : sval.toString());
195
196         }
197
198         @Override
199         public void remove()
200         {
201           throw new Error(
202                   MessageManager.getString("error.not_implemented_remove"));
203
204         }
205
206         @Override
207         protected Object clone() throws CloneNotSupportedException
208         {
209           throw new CloneNotSupportedException(
210                   MessageManager.getString("error.not_implemented_clone"));
211         }
212
213         @Override
214         public boolean equals(Object obj)
215         {
216           return super.equals(obj);
217         }
218
219         @Override
220         protected void finalize() throws Throwable
221         {
222           while (rvals.hasNext())
223           {
224             rvals.next();
225           }
226           super.finalize();
227         }
228       };
229     } catch (Exception foo)
230     {
231       throw new Exception(
232               MessageManager
233                       .getString("exception.couldnt_parse_responde_from_annotated3d_server"),
234               foo);
235     }
236
237   }
238
239   public static Iterator<Reader> getRNAMLForPDBId(String pdbid)
240           throws Exception
241   {
242     List<NameValuePair> vals = new ArrayList<NameValuePair>();
243     vals.add(new BasicNameValuePair("tool", "rnaview"));
244     vals.add(new BasicNameValuePair("pdbid", pdbid));
245     vals.add(new BasicNameValuePair("output", "rnaml"));
246     java.net.URL geturl = new URL(twoDtoolsURL + "?tool=rnaview&pdbid="
247             + pdbid + "&output=rnaml");
248     // return processJsonResponseFor(new
249     // InputStreamReader(geturl.openStream()));
250     ArrayList<Reader> readers = new ArrayList<Reader>();
251     readers.add(new InputStreamReader(geturl.openStream()));
252     return readers.iterator();
253   }
254
255 }