Skip to content

Commit f2de5f2

Browse files
committed
builder refactor
1 parent da4d55c commit f2de5f2

File tree

2 files changed

+139
-4
lines changed

2 files changed

+139
-4
lines changed

modelseedpy/core/msatpcorrection.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,34 @@
3535
}
3636

3737

38+
def load_default_medias(default_media_path=None, default_min_obj=0.01):
39+
if default_media_path is None:
40+
import os.path as _path
41+
import modelseedpy
42+
43+
current_file_path = _path.dirname(modelseedpy.__file__)
44+
default_media_path = f"{current_file_path}/data/atp_medias.tsv"
45+
# current_file_path = _path.dirname(_path.abspath(__file__))
46+
# default_media_path = f"{current_file_path}/../data/atp_medias.tsv"
47+
atp_medias = []
48+
filename = default_media_path
49+
medias = pd.read_csv(filename, sep="\t", index_col=0).to_dict()
50+
for media_id in medias:
51+
media_d = {}
52+
for exchange, v in medias[media_id].items():
53+
if v > 0:
54+
k = exchange.split("_")[1]
55+
media_d[k] = v
56+
media_d["cpd00001"] = 1000
57+
media_d["cpd00067"] = 1000
58+
media = MSMedia.from_dict(media_d)
59+
media.id = media_id
60+
media.name = media_id
61+
min_obj = min_gap.get(media_id, default_min_obj)
62+
atp_medias.append([media, min_obj])
63+
return atp_medias
64+
65+
3866
class MSATPCorrection:
3967

4068
DEBUG = False
@@ -84,7 +112,8 @@ def __init__(
84112
self.atp_medias = []
85113

86114
if load_default_medias:
87-
self.load_default_medias(default_media_path)
115+
self.atp_medias = load_default_medias(default_media_path)
116+
# self.load_default_medias(default_media_path)
88117

89118
media_ids = set()
90119
for media_or_list in atp_medias:
@@ -139,7 +168,7 @@ def load_default_template(self):
139168
get_template("template_core"), None
140169
).build()
141170

142-
def load_default_medias(self, default_media_path=None):
171+
def load_default_medias(self, default_media_path=None, min_obj=0.01):
143172
if default_media_path is None:
144173
import os.path as _path
145174

@@ -158,8 +187,8 @@ def load_default_medias(self, default_media_path=None):
158187
media = MSMedia.from_dict(media_d)
159188
media.id = media_id
160189
media.name = media_id
161-
min_obj = 0.01
162-
self.atp_medias.append((media, min_gap.get(media_d, min_obj)))
190+
191+
self.atp_medias.append((media, min_gap.get(media_id, min_obj)))
163192

164193
@staticmethod
165194
def find_reaction_in_template(model_reaction, template, compartment):

