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  Record            Stores the information about one keyword or one category 
 14                    in the keywlist.txt file. 
 15   
 16  Functions: 
 17  parse             Parses the keywlist.txt file and returns an iterator to 
 18                    the records it contains. 
 19   
 20  DEPRECATED: 
 21   
 22  Classes: 
 23  ListParser        Parses a keywlist.txt file into a list of keywords. 
 24   
 25  _Scanner          Scans the keywlist.txt file. 
 26  _ListConsumer     Consumes keywlist data to a list. 
 27   
 28   
 29  Functions: 
 30  extract_keywords  Return the keywords from a keywlist.txt file. 
 31   
 32  """ 
 33   
 34   
35 -class Record(dict):
36 """ 37 This record stores the information of one keyword or category in the 38 keywlist.txt as a Python dictionary. The keys in this dictionary are 39 the line codes that can appear in the keywlist.txt file: 40 41 --------- --------------------------- ---------------------- 42 Line code Content Occurrence in an entry 43 --------- --------------------------- ---------------------- 44 ID Identifier (keyword) Once; starts a keyword entry 45 IC Identifier (category) Once; starts a category entry 46 AC Accession (KW-xxxx) Once 47 DE Definition Once or more 48 SY Synonyms Optional; once or more 49 GO Gene ontology (GO) mapping Optional; once or more 50 HI Hierarchy Optional; once or more 51 WW Relevant WWW site Optional; once or more 52 CA Category Once per keyword entry; absent 53 in category entries 54 """
55 - def __init__(self):
56 dict.__init__(self) 57 for keyword in ("DE", "SY", "GO", "HI", "WW"): 58 self[keyword] = []
59
60 -def parse(handle):
61 # First, skip the header 62 for line in handle: 63 if line.startswith("______________________________________"): 64 break 65 # Now parse the records 66 record = Record() 67 for line in handle: 68 if line.startswith("-------------------------------------"): 69 # We have reached the footer 70 break 71 key = line[:2] 72 if key=="//": 73 record["DE"] = " ".join(record["DE"]) 74 record["SY"] = " ".join(record["SY"]) 75 yield record 76 record = Record() 77 else: 78 value = line[5:].strip() 79 if key in ("ID", "IC", "AC", "CA"): 80 record[key] = value 81 elif key in ("DE", "SY", "GO", "HI", "WW"): 82 record[key].append(value) 83 # Read the footer and throw it away 84 for line in handle: 85 pass
86 87 88 # Everything below is deprecated. 89 90 from types import * 91 92 from Bio import File 93 from Bio.ParserSupport import * 94
95 -class ListParser(AbstractParser):
96 """Parses keywlist.txt data into a list of keywords. 97 98 """
99 - def __init__(self):
100 import warnings 101 warnings.warn("Bio.SwissProt.KeyWList.ListParser is deprecated. Please use the function Bio.SwissProt.KeyWList.parse instead to parse the keywlist.txt file. In case of any problems, please contact the Biopython developers (biopython-dev@biopython.org).", 102 DeprecationWarning) 103 self._scanner = _Scanner() 104 self._consumer = _ListConsumer()
105
106 - def parse(self, handle):
107 self._scanner.feed(handle, self._consumer) 108 return self._consumer.keywords
109 110
111 -class _Scanner:
112 """Scan the keywlist.txt file included with the SwissProt distribution. 113 114 Tested with: 115 Release 37 116 Release 38 117 """ 118
119 - def __init__(self):
120 import warnings 121 warnings.warn("Bio.SwissProt.KeyWList._Scanner is deprecated. Please use the function Bio.SwissProt.KeyWList.parse instead to parse the keywlist.txt file. In case of any problems, please contact the Biopython developers (biopython-dev@biopython.org).", 122 DeprecationWarning)
123
124 - def feed(self, handle, consumer):
125 """feed(self, handle, consumer) 126 127 Feed in the keywlist.txt file for scanning. handle is a file-like 128 object that contains keyword information. consumer is a 129 Consumer object that will receive events as the report is scanned. 130 131 """ 132 if isinstance(handle, File.UndoHandle): 133 uhandle = handle 134 else: 135 uhandle = File.UndoHandle(handle) 136 137 self._scan_header(uhandle, consumer) 138 self._scan_keywords(uhandle, consumer) 139 self._scan_footer(uhandle, consumer)
140
141 - def _scan_header(self, uhandle, consumer):
142 consumer.start_header() 143 144 read_and_call(uhandle, consumer.noevent, start='----') 145 read_and_call(uhandle, consumer.noevent, blank=1) 146 read_and_call(uhandle, consumer.noevent, contains="SWISS-PROT") 147 read_and_call(uhandle, consumer.noevent, contains="Release") 148 read_and_call(uhandle, consumer.noevent, blank=1) 149 read_and_call(uhandle, consumer.noevent, start='----') 150 151 read_and_call(uhandle, consumer.noevent, blank=1) 152 read_and_call(uhandle, consumer.noevent, start='List of keywords') 153 read_and_call(uhandle, consumer.noevent, blank=1) 154 read_and_call(uhandle, consumer.noevent, start='----') 155 156 while 1: 157 if attempt_read_and_call(uhandle, consumer.noevent, start='----'): 158 break 159 read_and_call(uhandle, consumer.noevent, blank=0) 160 161 read_and_call(uhandle, consumer.noevent, start='Document name') 162 read_and_call(uhandle, consumer.noevent, start='----') 163 read_and_call(uhandle, consumer.noevent, blank=1) 164 165 consumer.end_header()
166
167 - def _scan_keywords(self, uhandle, consumer):
168 consumer.start_keywords() 169 170 # SwissProt38 starts with lines: 171 # Keyword 172 # ______________________________________ 173 # 174 # Check and see if it's release 38, and parse it. 175 if attempt_read_and_call(uhandle, consumer.noevent, start='Keyword'): 176 read_and_call(uhandle, consumer.noevent, start='____') 177 178 while 1: 179 if not attempt_read_and_call(uhandle, consumer.keyword, blank=0): 180 break 181 read_and_call(uhandle, consumer.noevent, blank=1) 182 183 consumer.end_keywords()
184
195
196 -class _ListConsumer(AbstractConsumer):
197 """Consumer that converts a keywlist.txt file into a list of keywords. 198 199 Members: 200 keywords List of keywords. 201 202 """
203 - def __init__(self):
204 import warnings 205 warnings.warn("Bio.SwissProt.KeyWList._ListConsumer is deprecated. Please use the function Bio.SwissProt.KeyWList.parse instead to parse the keywlist.txt file. In case of any problems, please contact the Biopython developers (biopython-dev@biopython.org).", 206 DeprecationWarning) 207 self.keywords = None
208
209 - def start_keywords(self):
210 self.keywords = []
211
212 - def keyword(self, line):
213 self.keywords.append(string.rstrip(line))
214
215 -def extract_keywords(keywlist_handle):
216 """extract_keywords(keywlist_handle) -> list of keywords 217 218 Return the keywords from a keywlist.txt file. 219 220 """ 221 import warnings 222 warnings.warn("Bio.SwissProt.KeyWList.extract_keywords is deprecated. Please use the function Bio.SwissProt.KeyWList.parse instead to parse the keywlist.txt file. In case of any problems, please contact the Biopython developers (biopython-dev@biopython.org).", 223 DeprecationWarning) 224 if type(keywlist_handle) is not FileType and \ 225 type(keywlist_handle) is not InstanceType: 226 raise ValueError, "I expected a file handle or file-like object" 227 return ListParser().parse(keywlist_handle)
228