1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import urllib2
import sys
from xml.dom.ext.reader import Sax2
from xml.dom.NodeFilter import NodeFilter
from xml import xpath
# ------------------ FUNCTIONS ------------------
class PsicquicService:
def __init__(self, name, restUrl):
self.name = name
self.restUrl = restUrl
def readURL(url):
try:
fileHandle = urllib2.urlopen(url)
content = fileHandle.read()
fileHandle.close()
except IOError:
print 'Cannot open URL' % url
content = ''
return content
def readActiveServicesFromRegistry():
registryActiveUrl = 'http://www.ebi.ac.uk/Tools/webservices/psicquic/registry/registry?action=ACTIVE&format=xml'
content = readURL(registryActiveUrl)
###################C'est à cet endroit là que ça merde. Il compile la fonction mais quand je la lance avec les commandes du bas, ça merde
reader = Sax2.Reader()
doc = reader.fromString(content)
serviceNodes = xpath.Evaluate('service', doc.documentElement)
services = []
for serviceNode in serviceNodes:
name = serviceNode.getElementsByTagName('name')[0].firstChild.nodeValue
restUrl = serviceNode.getElementsByTagName('restUrl')[0].firstChild.nodeValue
service = PsicquicService(name, restUrl)
services.append(service)
return services
def getXrefByDatabase(line, database):
fields = line.split('|')
for field in fields:
parts = field.split(':')
db = parts[0]
value = parts[1].split('(')[0]
if database == db:
return value
else:
# if no db found, return the first field
return fields[0]
def queryPsicquic(psicquicRestUrl, query, offset, maxResults):
psicquicUrl = psicquicRestUrl + 'query/' + query + '?firstResult=' + str(offset) + '&maxResults=' + str(maxResults);
print '\t\tURL: ' + psicquicUrl
psicquicResultLines = readURL(psicquicUrl).splitlines()
for line in psicquicResultLines:
cols = line.split('\t')
print '\t' + getXrefByDatabase(cols[0], 'uniprotkb') + ' interacts with ' + getXrefByDatabase(cols[1], 'uniprotkb')
# -----------------------------------------------------
query = 'BBC1'
services = readActiveServicesFromRegistry()
for service in services:
print 'Service: ' + service.name + ' ================================================================== '
queryPsicquic(service.restUrl, query, 0, 200)
print '\n' |
Partager