JAL-1675 Chimera highlight side-chain; timeout added to REST calls
[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.IOException;
27 import java.io.InputStreamReader;
28 import java.io.Reader;
29 import java.io.StringReader;
30 import java.net.URL;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34
35 import org.apache.http.NameValuePair;
36 import org.apache.http.message.BasicNameValuePair;
37 import org.json.simple.JSONArray;
38 import org.json.simple.JSONObject;
39 import org.json.simple.parser.ContentHandler;
40 import org.json.simple.parser.ParseException;
41
42 /**
43  * simple methods for calling the various paradise RNA tools
44  * 
45  * @author jimp
46  * 
47  *         History: v1.0 revised from original due to refactoring of
48  *         paradise-ubmc.u-strasbg.fr/webservices/annotate3d to
49  *         http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview
50  */
51 public class Annotate3D
52 {
53   private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d";
54
55   private static ContentHandler createContentHandler()
56   {
57     ContentHandler ch = new ContentHandler()
58     {
59
60       @Override
61       public void startJSON() throws ParseException, IOException
62       {
63         // TODO Auto-generated method stub
64
65       }
66
67       @Override
68       public void endJSON() throws ParseException, IOException
69       {
70         // TODO Auto-generated method stub
71
72       }
73
74       @Override
75       public boolean startObject() throws ParseException, IOException
76       {
77         // TODO Auto-generated method stub
78         return false;
79       }
80
81       @Override
82       public boolean endObject() throws ParseException, IOException
83       {
84         // TODO Auto-generated method stub
85         return false;
86       }
87
88       @Override
89       public boolean startObjectEntry(String key) throws ParseException,
90               IOException
91       {
92         // TODO Auto-generated method stub
93         return false;
94       }
95
96       @Override
97       public boolean endObjectEntry() throws ParseException, IOException
98       {
99         // TODO Auto-generated method stub
100         return false;
101       }
102
103       @Override
104       public boolean startArray() throws ParseException, IOException
105       {
106         // TODO Auto-generated method stub
107         return false;
108       }
109
110       @Override
111       public boolean endArray() throws ParseException, IOException
112       {
113         // TODO Auto-generated method stub
114         return false;
115       }
116
117       @Override
118       public boolean primitive(Object value) throws ParseException,
119               IOException
120       {
121         // TODO Auto-generated method stub
122         return false;
123       }
124
125     };
126     return ch;
127   }
128
129   public static Iterator<Reader> getRNAMLForPDBFileAsString(String pdbfile)
130           throws Exception
131   {
132     List<NameValuePair> vals = new ArrayList<NameValuePair>();
133     vals.add(new BasicNameValuePair("tool", "rnaview"));
134     vals.add(new BasicNameValuePair("data", pdbfile));
135     vals.add(new BasicNameValuePair("output", "rnaml"));
136     // return processJsonResponseFor(HttpClientUtils.doHttpUrlPost(twoDtoolsURL,
137     // vals));
138     ArrayList<Reader> readers = new ArrayList<Reader>();
139     readers.add(HttpClientUtils.doHttpUrlPost(twoDtoolsURL, vals, 0, 0));
140     return readers.iterator();
141
142   }
143
144   public static Iterator<Reader> processJsonResponseFor(Reader respons)
145           throws Exception
146   {
147     org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
148     try
149     {
150       final JSONArray responses = (JSONArray) jp.parse(respons);
151       final Iterator rvals = responses.iterator();
152       return new Iterator<Reader>()
153       {
154         @Override
155         public boolean hasNext()
156         {
157           return rvals.hasNext();
158         }
159
160         @Override
161         public Reader next()
162         {
163           JSONObject val = (JSONObject) rvals.next();
164
165           Object sval = null;
166           try
167           {
168             sval = val.get("2D");
169           } catch (Exception x)
170           {
171             x.printStackTrace();
172           }
173           ;
174           if (sval == null)
175           {
176             System.err
177                     .println("DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"
178                             + val.toString());
179
180             sval = "";
181           }
182           return new StringReader(
183                   (sval instanceof JSONObject) ? ((JSONObject) sval)
184                           .toString() : sval.toString());
185
186         }
187
188         @Override
189         public void remove()
190         {
191           throw new Error(MessageManager.getString("error.not_implemented_remove"));
192
193         }
194
195         @Override
196         protected Object clone() throws CloneNotSupportedException
197         {
198           throw new CloneNotSupportedException(MessageManager.getString("error.not_implemented_clone"));
199         }
200
201         @Override
202         public boolean equals(Object obj)
203         {
204           return super.equals(obj);
205         }
206
207         @Override
208         protected void finalize() throws Throwable
209         {
210           while (rvals.hasNext())
211           {
212             rvals.next();
213           }
214           super.finalize();
215         }
216       };
217     } catch (Exception foo)
218     {
219       throw new Exception(MessageManager.getString("exception.couldnt_parse_responde_from_annotated3d_server"), foo);
220     }
221
222   }
223
224   public static Iterator<Reader> getRNAMLForPDBId(String pdbid)
225           throws Exception
226   {
227     List<NameValuePair> vals = new ArrayList<NameValuePair>();
228     vals.add(new BasicNameValuePair("tool", "rnaview"));
229     vals.add(new BasicNameValuePair("pdbid", pdbid));
230     vals.add(new BasicNameValuePair("output", "rnaml"));
231     java.net.URL geturl = new URL(twoDtoolsURL + "?tool=rnaview&pdbid="
232             + pdbid + "&output=rnaml");
233     // return processJsonResponseFor(new
234     // InputStreamReader(geturl.openStream()));
235     ArrayList<Reader> readers = new ArrayList<Reader>();
236     readers.add(new InputStreamReader(geturl.openStream()));
237     return readers.iterator();
238   }
239
240 }