EBI web service interface update (and SequenceFetcher bugfix)
[jalview.git] / src / jalview / io / EBIFetchClient.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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.Enumeration;
23 import java.util.Hashtable;
24 import java.util.StringTokenizer;
25
26 import org.apache.axis.client.*;
27 import org.apache.axis.encoding.XMLType;
28
29 import javax.xml.namespace.QName;
30 import javax.xml.rpc.ParameterMode;
31
32
33 /**
34  * DOCUMENT ME!
35  *
36  * @author $author$
37  * @version $Revision$
38  */
39 public class EBIFetchClient
40 {
41     Call call;
42     String format = "default";
43     String style = "raw";
44
45     /**
46      * Creates a new EBIFetchClient object.
47      */
48     public EBIFetchClient()
49     {
50         try
51         {
52             call = (Call) new Service().createCall();
53             call.setTargetEndpointAddress(new java.net.URL(
54                     "http://www.ebi.ac.uk/ws/services/Dbfetch"));
55         }
56         catch (Exception ex)
57         {
58         }
59     }
60
61     /**
62      * DOCUMENT ME!
63      *
64      * @return DOCUMENT ME!
65      */
66     public String[] getSupportedDBs()
67     {
68         try
69         {
70             call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs"));
71             call.setReturnType(XMLType.SOAP_ARRAY);
72
73             return (String[]) call.invoke(new Object[] {  });
74         }
75         catch (Exception ex)
76         {
77             return null;
78         }
79     }
80
81     /**
82      * DOCUMENT ME!
83      *
84      * @return DOCUMENT ME!
85      */
86     public String[] getSupportedFormats()
87     {
88         try
89         {
90             call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats"));
91             call.setReturnType(XMLType.SOAP_ARRAY);
92
93             return (String[]) call.invoke(new Object[] {  });
94         }
95         catch (Exception ex)
96         {
97             return null;
98         }
99     }
100
101     /**
102      * DOCUMENT ME!
103      *
104      * @return DOCUMENT ME!
105      */
106     public String[] getSupportedStyles()
107     {
108         try
109         {
110             call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles"));
111             call.setReturnType(XMLType.SOAP_ARRAY);
112
113             return (String[]) call.invoke(new Object[] {  });
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        java.io.PrintWriter out = new java.io.PrintWriter(
129       new java.io.FileWriter("out.xml"));
130
131
132        for(int i=0; i<result.length; i++)
133        {
134          out.println(result[i]);
135        }
136        out.close();
137      }catch(Exception ex){ex.printStackTrace();}
138
139     }
140
141
142     public File fetchDataAsFile(String ids, String f, String s)
143     {
144       String [] data = fetchData(ids, f, s);
145       File outFile = null;
146       try{
147         outFile = File.createTempFile("jalview", ".xml");
148         outFile.deleteOnExit();
149         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
150         int index=0;
151         while( index < data.length )
152         {
153           out.println(data[index]);
154           index++;
155         }
156         out.close();
157       }catch(Exception ex){}
158       return outFile;
159     }
160
161     /**
162      * Single DB multiple record retrieval
163      *
164      * @param ids db:query1;query2;query3
165      * @param f raw/xml
166      * @param s ?
167      *
168      * @return Raw string array result of query set
169      */
170     public String[] fetchData(String ids, String f, String s)
171     {
172         // Need to split
173         // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
174         
175         StringTokenizer queries=new StringTokenizer(ids,";");
176         String db=null;
177         StringBuffer querystring=null;
178         while (queries.hasMoreTokens()) {
179             String query=queries.nextToken();
180             int p;
181             if ((p=query.indexOf(':'))>-1 && (p+3<query.length())) {
182                 db = query.substring(0,p);
183                 query = query.substring(p+1);
184             }
185             if (querystring==null) {
186                 querystring = new StringBuffer(query);
187             } else {
188                 querystring.append(","+query);
189             }
190         }
191         if (db==null) {
192             System.err.println("Invalid Query string : '"+ids+"'\nShould be of form 'dbname:q1;q2;q3;q4'");
193         }
194         return fetchBatch(querystring.toString(), db, f, s);
195     }
196     public String[] fetchBatch(String ids, String db, String f, String s) {
197         // max 50 ids can be added at one time
198         try
199         {
200             //call.setOperationName(new QName("urn:Dbfetch", "fetchData"));
201             call.setOperationName(new QName("urn:Dbfetch", "fetchBatch"));
202             call.addParameter("ids", XMLType.XSD_STRING, ParameterMode.IN);
203             call.addParameter("db", XMLType.XSD_STRING, ParameterMode.IN);
204             call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);
205             call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);
206             call.setReturnType(XMLType.SOAP_ARRAY);
207
208             if (f != null)
209             {
210                 format = f;
211             }
212
213             if (s != null)
214             {
215                 style = s;
216             }
217
218             try{
219               return (String[]) call.invoke(new Object[]
220                                             {ids.toLowerCase(), db.toLowerCase(), format, style});
221             }catch(OutOfMemoryError er)
222             {
223               System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM "+db+":\n"+ids);
224             }
225             return null;
226         }
227         catch (Exception ex)
228         {
229             if (ex.getMessage().startsWith("uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
230                 return null;
231             System.err.println("Unexpected exception when retrieving from "+db+"\nQuery was : '"+ids+"'");
232             ex.printStackTrace(System.err);
233             return null;
234         }
235     }
236 }