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  """Code to work with the KEGG Ligand/Compound database. 
  8   
  9  Functions: 
 10  parse - Returns an iterator giving Record objects. 
 11   
 12  Classes: 
 13  Record - A representation of a KEGG Ligand/Compound. 
 14  """ 
 15   
 16  # other Biopython stuff 
 17  from __future__ import print_function 
 18   
 19  from Bio.KEGG import _write_kegg 
 20  from Bio.KEGG import _wrap_kegg 
 21   
 22   
 23  # Set up line wrapping rules (see Bio.KEGG._wrap_kegg) 
 24  name_wrap = [0, "", 
 25               (" ", "$", 1, 1), 
 26               ("-", "$", 1, 1)] 
 27  id_wrap = lambda indent: [indent, "", (" ", "", 1, 0)] 
 28  struct_wrap = lambda indent: [indent, "", ("  ", "", 1, 1)] 
 29   
 30   
31 -class Record(object):
32 """Holds info from a KEGG Ligand/Compound record. 33 34 Members: 35 entry The entry identifier. 36 name A list of the compund names. 37 formula The chemical formula for the compound 38 mass The molecular weight for the compound 39 pathway A list of 3-tuples: (database, id, pathway) 40 enzyme A list of 2-tuples: (enzyme id, role) 41 structures A list of 2-tuples: (database, list of struct ids) 42 dblinks A list of 2-tuples: (database, list of link ids) 43 44 """ 45
46 - def __init__(self):
47 """__init___(self) 48 49 Create a new Record. 50 """ 51 self.entry = "" 52 self.name = [] 53 self.formula = "" 54 self.mass = "" 55 self.pathway = [] 56 self.enzyme = [] 57 self.structures = [] 58 self.dblinks = []
59
60 - def __str__(self):
61 """__str__(self) 62 63 Returns a string representation of this Record. 64 """ 65 return self._entry() + \ 66 self._name() + \ 67 self._formula() + \ 68 self._mass() + \ 69 self._pathway() + \ 70 self._enzyme() + \ 71 self._structures() + \ 72 self._dblinks() + \ 73 "///"
74
75 - def _entry(self):
76 return _write_kegg("ENTRY", 77 [self.entry])
78
79 - def _name(self):
80 return _write_kegg("NAME", 81 [_wrap_kegg(l, wrap_rule=name_wrap) 82 for l in self.name])
83
84 - def _formula(self):
85 return _write_kegg("FORMULA", 86 [self.formula])
87
88 - def _mass(self):
89 return _write_kegg("MASS", 90 [self.mass])
91
92 - def _pathway(self):
93 s = [] 94 for entry in self.pathway: 95 s.append(entry[0] + ": " + entry[1] + " " + entry[2]) 96 return _write_kegg("PATHWAY", 97 [_wrap_kegg(l, wrap_rule=id_wrap(16)) 98 for l in s])
99
100 - def _enzyme(self):
101 s = "" 102 for entry in self.enzyme: 103 if entry[1]: 104 t = entry[0] + " (" + entry[1] + ")" 105 else: 106 t = entry[0] 107 s = s + t.ljust(16) 108 return _write_kegg("ENZYME", 109 [_wrap_kegg(s, wrap_rule=id_wrap(0))])
110
111 - def _structures(self):
112 s = [] 113 for entry in self.structures: 114 s.append(entry[0] + ": " + " ".join(entry[1]) + " ") 115 return _write_kegg("STRUCTURES", 116 [_wrap_kegg(l, wrap_rule=struct_wrap(5)) 117 for l in s])
118
126 127
128 -def parse(handle):
129 """Parse a KEGG Ligan/Compound file, returning Record objects. 130 131 This is an iterator function, typically used in a for loop. For 132 example, using one of the example KEGG files in the Biopython 133 test suite, 134 135 >>> with open("KEGG/compound.sample") as handle: 136 ... for record in parse(handle): 137 ... print("%s %s" % (record.entry, record.name[0])) 138 ... 139 C00023 Iron 140 C00017 Protein 141 C00099 beta-Alanine 142 C00294 Inosine 143 C00298 Trypsin 144 C00348 Undecaprenyl phosphate 145 C00349 2-Methyl-3-oxopropanoate 146 C01386 NH2Mec 147 148 """ 149 record = Record() 150 for line in handle: 151 if line[:3] == "///": 152 yield record 153 record = Record() 154 continue 155 if line[:12] != " ": 156 keyword = line[:12] 157 data = line[12:].strip() 158 if keyword == "ENTRY ": 159 words = data.split() 160 record.entry = words[0] 161 elif keyword == "NAME ": 162 data = data.strip(";") 163 record.name.append(data) 164 elif keyword == "ENZYME ": 165 while data: 166 column = data[:16] 167 data = data[16:] 168 if '(' in column: 169 entry = column.split() 170 enzyme = (entry[0], entry[1][1:-1]) 171 else: 172 enzyme = (column.strip(), "") 173 record.enzyme.append(enzyme) 174 elif keyword == "PATHWAY ": 175 if data[:5] == 'PATH:': 176 path, map, name = data.split(None, 2) 177 pathway = (path[:-1], map, name) 178 record.pathway.append(pathway) 179 else: 180 pathway = record.pathway[-1] 181 path, map, name = pathway 182 name = name + " " + data 183 pathway = path, map, name 184 record.pathway[-1] = pathway 185 elif keyword == "FORMULA ": 186 record.formula = data 187 elif keyword == "MASS ": 188 record.mass = data 189 elif keyword == "DBLINKS ": 190 if ":" in data: 191 key, values = data.split(":") 192 values = values.split() 193 row = (key, values) 194 record.dblinks.append(row) 195 else: 196 row = record.dblinks[-1] 197 key, values = row 198 values.extend(data.split()) 199 row = key, values 200 record.dblinks[-1] = row
201 202 203 if __name__ == "__main__": 204 from Bio._utils import run_doctest 205 run_doctest() 206