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