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   
28 -def parse(handle):
29 """Parse ENZYME records. 30 31 This function is for parsing ENZYME files containing multiple 32 records. 33 34 handle - handle to the file.""" 35 36 while True: 37 record = __read(handle) 38 if not record: 39 break 40 yield record
41 42
43 -def read(handle):
44 """Read one ENZYME record. 45 46 This function is for parsing ENZYME files containing 47 exactly one record. 48 49 handle - handle to the file.""" 50 51 record = __read(handle) 52 # We should have reached the end of the record by now 53 remainder = handle.read() 54 if remainder: 55 raise ValueError("More than one ENZYME record found") 56 return record
57 58
59 -class Record(dict):
60 """\ 61 Holds information from an ExPASy ENZYME record as a Python dictionary. 62 63 Each record contains the following keys: 64 - ID: EC number 65 - DE: Recommended name 66 - AN: Alternative names (if any) 67 - CA: Catalytic activity 68 - CF: Cofactors (if any) 69 - PR: Pointers to the Prosite documentation entrie(s) that 70 correspond to the enzyme (if any) 71 - DR: Pointers to the Swiss-Prot protein sequence entrie(s) 72 that correspond to the enzyme (if any) 73 - CC: Comments 74 """ 75
76 - def __init__(self):
77 dict.__init__(self) 78 self["ID"] = '' 79 self["DE"] = '' 80 self["AN"] = [] 81 self["CA"] = '' 82 self["CF"] = '' 83 self["CC"] = [] # one comment per line 84 self["PR"] = [] 85 self["DR"] = []
86
87 - def __repr__(self):
88 if self["ID"]: 89 if self["DE"]: 90 return "%s (%s, %s)" % (self.__class__.__name__, 91 self["ID"], self["DE"]) 92 else: 93 return "%s (%s)" % (self.__class__.__name__, 94 self["ID"]) 95 else: 96 return "%s ( )" % (self.__class__.__name__)
97
98 - def __str__(self):
99 output = "ID: " + self["ID"] 100 output += " DE: " + self["DE"] 101 output += " AN: " + repr(self["AN"]) 102 output += " CA: '" + self["CA"] + "'" 103 output += " CF: " + self["CF"] 104 output += " CC: " + repr(self["CC"]) 105 output += " PR: " + repr(self["PR"]) 106 output += " DR: %d Records" % len(self["DR"]) 107 return output
108 109 # Everything below is private 110 111
112 -def __read(handle):
113 record = None 114 for line in handle: 115 key, value = line[:2], line[5:].rstrip() 116 if key == "ID": 117 record = Record() 118 record["ID"] = value 119 elif key == "DE": 120 record["DE"] += value 121 elif key == "AN": 122 if record["AN"] and not record["AN"][-1].endswith("."): 123 record["AN"][-1] += " " + value 124 else: 125 record["AN"].append(value) 126 elif key == "CA": 127 record["CA"] += value 128 elif key == "DR": 129 pair_data = value.rstrip(";").split(';') 130 for pair in pair_data: 131 t1, t2 = pair.split(',') 132 row = [t1.strip(), t2.strip()] 133 record["DR"].append(row) 134 elif key == "CF": 135 if record["CF"]: 136 record["CF"] += " " + value 137 else: 138 record["CF"] = value 139 elif key == "PR": 140 assert value.startswith("PROSITE; ") 141 value = value[9:].rstrip(";") 142 record["PR"].append(value) 143 elif key == 'CC': 144 if value.startswith("-!- "): 145 record["CC"].append(value[4:]) 146 elif value.startswith(" ") and record["CC"]: 147 record["CC"][-1] += value[3:] 148 # copyright notice is silently skipped 149 elif key == "//": 150 if record: 151 return record 152 else: # This was the copyright notice 153 continue 154 if record: 155 raise ValueError("Unexpected end of stream")
156