1
1
import logging
2
2
import re
3
3
from cobra import Model , Reaction , Metabolite
4
+ from modelseedpy .fbapkg .mspackagemanager import MSPackageManager
4
5
5
6
logger = logging .getLogger (__name__ )
6
7
@@ -14,6 +15,7 @@ class MSModelUtil:
14
15
15
16
def __init__ (self ,model ):
16
17
self .model = model
18
+ self .pkgmgr = MSPackageManager .get_pkg_mgr (model )
17
19
self .metabolite_hash = None
18
20
self .search_metabolite_hash = None
19
21
@@ -40,6 +42,8 @@ def add_name_to_metabolite_hash(self,name,met):
40
42
self .search_metabolite_hash [sname ].append (met )
41
43
42
44
def find_met (self ,name ):
45
+ if self .metabolite_hash == None :
46
+ self .build_metabolite_hash ()
43
47
if name in self .metabolite_hash :
44
48
return self .metabolite_hash [name ]
45
49
sname = search_name (name )
@@ -107,4 +111,125 @@ def add_exchanges_for_metabolites(self,cpds,uptake=0,excretion=0,prefix='EX_', p
107
111
return drains
108
112
109
113
def reaction_scores (self ):
110
- return {}
114
+ return {}
115
+
116
+ #Required this function to add gapfilled compounds to a KBase model for saving gapfilled model
117
+ def convert_cobra_compound_to_kbcompound (self ,cpd ,kbmodel ,add_to_model = 1 ):
118
+ refid = "cpd00000"
119
+ if re .search ('cpd\d+_[a-z]+' ,cpd .id ):
120
+ refid = cpd .id
121
+ refid = re .sub ("_[a-z]\d+$" ,"" ,refid )
122
+ cpd_data = {
123
+ "aliases" : [],
124
+ "charge" : cpd .charge ,
125
+ "compound_ref" : "~/template/compounds/id/" + refid ,
126
+ "dblinks" : {},
127
+ "formula" : cpd .formula ,
128
+ "id" : cpd .id ,
129
+ "modelcompartment_ref" : "~/modelcompartments/id/" + cpd .id .split ("_" ).pop (),
130
+ "name" : cpd .name ,
131
+ "numerical_attributes" : {},
132
+ "string_attributes" : {}
133
+ }
134
+ if add_to_model == 1 :
135
+ kbmodel ["modelcompounds" ].append (cpd_data )
136
+ return cpd_data
137
+
138
+ #Required this function to add gapfilled reactions to a KBase model for saving gapfilled model
139
+ def convert_cobra_reaction_to_kbreaction (self ,rxn ,kbmodel ,cpd_hash ,direction = "=" ,add_to_model = 1 ,reaction_genes = None ):
140
+ rxnref = "~/template/reactions/id/rxn00000_c"
141
+ if re .search ('rxn\d+_[a-z]+' ,rxn .id ):
142
+ rxnref = "~/template/reactions/id/" + rxn .id
143
+ rxnref = re .sub ("\d+$" ,"" ,rxnref )
144
+ rxn_data = {
145
+ "id" : rxn .id ,
146
+ "aliases" : [],
147
+ "dblinks" : {},
148
+ "direction" : direction ,
149
+ "edits" : {},
150
+ "gapfill_data" : {},
151
+ "maxforflux" : 1000000 ,
152
+ "maxrevflux" : 1000000 ,
153
+ "modelReactionProteins" : [],
154
+ "modelReactionReagents" : [],
155
+ "modelcompartment_ref" : "~/modelcompartments/id/" + rxn .id .split ("_" ).pop (),
156
+ "name" : rxn .name ,
157
+ "numerical_attributes" : {},
158
+ "probability" : 0 ,
159
+ "protons" : 0 ,
160
+ "reaction_ref" : rxnref ,
161
+ "string_attributes" : {}
162
+ }
163
+ for cpd in rxn .metabolites :
164
+ if cpd .id not in kbmodel ["modelcompounds" ]:
165
+ cpd_hash [cpd .id ] = self .convert_cobra_compound_to_kbcompound (cpd ,kbmodel ,1 )
166
+ rxn_data ["modelReactionReagents" ].append ({
167
+ "coefficient" : rxn .metabolites [cpd ],
168
+ "modelcompound_ref" : "~/modelcompounds/id/" + cpd .id
169
+ })
170
+ if reaction_genes != None and rxn .id in reaction_genes :
171
+ best_gene = None
172
+ for gene in reaction_genes [rxn .id ]:
173
+ if best_gene == None or reaction_genes [rxn .id ][gene ] > reaction_genes [rxn .id ][best_gene ]:
174
+ best_gene = gene
175
+ rxn_data ["modelReactionProteins" ] = [{"note" :"Added from gapfilling" ,"modelReactionProteinSubunits" :[],"source" :"Unknown" }]
176
+ rxn_data ["modelReactionProteins" ][0 ]["modelReactionProteinSubunits" ] = [{"note" :"Added from gapfilling" ,"optionalSubunit" :0 ,"triggering" :1 ,"feature_refs" :["~/genome/features/id/" + best_gene ],"role" :"Unknown" }]
177
+ if add_to_model == 1 :
178
+ kbmodel ["modelreactions" ].append (rxn_data )
179
+ return rxn_data
180
+
181
+ def add_gapfilling_solution_to_kbase_model (self ,newmodel ,gapfilled_reactions ,gfid = None ,media_ref = None ,reaction_genes = None ):
182
+ rxn_table = []
183
+ gapfilling_obj = None
184
+ if gfid == None :
185
+ largest_index = 0
186
+ for gapfilling in newmodel ["gapfillings" ]:
187
+ current_index = int (gapfilling ["id" ].split ("." ).pop ())
188
+ if largest_index == 0 or largest_index < current_index :
189
+ largest_index = current_index
190
+ largest_index += 1
191
+ gfid = "gf." + str (largest_index )
192
+ else :
193
+ for gapfilling in newmodel ["gapfillings" ]:
194
+ if gapfilling ["id" ] == gfid :
195
+ gapfilling_obj = gapfilling
196
+ if gapfilling_obj == None :
197
+ gapfilling_obj = {
198
+ "gapfill_id" : newmodel ["id" ]+ "." + gfid ,
199
+ "id" : gfid ,
200
+ "integrated" : 1 ,
201
+ "integrated_solution" : "0" ,
202
+ "media_ref" : media_ref
203
+ }
204
+ newmodel ["gapfillings" ].append (gapfilling_obj )
205
+ cpd_hash = {}
206
+ for cpd in newmodel ["modelcompounds" ]:
207
+ cpd_hash [cpd ["id" ]] = cpd
208
+ for rxn in gapfilled_reactions ["new" ]:
209
+ reaction = self .model .reactions .get_by_id (rxn )
210
+ kbrxn = self .convert_cobra_reaction_to_kbreaction (reaction ,newmodel ,cpd_hash ,gapfilled_reactions ["new" ][rxn ],1 ,reaction_genes )
211
+ kbrxn ["gapfill_data" ][gfid ] = dict ()
212
+ kbrxn ["gapfill_data" ][gfid ]["0" ] = [gapfilled_reactions ["new" ][rxn ],1 ,[]]
213
+ rxn_table .append ({
214
+ 'id' :kbrxn ["id" ],
215
+ 'name' :kbrxn ["name" ],
216
+ 'direction' :format_direction (kbrxn ["direction" ]),
217
+ 'gene' :format_gpr (kbrxn ),
218
+ 'equation' :format_equation (kbrxn ,cpd_hash ),
219
+ 'newrxn' :1
220
+ })
221
+ for rxn in gapfilled_reactions ["reversed" ]:
222
+ for kbrxn in newmodel ["modelreactions" ]:
223
+ if kbrxn ["id" ] == rxn :
224
+ kbrxn ["direction" ] = "="
225
+ rxn_table .append ({
226
+ 'id' :kbrxn ["id" ],
227
+ 'name' :kbrxn ["name" ],
228
+ 'direction' :format_direction (kbrxn ["direction" ]),
229
+ 'gene' :format_gpr (kbrxn ),
230
+ 'equation' :format_equation (kbrxn ,cpd_hash ),
231
+ 'newrxn' :0
232
+ })
233
+ kbrxn ["gapfill_data" ][gfid ] = dict ()
234
+ kbrxn ["gapfill_data" ][gfid ]["0" ] = [gapfilled_reactions ["reversed" ][rxn ],1 ,[]]
235
+ return rxn_table
0 commit comments