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