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
23
25 """Wraps the input line for KEGG output.
26
27 Arguments:
28
29 o info - String holding the information we want wrapped
30 for KEGG output.
31 o max_width - Maximum width of a line.
32 o wrap_rule - A wrap rule (see above) for deciding how to split
33 strings that must be wrapped.
34 """
35 s = ""
36 wrapped_line = ""
37 indent = " " * wrap_rule[0]
38 connect = wrap_rule[1]
39 rules = wrap_rule[2:]
40 while 1:
41 if len(line) <= max_width:
42 wrapped_line = wrapped_line + line
43 s = s + wrapped_line
44 break
45 else:
46 did_split = 0
47 for rule in rules:
48 to = max_width
49 if not rule[2]:
50 to = to + len(rule[0])
51 split_idx = line.rfind(rule[0], 0, to)
52 if split_idx > -1:
53 if rule[2] and rule[3]:
54 split_idx = split_idx + len(rule[0])
55 wrapped_line = wrapped_line + line[0:split_idx] + "\n"
56 if not rule[3]:
57 split_idx = split_idx + len(rule[0])
58 line = indent + rule[1] + line[split_idx:]
59 did_split = 1
60 break
61 if not did_split:
62 wrapped_line = wrapped_line + line[0:max_width] + "\n"
63 line = indent + connect + line[max_width:]
64 return s
65
66
68 """Write a indented KEGG record item.
69
70 Arguments:
71
72 o item - The name of the item to be written.
73 o info - The (wrapped) information to write.
74 o indent - Width of item field.
75 """
76 s = ""
77 for line in info:
78 partial_lines = line.splitlines()
79 for l in partial_lines:
80 s = s + item.ljust(indent) + l + "\n"
81 if item is not "":
82 item = ""
83 return s
84