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