annotate COBRAxy/utils/reaction_parsing.py @ 7:515cd9e4f518 draft

Uploaded
author luca_milaz
date Wed, 18 Sep 2024 12:09:06 +0000
parents 41f35c2f0c7b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
1 from enum import Enum
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
2 import utils.general_utils as utils
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
3 from typing import Dict
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
4 import csv
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
5 import re
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
6
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
7 # Reaction direction encoding:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
8 class ReactionDir(Enum):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
9 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
10 A reaction can go forwards, backwards or be reversible (able to proceed in both directions).
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
11 Models created / managed with cobrapy encode this information within the reaction's
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
12 formula using the arrows this enum keeps as values.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
13 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
14 FORWARD = "-->"
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
15 BACKWARD = "<--"
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
16 REVERSIBLE = "<=>"
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
17
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
18 @classmethod
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
19 def fromReaction(cls, reaction :str) -> 'ReactionDir':
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
20 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
21 Takes a whole reaction formula string and looks for one of the arrows, returning the
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
22 corresponding reaction direction.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
23
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
24 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
25 reaction : the reaction's formula.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
26
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
27 Raises:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
28 ValueError : if no valid arrow is found.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
29
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
30 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
31 ReactionDir : the corresponding reaction direction.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
32 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
33 for member in cls:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
34 if member.value in reaction: return member
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
35
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
36 raise ValueError("No valid arrow found within reaction string.")
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
37
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
38 ReactionsDict = Dict[str, Dict[str, float]]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
39
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
40
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
41 def add_custom_reaction(reactionsDict :ReactionsDict, rId :str, reaction :str) -> None:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
42 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
43 Adds an entry to the given reactionsDict. Each entry consists of a given unique reaction id
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
44 (key) and a :dict (value) matching each substrate in the reaction to its stoichiometric coefficient.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
45 Keys and values are both obtained from the reaction's formula: if a substrate (custom metabolite id)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
46 appears without an explicit coeff, the value 1.0 will be used instead.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
47
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
48 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
49 reactionsDict : dictionary encoding custom reactions information.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
50 rId : unique reaction id.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
51 reaction : the reaction's formula.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
52
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
53 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
54 None
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
55
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
56 Side effects:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
57 reactionsDict : mut
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
58 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
59 reaction = reaction.strip()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
60 if not reaction: return
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
61
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
62 reactionsDict[rId] = {}
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
63 # We assume the '+' separating consecutive metabs in a reaction is spaced from them,
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
64 # to avoid confusing it for electrical charge:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
65 for word in reaction.split(" + "):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
66 metabId, stoichCoeff = word, 1.0
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
67 # Implicit stoichiometric coeff is equal to 1, some coeffs are floats.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
68
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
69 # Accepted coeffs can be integer or floats with a dot (.) decimal separator
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
70 # and must be separated from the metab with a space:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
71 foundCoeff = re.search(r"\d+(\.\d+)? ", word)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
72 if foundCoeff:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
73 wholeMatch = foundCoeff.group(0)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
74 metabId = word[len(wholeMatch):].strip()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
75 stoichCoeff = float(wholeMatch.strip())
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
76
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
77 reactionsDict[rId][metabId] = stoichCoeff
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
78
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
79 if not reactionsDict[rId]: del reactionsDict[rId] # Empty reactions are removed.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
80
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
81
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
82 def create_reaction_dict(unparsed_reactions: Dict[str, str]) -> ReactionsDict:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
83 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
84 Parses the given dictionary into the correct format.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
85
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
86 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
87 unparsed_reactions (Dict[str, str]): A dictionary where keys are reaction IDs and values are unparsed reaction strings.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
88
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
89 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
90 ReactionsDict: The correctly parsed dict.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
91 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
92 reactionsDict :ReactionsDict = {}
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
93 for rId, reaction in unparsed_reactions.items():
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
94 reactionDir = ReactionDir.fromReaction(reaction)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
95 left, right = reaction.split(f" {reactionDir.value} ")
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
96
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
97 # Reversible reactions are split into distinct reactions, one for each direction.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
98 # In general we only care about substrates, the product information is lost.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
99 reactionIsReversible = reactionDir is ReactionDir.REVERSIBLE
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
100 if reactionDir is not ReactionDir.BACKWARD:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
101 add_custom_reaction(reactionsDict, rId + "_F" * reactionIsReversible, left)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
102
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
103 if reactionDir is not ReactionDir.FORWARD:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
104 add_custom_reaction(reactionsDict, rId + "_B" * reactionIsReversible, right)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
105
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
106 # ^^^ to further clarify: if a reaction is NOT reversible it will not be marked as _F or _B
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
107 # and whichever direction we DO keep (forward if --> and backward if <--) loses this information.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
108 # This IS a small problem when coloring the map in marea.py because the arrow IDs in the map follow
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
109 # through with a similar convention on ALL reactions and correctly encode direction based on their
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
110 # model of origin. TODO: a proposed solution is to unify the standard in RPS to fully mimic the maps,
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
111 # which involves re-writing the "reactions" dictionary.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
112
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
113 return reactionsDict
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
114
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
115
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
116 def parse_custom_reactions(customReactionsPath :str) -> ReactionsDict:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
117 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
118 Creates a custom dictionary encoding reactions information from a csv file containing
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
119 data about these reactions, the path of which is given as input.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
120
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
121 Args:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
122 customReactionsPath : path to the reactions information file.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
123
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
124 Returns:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
125 ReactionsDict : dictionary encoding custom reactions information.
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
126 """
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
127 reactionsData :Dict[str, str] = {row[0]: row[1] for row in utils.readCsv(utils.FilePath.fromStrPath(customReactionsPath))}
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
128
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
129 return create_reaction_dict(reactionsData)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
130