File: enzyme_format.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (283 lines) | stat: -rw-r--r-- 8,869 bytes parent folder | download | duplicates (2)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""Martel based parser to read KEGG Ligand/Enzyme files.

This is a huge regular expression for KEGG Ligand/Enzyme,
built using the 'regular expressions on steroids' capabilities of
Martel.

A description of the format can be found in the 'ligand.doc' file
from the Ligand distribution, available from:

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

(Note that as of KEGG release 19.0, the 'enzyme' file that comes
as part of the Ligand distribution does not conform 100% to this
description. In particular, some entries contain data items that
are not ordered as they should be according the description. This
Biopython format description has been weakened to accomodate parsing
of these irregular entries.)
 
"""

# Martel
from Martel import *
from Martel import RecordReader

# --- First set up some helper constants and functions
INDENT = 12

blank_space = Rep1(Str1(" "))
point = Str1(".")

def _define_block_seq(block_tag, block_group, item):
    """Define a Martel grouping that can parse a block of groups.

    Many of the KEGG entries are formatted as:

    TAG         blah blah blah blah blah blah blah blah
                blah blah
                zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

    Where 'blah blah...' and 'zzzz...' are identically formatted
    items that can wrap over multiple lines. This function defines
    a consistent grouping for these blocks.

    Arguments:
    o block_tag - A string of length <= INDENT giving the block tag
                  (i.e. 'TAG' in the example above).
    o block_group - A callbakc tag for the entire block.
    o item - A Martel group/expression for a single instance of the
             data item in this block. (Must NOT include the final
             newline character).
    """
    return Group(block_group,
                 Str1(block_tag + " " * (INDENT - len(block_tag))) +
                 item +
                 Rep(AnyEol() +
                     Str1(" " * INDENT) + item) +
                 AnyEol())


# --- Create regular expressions for each data item

# ENTRY
entry_tag = "ENTRY"
entry = Group("entry",
              Str1("EC ") +
              Rep(Rep1(Integer()) + point) +
              Rep1(Integer()))

entry_line = Group("entry_line",
                   Str1(entry_tag + " " * (INDENT - len(entry_tag))) +
                   entry +
                   AnyEol())


# NAME
name_tag = "NAME"
partial_name = Re("[\S]+")
composite_name = Rep1(Opt(Str1(" ")) + partial_name + Opt(Str1(" ")))
wrapped_name = composite_name + Rep(AnyEol() +
                                    Str1(" " * INDENT + "$") +
                                    composite_name)

name = Group("name", wrapped_name)
name_block = _define_block_seq("NAME", "name_block", name)


# CLASS (renamed 'classname' to avoid conflict with Python)
class_name = Group("classname", composite_name)
class_block = _define_block_seq("CLASS", "class_block", class_name)


# SYSNAME
sysname = Group("sysname", wrapped_name)
sysname_block = _define_block_seq("SYSNAME", "sysname_block", sysname)


# REACTION
#
# Note: It would be nice to do something fancier with this block,
#       like doing a complete parse of the reaction equations.
#       Unfortunately this would'nt work in general as the KEGG
#       format specification allows this field to be an arbitrary
#       string.
#
reaction_block = _define_block_seq("REACTION",
                                   "reaction_block",
                                   Group("reaction", Re(".*")))


# SUBSTRATE
substrate = Group("substrate", wrapped_name)
substrate_block = _define_block_seq("SUBSTRATE",
                                    "substrate_block",
                                    substrate)


# PRODUCT
product = Group("product", wrapped_name)
product_block = _define_block_seq("PRODUCT",
                                  "product_block",
                                  product)


# INHIBITOR
inhibitor = Group("inhibitor", wrapped_name)
inhibitor_block = _define_block_seq("INHIBITOR",
                                    "inhibitor_block",
                                    inhibitor)


# COFACTOR
cofactor = Group("cofactor", wrapped_name) 
cofactor_block = _define_block_seq("COFACTOR",
                                   "cofactor_block",
                                   cofactor)


# EFFECTOR
effector = Group("effector", wrapped_name)
effector_block = _define_block_seq("EFFECTOR",
                                   "effector_block",
                                   effector)


# COMMENT
comment_block = _define_block_seq("COMMENT",
                                  "comment_block",
                                  Group("comment", Re(".*")))


# PATHWAY
pathway_db = Group("pathway_db", Re("[\w]+"))
pathway_id = Group("pathway_id", Re("[\w]+"))
pathway_desc = Group("pathway_desc", Re(".*"))
pathway_line = pathway_db + Str1(":") + \
               blank_space + \
               pathway_id + \
               blank_space + \
               pathway_desc + \
               Rep(AnyEol() +
                   Str1(" " * (INDENT + 1)) +
                   blank_space +
                   pathway_desc)

pathway_block = _define_block_seq("PATHWAY", "pathway_block", pathway_line)


# GENES
organism = Group("organism", Re("[\w]{3}"))
gene_id = Group("gene_id", Re("[\S]+"))
genes = organism + \
        Str1(":") + \
        Rep1(blank_space + gene_id) + \
        Rep(AnyEol() +
            Str1(" " * (INDENT + 1)) +
            Rep1(blank_space + gene_id))

genes_block = _define_block_seq("GENES", "genes_block", genes)


# DISEASE
disease_db = Group("disease_db", Re("[\w]+"))
disease_id = Group("disease_id", Re("[\w]+"))
partial_desc = Group("disease_desc", Re(".*"))
disease_desc = partial_desc + \
               Rep(AnyEol() +
                   Str1(" " * (INDENT + 1)) +
                   blank_space +
                   partial_desc)
disease_line = disease_db + Str1(":") + \
               blank_space + \
               disease_id + \
               blank_space + \
               disease_desc

disease_block = _define_block_seq("DISEASE", "disease_block", disease_line)


# MOTIF
partial_motif = Re("\S.*")
wrapped_motif = partial_motif + \
                Rep(AnyEol() +
                    Str1(" " * (INDENT + 1)) +
                    blank_space +
                    partial_motif)

motif_db = Group("motif_db", Re("[\w]+"))
motif_id = Group("motif_id", Re("[\w]+"))
motif = Group("motif", wrapped_motif)
motif_line = motif_db + Str1(":") + \
             blank_space + \
             motif_id + \
             blank_space + \
             motif

motif_block = _define_block_seq("MOTIF", "motif_block", motif_line)


# STRUCTURES
structure_db = Group("structure_db", Re("[\w]+"))
structure_id = Group("structure_id", Re("[\w]+"))
structures_line = structure_db + \
                  Str1(":") + \
                  blank_space + \
                  Rep1(structure_id + blank_space) + \
                  Rep(AnyEol() +
                      Str1(" " * (INDENT + 1)) +
                      blank_space + 
                      Rep1(structure_id + blank_space))

structures_block = _define_block_seq("STRUCTURES",
                                     "structures_block",
                                     structures_line)


# DBLINKS
dblinks_db = Group("dblinks_db", Rep1(AnyBut(":")))
dblinks_id = Group("dblinks_id", Re("[\S]+"))
dblinks_line = dblinks_db + \
               Str1(":") + \
               blank_space + \
               Rep1(dblinks_id + Opt(blank_space)) + \
               Rep(AnyEol() +
                   Str1(" " * (INDENT + 1)) +
                   blank_space +
                   Rep1(dblinks_id + blank_space))

dblinks_block = _define_block_seq("DBLINKS", "dblinks_line", dblinks_line)


# --- Assemble the full record format

record_end = Group("record_end",
                   Str1("///") +
                   Rep1(AnyEol()))

# The irregularities in the following format are present to avoid
# conflict with a few inconsistently formatted records in the KEGG
# database.
record = Group("KEGG_Enzyme_record",
               entry_line +
               Rep1(name_block) +
               Rep(Alt(class_block,
                       sysname_block,
                       reaction_block,
                       substrate_block,
                       product_block,
                       inhibitor_block,
                       cofactor_block,
                       effector_block,
                       comment_block,
                       pathway_block,
                       genes_block,
                       disease_block,
                       motif_block,
                       structures_block,
                       dblinks_block)) +
               record_end)

record_format = ParseRecords("KEGG_Enzyme_file", {},
                             record, RecordReader.EndsWith, ("///",))