Return accurate RGB for transparent residues
[jalview.git] / src / jalview / io / EBIFetchClient.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.io;\r
20 \r
21 import java.io.*;\r
22 \r
23 import org.apache.axis.AxisFault;\r
24 import org.apache.axis.client.*;\r
25 import org.apache.axis.encoding.XMLType;\r
26 import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;\r
27 import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;\r
28 \r
29 import javax.activation.DataHandler;\r
30 \r
31 import javax.xml.namespace.QName;\r
32 import javax.xml.rpc.ParameterMode;\r
33 \r
34 \r
35 /**\r
36  * DOCUMENT ME!\r
37  *\r
38  * @author $author$\r
39  * @version $Revision$\r
40  */\r
41 public class EBIFetchClient\r
42 {\r
43     Call call;\r
44     String format = "default";\r
45     String style = "raw";\r
46 \r
47     /**\r
48      * Creates a new EBIFetchClient object.\r
49      */\r
50     public EBIFetchClient()\r
51     {\r
52         try\r
53         {\r
54             call = (Call) new Service().createCall();\r
55             call.setTargetEndpointAddress(new java.net.URL(\r
56                     "http://www.ebi.ac.uk/ws/services/Dbfetch"));\r
57         }\r
58         catch (Exception ex)\r
59         {\r
60         }\r
61     }\r
62 \r
63     /**\r
64      * DOCUMENT ME!\r
65      *\r
66      * @return DOCUMENT ME!\r
67      */\r
68     public String[] getSupportedDBs()\r
69     {\r
70         try\r
71         {\r
72             call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs"));\r
73             call.setReturnType(XMLType.SOAP_ARRAY);\r
74 \r
75             return (String[]) call.invoke(new Object[] {  });\r
76         }\r
77         catch (Exception ex)\r
78         {\r
79             return null;\r
80         }\r
81     }\r
82 \r
83     /**\r
84      * DOCUMENT ME!\r
85      *\r
86      * @return DOCUMENT ME!\r
87      */\r
88     public String[] getSupportedFormats()\r
89     {\r
90         try\r
91         {\r
92             call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats"));\r
93             call.setReturnType(XMLType.SOAP_ARRAY);\r
94 \r
95             return (String[]) call.invoke(new Object[] {  });\r
96         }\r
97         catch (Exception ex)\r
98         {\r
99             return null;\r
100         }\r
101     }\r
102 \r
103     /**\r
104      * DOCUMENT ME!\r
105      *\r
106      * @return DOCUMENT ME!\r
107      */\r
108     public String[] getSupportedStyles()\r
109     {\r
110         try\r
111         {\r
112             call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles"));\r
113             call.setReturnType(XMLType.SOAP_ARRAY);\r
114 \r
115             return (String[]) call.invoke(new Object[] {  });\r
116         }\r
117         catch (Exception ex)\r
118         {\r
119             return null;\r
120         }\r
121     }\r
122 \r
123     public static void main (String [] args)\r
124     {\r
125       EBIFetchClient ebi = new EBIFetchClient();\r
126       String[] result = ebi.fetchData("uniprot:25K89D_SARPE;G6PblobD_HUMAN",\r
127                            "xml", null);\r
128 \r
129      try{\r
130        java.io.PrintWriter out = new java.io.PrintWriter(\r
131       new java.io.FileWriter("out.xml"));\r
132 \r
133 \r
134        for(int i=0; i<result.length; i++)\r
135        {\r
136          out.println(result[i]);\r
137        }\r
138        out.close();\r
139      }catch(Exception ex){ex.printStackTrace();}\r
140 \r
141     }\r
142 \r
143 \r
144     public File fetchDataAsFile(String ids, String f, String s)\r
145     {\r
146       String [] data = fetchData(ids, f, s);\r
147       File outFile = null;\r
148       try{\r
149         outFile = File.createTempFile("jalview", ".xml");\r
150         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));\r
151         int index=0;\r
152         while( index < data.length )\r
153         {\r
154           out.println(data[index]);\r
155           index++;\r
156         }\r
157         out.close();\r
158       }catch(Exception ex){}\r
159       return outFile;\r
160     }\r
161 \r
162     /**\r
163      * DOCUMENT ME!\r
164      *\r
165      * @param ids DOCUMENT ME!\r
166      * @param f DOCUMENT ME!\r
167      * @param s DOCUMENT ME!\r
168      *\r
169      * @return DOCUMENT ME!\r
170      */\r
171     public String[] fetchData(String ids, String f, String s)\r
172     {\r
173         // ids should be of the form uniprot:25KD_SARPE;ADHR_DROPS;\r
174         // max 50 ids can be added at one time\r
175         try\r
176         {\r
177             call.setOperationName(new QName("urn:Dbfetch", "fetchData"));\r
178             call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN);\r
179             call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
180             call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
181             call.setReturnType(XMLType.SOAP_ARRAY);\r
182 \r
183             if (f != null)\r
184             {\r
185                 format = f;\r
186             }\r
187 \r
188             if (s != null)\r
189             {\r
190                 style = s;\r
191             }\r
192 \r
193             return (String[]) call.invoke(new Object[] { ids, format, style });\r
194         }\r
195         catch (Exception ex)\r
196         {\r
197             return null;\r
198         }\r
199     }\r
200 \r
201     /**\r
202      * DOCUMENT ME!\r
203      *\r
204      * @param arg DOCUMENT ME!\r
205      * @param f DOCUMENT ME!\r
206      * @param s DOCUMENT ME!\r
207      *\r
208      * @return DOCUMENT ME!\r
209      */\r
210     public String fetchDataFile(String arg, String f, String s)\r
211     {\r
212         if (f != null)\r
213         {\r
214             format = f;\r
215         }\r
216 \r
217         if (s != null)\r
218         {\r
219             style = s;\r
220         }\r
221 \r
222         call.setOperationName(new QName("urn:Dbfetch", "fetchDataFile"));\r
223         call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN);\r
224         call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
225         call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
226 \r
227         QName qnameAttachment = new QName("urn:Dbfetch", "DataHandler");\r
228         call.registerTypeMapping(javax.activation.DataSource.class,\r
229             qnameAttachment, JAFDataHandlerSerializerFactory.class,\r
230             JAFDataHandlerDeserializerFactory.class);\r
231 \r
232         call.setReturnType(qnameAttachment);\r
233 \r
234         try\r
235         {\r
236             Object ret = call.invoke(new Object[] { arg, format, style });\r
237 \r
238             if (null == ret)\r
239             {\r
240                 System.err.println("Received null ");\r
241                 throw new AxisFault("", "Received null", null, null);\r
242             }\r
243 \r
244             if (ret instanceof String)\r
245             {\r
246                 System.err.println("Received problem response from server: " +\r
247                     ret);\r
248                 throw new AxisFault("", (String) ret, null, null);\r
249             }\r
250 \r
251             if (!(ret instanceof DataHandler))\r
252             {\r
253                 //The wrong type of object that what was expected.\r
254                 System.err.println("Received problem response from server:" +\r
255                     ret.getClass().getName());\r
256                 throw new AxisFault("",\r
257                     "Received problem response from server:" +\r
258                     ret.getClass().getName(), null, null);\r
259             }\r
260 \r
261             //Still here, so far so good.\r
262             DataHandler rdh = (DataHandler) ret;\r
263 \r
264             //From here we'll just treat the data resource as file.\r
265             String receivedfileName = rdh.getName(); //Get the filename.\r
266 \r
267             if (receivedfileName == null)\r
268             {\r
269                 System.err.println("Could not get the file name.");\r
270                 throw new AxisFault("", "Could not get the file name.", null,\r
271                     null);\r
272             }\r
273 \r
274             if (arg.equalsIgnoreCase("medline"))\r
275             {\r
276                 return receivedfileName;\r
277             }\r
278             else if (arg.equalsIgnoreCase("interpro"))\r
279             {\r
280                 return receivedfileName;\r
281             }\r
282             else\r
283             {\r
284                 System.err.println(receivedfileName);\r
285             }\r
286         }\r
287         catch (Exception ex)\r
288         {\r
289             ex.printStackTrace();\r
290         }\r
291 \r
292         return "ERROR";\r
293     }\r
294 }\r