Package Bio :: Package Enzyme
[hide private]
[frames] | no frames]

Source Code for Package Bio.Enzyme

  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  """Module to work with enzyme.dat file (DEPRECATED). 
  7   
  8  This module provides code to work with the enzyme.dat file from 
  9  Enzyme (OBSOLETE as of Biopython version 1.50). 
 10  http://www.expasy.ch/enzyme/ 
 11   
 12  The functionality of Bio.Enzyme has moved to Bio.ExPASy.ExPASy; 
 13  please use that module instead of Bio.Enzyme. Bio.Enzyme is now 
 14  deprecated and will be removed in a future release of Biopython. 
 15  """ 
 16   
 17  import warnings 
 18  import Bio 
 19  warnings.warn("Bio.Enzyme is deprecated, and will be removed in a"\ 
 20                " future release of Biopython. Most of the functionality " 
 21                " is now provided by Bio.ExPASy.Enzyme.  If you want to " 
 22                " continue to use Bio.Enzyme, please get in contact " 
 23                " via the mailing lists to avoid its permanent removal from"\ 
 24                " Biopython.", Bio.BiopythonDeprecationWarning) 
 25   
 26  from Bio import File 
 27  from Bio.ParserSupport import * 
 28   
29 -class _Scanner:
30 """Scans Enzyme data (PRIVATE). 31 32 Tested with: 33 Release 33 34 """ 35
36 - def feed(self, handle, consumer):
37 """feed(self, handle, consumer) 38 39 Feed in Enzyme data for scanning. handle is a file-like object 40 that contains keyword information. consumer is a Consumer 41 object that will receive events as the report is scanned. 42 43 """ 44 if isinstance(handle, File.UndoHandle): 45 uhandle = handle 46 else: 47 uhandle = File.UndoHandle(handle) 48 49 while not is_blank_line(uhandle.peekline()): # Am I done yet? 50 self._scan_record(uhandle, consumer)
51
52 - def _scan_record(self, uhandle, consumer):
53 # The first record is just copyright information embedded in 54 # comments. Check to see if I'm at the first record. If so, 55 # then just scan the comments and the terminator. 56 consumer.start_record() 57 line = uhandle.peekline() 58 if line[:2] == 'CC': 59 self._scan_cc(uhandle, consumer) 60 self._scan_terminator(uhandle, consumer) 61 else: 62 for fn in self._scan_fns: 63 fn(self, uhandle, consumer) 64 consumer.end_record()
65
66 - def _scan_line(self, line_type, uhandle, event_fn, 67 exactly_one=None, one_or_more=None, any_number=None, 68 up_to_one=None):
69 # Callers must set exactly one of exactly_one, one_or_more, or 70 # any_number to a true value. I do not explicitly check to 71 # make sure this function is called correctly. 72 73 # This does not guarantee any parameter safety, but I 74 # like the readability. The other strategy I tried was have 75 # parameters min_lines, max_lines. 76 77 if exactly_one or one_or_more: 78 read_and_call(uhandle, event_fn, start=line_type) 79 if one_or_more or any_number: 80 while 1: 81 if not attempt_read_and_call(uhandle, event_fn, 82 start=line_type): 83 break 84 if up_to_one: 85 attempt_read_and_call(uhandle, event_fn, start=line_type)
86
87 - def _scan_id(self, uhandle, consumer):
88 self._scan_line('ID', uhandle, consumer.identification, exactly_one=1)
89
90 - def _scan_de(self, uhandle, consumer):
91 self._scan_line('DE', uhandle, consumer.description, one_or_more=1)
92
93 - def _scan_an(self, uhandle, consumer):
94 self._scan_line('AN', uhandle, consumer.alternate_name, any_number=1)
95
96 - def _scan_ca(self, uhandle, consumer):
97 self._scan_line('CA', uhandle, consumer.catalytic_activity, 98 any_number=1)
99
100 - def _scan_cf(self, uhandle, consumer):
101 self._scan_line('CF', uhandle, consumer.cofactor, any_number=1)
102
103 - def _scan_cc(self, uhandle, consumer):
104 self._scan_line('CC', uhandle, consumer.comment, any_number=1)
105
106 - def _scan_di(self, uhandle, consumer):
107 self._scan_line('DI', uhandle, consumer.disease, any_number=1)
108
109 - def _scan_pr(self, uhandle, consumer):
110 self._scan_line('PR', uhandle, consumer.prosite_reference, 111 any_number=1)
112
113 - def _scan_dr(self, uhandle, consumer):
114 self._scan_line('DR', uhandle, consumer.databank_reference, 115 any_number=1)
116
117 - def _scan_terminator(self, uhandle, consumer):
118 self._scan_line('//', uhandle, consumer.terminator, exactly_one=1)
119 120 _scan_fns = [ 121 _scan_id, 122 _scan_de, 123 _scan_an, 124 _scan_ca, 125 _scan_cf, 126 _scan_cc, 127 _scan_di, 128 _scan_pr, 129 _scan_dr, 130 _scan_terminator 131 ]
132 -class DataRecord:
133 - def __init__(self,tr_code='',sw_code=''):
134 self.tr_code = tr_code 135 self.sw_code = sw_code
136
137 - def __str__(self):
138 return self.tr_code + ", " + self.sw_code
139
140 -class EnzymeRecord:
141 - def __init__(self):
142 self.ID = '' 143 self.DE = [] 144 self.AN = [] 145 self.CA = '' 146 self.CF = [] 147 self.CC = [] # one comment per line 148 self.DI = [] 149 self.PR = [] 150 self.DR = []
151
152 - def __repr__(self):
153 if self.ID: 154 if self.DE: 155 return "%s (%s, %s)" % (self.__class__.__name__, 156 self.ID, self.DE[0]) 157 else: 158 return "%s (%s)" % (self.__class__.__name__, 159 self.ID) 160 else: 161 return "%s ( )" % (self.__class__.__name__)
162
163 - def __str__(self):
164 output = "ID: " + self.ID 165 output += " DE: " + repr(self.DE) 166 output += " AN: " + repr(self.AN) 167 output += " CA: '" + self.CA + "'" 168 output += " CF: " + repr(self.CF) 169 output += " CC: " + repr(self.CC) 170 output += " DI: " + repr(self.DI) 171 output += " PR: " + repr(self.PR) 172 output += " DR: %d Records" % len(self.DR) 173 174 return output
175
176 -class RecordParser(AbstractParser):
177 - def __init__(self):
178 self._scanner = _Scanner() 179 self._consumer = _RecordConsumer()
180
181 - def parse(self, handle):
182 if isinstance(handle, File.UndoHandle): 183 uhandle = handle 184 else: 185 uhandle = File.UndoHandle(handle) 186 self._scanner.feed(uhandle, self._consumer) 187 return self._consumer.enzyme_record
188
189 -class Iterator:
190 - def __init__(self, handle, parser=None):
191 self._uhandle = File.UndoHandle(handle)
192
193 - def next(self):
194 self._parser = RecordParser() 195 lines = [] 196 while True: 197 line = self._uhandle.readline() 198 if not line: break 199 if line[:2] == '//': 200 break 201 lines.append(line) 202 if not lines: 203 return None 204 lines.append('//') 205 data = ''.join(lines) 206 if self._parser is not None: 207 return self._parser.parse(File.StringHandle(data)) 208 return data
209
210 - def __iter__(self):
211 return iter(self.next, None)
212
213 -class _RecordConsumer(AbstractConsumer):
214 - def __init__(self):
215 self.enzyme_record = EnzymeRecord()
216 - def identification(self, id_info):
217 self.enzyme_record.ID = id_info.split()[1]
218 - def description(self,de_info):
219 self.enzyme_record.DE.append(de_info[2:].strip())
220 - def alternate_name(self,an_info):
221 self.enzyme_record.AN.append(an_info[2:].strip())
222 - def catalytic_activity(self, ca_info):
223 self.enzyme_record.CA = ''.join([self.enzyme_record.CA, ca_info[2:].strip()])
224 - def cofactor(self, cf_info):
225 self.enzyme_record.CF.append(cf_info[2:].strip())
226 - def comment(self, cc_info):
227 cc = cc_info[2:].strip() 228 if cc.startswith("-!-"): 229 self.enzyme_record.CC.append(cc[len("-!-"):].strip()) 230 else: 231 # The header is all CC, but doesn't start with -!- 232 if self.enzyme_record.CC: 233 pre_cc = self.enzyme_record.CC.pop() 234 else: 235 pre_cc = "" 236 new_cc = pre_cc + " " + cc 237 self.enzyme_record.CC.append(new_cc)
238 - def disease(self, di_info):
239 self.enzyme_record.DI.append(di_info[2:].strip())
240
241 - def prosite_reference(self,pr_info):
242 self.enzyme_record.PR.append(pr_info.split(';')[1].strip())
243
244 - def databank_reference(self,dr_info):
245 good_data = dr_info[2:].strip() 246 pair_data = good_data.split(';') 247 for pair in pair_data: 248 if not pair: continue 249 data_record = DataRecord() 250 t1, t2 = pair.split(',') 251 data_record.tr_code, data_record.sw_code = \ 252 t1.strip(), t2.strip() 253 self.enzyme_record.DR.append(data_record)
254
255 - def terminator(self,schwarzenegger):
256 pass # Hasta la Vista, baby!
257