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