JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / jws1 / Annotate3D.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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.ws.jws1;
22
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
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, "RNAML");
91         if (al == null)
92         {
93           al = nal;
94         }
95         else
96         {
97           al.append(nal);
98         }
99       }
100       return al;
101     } catch (Throwable x)
102     {
103       if (x instanceof IOException)
104       {
105         throw ((IOException) x);
106       }
107       else
108       {
109         throw new IOException(MessageManager.getString("exception.unexpected_handling_rnaml_translation_for_pdb"),
110                 x);
111       }
112     }
113   }
114
115   public Annotate3D(String path) throws InterruptedException
116   {
117     System.out.println("Annotate3D");
118     try
119     {
120       // //URL url = new
121       // URL("http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d?data="+inFile);
122       // System.out.println("Step1");
123       // FileReader r = new FileReader(inFile);
124       // BufferedReader in = new BufferedReader(r);
125       // StringBuffer content = new StringBuffer();
126       // System.out.println("Step2");
127       // while(in.readLine()!=null){
128       // content.append(in.readLine());
129       // //System.out.println("Step3"+in.readLine());
130       // }
131       //
132       // String data = URLEncoder.encode("data", "UTF-8") + "=" +
133       // URLEncoder.encode(content.toString(), "UTF-8");
134       // for (int i=0;i<data.length();i++)
135       // {
136       // System.out.print(data.charAt(i));
137       // }
138
139       // String data = "width=50&height=100";
140
141       // // Send the request
142       // FileReader r = new FileReader(path);
143       // BufferedReader in = new BufferedReader(r);
144       // StringBuffer content = new StringBuffer();
145       // System.out.println("Step1");
146       // while(in.readLine()!=null){
147       // content.append(in.readLine());
148       //
149       // }
150       // System.out.println("Step2");
151       // String data = URLEncoder.encode("data", "UTF-8") + "=" +
152       // URLEncoder.encode(content.toString(), "UTF-8");
153       // System.out.println("Step2");
154       // URL url = new
155       // URL("http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d?data="+data);
156       // DataInputStream is = new DataInputStream(url.openStream());
157       // String str;
158       // while ((str = is.readLine()) != null) {
159       // System.out.println(str);
160       // //out.write(str);
161       // }
162       FileReader r = new FileReader(path);
163       BufferedReader in = new BufferedReader(r);
164       String content = "";
165       String str;
166
167       while ((str = in.readLine()) != null)
168       {
169         // System.out.println(str);
170
171         content = content + str;
172       }
173       System.out.println("pdbfile=" + content.toString());
174       System.out.println("capacité=" + content.length());
175       String paramfile = URLEncoder.encode(content.toString(), "UTF-8");
176       System.out.println("param=" + paramfile);
177       URL url = new URL(
178               "http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d?data="
179                       + content);
180       BufferedReader is = new BufferedReader(new InputStreamReader(
181               url.openStream()));
182       String str4;
183       while ((str4 = is.readLine()) != null)
184       {
185         System.out.println(str4);
186         // out.write(str);
187       }
188       in.close();
189       is.close();
190
191       // HttpURLConnection connection = (HttpURLConnection)url.openConnection();
192       // connection.setRequestMethod("POST" );
193       // connection.setRequestProperty("data", path );
194       // //connection.setRequestProperty("nomDuChamp2", "valeurDuChamp2" );
195       // BufferedReader input = new BufferedReader(new
196       // InputStreamReader(connection.getInputStream()));
197       // //DataInputStream input = new
198       // DataInputStream(connection.getInputStream());
199       // String c;
200       // while((c=input.readLine())!=null){
201       // System.out.print(c);
202       // }
203       // input.close();
204       // BufferedReader in1 = new BufferedReader(is);
205
206       // OutputStream out1 = null;
207       // System.out.println("Step3");
208       // BufferedWriter out = new BufferedWriter(new OutputStreamWriter(out1,
209       // "temp.rnaml"));
210       //
211       // in.close();
212       // out.close();
213
214       // return;
215
216       // System.out.println(data.length());
217       // System.out.println("step2");
218       // URL url = new
219       // URL("http://paradise-ibmc.u-strasbg.fr/webservices/annotate3d?data="+data);
220       // System.out.println("step3");
221       // URLConnection conn = url.openConnection();
222       // conn.setDoOutput(true);
223       // OutputStreamWriter writer = new
224       // OutputStreamWriter(conn.getOutputStream());
225
226       // write parameters
227       // writer.write(data);
228       // writer.flush();
229
230       // Get the response
231       // StringBuffer answer = new StringBuffer();
232       // //BufferedReader reader = new BufferedReader(new
233       // InputStreamReader(conn.getInputStream()));
234       // //String line;
235       // while ((line = reader.readLine()) != null) {
236       // answer.append(line);
237       // System.out.println(line);
238       // }
239       // writer.close();
240       // reader.close();
241
242       // Output the response
243
244     } catch (MalformedURLException ex)
245     {
246       ex.printStackTrace();
247     } catch (IOException ex)
248     {
249       ex.printStackTrace();
250     }
251   }
252
253   // in = new BufferedReader(new InputStreamReader(url.openStream()));
254
255   // String str;
256
257   // out = new FileOutputStream("temp.rnaml");
258   // out = new BufferedWriter(new FileWriter("temp.rnaml"));
259
260   // while ((str = in.readLine()) != null) {
261   // System.out.println(str);
262   // out.write(str);
263   // System.out.println(str);
264   // in.close();
265
266   // out.close();
267   // } catch (MalformedURLException e) {
268   // } catch (IOException e) {
269   // }
270   //
271   // }
272
273   // public BufferedWriter getReader()
274   // {
275   // System.out.println("The buffer");
276
277   // return out;
278
279   // }
280
281 }