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