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