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