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