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

Source Code for Module Bio.ExPASy.ScanProsite

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