Package Bio :: Package KEGG
[hide private]
[frames] | no frames]

Source Code for Package Bio.KEGG

 1  # Copyright 2001 by Tarjei Mikkelsen.  All rights reserved. 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license.  Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 5   
 6  """Code to work with data from the KEGG database. 
 7   
 8  References: 
 9   
10  Kanehisa, M. and Goto, S.; KEGG: Kyoto Encyclopedia of Genes and Genomes. 
11  Nucleic Acids Res. 28, 29-34 (2000). 
12   
13  URL: http://www.genome.ad.jp/kegg/ 
14  """ 
15   
16   
17  KEGG_ITEM_LENGTH = 12 
18  KEGG_LINE_LENGTH = 80 
19  KEGG_DATA_LENGTH = KEGG_LINE_LENGTH - KEGG_ITEM_LENGTH 
20   
21  # wrap rule = [indent, connect, (splitstr, connect, splitafter, keep), ...] 
22  _default_wrap = lambda indent: [indent, "", (" ", "", 1, 0)] 
23   
24   
25 -def _wrap_kegg(line, max_width=KEGG_DATA_LENGTH, wrap_rule=_default_wrap):
26 """Wraps the input line for KEGG output. 27 28 Arguments: 29 30 o info - String holding the information we want wrapped 31 for KEGG output. 32 o max_width - Maximum width of a line. 33 o wrap_rule - A wrap rule (see above) for deciding how to split 34 strings that must be wrapped. 35 """ 36 s = "" 37 wrapped_line = "" 38 indent = " " * wrap_rule[0] 39 connect = wrap_rule[1] 40 rules = wrap_rule[2:] 41 while True: 42 if len(line) <= max_width: 43 wrapped_line = wrapped_line + line 44 s = s + wrapped_line 45 break 46 else: 47 did_split = 0 48 for rule in rules: 49 to = max_width 50 if not rule[2]: 51 to = to + len(rule[0]) 52 split_idx = line.rfind(rule[0], 0, to) 53 if split_idx > -1: 54 if rule[2] and rule[3]: 55 split_idx = split_idx + len(rule[0]) 56 wrapped_line = wrapped_line + line[0:split_idx] + "\n" 57 if not rule[3]: 58 split_idx = split_idx + len(rule[0]) 59 line = indent + rule[1] + line[split_idx:] 60 did_split = 1 61 break 62 if not did_split: 63 wrapped_line = wrapped_line + line[0:max_width] + "\n" 64 line = indent + connect + line[max_width:] 65 return s
66 67
68 -def _write_kegg(item, info, indent=KEGG_ITEM_LENGTH):
69 """Write a indented KEGG record item. 70 71 Arguments: 72 73 o item - The name of the item to be written. 74 o info - The (wrapped) information to write. 75 o indent - Width of item field. 76 """ 77 s = "" 78 for line in info: 79 partial_lines = line.splitlines() 80 for l in partial_lines: 81 s = s + item.ljust(indent) + l + "\n" 82 if item is not "": # ensure item is only written on first line 83 item = "" 84 return s
85