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