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