Formatting
[jalview.git] / src / jalview / io / EBIFetchClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 import java.io.*;
22 import java.util.*;
23 import javax.xml.namespace.*;
24 import javax.xml.rpc.*;
25
26 import org.apache.axis.client.Call;
27 import org.apache.axis.client.Service;
28 import org.apache.axis.encoding.*;
29
30 /**
31  * DOCUMENT ME!
32  *
33  * @author $author$
34  * @version $Revision$
35  */
36 public class EBIFetchClient
37 {
38   Call call;
39   String format = "default";
40   String style = "raw";
41
42   /**
43    * Creates a new EBIFetchClient object.
44    */
45   public EBIFetchClient()
46   {
47     try
48     {
49       call = (Call)new Service().createCall();
50       call.setTargetEndpointAddress(new java.net.URL(
51           "http://www.ebi.ac.uk/ws/services/Dbfetch"));
52     }
53     catch (Exception ex)
54     {
55     }
56   }
57
58   /**
59    * DOCUMENT ME!
60    *
61    * @return DOCUMENT ME!
62    */
63   public String[] getSupportedDBs()
64   {
65     try
66     {
67       call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs"));
68       call.setReturnType(XMLType.SOAP_ARRAY);
69
70       return (String[]) call.invoke(new Object[]
71                                     {});
72     }
73     catch (Exception ex)
74     {
75       return null;
76     }
77   }
78
79   /**
80    * DOCUMENT ME!
81    *
82    * @return DOCUMENT ME!
83    */
84   public String[] getSupportedFormats()
85   {
86     try
87     {
88       call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats"));
89       call.setReturnType(XMLType.SOAP_ARRAY);
90
91       return (String[]) call.invoke(new Object[]
92                                     {});
93     }
94     catch (Exception ex)
95     {
96       return null;
97     }
98   }
99
100   /**
101    * DOCUMENT ME!
102    *
103    * @return DOCUMENT ME!
104    */
105   public String[] getSupportedStyles()
106   {
107     try
108     {
109       call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles"));
110       call.setReturnType(XMLType.SOAP_ARRAY);
111
112       return (String[]) call.invoke(new Object[]
113                                     {});
114     }
115     catch (Exception ex)
116     {
117       return null;
118     }
119   }
120
121   public static void main(String[] args)
122   {
123     EBIFetchClient ebi = new EBIFetchClient();
124     String[] result = ebi.fetchData("uniprot:25KD_SARPE;G6PD_HUMAN",
125                                     "xml", null);
126
127     try
128     {
129       java.io.PrintWriter out = new java.io.PrintWriter(
130           new java.io.FileWriter("out.xml"));
131
132       for (int i = 0; i < result.length; i++)
133       {
134         out.println(result[i]);
135       }
136       out.close();
137     }
138     catch (Exception ex)
139     {
140       ex.printStackTrace();
141     }
142
143   }
144
145   public File fetchDataAsFile(String ids, String f, String s)
146   {
147     String[] data = fetchData(ids, f, s);
148     File outFile = null;
149     try
150     {
151       outFile = File.createTempFile("jalview", ".xml");
152       outFile.deleteOnExit();
153       PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
154       int index = 0;
155       while (index < data.length)
156       {
157         out.println(data[index]);
158         index++;
159       }
160       out.close();
161     }
162     catch (Exception ex)
163     {}
164     return outFile;
165   }
166
167   /**
168    * Single DB multiple record retrieval
169    *
170    * @param ids db:query1;query2;query3
171    * @param f raw/xml
172    * @param s ?
173    *
174    * @return Raw string array result of query set
175    */
176   public String[] fetchData(String ids, String f, String s)
177   {
178     // Need to split
179     // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
180
181     StringTokenizer queries = new StringTokenizer(ids, ";");
182     String db = null;
183     StringBuffer querystring = null;
184     while (queries.hasMoreTokens())
185     {
186       String query = queries.nextToken();
187       int p;
188       if ( (p = query.indexOf(':')) > -1 && (p + 3 < query.length()))
189       {
190         db = query.substring(0, p);
191         query = query.substring(p + 1);
192       }
193       if (querystring == null)
194       {
195         querystring = new StringBuffer(query);
196       }
197       else
198       {
199         querystring.append("," + query);
200       }
201     }
202     if (db == null)
203     {
204       System.err.println("Invalid Query string : '" + ids +
205                          "'\nShould be of form 'dbname:q1;q2;q3;q4'");
206     }
207     return fetchBatch(querystring.toString(), db, f, s);
208   }
209
210   public String[] fetchBatch(String ids, String db, String f, String s)
211   {
212     // max 50 ids can be added at one time
213     try
214     {
215       //call.setOperationName(new QName("urn:Dbfetch", "fetchData"));
216       call.setOperationName(new QName("urn:Dbfetch", "fetchBatch"));
217       call.addParameter("ids", XMLType.XSD_STRING, ParameterMode.IN);
218       call.addParameter("db", XMLType.XSD_STRING, ParameterMode.IN);
219       call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);
220       call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);
221       call.setReturnType(XMLType.SOAP_ARRAY);
222
223       if (f != null)
224       {
225         format = f;
226       }
227
228       if (s != null)
229       {
230         style = s;
231       }
232
233       try
234       {
235         return (String[]) call.invoke(new Object[]
236                                       {ids.toLowerCase(), db.toLowerCase(),
237                                       format, style});
238       }
239       catch (OutOfMemoryError er)
240       {
241         System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db + ":\n" +
242                            ids);
243       }
244       return null;
245     }
246     catch (Exception ex)
247     {
248       if (ex.getMessage().startsWith(
249           "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
250       {
251         return null;
252       }
253       System.err.println("Unexpected exception when retrieving from " + db +
254                          "\nQuery was : '" + ids + "'");
255       ex.printStackTrace(System.err);
256       return null;
257     }
258   }
259 }