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