updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[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
23 import org.apache.axis.client.*;
24 import org.apache.axis.encoding.XMLType;
25
26 import javax.xml.namespace.QName;
27 import javax.xml.rpc.ParameterMode;
28
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         catch (Exception ex)
73         {
74             return null;
75         }
76     }
77
78     /**
79      * DOCUMENT ME!
80      *
81      * @return DOCUMENT ME!
82      */
83     public String[] getSupportedFormats()
84     {
85         try
86         {
87             call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats"));
88             call.setReturnType(XMLType.SOAP_ARRAY);
89
90             return (String[]) call.invoke(new Object[] {  });
91         }
92         catch (Exception ex)
93         {
94             return null;
95         }
96     }
97
98     /**
99      * DOCUMENT ME!
100      *
101      * @return DOCUMENT ME!
102      */
103     public String[] getSupportedStyles()
104     {
105         try
106         {
107             call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles"));
108             call.setReturnType(XMLType.SOAP_ARRAY);
109
110             return (String[]) call.invoke(new Object[] {  });
111         }
112         catch (Exception ex)
113         {
114             return null;
115         }
116     }
117
118     public static void main (String [] args)
119     {
120       EBIFetchClient ebi = new EBIFetchClient();
121       String[] result = ebi.fetchData("uniprot:25KD_SARPE;G6PD_HUMAN",
122                            "xml", null);
123
124      try{
125        java.io.PrintWriter out = new java.io.PrintWriter(
126       new java.io.FileWriter("out.xml"));
127
128
129        for(int i=0; i<result.length; i++)
130        {
131          out.println(result[i]);
132        }
133        out.close();
134      }catch(Exception ex){ex.printStackTrace();}
135
136     }
137
138
139     public File fetchDataAsFile(String ids, String f, String s)
140     {
141       String [] data = fetchData(ids, f, s);
142       File outFile = null;
143       try{
144         outFile = File.createTempFile("jalview", ".xml");
145         outFile.deleteOnExit();
146         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
147         int index=0;
148         while( index < data.length )
149         {
150           out.println(data[index]);
151           index++;
152         }
153         out.close();
154       }catch(Exception ex){}
155       return outFile;
156     }
157
158     /**
159      * DOCUMENT ME!
160      *
161      * @param ids DOCUMENT ME!
162      * @param f DOCUMENT ME!
163      * @param s DOCUMENT ME!
164      *
165      * @return DOCUMENT ME!
166      */
167     public String[] fetchData(String ids, String f, String s)
168     {
169         // ids should be of the form uniprot:25KD_SARPE;ADHR_DROPS;
170         // max 50 ids can be added at one time
171         try
172         {
173             call.setOperationName(new QName("urn:Dbfetch", "fetchData"));
174             call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN);
175             call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);
176             call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);
177             call.setReturnType(XMLType.SOAP_ARRAY);
178
179             if (f != null)
180             {
181                 format = f;
182             }
183
184             if (s != null)
185             {
186                 style = s;
187             }
188
189             return (String[]) call.invoke(new Object[] { ids, format, style });
190         }
191         catch (Exception ex)
192         {
193             return null;
194         }
195     }
196 }