Package Bio :: Package ExPASy :: Module ScanProsite
[hide private]
[frames] | no frames]

Source Code for Module Bio.ExPASy.ScanProsite

  1  import urllib 
  2  from xml.sax import handler 
  3  from xml.sax.expatreader import ExpatParser 
  4   
  5   
6 -class Record(list):
7 """\ 8 This record is a list containing the search results returned by 9 ScanProsite. The record also contains the data members n_match, n_seq, 10 capped, and warning.""" 11
12 - def __init__(self):
13 self.n_match = None 14 self.n_seq = None 15 self.capped = None 16 self.warning = None
17 18
19 -def scan(seq="", mirror='http://www.expasy.org', output='xml', **keywords):
20 """Execute a ScanProsite search. 21 22 mirror: The ScanProsite mirror to be used 23 (default: http://www.expasy.org). 24 seq: The query sequence, or UniProtKB (Swiss-Prot, 25 TrEMBL) accession 26 output: Format of the search results 27 (default: xml) 28 29 Further search parameters can be passed as keywords; see the 30 documentation for programmatic access to ScanProsite at 31 http://www.expasy.org/tools/scanprosite/ScanPrositeREST.html 32 for a description of such parameters. 33 34 This function returns a handle to the search results returned by 35 ScanProsite. Search results in the XML format can be parsed into a 36 Python object, by using the Bio.ExPASy.ScanProsite.read function. 37 """ 38 parameters = {'seq': seq, 39 'output': output} 40 for key, value in keywords.iteritems(): 41 if value is not None: 42 parameters[key] = value 43 command = urllib.urlencode(parameters) 44 url = "%s/cgi-bin/prosite/PSScan.cgi?%s" % (mirror, command) 45 handle = urllib.urlopen(url) 46 return handle
47 48
49 -def read(handle):
50 "Parse search results returned by ScanProsite into a Python object" 51 content_handler = ContentHandler() 52 saxparser = Parser() 53 saxparser.setContentHandler(content_handler) 54 saxparser.parse(handle) 55 record = content_handler.record 56 return record
57 58 # The functions below are considered private 59 60
61 -class Parser(ExpatParser):
62
63 - def __init__(self):
64 ExpatParser.__init__(self) 65 self.firsttime = True
66
67 - def feed(self, data, isFinal = 0):
68 # Error messages returned by the ScanProsite server are formatted as 69 # as plain text instead of an XML document. To catch such error 70 # messages, we override the feed method of the Expat parser. 71 # The error message is (hopefully) contained in the data that was just 72 # fed to the parser. 73 if self.firsttime: 74 if data[:5]!="<?xml": 75 raise ValueError(data) 76 self.firsttime = False 77 return ExpatParser.feed(self, data, isFinal)
78 79
80 -class ContentHandler(handler.ContentHandler):
81 integers = ("start", "stop") 82 strings = ("sequence_ac", 83 "sequence_id", 84 "sequence_db", 85 "signature_ac", 86 "level", 87 "level_tag") 88
89 - def __init__(self):
90 self.element = []
91
92 - def startElement(self, name, attrs):
93 self.element.append(name) 94 self.content = "" 95 if self.element==["matchset"]: 96 self.record = Record() 97 self.record.n_match = int(attrs["n_match"]) 98 self.record.n_seq = int(attrs["n_seq"]) 99 elif self.element==["matchset", "match"]: 100 match = {} 101 self.record.append(match)
102
103 - def endElement(self, name):
104 assert name==self.element.pop() 105 name = str(name) 106 if self.element==["matchset", "match"]: 107 match = self.record[-1] 108 if name in ContentHandler.integers: 109 match[name] = int(self.content) 110 elif name in ContentHandler.strings: 111 match[name] = self.content 112 else: 113 # Unknown type, treat it as a string 114 match[name] = self.content
115
116 - def characters(self, content):
117 self.content += content
118