File size: 3,039 Bytes
b7731cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright 2001 by Tarjei Mikkelsen.  All rights reserved.
#
# This file is part of the Biopython distribution and governed by your
# choice of the "Biopython License Agreement" or the "BSD 3-Clause License".
# Please see the LICENSE file that should have been included as part of this
# package.

"""Code to work with data from the KEGG database.

References:
Kanehisa, M. and Goto, S.; KEGG: Kyoto Encyclopedia of Genes and Genomes.
Nucleic Acids Res. 28, 29-34 (2000).

URL: http://www.genome.ad.jp/kegg/

"""


KEGG_ITEM_LENGTH = 12
KEGG_LINE_LENGTH = 80
KEGG_DATA_LENGTH = KEGG_LINE_LENGTH - KEGG_ITEM_LENGTH


def _default_wrap(indent):
    """Return default wrap rule for _wrap_kegg (PRIVATE).

    A wrap rule is a list with the following elements:
    [indent, connect, (splitstr, connect, splitafter, keep), ...]
    """
    return [indent, "", (" ", "", 1, 0)]


def _struct_wrap(indent):
    """Return wrap rule for KEGG STRUCTURE (PRIVATE)."""
    return [indent, "", ("  ", "", 1, 1)]


def _wrap_kegg(line, max_width=KEGG_DATA_LENGTH, wrap_rule=_default_wrap):
    """Wrap the input line for KEGG output (PRIVATE).

    Arguments:
     - info - String holding the information we want wrapped
       for KEGG output.
     - max_width - Maximum width of a line.
     - wrap_rule - A wrap rule (see above) for deciding how to split
       strings that must be wrapped.

    """
    s = ""
    wrapped_line = ""
    indent = " " * wrap_rule[0]
    connect = wrap_rule[1]
    rules = wrap_rule[2:]
    while True:
        if len(line) <= max_width:
            wrapped_line = wrapped_line + line
            s = s + wrapped_line
            break
        else:
            did_split = 0
            for rule in rules:
                to = max_width
                if not rule[2]:
                    to = to + len(rule[0])
                split_idx = line.rfind(rule[0], 0, to)
                if split_idx > -1:
                    if rule[2] and rule[3]:
                        split_idx = split_idx + len(rule[0])
                    wrapped_line = wrapped_line + line[0:split_idx] + "\n"
                    if not rule[3]:
                        split_idx = split_idx + len(rule[0])
                    line = indent + rule[1] + line[split_idx:]
                    did_split = 1
                    break
            if not did_split:
                wrapped_line = wrapped_line + line[0:max_width] + "\n"
                line = indent + connect + line[max_width:]
    return s


def _write_kegg(item, info, indent=KEGG_ITEM_LENGTH):
    """Write a indented KEGG record item (PRIVATE).

    Arguments:
     - item - The name of the item to be written.
     - info - The (wrapped) information to write.
     - indent - Width of item field.

    """
    s = ""
    for line in info:
        partial_lines = line.splitlines()
        for partial in partial_lines:
            s += item.ljust(indent) + partial + "\n"
            if item:  # ensure item is only written on first line
                item = ""
    return s