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 - def __init__(self):
46 """__init___(self) 47 48 Create a new Record. 49 """ 50 self.entry = "" 51 self.name = [] 52 self.formula = "" 53 self.mass = "" 54 self.pathway = [] 55 self.enzyme = [] 56 self.structures = [] 57 self.dblinks = []
58
59 - def __str__(self):
60 """__str__(self) 61 62 Returns a string representation of this Record. 63 """ 64 return self._entry() + \ 65 self._name() + \ 66 self._formula() + \ 67 self._mass() + \ 68 self._pathway() + \ 69 self._enzyme() + \ 70 self._structures() + \ 71 self._dblinks() + \ 72 "///"
73
74 - def _entry(self):
75 return _write_kegg("ENTRY", 76 [self.entry])
77
78 - def _name(self):
79 return _write_kegg("NAME", 80 [_wrap_kegg(l, wrap_rule=name_wrap) 81 for l in self.name])
82
83 - def _formula(self):
84 return _write_kegg("FORMULA", 85 [self.formula])
86
87 - def _mass(self):
88 return _write_kegg("MASS", 89 [self.mass])
90
91 - def _pathway(self):
92 s = [] 93 for entry in self.pathway: 94 s.append(entry[0] + ": " + entry[1] + " " + entry[2]) 95 return _write_kegg("PATHWAY", 96 [_wrap_kegg(l, wrap_rule=id_wrap(16)) 97 for l in s])
98
99 - def _enzyme(self):
100 s = "" 101 for entry in self.enzyme: 102 if entry[1]: 103 t = entry[0] + " (" + entry[1] + ")" 104 else: 105 t = entry[0] 106 s = s + t.ljust(16) 107 return _write_kegg("ENZYME", 108 [_wrap_kegg(s, wrap_rule=id_wrap(0))])
109
110 - def _structures(self):
111 s = [] 112 for entry in self.structures: 113 s.append(entry[0] + ": " + " ".join(entry[1]) + " ") 114 return _write_kegg("STRUCTURES", 115 [_wrap_kegg(l, wrap_rule=struct_wrap(5)) 116 for l in s])
117
125 126
127 -def parse(handle):
128 """Parse a KEGG Ligan/Compound file, returning Record objects. 129 130 This is an iterator function, typically used in a for loop. For 131 example, using one of the example KEGG files in the Biopython 132 test suite, 133 134 >>> with open("KEGG/compound.sample") as handle: 135 ... for record in parse(handle): 136 ... print("%s %s" % (record.entry, record.name[0])) 137 ... 138 C00023 Iron 139 C00017 Protein 140 C00099 beta-Alanine 141 C00294 Inosine 142 C00298 Trypsin 143 C00348 Undecaprenyl phosphate 144 C00349 2-Methyl-3-oxopropanoate 145 C01386 NH2Mec 146 147 """ 148 record = Record() 149 for line in handle: 150 if line[:3]=="///": 151 yield record 152 record = Record() 153 continue 154 if line[:12]!=" ": 155 keyword = line[:12] 156 data = line[12:].strip() 157 if keyword=="ENTRY ": 158 words = data.split() 159 record.entry = words[0] 160 elif keyword=="NAME ": 161 data = data.strip(";") 162 record.name.append(data) 163 elif keyword=="ENZYME ": 164 while data: 165 column = data[:16] 166 data = data[16:] 167 if '(' in column: 168 entry = column.split() 169 enzyme = (entry[0], entry[1][1:-1]) 170 else: 171 enzyme = (column.strip(), "") 172 record.enzyme.append(enzyme) 173 elif keyword=="PATHWAY ": 174 if data[:5]=='PATH:': 175 path, map, name = data.split(None, 2) 176 pathway = (path[:-1], map, name) 177 record.pathway.append(pathway) 178 else: 179 pathway = record.pathway[-1] 180 path, map, name = pathway 181 name = name + " " + data 182 pathway = path, map, name 183 record.pathway[-1] = pathway 184 elif keyword=="FORMULA ": 185 record.formula = data 186 elif keyword=="MASS ": 187 record.mass = data 188 elif keyword=="DBLINKS ": 189 if ":" in data: 190 key, values = data.split(":") 191 values = values.split() 192 row = (key, values) 193 record.dblinks.append(row) 194 else: 195 row = record.dblinks[-1] 196 key, values = row 197 values.extend(data.split()) 198 row = key, values 199 record.dblinks[-1] = row
200 201 202 if __name__ == "__main__": 203 from Bio._utils import run_doctest 204 run_doctest() 205