Package Bio :: Package SwissProt :: Module KeyWList
[hide private]
[frames] | no frames]

Source Code for Module Bio.SwissProt.KeyWList

  1  # Copyright 1999 by Jeffrey Chang.  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  """ 
  7  This module provides code to work with the keywlist.txt file from 
  8  SwissProt. 
  9  http://www.expasy.ch/sprot/sprot-top.html 
 10   
 11   
 12  Classes: 
 13  ListParser        Parses a keywlist.txt file into a list of keywords. 
 14   
 15  _Scanner          Scans the keywlist.txt file. 
 16  _ListConsumer     Consumes keywlist data to a list. 
 17   
 18   
 19  Functions: 
 20  extract_keywords  Return the keywords from a keywlist.txt file. 
 21   
 22  """ 
 23   
 24  from types import * 
 25   
 26  from Bio import File 
 27  from Bio.ParserSupport import * 
 28   
29 -class ListParser(AbstractParser):
30 """Parses keywlist.txt data into a list of keywords. 31 32 """
33 - def __init__(self):
34 self._scanner = _Scanner() 35 self._consumer = _ListConsumer()
36
37 - def parse(self, handle):
38 self._scanner.feed(handle, self._consumer) 39 return self._consumer.keywords
40 41
42 -class _Scanner:
43 """Scan the keywlist.txt file included with the SwissProt distribution. 44 45 Tested with: 46 Release 37 47 Release 38 48 """ 49
50 - def feed(self, handle, consumer):
51 """feed(self, handle, consumer) 52 53 Feed in the keywlist.txt file for scanning. handle is a file-like 54 object that contains keyword information. consumer is a 55 Consumer object that will receive events as the report is scanned. 56 57 """ 58 if isinstance(handle, File.UndoHandle): 59 uhandle = handle 60 else: 61 uhandle = File.UndoHandle(handle) 62 63 self._scan_header(uhandle, consumer) 64 self._scan_keywords(uhandle, consumer) 65 self._scan_footer(uhandle, consumer)
66
67 - def _scan_header(self, uhandle, consumer):
68 consumer.start_header() 69 70 read_and_call(uhandle, consumer.noevent, start='----') 71 read_and_call(uhandle, consumer.noevent, blank=1) 72 read_and_call(uhandle, consumer.noevent, contains="SWISS-PROT") 73 read_and_call(uhandle, consumer.noevent, contains="Release") 74 read_and_call(uhandle, consumer.noevent, blank=1) 75 read_and_call(uhandle, consumer.noevent, start='----') 76 77 read_and_call(uhandle, consumer.noevent, blank=1) 78 read_and_call(uhandle, consumer.noevent, start='List of keywords') 79 read_and_call(uhandle, consumer.noevent, blank=1) 80 read_and_call(uhandle, consumer.noevent, start='----') 81 82 while 1: 83 if attempt_read_and_call(uhandle, consumer.noevent, start='----'): 84 break 85 read_and_call(uhandle, consumer.noevent, blank=0) 86 87 read_and_call(uhandle, consumer.noevent, start='Document name') 88 read_and_call(uhandle, consumer.noevent, start='----') 89 read_and_call(uhandle, consumer.noevent, blank=1) 90 91 consumer.end_header()
92
93 - def _scan_keywords(self, uhandle, consumer):
94 consumer.start_keywords() 95 96 # SwissProt38 starts with lines: 97 # Keyword 98 # ______________________________________ 99 # 100 # Check and see if it's release 38, and parse it. 101 if attempt_read_and_call(uhandle, consumer.noevent, start='Keyword'): 102 read_and_call(uhandle, consumer.noevent, start='____') 103 104 while 1: 105 if not attempt_read_and_call(uhandle, consumer.keyword, blank=0): 106 break 107 read_and_call(uhandle, consumer.noevent, blank=1) 108 109 consumer.end_keywords()
110
121
122 -class _ListConsumer(AbstractConsumer):
123 """Consumer that converts a keywlist.txt file into a list of keywords. 124 125 Members: 126 keywords List of keywords. 127 128 """
129 - def __init__(self):
130 self.keywords = None
131
132 - def start_keywords(self):
133 self.keywords = []
134
135 - def keyword(self, line):
136 self.keywords.append(string.rstrip(line))
137
138 -def extract_keywords(keywlist_handle):
139 """extract_keywords(keywlist_handle) -> list of keywords 140 141 Return the keywords from a keywlist.txt file. 142 143 """ 144 if type(keywlist_handle) is not FileType and \ 145 type(keywlist_handle) is not InstanceType: 146 raise ValueError, "I expected a file handle or file-like object" 147 return ListParser().parse(keywlist_handle)
148