b588161ff50f0888559a094af689da73c83eda70
[jalview.git] / src / jalview / ext / paradise / Annotate3D.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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.ws.HttpClientUtils;
24
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.Reader;
28 import java.io.StringReader;
29 import java.net.URL;
30 import java.util.ArrayList;
31 import java.util.Collection;
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.JSONStreamAware;
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   private static ContentHandler createContentHandler()
56   {
57     ContentHandler ch = new ContentHandler() {
58
59       @Override
60       public void startJSON() throws ParseException, IOException
61       {
62         // TODO Auto-generated method stub
63         
64       }
65
66       @Override
67       public void endJSON() throws ParseException, IOException
68       {
69         // TODO Auto-generated method stub
70         
71       }
72
73       @Override
74       public boolean startObject() throws ParseException, IOException
75       {
76         // TODO Auto-generated method stub
77         return false;
78       }
79
80       @Override
81       public boolean endObject() throws ParseException, IOException
82       {
83         // TODO Auto-generated method stub
84         return false;
85       }
86
87       @Override
88       public boolean startObjectEntry(String key) throws ParseException,
89               IOException
90       {
91         // TODO Auto-generated method stub
92         return false;
93       }
94
95       @Override
96       public boolean endObjectEntry() throws ParseException, IOException
97       {
98         // TODO Auto-generated method stub
99         return false;
100       }
101
102       @Override
103       public boolean startArray() throws ParseException, IOException
104       {
105         // TODO Auto-generated method stub
106         return false;
107       }
108
109       @Override
110       public boolean endArray() throws ParseException, IOException
111       {
112         // TODO Auto-generated method stub
113         return false;
114       }
115
116       @Override
117       public boolean primitive(Object value) throws ParseException,
118               IOException
119       {
120         // TODO Auto-generated method stub
121         return false;
122       }
123       
124     };
125     return ch;
126   }
127   public static Iterator<Reader> getRNAMLForPDBFileAsString(String pdbfile)
128           throws Exception
129   {
130     List<NameValuePair> vals = new ArrayList<NameValuePair>();
131     vals.add(new BasicNameValuePair("tool", "rnaview"));
132     vals.add(new BasicNameValuePair("data", pdbfile));
133     vals.add(new BasicNameValuePair("output", "rnaml"));
134     // return processJsonResponseFor(HttpClientUtils.doHttpUrlPost(twoDtoolsURL, vals));
135     ArrayList<Reader> readers = new ArrayList<Reader>();
136     readers.add(HttpClientUtils.doHttpUrlPost(twoDtoolsURL, vals));
137     return readers.iterator();
138
139   }
140   public static Iterator<Reader> processJsonResponseFor(Reader respons) throws Exception
141   {
142     org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
143     try {
144       final JSONArray responses = (JSONArray) jp.parse(respons);
145       final Iterator rvals = responses.iterator();
146       return new Iterator<Reader>() 
147         {
148           @Override
149           public boolean hasNext()
150           {
151             return rvals.hasNext();
152           }
153           @Override
154           public Reader next()
155           {
156             JSONObject val=(JSONObject) rvals.next();
157             
158             Object sval = null;
159             try {
160               sval = val.get("2D");
161             } catch (Exception x) {x.printStackTrace();};
162             if (sval==null)
163             {
164               System.err.println("DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"+val.toString());
165               
166               sval = "";
167             }
168             return new StringReader((sval instanceof JSONObject) ? ((JSONObject)sval).toString():sval.toString());
169             
170           }@Override
171           public void remove()
172           {
173             throw new Error("Remove: Not implemented");
174             
175           }@Override
176           protected Object clone() throws CloneNotSupportedException
177           {
178             throw new CloneNotSupportedException("Clone: Not implemented");
179           }@Override
180           public boolean equals(Object obj)
181           {
182             return super.equals(obj);
183           }@Override
184           protected void finalize() throws Throwable
185           {
186             while (rvals.hasNext())
187             {
188               rvals.next();
189             }
190             super.finalize();
191           }
192         };
193     } catch (Exception foo)
194     {
195       throw new Exception("Couldn't parse response from Annotate3d server.",foo);
196     }
197     
198     
199   }
200
201   public static Iterator<Reader> getRNAMLForPDBId(String pdbid) throws Exception
202   {
203     List<NameValuePair> vals = new ArrayList<NameValuePair>();
204     vals.add(new BasicNameValuePair("tool", "rnaview"));
205     vals.add(new BasicNameValuePair("pdbid", pdbid));
206     vals.add(new BasicNameValuePair("output", "rnaml"));
207     java.net.URL geturl = new URL(twoDtoolsURL+"?tool=rnaview&pdbid="+pdbid+"&output=rnaml");
208     //return processJsonResponseFor(new InputStreamReader(geturl.openStream()));
209     ArrayList<Reader> readers = new ArrayList<Reader>();
210     readers.add(new InputStreamReader(geturl.openStream()));
211     return readers.iterator();
212   }
213
214 }