I'm trying to write a parser for this type of file but it's proven to a bit more tricky than I thought. If this was a few years ago, I would just read the file multiple times but now I think about efficiency and that is no bueno as there must be a better way.
How can I parse each section while reading the file line-by-line only once?
My attempt was by setting a variable that is changed depending on the section. This started working but as you can see it gets messed up on the second grouping (e.g. REACTION). I feel like while
loops might be the answer but I am not sure how to implement that in this context.
from io import StringIO
f = StringIO(
"""
ENTRY M00001 Pathway Module
NAME Glycolysis (Embden-Meyerhof pathway), glucose => pyruvate
DEFINITION (K00844,K12407,K00845,K00886,K08074,K00918) (K01810,K06859,K13810,K15916) (K00850,K16370,K21071,K00918) (K01623,K01624,K11645,K16305,K16306) K01803 ((K00134,K00150) K00927,K11389) (K01834,K15633,K15634,K15635) K01689 (K00873,K12406)
ORTHOLOGY K00844,K12407,K00845 hexokinase/glucokinase [EC:2.7.1.1 2.7.1.2] [RN:R01786]
K00886 polyphosphate glucokinase [EC:2.7.1.63] [RN:R02189]
K08074,K00918 ADP-dependent glucokinase [EC:2.7.1.147] [RN:R09085]
K01810,K06859,K13810,K15916 glucose-6-phosphate isomerase [EC:5.3.1.9] [RN:R02740]
K00850,K16370,K21071 6-phosphofructokinase [EC:2.7.1.11] [RN:R04779]
K00918 ADP-dependent phosphofructokinase [EC:2.7.1.146] [RN:R09084]
K01623,K01624,K11645,K16305,K16306 fructose-bisphosphate aldolase [EC:4.1.2.13] [RN:R01070]
K01803 triosephosphate isomerase [EC:5.3.1.1] [RN:R01015]
K00134,K00150 glyceraldehyde 3-phosphate dehydrogenase [EC:1.2.1.12 1.2.1.59] [RN:R01061 R01063]
K00927 phosphoglycerate kinase [EC:2.7.2.3] [RN:R01512]
K11389 glyceraldehyde-3-phosphate dehydrogenase (ferredoxin) [EC:1.2.7.6] [RN:R07159]
K01834,K15633,K15634,K15635 phosphoglycerate mutase [EC:5.4.2.11 5.4.2.12] [RN:R01518]
K01689 enolase [EC:4.2.1.11] [RN:R00658]
K00873,K12406 pyruvate kinase [EC:2.7.1.40] [RN:R00200]
CLASS Pathway modules; Carbohydrate metabolism; Central carbohydrate metabolism
PATHWAY map00010 Glycolysis / Gluconeogenesis
map01200 Carbon metabolism
map01100 Metabolic pathways
REACTION R01786,R02189,R09085 C00267 -> C00668
R02740 C00668 -> C05345
R04779,R09084 C05345 -> C05378
R01070 C05378 -> C00111 + C00118
R01015 C00111 -> C00118
R01061,R01063 C00118 -> C00236
R01512 C00236 -> C00197
R07159 C00118 -> C00197
R01518 C00197 -> C00631
R00658 C00631 -> C00074
R00200 C00074 -> C00022
COMPOUND C00267 alpha-D-Glucose
C00668 alpha-D-Glucose 6-phosphate
C05345 beta-D-Fructose 6-phosphate
C05378 beta-D-Fructose 1,6-bisphosphate
C00111 Glycerone phosphate
C00118 D-Glyceraldehyde 3-phosphate
C00236 3-Phospho-D-glyceroyl phosphate
C00197 3-Phospho-D-glycerate
C00631 2-Phospho-D-glycerate
C00074 Phosphoenolpyruvate
C00022 Pyruvate
///
"""
)
kegg_definition = None
kegg_orthology = list()
kegg_ortholog_set = set()
kegg_class = None
kegg_pathways = list()
kegg_pathway_set = set()
kegg_reactions = list()
kegg_reaction_set = set()
kegg_compounds = list()
kegg_compound_set = set()
# Read KEGG module text
parsing = None
for line_level_1 in f:
line_level_1 = line_level_1.strip()
if not line_level_1.startswith("/"):
# Get orthologs
if line_level_1.startswith("DEFINITION"):
kegg_definition = line_level_1.replace("DEFINITION","").strip()
kegg_ortholog_set = str(kegg_definition)
for character in list("(+ -)"):
kegg_ortholog_set.replace(character, ",")
kegg_ortholog_set = set(filter(bool, kegg_ortholog_set.split(",")))
parsing = "ORTHOLOGY"
if parsing == "ORTHOLOGY":
ko_annot = line_level_1.replace("DEFINITION","").strip()
kegg_orthology.append(ko_annot)
# Get class
if line_level_1.startswith("CLASS"):
# parsing = None
kegg_class = line_level_1.replace("CLASS","").strip().split("; ")
# Get pathways
if line_level_1.startswith("PATHWAY"):
parsing = "PATHWAY"
if parsing == "PATHWAY":
kegg_pathway = line_level_1.replace("PATHWAY","").strip().split("; ")
kegg_pathways.append(kegg_pathway)
# Get reactions
if line_level_1.startswith("REACTION"):
parsing = "REACTION"
if parsing == "REACTION":
kegg_reaction = line_level_1.replace("REACTION","").strip().split("; ")
kegg_reactions.append(kegg_reaction)
# Get compounds
if line_level_1.startswith("COMPOUND"):
parsing = "COMPOUND"
if parsing == "COMPOUND":
kegg_compound = line_level_1.replace("COMPOUND","").strip().split("; ")
kegg_compounds.append(kegg_compound)
kegg_pathways
# [['map00010 Glycolysis / Gluconeogenesis'],
# ['map01200 Carbon metabolism'],
# ['map01100 Metabolic pathways'],
# ['REACTION R01786,R02189,R09085 C00267 -> C00668']]