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