114de42ee4afa4b35a8fe864a956e91ac352fdcb
[jalview.git] / src / jalview / ext / paradise / Annotate3D.java
1 package jalview.ext.paradise;
2
3 import jalview.ws.HttpClientUtils;
4
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.Reader;
8 import java.io.StringReader;
9 import java.net.URL;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.List;
14
15 import org.apache.http.NameValuePair;
16 import org.apache.http.message.BasicNameValuePair;
17 import org.json.simple.JSONArray;
18 import org.json.simple.JSONObject;
19 import org.json.simple.JSONStreamAware;
20 import org.json.simple.parser.ContentHandler;
21 import org.json.simple.parser.ParseException;
22
23 /**
24  * simple methods for calling the various paradise RNA tools
25  * 
26  * @author jimp
27  * 
28  *         History: v1.0 revised from original due to refactoring of
29  *         paradise-ubmc.u-strasbg.fr/webservices/annotate3d to
30  *         http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview
31  */
32 public class Annotate3D
33 {
34   private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d";
35   private static ContentHandler createContentHandler()
36   {
37     ContentHandler ch = new ContentHandler() {
38
39       @Override
40       public void startJSON() throws ParseException, IOException
41       {
42         // TODO Auto-generated method stub
43         
44       }
45
46       @Override
47       public void endJSON() throws ParseException, IOException
48       {
49         // TODO Auto-generated method stub
50         
51       }
52
53       @Override
54       public boolean startObject() throws ParseException, IOException
55       {
56         // TODO Auto-generated method stub
57         return false;
58       }
59
60       @Override
61       public boolean endObject() throws ParseException, IOException
62       {
63         // TODO Auto-generated method stub
64         return false;
65       }
66
67       @Override
68       public boolean startObjectEntry(String key) throws ParseException,
69               IOException
70       {
71         // TODO Auto-generated method stub
72         return false;
73       }
74
75       @Override
76       public boolean endObjectEntry() throws ParseException, IOException
77       {
78         // TODO Auto-generated method stub
79         return false;
80       }
81
82       @Override
83       public boolean startArray() throws ParseException, IOException
84       {
85         // TODO Auto-generated method stub
86         return false;
87       }
88
89       @Override
90       public boolean endArray() throws ParseException, IOException
91       {
92         // TODO Auto-generated method stub
93         return false;
94       }
95
96       @Override
97       public boolean primitive(Object value) throws ParseException,
98               IOException
99       {
100         // TODO Auto-generated method stub
101         return false;
102       }
103       
104     };
105     return ch;
106   }
107   public static Iterator<Reader> getRNAMLForPDBFileAsString(String pdbfile)
108           throws Exception
109   {
110     List<NameValuePair> vals = new ArrayList<NameValuePair>();
111     vals.add(new BasicNameValuePair("tool", "rnaview"));
112     vals.add(new BasicNameValuePair("data", pdbfile));
113     vals.add(new BasicNameValuePair("output", "rnaml"));
114     return processJsonResponseFor(HttpClientUtils.doHttpUrlPost(twoDtoolsURL, vals));
115   }
116   public static Iterator<Reader> processJsonResponseFor(Reader respons) throws Exception
117   {
118     org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
119     try {
120       final JSONArray responses = (JSONArray) jp.parse(respons);
121       final Iterator rvals = responses.iterator();
122       return new Iterator<Reader>() 
123         {
124           @Override
125           public boolean hasNext()
126           {
127             return rvals.hasNext();
128           }
129           @Override
130           public Reader next()
131           {
132             JSONObject val=(JSONObject) rvals.next();
133             
134             Object sval = null;
135             try {
136               sval = val.get("2D");
137             } catch (Exception x) {x.printStackTrace();};
138             if (sval==null)
139             {
140               System.err.println("DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"+val.toString());
141               
142               sval = "";
143             }
144             return new StringReader((sval instanceof JSONObject) ? ((JSONObject)sval).toString():sval.toString());
145             
146           }@Override
147           public void remove()
148           {
149             throw new Error("Remove: Not implemented");
150             
151           }@Override
152           protected Object clone() throws CloneNotSupportedException
153           {
154             throw new CloneNotSupportedException("Clone: Not implemented");
155           }@Override
156           public boolean equals(Object obj)
157           {
158             return super.equals(obj);
159           }@Override
160           protected void finalize() throws Throwable
161           {
162             while (rvals.hasNext())
163             {
164               rvals.next();
165             }
166             super.finalize();
167           }
168         };
169     } catch (Exception foo)
170     {
171       throw new Exception("Couldn't parse response from Annotate3d server.",foo);
172     }
173     
174     
175   }
176
177   public static Iterator<Reader> getRNAMLForPDBId(String pdbid) throws Exception
178   {
179     List<NameValuePair> vals = new ArrayList<NameValuePair>();
180     vals.add(new BasicNameValuePair("tool", "rnaview"));
181     vals.add(new BasicNameValuePair("pdbid", pdbid));
182     vals.add(new BasicNameValuePair("format", "rnaml"));
183     java.net.URL geturl = new URL(twoDtoolsURL+"?tool=rnaview&output=rnaml&pdbid="+pdbid);
184     return processJsonResponseFor(new InputStreamReader(geturl.openStream()));
185   }
186
187 }