1
2
3
4
5
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
17 from Bio.KEGG import _write_kegg
18 from Bio.KEGG import _wrap_kegg
19
20
21
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
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 """
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 = []
72 return _write_kegg("ENTRY",
73 [self.entry])
82
86
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))])
118
119
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
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
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
200 return iter(self.next, None)
201