modelseedpy/core/msbuilder.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import logging
33
import itertools
44
import cobra
5+
6+
from modelseedpy import MSATPCorrection
57
from modelseedpy.core.exceptions import ModelSEEDError
68
from modelseedpy.core.rast_client import RastClient
79
from modelseedpy.core.msgenome import normalize_role
@@ -915,6 +917,23 @@ def build_non_metabolite_reactions(
915917

916918
return reactions
917919

920+
@staticmethod
921+
def add_atpm(model):
922+
from cobra.core import Reaction
923+
924+
if "ATPM_c0" not in model.reactions:
925+
atpm = Reaction(f"ATPM_c0", f"ATPM", "ATPM", 0, 1000)
926+
atpm.add_metabolites(
927+
{
928+
model.metabolites.cpd00001_c0: -1,
929+
model.metabolites.cpd00002_c0: -1,
930+
model.metabolites.cpd00008_c0: 1,
931+
model.metabolites.cpd00009_c0: 1,
932+
model.metabolites.cpd00067_c0: 1,
933+
}
934+
)
935+
model.add_reactions([atpm])
936+
918937
def build_biomass(self, rxn_id, cobra_model, template, biomass_compounds):
919938
bio_rxn = Reaction(rxn_id, "biomass", "", 0, 1000)
920939
metabolites = {}
@@ -936,6 +955,93 @@ def build_biomass(self, rxn_id, cobra_model, template, biomass_compounds):
936955
return bio_rxn
937956

938957
def build(
958+
self,
959+
model_or_id,
960+
gapfill_media,
961+
index="0",
962+
allow_all_non_grp_reactions=False,
963+
annotate_with_rast=True,
964+
biomass_classic=False,
965+
biomass_gc=0.5,
966+
add_reaction_from_rast_annotation=True,
967+
add_maintenance_atp_reaction=True,
968+
):
969+
970+
logger.debug("Build Base Model")
971+
model_base = self.base_model(
972+
model_or_id,
973+
index,
974+
allow_all_non_grp_reactions,
975+
annotate_with_rast,
976+
biomass_classic,
977+
biomass_gc,
978+
add_reaction_from_rast_annotation,
979+
)
980+
981+
rxn_atpm_id = None
982+
if add_maintenance_atp_reaction:
983+
logger.debug("Add ATPM Reaction")
984+
MSBuilder.add_atpm(model_base)
985+
rxn_atpm_id = "ATPM_c0"
986+
987+
from modelseedpy.core.msatpcorrection import load_default_medias
988+
989+
medias_test_atp = load_default_medias()
990+
991+
logger.debug("ATP Analysis")
992+
atp_correction = MSATPCorrection(
993+
model_base,
994+
self.template_core,
995+
medias_test_atp,
996+
compartment="c0",
997+
atp_hydrolysis_id=rxn_atpm_id,
998+
load_default_medias=False,
999+
)
1000+
1001+
media_eval = atp_correction.evaluate_growth_media()
1002+
atp_correction.determine_growth_media()
1003+
atp_correction.apply_growth_media_gapfilling()
1004+
atp_correction.expand_model_to_genome_scale()
1005+
tests = atp_correction.build_tests()
1006+
1007+
logger.debug("Gapfill Model")
1008+
gapfill = MSGapfill(
1009+
model_base,
1010+
default_gapfill_templates=[self.template],
1011+
test_conditions=tests,
1012+
default_target="bio1",
1013+
)
1014+
1015+
gapfill_res = gapfill.run_gapfilling(gapfill_media)
1016+
1017+
def _integrate_solution(template, model, gap_fill_solution):
1018+
added_reactions = []
1019+
for rxn_id, (lb, ub) in gap_fill_solution.items():
1020+
template_reaction = template.reactions.get_by_id(rxn_id)
1021+
model_reaction = template_reaction.to_reaction(model)
1022+
model_reaction.lower_bound = lb
1023+
model_reaction.upper_bound = ub
1024+
_str = model_reaction.build_reaction_string(True)
1025+
# print(f'{model.id} add {model_reaction.id}: {_str}')
1026+
added_reactions.append(model_reaction)
1027+
model.add_reactions(added_reactions)
1028+
add_exchanges = MSBuilder.add_exchanges_to_model(model)
1029+
1030+
return added_reactions, add_exchanges
1031+
1032+
from modelseedpy.core.msmodel import get_reaction_constraints_from_direction
1033+
1034+
gap_sol = {}
1035+
for rxn_id, d in gapfill_res["new"].items():
1036+
if rxn_id[:-1] in template.reactions:
1037+
gap_sol[rxn_id[:-1]] = get_reaction_constraints_from_direction(d)
1038+
print(gap_sol)
1039+
model_gapfilled = model_base.copy()
1040+
_integrate_solution(self.template, model_gapfilled, gap_sol)
1041+
1042+
return model_gapfilled, atp_correction, tests, gap_sol
1043+
1044+
def build_base_model(
9391045
self,
9401046
model_or_id,
9411047
index="0",

0 commit comments

Comments
 (0)