Package Bio :: Package KEGG :: Package Compound
[hide private]
[frames] | no frames]

Source Code for Package Bio.KEGG.Compound

  1  # Copyright 2001 by Tarjei Mikkelsen.  All rights reserved. 
  2  # Copyright 2007 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 KEGG Ligand/Compound database. 
  9   
 10   
 11  Classes: 
 12  Record 
 13  Iterator 
 14  """ 
 15   
 16  # other Biopython stuff 
 17  from Bio.KEGG import _write_kegg 
 18  from Bio.KEGG import _wrap_kegg 
 19   
 20   
 21  # Set up line wrapping rules (see Bio.KEGG._wrap_kegg) 
 22  name_wrap = [0, "", 
 23               (" ","$",1,1), 
 24               ("-","$",1,1)] 
 25  id_wrap = lambda indent : [indent, "", 
 26                             (" ","",1,0)] 
 27  struct_wrap = lambda indent : [indent, "", 
 28                                 ("  ","",1,1)] 
 29   
30 -class Record:
31 """Holds info from a KEGG Ligand/Compound record. 32 33 Members: 34 entry The entry identifier. 35 name A list of the compund names. 36 formula The chemical formula for the compound 37 mass The molecular weight for the compound 38 pathway A list of 3-tuples: (database, id, pathway) 39 enzyme A list of 2-tuples: (enzyme id, role) 40 structures A list of 2-tuples: (database, list of struct ids) 41 dblinks A list of 2-tuples: (database, list of link ids) 42 43 """
44 - def __init__(self):
45 """__init___(self) 46 47 Create a new Record. 48 """ 49 self.entry = "" 50 self.name = [] 51 self.formula = "" 52 self.mass = "" 53 self.pathway = [] 54 self.enzyme = [] 55 self.structures = [] 56 self.dblinks = []
57 - def __str__(self):
58 """__str__(self) 59 60 Returns a string representation of this Record. 61 """ 62 return self._entry() + \ 63 self._name() + \ 64 self._formula() + \ 65 self._mass() + \ 66 self._pathway() + \ 67 self._enzyme() + \ 68 self._structures() + \ 69 self._dblinks() + \ 70 "///"
71 - def _entry(self):
72 return _write_kegg("ENTRY", 73 [self.entry])
74 - def _name(self):
75 return _write_kegg("NAME", 76 map(lambda l: 77 _wrap_kegg(l, wrap_rule = name_wrap), 78 self.name))
79 - def _formula(self):
80 return _write_kegg("FORMULA", 81 [self.formula])
82
83 - def _mass(self):
84 return _write_kegg("MASS", 85 [self.mass])
86
87 - def _pathway(self):
88 s = [] 89 for entry in self.pathway: 90 s.append(entry[0] + ": " + entry[1] + " " + entry[2]) 91 return _write_kegg("PATHWAY", 92 [_wrap_kegg(l, wrap_rule = id_wrap(16)) \ 93 for l in s])
94 - def _enzyme(self):
95 s = "" 96 for entry in self.enzyme: 97 if entry[1]: 98 t = entry[0] + " (" + entry[1] + ")" 99 else: 100 t = entry[0] 101 s = s + t.ljust(16) 102 return _write_kegg("ENZYME", 103 [_wrap_kegg(s, wrap_rule = id_wrap(0))])
104 - def _structures(self):
105 s = [] 106 for entry in self.structures: 107 s.append(entry[0] + ": " + " ".join(entry[1]) + " ") 108 return _write_kegg("STRUCTURES", 109 [_wrap_kegg(l, wrap_rule = struct_wrap(5)) \ 110 for l in s])
118 119
120 -def parse(handle):
121 record = Record() 122 for line in handle: 123 if line[:3]=="///": 124 yield record 125 record = Record() 126 continue 127 if line[:12]!=" ": 128 keyword = line[:12] 129 data = line[12:].strip() 130 if keyword=="ENTRY ": 131 words = data.split() 132 record.entry = words[0] 133 elif keyword=="NAME ": 134 data = data.strip(";") 135 record.name.append(data) 136 elif keyword=="ENZYME ": 137 while data: 138 column = data[:16] 139 data = data[16:] 140 if '(' in column: 141 entry = column.split() 142 enzyme = (entry[0], entry[1][1:-1]) 143 else: 144 enzyme = (column.strip(), "") 145 record.enzyme.append(enzyme) 146 elif keyword=="PATHWAY ": 147 if data[:5]=='PATH:': 148 path, map, name = data.split(None,2) 149 pathway = (path[:-1], map, name) 150 record.pathway.append(pathway) 151 else: 152 pathway = record.pathway[-1] 153 path, map, name = pathway 154 name = name + " " + data 155 pathway = path, map, name 156 record.pathway[-1] = pathway 157 elif keyword=="FORMULA ": 158 record.formula = data 159 elif keyword=="MASS ": 160 record.mass = data 161 elif keyword=="DBLINKS ": 162 if ":" in data: 163 key, values = data.split(":") 164 values = values.split() 165 row = (key, values) 166 record.dblinks.append(row) 167 else: 168 row = record.dblinks[-1] 169 key, values = row 170 values.extend(data.split()) 171 row = key, values 172 record.dblinks[-1] = row
173 174 175
176 -class Iterator:
177 """Iterator to read a file of KEGG Ligand/Compund entries one at a time. 178 """
179 - def __init__(self, handle, parser = None):
180 """Initialize the iterator. 181 182 Arguments: 183 o handle - A handle with Compound entries to iterate through. 184 o parser - An optional parser to pass the entries through before 185 returning them. If None, then the raw entry will be returned. 186 """ 187 import warnings 188 warnings.warn("Bio.KEGG.Compound.Iterator(handle, parser) is deprecated. Please use Bio.KEGG.Compound.parse(handle) instead. It also returns an iterator.", 189 DeprecationWarning) 190 self.records = parse(handle)
191
192 - def next(self):
193 """Return the next KEGG Ligand/Compound record from the handle. 194 195 Will return None if we ran out of records. 196 """ 197 return self.records.next()
198
199 - def __iter__(self):
200 return iter(self.next, None)
201