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  Functions: 
 11  parse - Returns an iterator giving Record objects. 
 12   
 13  Classes: 
 14  Record - A representation of a KEGG Ligand/Compound. 
 15  """ 
 16   
 17  # other Biopython stuff 
 18  from Bio.KEGG import _write_kegg 
 19  from Bio.KEGG import _wrap_kegg 
 20   
 21   
 22  # Set up line wrapping rules (see Bio.KEGG._wrap_kegg) 
 23  name_wrap = [0, "", 
 24               (" ","$",1,1), 
 25               ("-","$",1,1)] 
 26  id_wrap = lambda indent : [indent, "", 
 27                             (" ","",1,0)] 
 28  struct_wrap = lambda indent : [indent, "", 
 29                                 ("  ","",1,1)] 
 30   
 31   
32 -class Record(object):
33 """Holds info from a KEGG Ligand/Compound record. 34 35 Members: 36 entry The entry identifier. 37 name A list of the compund names. 38 formula The chemical formula for the compound 39 mass The molecular weight for the compound 40 pathway A list of 3-tuples: (database, id, pathway) 41 enzyme A list of 2-tuples: (enzyme id, role) 42 structures A list of 2-tuples: (database, list of struct ids) 43 dblinks A list of 2-tuples: (database, list of link ids) 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 >>> handle = open("KEGG/compound.sample") 136 >>> for record in parse(handle): 137 ... print 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 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