upgrade to latest paradise services model using JSon response.
[jalview.git] / src / jalview / ws / jws1 / Annotate3D.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws.jws1;
19
20 import jalview.datamodel.Alignment;
21 import jalview.datamodel.AlignmentI;
22 import jalview.io.FileParse;
23 import jalview.io.FormatAdapter;
24 import jalview.io.InputStreamParser;
25
26 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.io.Reader;
31 import java.net.MalformedURLException;
32 import java.net.URL;
33 import java.net.URLEncoder;
34 import java.util.Iterator;
35
36 public class Annotate3D
37 {
38   // protected BufferedReader in;
39   // protected BufferedWriter out;
40
41   public Annotate3D()
42   {
43     System.out.println("Annotate3D");
44     // try {
45     // Create a URL for the desired page
46     // String id = "1HR2";
47     // URL url = new
48     // URL("http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d?pdbid="+id);
49     // in = new BufferedReader(new InputStreamReader(url.openStream()));
50     // String str;
51     // OutputStream out1 = null;
52     // out = new BufferedWriter(new OutputStreamWriter(out1, "temp.rnaml"));
53     // while ((str = in.readLine()) != null) {
54     // System.out.println(str);
55     // out.write(str);
56     // }
57     // in.close();
58     // out.close();
59     // } catch (MalformedURLException e) {
60     // } catch (IOException e) {
61     // }
62   }
63
64   public AlignmentI getRNAMLFor(final FileParse source) throws IOException
65   {
66     try
67     {
68       StringBuffer sb = new StringBuffer();
69
70       Reader fpr = source.getReader();
71       int p = 0;
72       char[] cbuff = new char[2048];
73       while ((p = fpr.read(cbuff)) > 0)
74       {
75         for (int i = 0; i < p; i++)
76         {
77           sb.append(cbuff[i]);
78         }
79       }
80       Iterator<Reader> r = jalview.ext.paradise.Annotate3D
81               .getRNAMLForPDBFileAsString(sb.toString());
82       AlignmentI al=null;
83       while (r.hasNext())
84       {
85         FileParse fp = new InputStreamParser(r.next(), source.getDataName());
86         AlignmentI nal = new FormatAdapter().readFromFile(fp, "RNAML");
87         if (al==null)
88         {
89           al = nal;
90         } else {
91           al.append(nal);
92         }
93       }
94       return al;
95     } catch (Throwable x)
96     {
97       if (x instanceof IOException)
98       {
99         throw ((IOException) x);
100       }
101       else
102       {
103         throw new IOException(
104                 "Unexpected exception when handling RNAML translation of PDB data",
105                 x);
106       }
107     }
108   }
109
110   public Annotate3D(String path) throws InterruptedException
111   {
112     System.out.println("Annotate3D");
113     try
114     {
115       // //URL url = new
116       // URL("http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d?data="+inFile);
117       // System.out.println("Step1");
118       // FileReader r = new FileReader(inFile);
119       // BufferedReader in = new BufferedReader(r);
120       // StringBuffer content = new StringBuffer();
121       // System.out.println("Step2");
122       // while(in.readLine()!=null){
123       // content.append(in.readLine());
124       // //System.out.println("Step3"+in.readLine());
125       // }
126       //
127       // String data = URLEncoder.encode("data", "UTF-8") + "=" +
128       // URLEncoder.encode(content.toString(), "UTF-8");
129       // for (int i=0;i<data.length();i++)
130       // {
131       // System.out.print(data.charAt(i));
132       // }
133
134       // String data = "width=50&height=100";
135
136       // // Send the request
137       // FileReader r = new FileReader(path);
138       // BufferedReader in = new BufferedReader(r);
139       // StringBuffer content = new StringBuffer();
140       // System.out.println("Step1");
141       // while(in.readLine()!=null){
142       // content.append(in.readLine());
143       //
144       // }
145       // System.out.println("Step2");
146       // String data = URLEncoder.encode("data", "UTF-8") + "=" +
147       // URLEncoder.encode(content.toString(), "UTF-8");
148       // System.out.println("Step2");
149       // URL url = new
150       // URL("http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d?data="+data);
151       // DataInputStream is = new DataInputStream(url.openStream());
152       // String str;
153       // while ((str = is.readLine()) != null) {
154       // System.out.println(str);
155       // //out.write(str);
156       // }
157       FileReader r = new FileReader(path);
158       BufferedReader in = new BufferedReader(r);
159       String content = "";
160       String str;
161
162       while ((str = in.readLine()) != null)
163       {
164         // System.out.println(str);
165
166         content = content + str;
167       }
168       System.out.println("pdbfile=" + content.toString());
169       System.out.println("capacité=" + content.length());
170       String paramfile = URLEncoder.encode(content.toString(), "UTF-8");
171       System.out.println("param=" + paramfile);
172       URL url = new URL(
173               "http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d?data="
174                       + content);
175       BufferedReader is = new BufferedReader(new InputStreamReader(
176               url.openStream()));
177       String str4;
178       while ((str4 = is.readLine()) != null)
179       {
180         System.out.println(str4);
181         // out.write(str);
182       }
183       in.close();
184       is.close();
185
186       // HttpURLConnection connection = (HttpURLConnection)url.openConnection();
187       // connection.setRequestMethod("POST" );
188       // connection.setRequestProperty("data", path );
189       // //connection.setRequestProperty("nomDuChamp2", "valeurDuChamp2" );
190       // BufferedReader input = new BufferedReader(new
191       // InputStreamReader(connection.getInputStream()));
192       // //DataInputStream input = new
193       // DataInputStream(connection.getInputStream());
194       // String c;
195       // while((c=input.readLine())!=null){
196       // System.out.print(c);
197       // }
198       // input.close();
199       // BufferedReader in1 = new BufferedReader(is);
200
201       // OutputStream out1 = null;
202       // System.out.println("Step3");
203       // BufferedWriter out = new BufferedWriter(new OutputStreamWriter(out1,
204       // "temp.rnaml"));
205       //
206       // in.close();
207       // out.close();
208
209       // return;
210
211       // System.out.println(data.length());
212       // System.out.println("step2");
213       // URL url = new
214       // URL("http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d?data="+data);
215       // System.out.println("step3");
216       // URLConnection conn = url.openConnection();
217       // conn.setDoOutput(true);
218       // OutputStreamWriter writer = new
219       // OutputStreamWriter(conn.getOutputStream());
220
221       // write parameters
222       // writer.write(data);
223       // writer.flush();
224
225       // Get the response
226       // StringBuffer answer = new StringBuffer();
227       // //BufferedReader reader = new BufferedReader(new
228       // InputStreamReader(conn.getInputStream()));
229       // //String line;
230       // while ((line = reader.readLine()) != null) {
231       // answer.append(line);
232       // System.out.println(line);
233       // }
234       // writer.close();
235       // reader.close();
236
237       // Output the response
238
239     } catch (MalformedURLException ex)
240     {
241       ex.printStackTrace();
242     } catch (IOException ex)
243     {
244       ex.printStackTrace();
245     }
246   }
247
248   // in = new BufferedReader(new InputStreamReader(url.openStream()));
249
250   // String str;
251
252   // out = new FileOutputStream("temp.rnaml");
253   // out = new BufferedWriter(new FileWriter("temp.rnaml"));
254
255   // while ((str = in.readLine()) != null) {
256   // System.out.println(str);
257   // out.write(str);
258   // System.out.println(str);
259   // in.close();
260
261   // out.close();
262   // } catch (MalformedURLException e) {
263   // } catch (IOException e) {
264   // }
265   //
266   // }
267
268   // public BufferedWriter getReader()
269   // {
270   // System.out.println("The buffer");
271
272   // return out;
273
274   // }
275
276 }