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, make_parser, expatreader 
  3  from xml.sax.expatreader import ExpatParser 
  4  from xml.sax._exceptions import SAXParseException 
  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 -def read(handle):
49 "Parse search results returned by ScanProsite into a Python object" 50 content_handler = ContentHandler() 51 saxparser = Parser() 52 saxparser.setContentHandler(content_handler) 53 saxparser.parse(handle) 54 record = content_handler.record 55 return record
56 57 # The functions below are considered private 58
59 -class Parser(ExpatParser):
60
61 - def __init__(self):
62 ExpatParser.__init__(self) 63 self.firsttime = True
64
65 - def feed(self, data, isFinal = 0):
66 # Error messages returned by the ScanProsite server are formatted as 67 # as plain text instead of an XML document. To catch such error 68 # messages, we override the feed method of the Expat parser. 69 # The error message is (hopefully) contained in the data that was just 70 # fed to the parser. 71 if self.firsttime: 72 if data[:5]!="<?xml": 73 raise ValueError, data 74 self.firsttime = False 75 return ExpatParser.feed(self, data, isFinal)
76 77
78 -class ContentHandler(handler.ContentHandler):
79 integers = ("start", "stop") 80 strings = ("sequence_ac", 81 "sequence_id", 82 "sequence_db", 83 "signature_ac", 84 "level", 85 "level_tag")
86 - def __init__(self):
87 self.element = []
88 - def startElement(self, name, attrs):
89 self.element.append(name) 90 self.content = "" 91 if self.element==["matchset"]: 92 self.record = Record() 93 self.record.n_match = int(attrs["n_match"]) 94 self.record.n_seq = int(attrs["n_seq"]) 95 elif self.element==["matchset", "match"]: 96 match = {} 97 self.record.append(match)
98 - def endElement(self, name):
99 assert name==self.element.pop() 100 name = str(name) 101 if self.element==["matchset", "match"]: 102 match = self.record[-1] 103 if name in ContentHandler.integers: 104 match[name] = int(self.content) 105 elif name in ContentHandler.strings: 106 match[name] = self.content 107 else: 108 # Unknown type, treat it as a string 109 match[name] = self.content
110 - def characters(self, content):
111 self.content += content
112