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