Back to index

python-biopython  1.60
__init__.py
Go to the documentation of this file.
00001 # Copyright 2001 by Tarjei Mikkelsen.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 """
00007 This module provides code to work with data from the KEGG database.
00008 
00009 References:
00010 
00011 Kanehisa, M. and Goto, S.; KEGG: Kyoto Encyclopedia of Genes and Genomes.
00012 Nucleic Acids Res. 28, 29-34 (2000). 
00013 
00014 URL: http://www.genome.ad.jp/kegg/
00015 """
00016 KEGG_ITEM_LENGTH = 12
00017 KEGG_LINE_LENGTH = 80
00018 KEGG_DATA_LENGTH = KEGG_LINE_LENGTH - KEGG_ITEM_LENGTH
00019 
00020 # wrap rule = [indent, connect, (splitstr, connect, splitafter, keep), ...]
00021 _default_wrap = lambda indent: [indent, "", (" ", "", 1, 0)]
00022 
00023 def _wrap_kegg(line, max_width = KEGG_DATA_LENGTH, wrap_rule = _default_wrap):
00024     """Wraps the input line  for KEGG output.
00025 
00026     Arguments:
00027     
00028     o info - String holding the information we want wrapped
00029     for KEGG output.
00030     o max_width - Maximum width of a line.
00031     o wrap_rule - A wrap rule (see above) for deciding how to split
00032     strings that must be wrapped.
00033     """
00034     s = ""
00035     wrapped_line = ""
00036     indent =  " " * wrap_rule[0]
00037     connect = wrap_rule[1]
00038     rules =   wrap_rule[2:]
00039     while 1:
00040         if len(line) <= max_width:
00041             wrapped_line = wrapped_line + line
00042             s = s + wrapped_line
00043             break
00044         else:
00045             did_split = 0
00046             for rule in rules:
00047                 to = max_width
00048                 if not rule[2]:
00049                     to = to + len(rule[0])
00050                 split_idx = line.rfind(rule[0], 0, to)
00051                 if split_idx > -1:
00052                     if rule[2] and rule[3]:
00053                         split_idx = split_idx + len(rule[0])
00054                     wrapped_line = wrapped_line + line[0:split_idx] + "\n"
00055                     if not rule[3]:
00056                         split_idx = split_idx + len(rule[0])
00057                     line = indent + rule[1] + line[split_idx:]
00058                     did_split = 1
00059                     break
00060             if not did_split:
00061                 wrapped_line = wrapped_line + line[0:max_width] + "\n"
00062                 line = indent + connect + line[max_width:]
00063     return s
00064 
00065 
00066 def _write_kegg(item, info, indent = KEGG_ITEM_LENGTH):
00067     """Write a indented KEGG record item.
00068     
00069     Arguments:
00070     
00071     o item - The name of the item to be written.
00072     o info - The (wrapped) information to write.
00073     o indent - Width of item field.
00074     """
00075     s = ""
00076     for line in info:
00077         partial_lines = line.splitlines()
00078         for l in partial_lines:
00079             s = s + item.ljust(indent) + l + "\n"
00080             if item is not "":  # ensure item is only written on first line
00081                 item = ""
00082     return s