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

Source Code for Module Bio.ExPASy.Enzyme

  1  # Copyright 1999 by Jeffrey Chang.  All rights reserved. 
  2  # Copyright 2009 by Michiel de Hoon.  All rights reserved. 
  3  # This code is part of the Biopython distribution and governed by its 
  4  # license.  Please see the LICENSE file that should have been included 
  5  # as part of this package. 
  6   
  7  """ 
  8  This module provides code to work with the enzyme.dat file from 
  9  Enzyme. 
 10  http://www.expasy.ch/enzyme/ 
 11   
 12  Tested with the release of 03-Mar-2009. 
 13   
 14  Functions: 
 15   
 16      - read       Reads a file containing one ENZYME entry 
 17      - parse      Reads a file containing multiple ENZYME entries 
 18   
 19  Classes: 
 20   
 21      - Record     Holds ENZYME data. 
 22   
 23  """ 
 24   
 25  __docformat__ = "restructuredtext en" 
 26   
27 -def parse(handle):
28 """Parse ENZYME records. 29 30 This function is for parsing ENZYME files containing multiple 31 records. 32 33 handle - handle to the file.""" 34 35 while True: 36 record = __read(handle) 37 if not record: 38 break 39 yield record
40 41
42 -def read(handle):
43 """Read one ENZYME record. 44 45 This function is for parsing ENZYME files containing 46 exactly one record. 47 48 handle - handle to the file.""" 49 50 record = __read(handle) 51 # We should have reached the end of the record by now 52 remainder = handle.read() 53 if remainder: 54 raise ValueError("More than one ENZYME record found") 55 return record
56 57
58 -class Record(dict):
59 """\ 60 Holds information from an ExPASy ENZYME record as a Python dictionary. 61 62 Each record contains the following keys: 63 - ID: EC number 64 - DE: Recommended name 65 - AN: Alternative names (if any) 66 - CA: Catalytic activity 67 - CF: Cofactors (if any) 68 - PR: Pointers to the Prosite documentation entrie(s) that 69 correspond to the enzyme (if any) 70 - DR: Pointers to the Swiss-Prot protein sequence entrie(s) 71 that correspond to the enzyme (if any) 72 - CC: Comments 73 """ 74
75 - def __init__(self):
76 dict.__init__(self) 77 self["ID"] = '' 78 self["DE"] = '' 79 self["AN"] = [] 80 self["CA"] = '' 81 self["CF"] = '' 82 self["CC"] = [] # one comment per line 83 self["PR"] = [] 84 self["DR"] = []
85
86 - def __repr__(self):
87 if self["ID"]: 88 if self["DE"]: 89 return "%s (%s, %s)" % (self.__class__.__name__, 90 self["ID"], self["DE"]) 91 else: 92 return "%s (%s)" % (self.__class__.__name__, 93 self["ID"]) 94 else: 95 return "%s ( )" % (self.__class__.__name__)
96
97 - def __str__(self):
98 output = "ID: " + self["ID"] 99 output += " DE: " + self["DE"] 100 output += " AN: " + repr(self["AN"]) 101 output += " CA: '" + self["CA"] + "'" 102 output += " CF: " + self["CF"] 103 output += " CC: " + repr(self["CC"]) 104 output += " PR: " + repr(self["PR"]) 105 output += " DR: %d Records" % len(self["DR"]) 106 return output
107 108 # Everything below is private 109 110
111 -def __read(handle):
112 record = None 113 for line in handle: 114 key, value = line[:2], line[5:].rstrip() 115 if key=="ID": 116 record = Record() 117 record["ID"] = value 118 elif key=="DE": 119 record["DE"]+=value 120 elif key=="AN": 121 if record["AN"] and not record["AN"][-1].endswith("."): 122 record["AN"][-1] += " " + value 123 else: 124 record["AN"].append(value) 125 elif key=="CA": 126 record["CA"] += value 127 elif key=="DR": 128 pair_data = value.rstrip(";").split(';') 129 for pair in pair_data: 130 t1, t2 = pair.split(',') 131 row = [t1.strip(), t2.strip()] 132 record["DR"].append(row) 133 elif key=="CF": 134 if record["CF"]: 135 record["CF"] += " " + value 136 else: 137 record["CF"] = value 138 elif key=="PR": 139 assert value.startswith("PROSITE; ") 140 value = value[9:].rstrip(";") 141 record["PR"].append(value) 142 elif key=='CC': 143 if value.startswith("-!- "): 144 record["CC"].append(value[4:]) 145 elif value.startswith(" ") and record["CC"]: 146 record["CC"][-1] += value[3:] 147 # copyright notice is silently skipped 148 elif key=="//": 149 if record: 150 return record 151 else: # This was the copyright notice 152 continue 153 if record: 154 raise ValueError("Unexpected end of stream")
155