refactored ebi soap client and AbstractSequenceFetcher machinery to their own packages.
[jalview.git] / src / jalview / ws / ebi / 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.ws.ebi;
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
122   public File fetchDataAsFile(String ids, String f, String s)
123   {
124     String[] data = fetchData(ids, f, s);
125     File outFile = null;
126     try
127     {
128       outFile = File.createTempFile("jalview", ".xml");
129       outFile.deleteOnExit();
130       PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
131       int index = 0;
132       while (index < data.length)
133       {
134         out.println(data[index]);
135         index++;
136       }
137       out.close();
138     }
139     catch (Exception ex)
140     {}
141     return outFile;
142   }
143
144   /**
145    * Single DB multiple record retrieval
146    *
147    * @param ids db:query1;query2;query3
148    * @param f raw/xml
149    * @param s ?
150    *
151    * @return Raw string array result of query set
152    */
153   public String[] fetchData(String ids, String f, String s)
154   {
155     // Need to split
156     // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
157     StringTokenizer queries = new StringTokenizer(ids, ";");
158     String db = null;
159     StringBuffer querystring = null;
160     while (queries.hasMoreTokens())
161     {
162       String query = queries.nextToken();
163       int p;
164       if ( (p = query.indexOf(':')) > -1)
165       {
166         db = query.substring(0, p);
167         query = query.substring(p + 1);
168       }
169       if (querystring == null)
170       {
171         querystring = new StringBuffer(query);
172       }
173       else
174       {
175         querystring.append("," + query);
176       }
177     }
178     if (db == null)
179     {
180       System.err.println("Invalid Query string : '" + ids +
181                          "'\nShould be of form 'dbname:q1;q2;q3;q4'");
182     }
183     return fetchBatch(querystring.toString(), db, f, s);
184   }
185
186   public String[] fetchBatch(String ids, String db, String f, String s)
187   {
188     // max 50 ids can be added at one time
189     try
190     {
191       //call.setOperationName(new QName("urn:Dbfetch", "fetchData"));
192       call.setOperationName(new QName("urn:Dbfetch", "fetchBatch"));
193       call.addParameter("ids", XMLType.XSD_STRING, ParameterMode.IN);
194       call.addParameter("db", XMLType.XSD_STRING, ParameterMode.IN);
195       call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);
196       call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);
197       call.setReturnType(XMLType.SOAP_ARRAY);
198
199       if (f != null)
200       {
201         format = f;
202       }
203
204       if (s != null)
205       {
206         style = s;
207       }
208
209       try
210       {
211         return (String[]) call.invoke(new Object[]
212                                       {ids.toLowerCase(), db.toLowerCase(),
213                                       format, style});
214       }
215       catch (OutOfMemoryError er)
216       {
217         System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db + ":\n" +
218                            ids);
219       }
220       return null;
221     }
222     catch (Exception ex)
223     {
224       if (ex.getMessage().startsWith(
225           "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
226       {
227         return null;
228       }
229       System.err.println("Unexpected exception when retrieving from " + db +
230                          "\nQuery was : '" + ids + "'");
231       ex.printStackTrace(System.err);
232       return null;
233     }
234   }
235 }