22import logging
33import itertools
44import cobra
5+
6+ from modelseedpy import MSATPCorrection
57from modelseedpy .core .exceptions import ModelSEEDError
68from modelseedpy .core .rast_client import RastClient
79from 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