JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / ext / paradise / Annotate3D.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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  *         History: 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
51  */
52 public class Annotate3D
53 {
54   private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d";
55
56   private static ContentHandler createContentHandler()
57   {
58     ContentHandler ch = new ContentHandler()
59     {
60
61       @Override
62       public void startJSON() throws ParseException, IOException
63       {
64         // TODO Auto-generated method stub
65
66       }
67
68       @Override
69       public void endJSON() throws ParseException, IOException
70       {
71         // TODO Auto-generated method stub
72
73       }
74
75       @Override
76       public boolean startObject() throws ParseException, IOException
77       {
78         // TODO Auto-generated method stub
79         return false;
80       }
81
82       @Override
83       public boolean endObject() throws ParseException, IOException
84       {
85         // TODO Auto-generated method stub
86         return false;
87       }
88
89       @Override
90       public boolean startObjectEntry(String key) throws ParseException,
91               IOException
92       {
93         // TODO Auto-generated method stub
94         return false;
95       }
96
97       @Override
98       public boolean endObjectEntry() throws ParseException, IOException
99       {
100         // TODO Auto-generated method stub
101         return false;
102       }
103
104       @Override
105       public boolean startArray() throws ParseException, IOException
106       {
107         // TODO Auto-generated method stub
108         return false;
109       }
110
111       @Override
112       public boolean endArray() throws ParseException, IOException
113       {
114         // TODO Auto-generated method stub
115         return false;
116       }
117
118       @Override
119       public boolean primitive(Object value) throws ParseException,
120               IOException
121       {
122         // TODO Auto-generated method stub
123         return false;
124       }
125
126     };
127     return ch;
128   }
129
130   public static Iterator<Reader> getRNAMLForPDBFileAsString(String pdbfile)
131           throws Exception
132   {
133     List<NameValuePair> vals = new ArrayList<NameValuePair>();
134     vals.add(new BasicNameValuePair("tool", "rnaview"));
135     vals.add(new BasicNameValuePair("data", pdbfile));
136     vals.add(new BasicNameValuePair("output", "rnaml"));
137     // return processJsonResponseFor(HttpClientUtils.doHttpUrlPost(twoDtoolsURL,
138     // vals));
139     ArrayList<Reader> readers = new ArrayList<Reader>();
140     final BufferedReader postResponse = HttpClientUtils.doHttpUrlPost(
141             twoDtoolsURL, vals, 0, 0);
142     readers.add(postResponse);
143     return readers.iterator();
144
145   }
146
147   public static Iterator<Reader> processJsonResponseFor(Reader respons)
148           throws Exception
149   {
150     org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
151     try
152     {
153       final JSONArray responses = (JSONArray) jp.parse(respons);
154       final Iterator rvals = responses.iterator();
155       return new Iterator<Reader>()
156       {
157         @Override
158         public boolean hasNext()
159         {
160           return rvals.hasNext();
161         }
162
163         @Override
164         public Reader next()
165         {
166           JSONObject val = (JSONObject) rvals.next();
167
168           Object sval = null;
169           try
170           {
171             sval = val.get("2D");
172           } catch (Exception x)
173           {
174             x.printStackTrace();
175           }
176           ;
177           if (sval == null)
178           {
179             System.err
180                     .println("DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"
181                             + val.toString());
182
183             sval = "";
184           }
185           return new StringReader(
186                   (sval instanceof JSONObject) ? ((JSONObject) sval)
187                           .toString() : sval.toString());
188
189         }
190
191         @Override
192         public void remove()
193         {
194           throw new Error(
195                   MessageManager.getString("error.not_implemented_remove"));
196
197         }
198
199         @Override
200         protected Object clone() throws CloneNotSupportedException
201         {
202           throw new CloneNotSupportedException(
203                   MessageManager.getString("error.not_implemented_clone"));
204         }
205
206         @Override
207         public boolean equals(Object obj)
208         {
209           return super.equals(obj);
210         }
211
212         @Override
213         protected void finalize() throws Throwable
214         {
215           while (rvals.hasNext())
216           {
217             rvals.next();
218           }
219           super.finalize();
220         }
221       };
222     } catch (Exception foo)
223     {
224       throw new Exception(
225               MessageManager
226                       .getString("exception.couldnt_parse_responde_from_annotated3d_server"),
227               foo);
228     }
229
230   }
231
232   public static Iterator<Reader> getRNAMLForPDBId(String pdbid)
233           throws Exception
234   {
235     List<NameValuePair> vals = new ArrayList<NameValuePair>();
236     vals.add(new BasicNameValuePair("tool", "rnaview"));
237     vals.add(new BasicNameValuePair("pdbid", pdbid));
238     vals.add(new BasicNameValuePair("output", "rnaml"));
239     java.net.URL geturl = new URL(twoDtoolsURL + "?tool=rnaview&pdbid="
240             + pdbid + "&output=rnaml");
241     // return processJsonResponseFor(new
242     // InputStreamReader(geturl.openStream()));
243     ArrayList<Reader> readers = new ArrayList<Reader>();
244     readers.add(new InputStreamReader(geturl.openStream()));
245     return readers.iterator();
246   }
247
248 }