X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FEBIFetchClient.java;h=b71e1521828444bcd08d3f7af469fb39c0d39710;hb=7bc226b58110fa26d9dbd3f0c78095d06909ffc3;hp=3c6e5f9050109f86badd33815603eada6e06b6e4;hpb=99c58ee0ae2a848f982552e53feaf6d5cb9925e5;p=jalview.git diff --git a/src/jalview/io/EBIFetchClient.java b/src/jalview/io/EBIFetchClient.java index 3c6e5f9..b71e152 100755 --- a/src/jalview/io/EBIFetchClient.java +++ b/src/jalview/io/EBIFetchClient.java @@ -1,181 +1,259 @@ -/* -* Jalview - A Sequence Alignment Editor and Viewer -* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -*/ - -package jalview.io; - -import org.apache.axis.client.*; - -import java.io.*; - -import javax.activation.DataHandler; -import org.apache.axis.AxisFault; -import org.apache.axis.encoding.XMLType; -import javax.xml.rpc.ParameterMode; -import javax.xml.namespace.QName; -import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory; -import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory; - - -public class EBIFetchClient -{ - Call call; - String format = "default"; - String style = "raw"; - - public EBIFetchClient() - { - try{ - call = (Call)new Service().createCall(); - call.setTargetEndpointAddress(new java.net.URL( - "http://www.ebi.ac.uk/ws/services/Dbfetch")); - }catch(Exception ex){} - } - - - public String [] getSupportedDBs() - { - try{ - - call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs")); - call.setReturnType(XMLType.SOAP_ARRAY); - return (String[]) call.invoke(new Object[]{}); - - }catch(Exception ex){return null;} - } - - public String [] getSupportedFormats() - { - try{ - call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats")); - call.setReturnType(XMLType.SOAP_ARRAY); - return (String[]) call.invoke(new Object[] {}); - - }catch(Exception ex){ return null; } - } - - public String [] getSupportedStyles() - { - try{ - call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles")); - call.setReturnType(XMLType.SOAP_ARRAY); - return (String[]) call.invoke(new Object[] {}); - - }catch(Exception ex){return null;} - } - - public String [] fetchData(String ids, String f, String s) - { - // ids should be of the form uniprot:25KD_SARPE;ADHR_DROPS; - // max 50 ids can be added at one time - try{ - call.setOperationName(new QName("urn:Dbfetch", "fetchData")); - call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN); - call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN); - call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN); - call.setReturnType(XMLType.SOAP_ARRAY); - - if (f != null) - format = f; - - if (s != null) - style = s; - - return (String[]) call.invoke(new Object[] {ids, format, style}); - - }catch(Exception ex){return null;} - - } - - public String fetchDataFile(String arg, String f, String s) - { - - if (f != null) - format = f; - - if (s != null) - style = s; - - - call.setOperationName(new QName("urn:Dbfetch", "fetchDataFile")); - call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN); - call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN); - call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN); - - QName qnameAttachment = new QName("urn:Dbfetch", "DataHandler"); - call.registerTypeMapping(javax.activation.DataSource.class, - qnameAttachment, - JAFDataHandlerSerializerFactory.class, - JAFDataHandlerDeserializerFactory.class); - - call.setReturnType(qnameAttachment); - - try{ - Object ret = call.invoke(new Object[] - {arg, format, style}); - - if (null == ret) - { - System.err.println("Received null "); - throw new AxisFault("", "Received null", null, null); - } - if (ret instanceof String) - { - System.err.println("Received problem response from server: " + ret); - throw new AxisFault("", (String) ret, null, null); - } - if (! (ret instanceof DataHandler)) - { - //The wrong type of object that what was expected. - System.err.println("Received problem response from server:" + - ret.getClass().getName()); - throw new AxisFault("", "Received problem response from server:" + - ret.getClass().getName(), null, null); - - } - //Still here, so far so good. - DataHandler rdh = (DataHandler) ret; - - //From here we'll just treat the data resource as file. - String receivedfileName = rdh.getName(); //Get the filename. - - if (receivedfileName == null) - { - System.err.println("Could not get the file name."); - throw new AxisFault("", "Could not get the file name.", null, null); - } - if (arg.equalsIgnoreCase("medline")) - { - return receivedfileName; - } - else if (arg.equalsIgnoreCase("interpro")) - { - return receivedfileName; - } - else - System.err.println(receivedfileName); - }catch(Exception ex) - { - ex.printStackTrace(); - } - - return "ERROR"; - } - - -} +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.io; + +import java.io.*; +import java.util.*; +import javax.xml.namespace.*; +import javax.xml.rpc.*; + +import org.apache.axis.client.Call; +import org.apache.axis.client.Service; +import org.apache.axis.encoding.*; + +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class EBIFetchClient +{ + Call call; + String format = "default"; + String style = "raw"; + + /** + * Creates a new EBIFetchClient object. + */ + public EBIFetchClient() + { + try + { + call = (Call)new Service().createCall(); + call.setTargetEndpointAddress(new java.net.URL( + "http://www.ebi.ac.uk/ws/services/Dbfetch")); + } + catch (Exception ex) + { + } + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String[] getSupportedDBs() + { + try + { + call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs")); + call.setReturnType(XMLType.SOAP_ARRAY); + + return (String[]) call.invoke(new Object[] + {}); + } + catch (Exception ex) + { + return null; + } + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String[] getSupportedFormats() + { + try + { + call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats")); + call.setReturnType(XMLType.SOAP_ARRAY); + + return (String[]) call.invoke(new Object[] + {}); + } + catch (Exception ex) + { + return null; + } + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String[] getSupportedStyles() + { + try + { + call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles")); + call.setReturnType(XMLType.SOAP_ARRAY); + + return (String[]) call.invoke(new Object[] + {}); + } + catch (Exception ex) + { + return null; + } + } + + public static void main(String[] args) + { + EBIFetchClient ebi = new EBIFetchClient(); + String[] result = ebi.fetchData("uniprot:25KD_SARPE;G6PD_HUMAN", + "xml", null); + + try + { + java.io.PrintWriter out = new java.io.PrintWriter( + new java.io.FileWriter("out.xml")); + + for (int i = 0; i < result.length; i++) + { + out.println(result[i]); + } + out.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + } + + public File fetchDataAsFile(String ids, String f, String s) + { + String[] data = fetchData(ids, f, s); + File outFile = null; + try + { + outFile = File.createTempFile("jalview", ".xml"); + outFile.deleteOnExit(); + PrintWriter out = new PrintWriter(new FileOutputStream(outFile)); + int index = 0; + while (index < data.length) + { + out.println(data[index]); + index++; + } + out.close(); + } + catch (Exception ex) + {} + return outFile; + } + + /** + * Single DB multiple record retrieval + * + * @param ids db:query1;query2;query3 + * @param f raw/xml + * @param s ? + * + * @return Raw string array result of query set + */ + public String[] fetchData(String ids, String f, String s) + { + // Need to split + // ids of the form uniprot:25KD_SARPE;ADHR_DROPS; + + StringTokenizer queries = new StringTokenizer(ids, ";"); + String db = null; + StringBuffer querystring = null; + while (queries.hasMoreTokens()) + { + String query = queries.nextToken(); + int p; + if ( (p = query.indexOf(':')) > -1 && (p + 3 < query.length())) + { + db = query.substring(0, p); + query = query.substring(p + 1); + } + if (querystring == null) + { + querystring = new StringBuffer(query); + } + else + { + querystring.append("," + query); + } + } + if (db == null) + { + System.err.println("Invalid Query string : '" + ids + + "'\nShould be of form 'dbname:q1;q2;q3;q4'"); + } + return fetchBatch(querystring.toString(), db, f, s); + } + + public String[] fetchBatch(String ids, String db, String f, String s) + { + // max 50 ids can be added at one time + try + { + //call.setOperationName(new QName("urn:Dbfetch", "fetchData")); + call.setOperationName(new QName("urn:Dbfetch", "fetchBatch")); + call.addParameter("ids", XMLType.XSD_STRING, ParameterMode.IN); + call.addParameter("db", XMLType.XSD_STRING, ParameterMode.IN); + call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN); + call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN); + call.setReturnType(XMLType.SOAP_ARRAY); + + if (f != null) + { + format = f; + } + + if (s != null) + { + style = s; + } + + try + { + return (String[]) call.invoke(new Object[] + {ids.toLowerCase(), db.toLowerCase(), + format, style}); + } + catch (OutOfMemoryError er) + { + System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db + ":\n" + + ids); + } + return null; + } + catch (Exception ex) + { + if (ex.getMessage().startsWith( + "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException")) + { + return null; + } + System.err.println("Unexpected exception when retrieving from " + db + + "\nQuery was : '" + ids + "'"); + ex.printStackTrace(System.err); + return null; + } + } +}