spacer
spacer

ZSI

Install ZSI

The ZSI module is available for download from the Python Web Services Project pages. See the documentation for details of how to install into your Python environment.

If you are using a package based system (e.g. Ubuntu, CentOS, Cygwin, etc.) check the package listing for a ZSI package.

Using ZSI ServiceProxy

ZSI provides an interface to services which provide a WSDL description in a similar way to SOAPpy using the ServiceProxy package. For example using the WSDbfetch service:

Import the ServiceProxy package:

from ZSI.ServiceProxy import ServiceProxy


Create an interface to the service using ServiceProxy passing the URL of the WSDL file describing the service:

wsdlUrl = 'http://www.ebi.ac.uk/ws/services/WSDbfetch?wsdl'
dbfetchSrv = ServiceProxy(wsdl=wsdlUrl)


The service object (dbfetchSrv) provides methods corresponding to the methods described in the WSDL. So to call the fetchData method of the WSDbfetch service we can do something like the following:

resultDict = dbfetchSrv.fetchData(query='UNIPROT:ADH1A_HUMAN', format='fasta', style='raw')
result = resultDict['fetchDataReturn']


The fetchData method call returns a dictionary representing the data structure returned by the server, to extract the string containing the entry we need to look in the fetchDataReturn element which in this example contains the ADH1A_HUMAN entry from UniProtKB in fasta sequence format.

The methods in the WSDbfetch all use simple string parameters. Many of the other EBI services use more complex input structures. For example WSWUBlast requires a structure containing the various parameters and the input sequence to be passed to the runWUBlast method:

# Import WSDL package
from ZSI.ServiceProxy import ServiceProxy
 
# Create service interface
wsdlUrl = 'http://www.ebi.ac.uk/Tools/webservices/wsdl/WSWUBlast.wsdl'
server = ServiceProxy(wsdl=wsdlUrl)
 
# Query sequence
seq = """>Q8E5Q5_STRA3
MKLSKRYRFWQKVIKALGVLALIATLVLVVYLYKLGILNDSNELKDLVHKYEFWGPMIFI
VAQIVQIVFPVIPGGVTTVAGFLIFGPTLGFIYNYIGIIIGSVILFWLVKFYGRKFVLLF
MDQKTFDKYESKLETSGYEKFFIFCMASPISPADIMVMITGLSNMSIKRFVTIIMITKPI
SIIGYSYLWIYGGDILKNFLN"""
 
# Structure containing parameters
blast_params = {
  'program':'blastp',      # Protein vs. protein search
  'database':'swissprot',  # Database to search
  'email':'your@email',    # User e-mail address
  'async':1                # Async submission
}
 
# Structure containing the sequence data
blast_data = [{'type':'sequence', 'content':seq}]
 
# Submit the job to the service passing the data structures
jobIdDict = server.runWUBlast(params=blast_params, content=blast_data)
jobId = jobIdDict['jobid']
print jobId


The runWUBlast method returns a job identifier which can be used with the checkStatus method to get the status of the job (e.g. RUNNING, DONE or ERROR) and the poll method to get the results of the job.

# Poll till job finishes
statusStr = 'PENDING'
while statusStr == 'RUNNING' or statusStr == 'PENDING':
    statusDict = server.checkStatus(jobid=jobId)
    statusStr = statusDict['status']
    print statusStr
    if statusStr == 'RUNNING' or statusStr == 'PENDING':
        time.sleep(15)
 
# Get the result
resultDict = server.poll(jobid=jobId, type='tooloutput')
result = resultDict['result']
print result


Using ZSI wsdl2py stubs

An alternative to using the ServiceProxy approach is to generate client stubs for the service which you can then use in your client. ZSI provides the wsdl2py tool to do this. For example using the WSDbfetch service:

Generate the stubs from the WSDL:

wsdl2py "http://www.ebi.ac.uk/ws/services/WSDbfetch?wsdl"


The creates a set of files containing the code required to interact with the service.

In your client you then import the package:

from WSDBFetchServerService_client import *


Create an instance of the service interface:

dbfetchSrv = WSDBFetchServerServiceLocator().getWSDbfetch()


The service object (dbfetchSrv) provides methods corresponding to the methods described in the WSDL. So to call the fetchData method of the WSDbfetch service we can do something like the following:

# Build the request
request = fetchDataRequest()
request._query = 'UNIPROT:ADH1A_HUMAN'
request._format = 'fasta'
request._style = 'raw'
# Call the service
response = dbfetchSrv.fetchData(request)
# Extract the result
result = response._fetchDataReturn


The fetchData method call returns an object representing the data structure returned by the server, to extract the string containing the entry we need to look in the fetchDataReturn element which in this example contains the ADH1A_HUMAN entry from UniProtKB in fasta sequence format.

The methods in the WSDbfetch all use simple string parameters. Many of the other EBI services use more complex input structures. For example WSWUBlast requires a structure containing the various parameters and the input sequence to be passed to the runWUBlast method:

# Import generated stubs package
from WSWUBlast_services import *
 
# Create service interface
server = WSWUBlastServiceLocator().getWSWUBlast()
 
# Query sequence
seq = """>Q8E5Q5_STRA3
MKLSKRYRFWQKVIKALGVLALIATLVLVVYLYKLGILNDSNELKDLVHKYEFWGPMIFI
VAQIVQIVFPVIPGGVTTVAGFLIFGPTLGFIYNYIGIIIGSVILFWLVKFYGRKFVLLF
MDQKTFDKYESKLETSGYEKFFIFCMASPISPADIMVMITGLSNMSIKRFVTIIMITKPI
SIIGYSYLWIYGGDILKNFLN"""
 
# Structure containing parameters
class inputParams_class:
    _program = 'blastp'      # Protein vs. protein search
    _database = 'swissprot'  # Database to search
    _email = 'your@email'    # User e-mail address
    _async = 1               # Async submission
 
# Structure containing the sequence data
class data_class:
    _type = 'sequence'
    _content = seq
 
# Create the request
request = runWUBlastRequest()
request._params = inputParams_class
request._content = [ data_class ]
 
# Submit the job to the service passing the data structures
response = server.runWUBlast(request)
jobId = response._jobid
print jobId


The runWUBlast method returns a job identifier which can be used with the checkStatus method to get the status of the job (e.g. RUNNING, DONE or ERROR) and the poll method to get the results of the job.

# Poll till job finishes
status = 'PENDING'
while status == 'RUNNING' or status == 'PENDING':
    request = statusRequest()
    request._jobid = jobId
    response = server.checkStatus(request)
    status = response._status
    print status
    if status == 'RUNNING' or status == 'PENDING':
        time.sleep(15)
 
# Get the result
request = pollRequest()
request._jobid = jobId
request._type = 'tooloutput'
response = server.poll(request)
result = response._result
print result


Examples

The complete source code for the examples is available:

Sample Clients

Most SOAP Web Services at EMBL-EBI have sample clients which provide command-line access to the service and example code. For Python some of the clients are based on ZSI.

Document/literal SOAP

RPC/encoded SOAP

Service Sample client
WSDbfetch wsdbfetch_zsi.py


Up Python Contents Contents
 
tutorials/06_programming/python/soap/zsi.txt · Last modified: 2011/07/26 07:41 by hpm
spacer
spacer